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 http proxy behavior #1221

Merged
merged 1 commit into from Feb 18, 2022
Merged

Fix http proxy behavior #1221

merged 1 commit into from Feb 18, 2022

Conversation

Aoang
Copy link
Contributor

@Aoang Aoang commented Feb 18, 2022

@Aoang
Copy link
Contributor Author

Aoang commented Feb 18, 2022

func FastTestProxy(proxyAddr string) {
	var (
		req  = &fasthttp.Request{}
		resp = &fasthttp.Response{}
		cc   = &fasthttp.Client{Dial: fasthttpproxy.FasthttpHTTPDialer(proxyAddr), ReadTimeout: 30 * time.Second}
	)

	req.SetRequestURI("https://ip.x2ox.com/api/qqwry?type=json")
	req.Header.SetMethod("GET")
	if err := cc.DoTimeout(req, resp, 30*time.Second); err != nil {
		log.Fatalln(err)
	}
	log.Println(string(resp.Body()))
}

func HttpTestProxy(proxyAddr string) {
	proxy, err := url.Parse(proxyAddr)
	if err != nil {
		log.Fatalln(err)
	}

	httpClient := &http.Client{
		Timeout: time.Second * 20,
		Transport: &http.Transport{
			Proxy:                 http.ProxyURL(proxy),
			MaxIdleConnsPerHost:   -1,
			ResponseHeaderTimeout: time.Second * time.Duration(10),
		},
	}

	var resp *http.Response
	resp, err = httpClient.Get("https://ip.x2ox.com/api/qqwry?type=json")
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	c, _ := ioutil.ReadAll(resp.Body)
	log.Println(string(c))
}

As expected, the data emitted is

CONNECT ip.x2ox.com:443 HTTP/1.1
Proxy-Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ

and

CONNECT ip.x2ox.com:443 HTTP/1.1
Host: ip.x2ox.com:443
Proxy-Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ

@Aoang
Copy link
Contributor Author

Aoang commented Feb 18, 2022

HTTP Proxy needs to follow the RFC7230 standard.

https://datatracker.ietf.org/doc/html/rfc7230

But long before RFC7230 was formulated in 2014, the CONNECT tunnel principle was widely implemented.

@erikdubbelboer erikdubbelboer merged commit b85d2a2 into valyala:master Feb 18, 2022
@erikdubbelboer
Copy link
Collaborator

Thanks!

@Aoang Aoang deleted the fix-http-proxy-behavior branch August 14, 2022 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants