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
Status Line does not have CRLF appended properly to it #1132
Comments
It might have something to do with this commit which modifies the statusline logic: 556aa81 |
Okay I've spent some time investigating and reverse engineering the bugs, and it looks like the problem is in this line: 556aa81#diff-445b7d49a79bb217c576f137077e18262a0a2d58e424239fcc01a437397188f7R1659 Before this change, the function call would be func init() {
// Fill all valid status lines
for i := 0; i < len(statusLines); i++ {
statusLines[i] = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", i, StatusMessage(i)))
}
} (that code snippet is available here: https://github.com/valyala/fasthttp/blob/master/status.go#L171) Now, since we're simply returning the editable // parse status code
h.statusCode, n, err = parseUintBuf(b)
if err != nil {
if h.secureErrorLogMessage {
return 0, fmt.Errorf("cannot parse response status code: %s", err)
}
return 0, fmt.Errorf("cannot parse response status code: %s. Response %q", err, buf)
}
if len(b) > n && b[n] != ' ' {
if h.secureErrorLogMessage {
return 0, fmt.Errorf("unexpected char at the end of status code")
}
return 0, fmt.Errorf("unexpected char at the end of status code. Response %q", buf)
}
if len(b) > n+1 && !bytes.Equal(b[n+1:], statusLine(h.statusCode)) {
h.SetStatusLine(b[n+1:])
} (from https://github.com/valyala/fasthttp/blob/master/header.go#L1871) We can see from the above snippet that we're not adding the status code or So the bug is more than just the CRLF endings. It is due to the way the I'll write up a fix for this. |
The way the What I'll need to do to fix this is create a function line this: func formatStatusLine(statusCode int, statusMessage []byte) (ret []byte) {
ret = append(httpHeader, ' ')
ret = append(ret, []byte(strconv.Itoa(statusCode))...)
ret = append(ret, ' ')
ret = append(ret, statusMessage...)
ret = append(ret, strCRLF...)
return
} But when this function gets called in the The best way around this in my opinion, is to remove the following: https://github.com/valyala/fasthttp/blob/master/header.go#L1883 Another option is to call the |
Parsing the status line only parsed the text while the code expects the full line (as the name of the functions implies). Fixes #1132
Parsing the status line only parsed the text while the code expects the full line (as the name of the functions implies). We change the function names to imply that only the text is meant. Fixes #1132
Oh wow I totally missed that. I'm proposing either #1134 or #1133 but I'm not sure what is best. I'm learning towards |
I think your solution #1134 is pretty good, but it misses the bug in I'm a complete idiot and didn't realize the |
Alright, while I was in there I added the ability to modify the protocol as well |
Added some test cases too, and brought in your fixes for SetStatusTest (which I'm proposing should be called |
The main differences are https://github.com/valyala/fasthttp/pull/1135/files#diff-445b7d49a79bb217c576f137077e18262a0a2d58e424239fcc01a437397188f7R1905 which fixes the bug where the It adds the |
Btw this is all proposed in #1135 |
Your commit (#1133) is also pretty good, but it should also take into account this: https://github.com/valyala/fasthttp/pull/1135/files#diff-445b7d49a79bb217c576f137077e18262a0a2d58e424239fcc01a437397188f7R1905 |
What bug? It turns it into the text only so there is no bug there anymore. We already have a huge API so adding even more methods like I'm still in favor of merging #1134 as is. What would you have against that? |
Maybe I'm missing something, but in this line |
Other than that though, I have nothing against #1134 I think since the request header can set protocols it might make sense to have the response headers able to do that as well (and maybe make the AppendStatusLine function unexported to keep the API clean), but I leave that up to your judgement |
* Adding zero-allocation uint64 to byte slice conversion and fixing the ResponseHeader.SetStatusLine function call signature * Removing unnecessary i2b function * Fixing various bugs * Adding test cases * Commenting AppendStatusLine * Update status.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Cleaning up references to strHTTP11, using formatStatusLine for invalidStatusLine, and making `appendStatusLine` an unexported function Issue: #1132 * Fixing merge conflicts Issue: #1132 * Replacing []byte{} with nil in some test cases Issue: #1132 * Cleaning up parsing first line, and improving StatusMessage function Issue: #1132 * Fixing as per PR * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Fixing as per requested changes * Update header_test.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
I think I've found a bug where a response status line is not properly formatted then the
ResponseHeader.AppendBytes
function is called.A screenshot of the error is attached:
You can see the error in the bottom part of the above screenshot
What seems to be happening is pretty clear, the response is parsed correctly (I have confirmed this), but when the
AppendBytes
function is being called, a newline (CRLF) is not being appended in the following lines:The text was updated successfully, but these errors were encountered: