github.com/alejandroEsc/spdy@v0.0.0-20200317064415-01a02f0eb389/examples/proxy_server/proxy_server.go (about)

     1  // Copyright 2014 Jamie Hall. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"net/http"
    12  
    13  	"github.com/SlyMarbo/spdy"
    14  )
    15  
    16  func handle(err error) {
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  }
    21  
    22  func handleProxy(conn spdy.Conn) {
    23  	url := "http://" + conn.Conn().RemoteAddr().String() + "/"
    24  
    25  	req, err := http.NewRequest("GET", url, nil)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	res, err := conn.RequestResponse(req, nil, 1)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  
    35  	buf := new(bytes.Buffer)
    36  	_, err = io.Copy(buf, res.Body)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  
    41  	res.Body.Close()
    42  
    43  	fmt.Println(buf.String())
    44  }
    45  
    46  func main() {
    47  	handler := spdy.ProxyConnHandlerFunc(handleProxy)
    48  	http.Handle("/", spdy.ProxyConnections(handler))
    49  	handle(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
    50  }