parseForwarded extracts the proto and host directives from the first element of an RFC 7239 Forwarded header (e.g. `for=x;proto=https;host=h, for=y`). Values may be quoted. Returns empty strings when absent or malformed so the caller can fall through to other signals.
(header string)
| 100 | // Values may be quoted. Returns empty strings when absent or malformed so the |
| 101 | // caller can fall through to other signals. |
| 102 | func parseForwarded(header string) (proto, host string) { |
| 103 | if header == "" { |
| 104 | return "", "" |
| 105 | } |
| 106 | // Only the first element (closest proxy to the client) matters here. |
| 107 | if i := strings.IndexByte(header, ','); i >= 0 { |
| 108 | header = header[:i] |
| 109 | } |
| 110 | for _, directive := range strings.Split(header, ";") { |
| 111 | key, value, ok := strings.Cut(strings.TrimSpace(directive), "=") |
| 112 | if !ok { |
| 113 | continue |
| 114 | } |
| 115 | value = strings.Trim(strings.TrimSpace(value), `"`) |
| 116 | switch strings.ToLower(strings.TrimSpace(key)) { |
| 117 | case "proto": |
| 118 | proto = value |
| 119 | case "host": |
| 120 | host = value |
| 121 | } |
| 122 | } |
| 123 | return proto, host |
| 124 | } |
no outgoing calls
no test coverage detected