Skip to content
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: DumpResponse error in HEAD request #572

Merged
merged 1 commit into from
Jun 19, 2024
Merged

Conversation

yuweizzz
Copy link
Contributor

fix:

bin/ecapture tls -m text
curl -I --http1.1  https://www.google.com/
......
2024-06-18T10:19:58+08:00 INF UUID:104243_104243_curl_0_1_0.0.0.0, Name:HTTPRequest, Type:1, Length:75
HEAD / HTTP/1.1
Host: www.google.com
Accept: */*
User-Agent: curl/7.88.1



2024/06/18 10:19:58 [http response] DumpResponse error: unexpected EOF
......

@cfc4n
Copy link
Member

cfc4n commented Jun 18, 2024

Can you provide some reasons for the bugs?

@yuweizzz
Copy link
Contributor Author

We use http.ReadResponse(buf, nil) to get the Response. This function defaults to handling it as a GET Method because no request param is passed. The Response.Body will be a new struct. When we dump the response, the Response.Body.Close() will be called and raise an IO error.

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
)

func main() {
	const body = "abc"
	ts := 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", "9999")
		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)
	}

	rd := bytes.NewReader(dump)
	buf := bufio.NewReader(rd)
	resp, err = http.ReadResponse(buf, nil)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(resp.Body)
	err = resp.Body.Close()
	fmt.Println(err)

       //&{0xc0000b4018 <nil> <nil> false false {0 0} false false false <nil>}
       //unexpected EOF
}

@cfc4n cfc4n added bug Something isn't working fix bug fix PR labels Jun 19, 2024
Copy link
Member

@cfc4n cfc4n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@cfc4n cfc4n merged commit 5a66d6a into gojue:master Jun 19, 2024
6 of 7 checks passed
@yuweizzz yuweizzz deleted the dumpresp branch June 20, 2024 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix bug fix PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants