fix: truncated body dump error#573
Merged
Merged
Conversation
Member
|
Can you add a unit test? Cover both cases where |
Contributor
Author
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
)
func main() {
// normal
const body = "abcde"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
fmt.Fprintln(w, body)
}))
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(dump))
rd := bytes.NewReader(dump)
buf := bufio.NewReader(rd)
read_resp, err := http.ReadResponse(buf, nil)
if err != nil {
fmt.Println(err)
}
data, err := io.ReadAll(read_resp.Body)
// normal, err is nil
fmt.Println(data, err)
fmt.Println("-------------------")
// normal, content-length is 0
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
}))
defer ts.Close()
resp, err = http.Get(ts.URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(dump))
rd = bytes.NewReader(dump)
buf = bufio.NewReader(rd)
read_resp, err = http.ReadResponse(buf, nil)
if err != nil {
fmt.Println(err)
}
data, err = io.ReadAll(read_resp.Body)
// normal, err is nil
fmt.Println(data, err)
fmt.Println("-------------------")
// head method respone, content-length is 6, real length is 0
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
fmt.Fprintln(w, body)
}))
defer ts.Close()
resp, err = http.Head(ts.URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
// head method respone
fmt.Println(string(dump))
rd = bytes.NewReader(dump)
buf = bufio.NewReader(rd)
read_resp, err = http.ReadResponse(buf, nil)
if err != nil {
fmt.Println(err)
}
data, err = io.ReadAll(read_resp.Body)
// head method respone, but DumpResponse required body, err is ErrUnexpectedEOF
fmt.Println(data, err)
fmt.Println("-------------------")
// chunked respone, no content-length
ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
w.Header().Set("Content-Length", "")
fmt.Fprintln(w, body)
}))
defer ts1.Close()
resp, err = http.Get(ts1.URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(dump))
rd = bytes.NewReader(dump)
buf = bufio.NewReader(rd)
read_resp, err = http.ReadResponse(buf, nil)
if err != nil {
fmt.Println(err)
}
data, err = io.ReadAll(read_resp.Body)
// chunked respone, err is nil
fmt.Println(data, err)
fmt.Println("-------------------")
// truncated respone, real data length smaller than content-length
t := "HTTP/1.1 200 OK\r\nContent-Length: 9999\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 19 Jul 1972 19:00:00 GMT\r\n\r\nabcde\n"
rd = bytes.NewReader([]byte(t))
buf = bufio.NewReader(rd)
read_resp, err = http.ReadResponse(buf, nil)
if err != nil {
fmt.Println(err)
}
data, err = io.ReadAll(read_resp.Body)
// truncated respone, err is ErrUnexpectedEOF
fmt.Println(data, err)
} |
cfc4n
reviewed
Jun 23, 2024
| hr.packerType = PacketTypeGzip | ||
| defer reader.Close() | ||
| default: | ||
| hr.packerType = PacketTypeNull |
Member
There was a problem hiding this comment.
Is the code from lines 108 to 143 merely for parsing packerType?
| default: | ||
| hr.packerType = PacketTypeNull | ||
| } | ||
| b, err := httputil.DumpRequest(hr.request, false) |
Member
|
@yuweizzz Thank you for your contribution. What I mean is "Could you add some unit tests in the PR to verify your code?" |
Member
|
Merge this PR first, and I will add unit tests. thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
this is the extend of #572 .
The request/response body may be truncated in events, so the 'Content-Length' header value not match the real data length, ReadAll/Close will always raise UnexpectedEOF error. #572 can only fix the real data length is 0, the pr will fix the others.