Skip to content

Instantly share code, notes, and snippets.

@zz
Forked from fabrizioc1/http-proxy.go
Created October 25, 2018 06:58
Show Gist options
  • Save zz/d0548baa1344be1377af2402563a2b53 to your computer and use it in GitHub Desktop.
Save zz/d0548baa1344be1377af2402563a2b53 to your computer and use it in GitHub Desktop.
Http proxy server in Go
package main
import (
"fmt"
"io"
"log"
"net/http"
)
type HttpConnection struct {
Request *http.Request
Response *http.Response
}
type HttpConnectionChannel chan *HttpConnection
var connChannel = make(HttpConnectionChannel)
func PrintHTTP(conn *HttpConnection) {
fmt.Printf("%v %v\n", conn.Request.Method, conn.Request.RequestURI)
for k, v := range conn.Request.Header {
fmt.Println(k, ":", v)
}
fmt.Println("==============================")
fmt.Printf("HTTP/1.1 %v\n", conn.Response.Status)
for k, v := range conn.Response.Header {
fmt.Println(k, ":", v)
}
fmt.Println(conn.Response.Body)
fmt.Println("==============================")
}
func HandleHTTP() {
for {
select {
case conn := <-connChannel:
PrintHTTP(conn)
}
}
}
type Proxy struct {
}
func NewProxy() *Proxy { return &Proxy{} }
func (p *Proxy) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
var resp *http.Response
var err error
var req *http.Request
client := &http.Client{}
//log.Printf("%v %v", r.Method, r.RequestURI)
req, err = http.NewRequest(r.Method, r.RequestURI, r.Body)
for name, value := range r.Header {
req.Header.Set(name, value[0])
}
resp, err = client.Do(req)
r.Body.Close()
// combined for GET/POST
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
conn := &HttpConnection{r, resp}
for k, v := range resp.Header {
wr.Header().Set(k, v[0])
}
wr.WriteHeader(resp.StatusCode)
io.Copy(wr, resp.Body)
resp.Body.Close()
PrintHTTP(conn)
//connChannel <- &HttpConnection{r,resp}
}
func main() {
//go HandleHTTP()
proxy := NewProxy()
fmt.Println("==============================")
err := http.ListenAndServe(":12345", proxy)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment