// Handles a single client connection.
Handles a single client connection.
// Handles a single client connection .
func handleConnection(conn net.Conn) (err error) {
	defer conn.Close()
	timestamp := time.Now().UnixNano()
	conn.SetDeadline(time.Now().Add(100 * time.Millisecond))
	in := bufio.NewReader(conn)
	buffer, err := in.Peek(1024)
	query := queryPattern.FindStringSubmatch(string(buffer))
	if query == nil {
		err = fmt.Errorf("Failed to parse HTTP query header.")
		return
	}
	uri, err := url.ParseRequestURI(query[2])
	if err != nil {
		return
	}
	var target string
	if portPattern.MatchString(uri.Host) {
		target = uri.Host
	} else {
		target = fmt.Sprintf("%s:80", uri.Host)
	}
	fmt.Printf("Handling request for %s.\n", uri)
	host, err := net.Dial("tcp", target)
	if err != nil {
		return
	}
	defer host.Close()
	reqLog, err := os.Create(fmt.Sprintf("%d_req", timestamp))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	resLog, err := os.Create(fmt.Sprintf("%d_res", timestamp))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	err = pipe(in, tee { host, reqLog, 1000 }, 100)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	err = pipe(host, tee { conn, resLog, 1000 }, 2000)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	return
}
func handleConnection ( conn net . Conn ) ( err error ) { defer conn . Close ( ) timestamp := time . Now ( ) . UnixNano ( ) conn . SetDeadline ( time . Now ( ) . Add ( 100 * time . Millisecond ) ) in := bufio . NewReader ( conn ) buffer , err := in . Peek ( 1024 ) query := queryPattern . FindStringSubmatch ( string ( buffer ) ) if query == nil { err = fmt . Errorf ( " " ) return } uri , err := url . ParseRequestURI ( query [ 2 ] ) if err != nil { return } var target string if portPattern . MatchString ( uri . Host ) { target = uri . Host } else { target = fmt . Sprintf ( " " , uri . Host ) } fmt . Printf ( " \n " , uri ) host , err := net . Dial ( " " , target ) if err != nil { return } defer host . Close ( ) reqLog , err := os . Create ( fmt . Sprintf ( " " , timestamp ) ) if err != nil { fmt . Fprintln ( os . Stderr , err ) return } resLog , err := os . Create ( fmt . Sprintf ( " " , timestamp ) ) if err != nil { fmt . Fprintln ( os . Stderr , err ) return } err = pipe ( in , tee { host , reqLog , 1000 } , 100 ) if err != nil { fmt . Fprintln ( os . Stderr , err ) return } err = pipe ( host , tee { conn , resLog , 1000 } , 2000 ) if err != nil { fmt . Fprintln ( os . Stderr , err ) return } return }
Handles a single client connection.
Handles a single client connection .