-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
140 lines (119 loc) · 2.93 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"github.com/hashicorp/go-cleanhttp"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
func serveHTTP(name, addr string) error {
if !isValidAddr(addr) {
return fmt.Errorf("-http-addr is invalid %q", addr)
}
log.Printf("HTTP listening on %s", addr)
clientHTTP1 := cleanhttp.DefaultClient()
clientHTTP2 := cleanhttp.DefaultClient()
clientHTTP2.Transport = &http2.Transport{
AllowHTTP: true,
DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
},
}
mux := http.NewServeMux()
mux.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
u := r.URL.Query().Get("url")
switch {
case strings.HasPrefix(u, "http://"):
case strings.HasPrefix(u, "https://"):
default:
http.Error(w, fmt.Sprintf("bad proxy url %q", u),
http.StatusBadRequest,
)
return
}
_, err := url.Parse(u)
if err != nil {
http.Error(w, fmt.Sprintf("bad proxy url %q: %v", u, err),
http.StatusBadRequest,
)
return
}
proxyReq, err := http.NewRequestWithContext(r.Context(), http.MethodGet, u, nil)
if err != nil {
http.Error(w, fmt.Sprintf("bad proxy url %q: %v", u, err),
http.StatusBadRequest,
)
return
}
var client *http.Client
switch r.ProtoMajor {
case 1:
client = clientHTTP1
case 2:
client = clientHTTP2
default:
http.Error(w, "unsupported http request protocol version",
http.StatusBadRequest,
)
return
}
proxyResp, err := client.Do(proxyReq)
if err != nil {
http.Error(w, fmt.Sprintf("error making request: %v", err),
http.StatusInternalServerError,
)
return
}
for name := range proxyResp.Header {
w.Header().Del(name)
for _, val := range proxyResp.Header.Values(name) {
w.Header().Add(name, val)
}
}
w.WriteHeader(proxyResp.StatusCode)
if proxyResp.Body != nil {
defer proxyResp.Body.Close()
_, err := io.Copy(w, proxyResp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("error returning response to caller: %v", err),
http.StatusInternalServerError,
)
return
}
}
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
type httpResponse struct {
Name string
}
out := &httpResponse{Name: name}
enc := json.NewEncoder(w)
w.Header().Set("content-type", "application/json")
if err := enc.Encode(&out); err != nil {
log.Printf("ERROR: %v", err)
}
})
h2s := &http2.Server{}
srv := &http.Server{
Addr: addr,
Handler: h2c.NewHandler(mux, h2s),
}
return srv.ListenAndServe()
}