-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmina.go
180 lines (155 loc) · 4.33 KB
/
mina.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"bytes"
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
)
const (
XHeaderName = "X-MINA-CACHE"
XHeaderValueHit = "hit"
XHeaderValueMiss = "miss"
XHeaderValueIgnore = "ignore"
RequestOptionsHeaderName = "X-MINA-OPTIONS"
)
type Mina struct {
Target *url.URL
CacheDir string
Headers map[string]string
}
// newSingleHostReverseProxy is copied from stdlib, except we are change
// req.Host here to req.URL.Host
func newSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
req.URL.Host = target.Host
req.Host = req.URL.Host
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
return &httputil.ReverseProxy{Director: director}
}
// singleJoiningSlash is coped from stdlib, because it was called from
// newSingleHostReverseProxy.
func singleJoiningSlash(a, b string) string {
aSlash := strings.HasSuffix(a, "/")
bSlash := strings.HasPrefix(b, "/")
switch {
case aSlash && bSlash:
return a + b[1:]
case !aSlash && !bSlash:
return a + "/" + b
}
return a + b
}
func writeHeadersToWR(wr http.ResponseWriter, resp *http.Response, headers map[string]string, xHeaderValue string) {
// write headers
for name := range resp.Header {
// overwrite custom headers
if _, ok := headers[name]; !ok {
wr.Header().Add(name, resp.Header.Get(name))
}
}
for name, value := range headers {
wr.Header().Add(name, value)
}
wr.Header().Add(XHeaderName, xHeaderValue)
}
func writeBodyToWR(wr http.ResponseWriter, resp *http.Response) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("\033[0;31mError: %s\033[0m", err)
return
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
wr.Write(body)
}
func cacheWrite(path string, filename string, body []byte) {
err := os.MkdirAll(path, 0755)
if err != nil {
log.Printf("Error while mkdir: %s", err)
return
}
err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
log.Printf("Error while writing: %s", err)
return
}
}
func isFileExist(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
func requestMD5(req *http.Request) (string, []byte) {
h := md5.New()
body, _ := httputil.DumpRequest(req, true)
io.WriteString(h, fmt.Sprintf("%+v", string(body)))
return fmt.Sprintf("%x", h.Sum(nil)), body
}
func (m *Mina) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
p := newSingleHostReverseProxy(m.Target)
req.Header.Del("If-Modified-Since")
req.Header.Del("If-None-Match")
if req.Header.Get(RequestOptionsHeaderName) == XHeaderValueIgnore {
wrRecorder := httptest.NewRecorder()
p.ServeHTTP(wrRecorder, req)
resp := wrRecorder.Result()
defer resp.Body.Close()
writeHeadersToWR(wr, resp, m.Headers, XHeaderValueMiss)
writeBodyToWR(wr, resp)
return
}
md5, reqDump := requestMD5(req)
reqFilename := filepath.Join(m.CacheDir, fmt.Sprintf("%s.req", md5))
resFilename := filepath.Join(m.CacheDir, fmt.Sprintf("%s.res", md5))
if isFileExist(resFilename) {
log.Printf("%s [HIT] %s %s", filepath.Base(resFilename)[:8], req.Method, req.URL)
resDump, err := ioutil.ReadFile(resFilename)
if err != nil {
log.Println(err)
return
}
dumpIO := bufio.NewReader(bytes.NewBuffer(resDump))
resp, err := http.ReadResponse(dumpIO, req)
if err != nil {
log.Printf("Error: %s", err)
return
}
defer resp.Body.Close()
writeHeadersToWR(wr, resp, m.Headers, XHeaderValueHit)
writeBodyToWR(wr, resp)
} else {
log.Printf("%s [MISS] %s %s", filepath.Base(resFilename)[:8], req.Method, req.URL)
wrRecorder := httptest.NewRecorder()
p.ServeHTTP(wrRecorder, req)
resp := wrRecorder.Result()
defer resp.Body.Close()
writeHeadersToWR(wr, resp, m.Headers, XHeaderValueMiss)
writeBodyToWR(wr, resp)
if resp.StatusCode == http.StatusNotModified {
return
}
resDump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Printf("Error: %s", err)
return
}
go cacheWrite(m.CacheDir, resFilename, resDump)
go cacheWrite(m.CacheDir, reqFilename, reqDump)
}
}