-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
108 lines (88 loc) · 2.36 KB
/
proxy.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
package main
import (
"errors"
"io"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
)
type Proxy struct {
config *config
contentTypeRegex *regexp.Regexp
}
func newProxy(config *config) *Proxy {
// Todo, add config error checking
contentTypeRegex, err := regexp.Compile(config.AllowedContentTypes)
if err != nil {
log.Panic(err)
}
return &Proxy{
config: config,
contentTypeRegex: contentTypeRegex,
}
}
func (p Proxy) handler(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
if err := p.validRequest(params); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
p.proxyRequest(w, params)
}
}
func (p Proxy) validRequest(params url.Values) error {
url := params.Get("url")
if url == "" {
return errors.New("No request url to proxy")
}
return nil
}
func (p Proxy) proxyRequest(w http.ResponseWriter, params url.Values) {
url := params.Get("url")
resp, err := http.Get(url) // http.Get follows up to 10 redirects
defer resp.Body.Close()
if err != nil {
log.Print(err)
// Todo, handle specific errors
http.Error(w, "Could not proxy", http.StatusInternalServerError)
}
if resp.StatusCode != 200 {
log.Printf("Upstream response: %v", resp.StatusCode)
http.Error(w, "Could not proxy", resp.StatusCode)
return
}
if err := p.validResponse(resp); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
width := params.Get("width")
height := params.Get("height")
if width != "" && height != "" {
img, _ := newImg(resp.Body)
if err != nil {
// TODO, handle error (redirect to original url?)
log.Println(err)
http.Error(w, "Could not create img", http.StatusInternalServerError)
return
}
// TODO, handle missing query params
widthInt, _ := strconv.Atoi(width)
heightInt, _ := strconv.Atoi(height)
img.resize(widthInt, heightInt)
// Write the resized image (as jpeg) to the http.ResponseWriter
img.write(w)
} else {
// Just copy the upstream response to the http.ResponseWriter
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
io.Copy(w, resp.Body)
}
}
func (p Proxy) validResponse(resp *http.Response) error {
if !p.contentTypeRegex.MatchString(resp.Header.Get("Content-Type")) {
return errors.New("Upstream content doesn't match configured allowedContentTypes")
}
return nil
}