fasthttp
fasthttp copied to clipboard
Peek Headers with the same name
Hi !
It's possible to get multiple headers with the same name without parsing all headers in requestHeader struct (without using visitAll function) ?
ctx.Request.Header.Peek("X-Your-Some-Header")
It returns the first header with "X-Your-Some-Header" name. I would like same method returns all headers named "X-Your-Some-Header"
@nsagnett What do you mean, you have multiple headers with the same key?
It's possible ! It's allowed by RFC
Can you show example your a request with multiple headers?
curl -v -H "X-Your-Some-Header: foo" -H "X-Your-Some-Header: bar" localhost:8080, you can see two headers with the same key
Could you show reference to the rfc part please?
https://tools.ietf.org/html/rfc3864 (2.2.1)
I found https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 tnx
Yes, multiple values are allowed, multiple keys are not possible, I though you meant multiple keys at my first reading, but I misunderstood, sorry. Practical you have right then, if fasthttp doesn't supports multiple headers values yet this is a problem but you can fix it with a PR, don't be afraid, we are here!
@kataras "Yes, multiple values are allowed, multiple keys are not possible"
Actually, the HTTP RFC does state that multiple (duplicated) keys MAY exist in an HTTP header. They very carefully explain that proxies are not allowed to change the order of duplicates as the value order may be significant:
Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.
A good library with the bad document, I even don't know how to send a customed HTTP GET request.
@aiddroid Using the Request structure :) Just like this
package main
import (
"log"
"github.com/valyala/fasthttp"
)
func main() {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.Header.SetMethod("GET")
req.SetRequestURI("https://borja.es")
req.Header.Add("X-Custom-Header", "Test")
if err := fasthttp.Do(req,res); err != nil {
log.Fatalln(err)
}
log.Printf("Response body length: %d\n", len(res.Body()))
}
@dgrr Thanks, Add this demo to the README file may be helpful.
It has been discussed previously I believe, with the suggestion to implement PeekAll that returns a slice.
For context, go stdlib merges headers since per HTTP spec K: v1\r\nK: v2 is identical to K: v1; v2. This of course impacts performance.
Hi all, what is the current status of this issue?
Currently, we only able to access our duplicate key headers as below:
reqHeaders := make(map[string][]string)
req.Header.VisitAll(func(k, v []byte) {
key := *(*string)(unsafe.Pointer(&k))
val := *(*string)(unsafe.Pointer(&v))
reqHeaders[key] = append(reqHeaders[key], val)
})
I'm ok with a pull request to add methods like func (h *RequestHeader) PeekAll(key string) [][]byte.