<commit_before>package bridgeproxy

import (
	"fmt"
	"io"
	"log"
	"net"
	"net/http"
	"net/url"
	"time"
)

// httpProxyClient implements a http.Handler for proxying requests
type httpProxyClient struct {
	http.Client
	peers []Peer
}

// ServeHTTP serves proxy requests
func (client *httpProxyClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// authenticate
	for k, vs := range client.peers[len(client.peers)-1].ConnectExtra {
		for _, v := range vs {
			r.Header.Add(k, v)
		}
	}
	// net/http.Client does not handle the CONNECT stuff that well below, so
	// let us go a more direct route here - this could be used for the other
	// methods as well, but that would prevent reusing connections to the
	// proxy.
	if r.Method == "CONNECT" {
		log.Println("Dialing for CONNECT to", r.URL)
		remote, err := DialProxy(client.peers)
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
			w.Write([]byte(err.Error()))
			return
		}

		if err = r.WriteProxy(remote); err != nil {
			log.Println(err)
			w.WriteHeader(500)
			w.Write([]byte(err.Error()))
			return
		}

		conn, _, err := w.(http.Hijacker).Hijack()
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
			w.Write([]byte(err.Error()))
			return
		}

		go copyAndClose(conn, remote)
		copyAndClose(remote, conn)
		return<commit_msg>http: Extract serveHTTPConnect() out of ServeHTTP()<commit_after>package bridgeproxy

import (
	"fmt"
	"io"
	"log"
	"net"
	"net/http"
	"net/url"
	"time"
)

// httpProxyClient implements a http.Handler for proxying requests
type httpProxyClient struct {
	http.Client
	peers []Peer
}

// serveHTTPConnect serves proxy requests for the CONNECT method. It does not
// print errors, but rather returns them for your proxy handler to handle.
func (client *httpProxyClient) serveHTTPConnect(w http.ResponseWriter, r *http.Request) error {
	log.Println("Dialing for CONNECT to", r.URL)
	remote, err := DialProxy(client.peers)
	if err != nil {
		return err
	}

	if err = r.WriteProxy(remote); err != nil {
		return err
	}

	conn, _, err := w.(http.Hijacker).Hijack()
	if err != nil {
		return err
	}

	go copyAndClose(conn, remote)
	copyAndClose(remote, conn)
	return nil
}

// ServeHTTP serves proxy requests
func (client *httpProxyClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// authenticate
	for k, vs := range client.peers[len(client.peers)-1].ConnectExtra {
		for _, v := range vs {
			r.Header.Add(k, v)
		}
	}
	// net/http.Client does not handle the CONNECT stuff that well below, so
	// let us go a more direct route here - this could be used for the other
	// methods as well, but that would prevent reusing connections to the
	// proxy.
	if r.Method == "CONNECT" {
		if err := client.serveHTTPConnect(w, r); err != nil {
			log.Println(err)
			w.WriteHeader(500)
			w.Write([]byte(err.Error()))
		}
		return
{'fraction_non_alphanumeric': 0.12834978843441466, 'fraction_numerical': 0.004936530324400564, 'mean_word_length': 3.5247208931419456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963946', 'n_tokens_mistral': 1063, 'n_tokens_neox': 979, 'n_words': 294}