コード例 #1
0
ファイル: httproxy.go プロジェクト: zengkid/goproxy
func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	log.Info("http: %s %s", req.Method, req.URL)

	if req.Method == "CONNECT" {
		p.Connect(w, req)
		return
	}

	req.RequestURI = ""
	for _, h := range hopHeaders {
		if req.Header.Get(h) != "" {
			req.Header.Del(h)
		}
	}

	resp, err := p.transport.RoundTrip(req)
	if err != nil {
		log.Error("%s", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	defer resp.Body.Close()

	copyHeader(w.Header(), resp.Header)
	w.WriteHeader(resp.StatusCode)
	_, err = sutils.CoreCopy(w, resp.Body)
	if err != nil {
		log.Error("%s", err)
		return
	}
	return
}
コード例 #2
0
ファイル: httproxy.go プロジェクト: xyz12810/goproxy-1
func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	log.Info("http: %s %s", req.Method, req.URL)

	if p.username != "" && p.password != "" {
		if !BasicAuth(w, req, p.username, p.password) {
			log.Error("Http Auth Required")
			w.Header().Set("Proxy-Authenticate", "Basic realm=\"GoProxy\"")
			http.Error(w, http.StatusText(407), 407)
			return
		}
	}

	if req.Method == "CONNECT" {
		p.Connect(w, req)
		return
	}

	req.RequestURI = ""
	for _, h := range hopHeaders {
		if req.Header.Get(h) != "" {
			req.Header.Del(h)
		}
	}

	resp, err := p.transport.RoundTrip(req)
	if err != nil {
		log.Error("%s", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	defer resp.Body.Close()

	copyHeader(w.Header(), resp.Header)
	w.WriteHeader(resp.StatusCode)

	_, err = sutils.CoreCopy(w, resp.Body)
	if err != nil {
		log.Error("%s", err)
		return
	}
	return
}