Repository: gofiber/fiber
File: res.go (Go)

Code before:
	MediaType string
}

// DefaultRes is the default implementation of Res used by DefaultCtx.
//
//go:generate ifacemaker --file res.go --struct DefaultRes --iface Res --pkg fiber --output res_interface_gen.go --not-exported true --iface-comment "Res is an interface for response-related Ctx methods."
type DefaultRes struct {
	c *DefaultCtx
}

// App returns the *App reference to the instance of the Fiber application
func (r *DefaultRes) App() *App {
	return r.c.app
}

// Append the specified value to the HTTP response header field.
// If the header is not already set, it creates the header with the specified value.
func (r *DefaultRes) Append(field string, values ...string) {
	if len(values) == 0 {
		return
	}
	h := r.c.app.toString(r.c.fasthttp.Response.Header.Peek(field))
	originalH := h

	// Use bytebufferpool to avoid repeated string concatenation allocations
	buf := bytebufferpool.Get()
	buf.WriteString(h)

	for _, value := range values {
		if buf.Len() == 0 {
			buf.WriteString(value)
		} else if !headerContainsValue(r.c.app.toString(buf.Bytes()), value) {
			buf.WriteString(", ")
			buf.WriteString(value)
		}
	}

	newH := r.c.app.toString(buf.Bytes())
	bytebufferpool.Put(buf)

	if originalH != newH {
		r.Set(field, newH)
	}
}

// headerContainsValue checks if a header value already contains the given value
// as a comma-separated element. Per RFC 9110, list elements are separated by commas
// with optional whitespace (OWS) around them.
func headerContainsValue(header, value string) bool {
	// Empty value should never match
	if value == "" {

Review comment (suggestion):
We should call

```go
defer bytebufferpool.Put(buf)
```

After the get.

Code after:
	MediaType string
}

// DefaultRes is the default implementation of Res used by DefaultCtx.
//
//go:generate ifacemaker --file res.go --struct DefaultRes --iface Res --pkg fiber --output res_interface_gen.go --not-exported true --iface-comment "Res is an interface for response-related Ctx methods."
type DefaultRes struct {
	c *DefaultCtx
}

// App returns the *App reference to the instance of the Fiber application
func (r *DefaultRes) App() *App {
	return r.c.app
}

// Append the specified value to the HTTP response header field.
// If the header is not already set, it creates the header with the specified value.
func (r *DefaultRes) Append(field string, values ...string) {
	if len(values) == 0 {
		return
	}
	h := r.c.app.toString(r.c.fasthttp.Response.Header.Peek(field))
	originalH := h
	for _, value := range values {
		if h == "" {
			h = value
		} else if !headerContainsValue(h, value) {
			h += ", " + value
		}
	}
	if originalH != h {
		r.Set(field, h)
	}
}

// headerContainsValue checks if a header value already contains the given value
// as a comma-separated element. Per RFC 9110, list elements are separated by commas
// with optional whitespace (OWS) around them.
func headerContainsValue(header, value string) bool {
	// Empty value should never match
	if value == "" {
		return false
	}

	// Exact match (single value header)
	if header == value {
		return true
	}

	// Check each comma-separated element, handling optional whitespace (OWS)
	for part := range strings.SplitSeq(header, ",") {