-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (102 loc) · 2.55 KB
/
main.go
File metadata and controls
120 lines (102 loc) · 2.55 KB
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/vulcand/oxy/forward"
)
// Trap ServeHTTP's ResponseWriter so that response headers and body can be
// written to Stdout.
type responseWriterTrap struct {
status int
writer http.ResponseWriter
}
func (w responseWriterTrap) Header() http.Header {
return w.writer.Header()
}
func (w responseWriterTrap) Write(p []byte) (int, error) {
if w.status != http.StatusOK {
os.Stdout.Write(p)
}
return w.writer.Write(p)
}
func (w *responseWriterTrap) WriteHeader(i int) {
fmt.Printf("\n-----\n")
fmt.Printf("RESPONSE STATUS: %d %s\n", i, http.StatusText(i))
for k, v := range w.writer.Header() {
fmt.Printf("%s: %s\n", k, v[0])
}
fmt.Println()
w.status = i
w.writer.WriteHeader(i)
}
// To log the request headers and body to Stdout.
type logger struct {
h http.Handler
}
func (l logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("\n---------------------------\n")
fmt.Printf("REQUEST : %s path:%s", r.Method, r.URL.Path)
if r.URL.RawQuery != "" {
fmt.Printf("?%s", r.URL.RawQuery)
}
fmt.Println()
fmt.Printf("Host: %s\n", r.Host)
for k, v := range r.Header {
fmt.Printf("%s: %s\n", k, v[0])
}
fmt.Println()
l.h.ServeHTTP(&responseWriterTrap{0, w}, r)
fmt.Printf("\n--------------------------\n")
}
// To forward the request to the address specified with -f
type forwarder struct {
scheme string
host string
h http.Handler
}
func (f forwarder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Scheme = f.scheme
r.URL.Host = f.host
// body := r.Body
// r.Body = struct {
// io.Reader
// io.Closer
// }{
// io.TeeReader(body, os.Stdout),
// closer(func() error {
// return body.Close()
// }),
// }
f.h.ServeHTTP(w, r)
}
// To typecast a func to io.Closer
type closer func() error
func (c closer) Close() error {
return c()
}
type rewrite struct{}
func (r rewrite) Rewrite(req *http.Request) {
}
func main() {
listenAddr := flag.String("l", ":8000", "listen address")
fwdAddr := flag.String("f", "localhost:9000", "forward address")
cert := flag.String("cert", "", "certificate")
key := flag.String("key", "", "private key")
flag.Parse()
fwd, _ := forward.New(
forward.PassHostHeader(true),
forward.Rewriter(rewrite{}),
forward.RoundTripper(&http.Transport{DisableCompression: true}),
)
server := &http.Server{
Addr: *listenAddr,
Handler: logger{forwarder{"http", *fwdAddr, fwd}},
}
if *cert != "" && *key != "" {
fmt.Println(server.ListenAndServeTLS(*cert, *key))
} else {
fmt.Println(server.ListenAndServe())
}
}