-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 #2383, accepts mimeType #2386
Conversation
current master: Benchmark_Ctx_Accepts/run-[]string{".xml"}-2 7432663 162.4 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_Accepts/run-[]string{"json",_"xml"}-2 5298415 226.2 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_Accepts/run-[]string{"application/json",_"application/xml"}-2 8494714 141.1 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsCharsets-2 19419148 61.88 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsEncodings-2 15028786 79.98 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsLanguages-2 18556627 62.99 ns/op 0 B/op 0 allocs/op |
i have reversed the loop for all accepts methods, so that the arguments are passed first and then the header values are checked @efectn @sixcolors @derkan |
after the fix: Benchmark_Ctx_Accepts/run-[]string{".xml"}-2 7367316 162.8 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_Accepts/run-[]string{"json",_"xml"}-2 6219842 192.5 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_Accepts/run-[]string{"application/json",_"application/xml"}-2 8405518 142.6 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsCharsets-2 19289839 62.33 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsEncodings-2 14561808 82.07 ns/op 0 B/op 0 allocs/op
Benchmark_Ctx_AcceptsLanguages-2 18876432 63.73 ns/op 0 B/op 0 allocs/op |
nice solution @ReneWerner87 thank you. 👍 |
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.
It's amazing, you made a good job, congrats dude.
if len(spec) >= 1 && spec[len(spec)-1] == '*' { | ||
return true | ||
} else if strings.HasPrefix(spec, offer) { |
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.
I believe that can we change that about one line. What do you think?
if len(spec) >= 1 && spec[len(spec)-1] == '*' { | |
return true | |
} else if strings.HasPrefix(spec, offer) { | |
if (len(spec) >= 1 && spec[len(spec)-1] == '*') || strings.HasPrefix(spec, offer) { | |
return true |
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.
yes good point, had only split it up for readability and would keep it that way as it is more understandable
@ReneWerner87 good job, this make the existing test cases behave as expected. Reading the ctx.Accepts docs my understanding is that:
Therefore, if my understanding of the intent of the Accept function is correct, the following should hold true:
For example: It is possible that a browser could use the Accept header with the value text/plain;charset=UTF-8;q=0.8,text/plain;charset=US-ASCII;q=0.4,text/html. While text/html is typically assumed to have a default quality value of 1 if not specified, the browser could still explicitly specify it with a quality value of 1, as it is allowed by the HTTP specifications. Furthermore, the browser could be indicating to the server that it prefers text/plain with a charset of UTF-8 over text/plain with a charset of US-ASCII, but it is still willing to accept both with different quality values. This could be useful in scenarios where the server has content in both character sets and wants to serve the content that is preferred by the client. However, it is worth noting that the Accept header sent by the browser may vary depending on the user agent and its configuration. Therefore, it is important for the server to properly parse and interpret the Accept header to determine the preferred content type for the client. Background/RefsThe relevant RFCs for the HTTP/1.1 specification are RFC 7231 and RFC 7232. Regarding the q parameter and its default value of 1, Section 5.3.1 of RFC 7231 states:
Regarding the order of the MIME types in the Accept header, Section 5.3.2 of RFC 7231 states:
MDN on Quality Values https://developer.mozilla.org/en-US/docs/Glossary/Quality_values
|
are you ok with the current fix and you then provide a subsequent solution that takes into account the quantity parameter ? we should also test how expressjs behaves with these new unittest etc. |
A full solution would likely involve a specificity function and sorting of a struct that looks something like:
A performance cost seems likely. Finally; If my understanding of ctx.Accpets() func is correct: May I suggest updating the docs with a similar text to my previous comment, so that there is no ambiguity? |
Yes, the current PR is an improvement, so I'll approve and then submit a PR to deal with quality value and specificity. |
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
Description
Correct the invalid behavior of the
ctx.Accepts
methodFixes #2383
Type of change
Checklist: