-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix go-ipfs#7242: Remove "HEAD" from Allow methods #195
Conversation
I think it will be less data transferred in each response if vales of That is:
Instead of
These repeated bytes will add up if the list of allowed methods is long. |
I don't know. The list of allowed methods is short (for 4 headers, 21 bytes overhead in the worst case?). But then I have to allocate a slice and an additional string for the Join operation the code becomes a bit less readable so I'm not sure it's super worth it either way. |
Another motivation to join the values would be better client support.As per the specification both styles are correct, but in most of the examples of the HTTP RFC they show the comma separated style. It has a consequence in poorly written clients or scripts to overlook the possibility of repeated headers and only consume the first or the last entry.
If you are concerned about programmers maintaining yet another slice and string, then you can do something like the following (one line inclusion at the end) to leverage the internal data structure that is maintained by the standard library to support Headers. w.Header().Add("Allow", http.MethodOptions)
w.Header().Add("Allow", http.MethodPost)
if allowGet {
w.Header().Add("Allow", http.MethodHead)
w.Header().Add("Allow", http.MethodGet)
}
w.Header().Set("Allow", strings.Join(w.Header()["Allow"], ", ")) However, if you are concerned about memory and execution micro efficiency then allocating a slice with the two default values, concatenating an anonymous slice with a couple more values to it conditionally, and then anonymously joining them to set the header at once should be more efficient. The memory efficiency comes from the fact that the lifecycle of our slice will be shorter and it can be garbage collected immediately after values are joined and the header is set. While, if we had atomic header values stored inside of the Header (which is stored as a slice per key), we will be storing four small strings in one slice vs. one long string in one slice. Time efficiency comes from minimized function call stack and reduced slice mutations. Each time |
@ibnesayeed I have given you write permissions, can you update the PR with your proposal? |
@hsanjuan which approach do you want me to push? The example with one line addition I illustrated above or the optimized one that I discussed? |
The optimized |
@hsanjuan I have updated the code in the first commit to reflect what I was suggesting. However, I have also pushed a second commit to change the function name (from |
Thanks @ibnesayeed for jumping in to improve things. Very much appreciated. I'll let @Stebalien merge at will. |
cd5dd93
to
31eee8c
Compare
Rebased to remove merge commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
(when GET is not allowed).
Fixes ipfs/kubo#7242