Home / Function/ getOffer() — fiber Function Reference

getOffer() — fiber Function Reference

Architecture documentation for the getOffer() function in helpers.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  e3cc6318_07a4_ecac_7c64_699264bff354["getOffer()"]
  bec0e401_e4cd_f765_6df3_a79059073e50["helpers.go"]
  e3cc6318_07a4_ecac_7c64_699264bff354 -->|defined in| bec0e401_e4cd_f765_6df3_a79059073e50
  b98f3fa5_2e76_2ad8_cd62_dbccbf59f37e["forEachMediaRange()"]
  e3cc6318_07a4_ecac_7c64_699264bff354 -->|calls| b98f3fa5_2e76_2ad8_cd62_dbccbf59f37e
  075b5250_dc10_53b4_3b25_4828630c476f["unescapeHeaderValue()"]
  e3cc6318_07a4_ecac_7c64_699264bff354 -->|calls| 075b5250_dc10_53b4_3b25_4828630c476f
  9c05c3f3_14ea_d339_1411_336245d4b7f1["sortAcceptedTypes()"]
  e3cc6318_07a4_ecac_7c64_699264bff354 -->|calls| 9c05c3f3_14ea_d339_1411_336245d4b7f1
  style e3cc6318_07a4_ecac_7c64_699264bff354 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

helpers.go lines 527–636

func getOffer(header []byte, isAccepted func(spec, offer string, specParams headerParams) bool, offers ...string) string {
	if len(offers) == 0 {
		return ""
	}
	if len(header) == 0 {
		return offers[0]
	}

	acceptedTypes := make([]acceptedType, 0, 8)
	order := 0

	// Parse header and get accepted types with their quality and specificity
	// See: https://www.rfc-editor.org/rfc/rfc9110#name-content-negotiation-fields
	forEachMediaRange(header, func(accept []byte) {
		order++
		spec, quality := accept, 1.0
		var params headerParams

		if i := bytes.IndexByte(accept, ';'); i != -1 {
			spec = accept[:i]

			// Optimized quality parsing
			qIndex := i + 3
			if bytes.HasPrefix(accept[i:], semicolonQEquals) && bytes.IndexByte(accept[qIndex:], ';') == -1 {
				if q, err := fasthttp.ParseUfloat(accept[qIndex:]); err == nil {
					quality = q
				}
			} else {
				params, _ = headerParamPool.Get().(headerParams) //nolint:errcheck // only contains headerParams
				for k := range params {
					delete(params, k)
				}
				fasthttp.VisitHeaderParams(accept[i:], func(key, value []byte) bool {
					if len(key) == 1 && key[0] == 'q' {
						if q, err := fasthttp.ParseUfloat(value); err == nil {
							quality = q
						}
						return false
					}
					lowerKey := utils.UnsafeString(utils.ToLowerBytes(key))
					val, err := unescapeHeaderValue(value)
					if err != nil {
						return true
					}
					params[lowerKey] = val
					return true
				})
			}

			// Skip this accept type if quality is 0.0
			// See: https://www.rfc-editor.org/rfc/rfc9110#quality.values
			if quality == 0.0 {
				return
			}
		}

		spec = utils.TrimSpace(spec)

		// Determine specificity
		var specificity int

		// check for wildcard this could be a mime */* or a wildcard character *
		switch {
		case len(spec) == 1 && spec[0] == '*':
			specificity = 1
		case bytes.Equal(spec, wildcardAll):
			specificity = 1
		case bytes.HasSuffix(spec, wildcardSuffix):
			specificity = 2
		case bytes.IndexByte(spec, '/') != -1:
			specificity = 3
		default:
			specificity = 4
		}

		// Add to accepted types
		acceptedTypes = append(acceptedTypes, acceptedType{
			spec:        utils.UnsafeString(spec),
			quality:     quality,
			specificity: specificity,
			order:       order,

Domain

Subdomains

Defined In

Frequently Asked Questions

What does getOffer() do?
getOffer() is a function in the fiber codebase, defined in helpers.go.
Where is getOffer() defined?
getOffer() is defined in helpers.go at line 527.
What does getOffer() call?
getOffer() calls 3 function(s): forEachMediaRange, sortAcceptedTypes, unescapeHeaderValue.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free