upstreamRequestURL extracts a best-effort URL string from a raw HTTP request buffer. Falls back to the dest-socket address if the request cannot be parsed (e.g. truncated / non-HTTP bytes). The returned string is log-only; callers must not depend on its format.
(rawReq []byte, destAddr net.Addr)
| 117 | // cannot be parsed (e.g. truncated / non-HTTP bytes). The returned string |
| 118 | // is log-only; callers must not depend on its format. |
| 119 | func upstreamRequestURL(rawReq []byte, destAddr net.Addr) string { |
| 120 | if len(rawReq) > 0 { |
| 121 | req, err := nhttp.ReadRequest(bufio.NewReader(bytes.NewReader(rawReq))) |
| 122 | if err == nil && req != nil { |
| 123 | host := req.Host |
| 124 | if host == "" && destAddr != nil { |
| 125 | host = destAddr.String() |
| 126 | } |
| 127 | path := "/" |
| 128 | if req.URL != nil && req.URL.RequestURI() != "" { |
| 129 | path = req.URL.RequestURI() |
| 130 | } |
| 131 | scheme := "http" |
| 132 | return fmt.Sprintf("%s://%s%s", scheme, host, path) |
| 133 | } |
| 134 | } |
| 135 | if destAddr != nil { |
| 136 | return destAddr.String() |
| 137 | } |
| 138 | return "unknown" |
| 139 | } |
| 140 | |
| 141 | // parseRequestMethodAndURL extracts (method, request-uri) from a raw HTTP/1.x |
| 142 | // request buffer on a best-effort basis. Both return values are "" if the |