-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (78 loc) · 3.04 KB
/
main.go
File metadata and controls
93 lines (78 loc) · 3.04 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
package main
import (
"compress/gzip"
"io"
"log"
"net/http"
"net/url"
"strings"
)
const backendURL = "http://sync:8080"
func main() {
http.HandleFunc("/odktables/", proxyHandler)
http.HandleFunc("/gogunzy-health", healthHandler)
log.Println("ODKX Go Gzip Proxy listening on :8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
targetURL := backendURL + r.URL.Path
if r.URL.RawQuery != "" {
targetURL += "?" + r.URL.RawQuery
}
log.Printf("Proxying %s request to %s", r.Method, targetURL)
var body io.Reader = nil
cleanHeaders := r.Header.Clone()
if r.Body != nil {
body = r.Body
if cleanHeaders.Get("Content-Encoding") == "gzip" {
log.Println("Decompressing gzipped request body...")
gz, err := gzip.NewReader(r.Body)
if err != nil {
http.Error(w, "Failed to decompress request body", http.StatusBadRequest)
return
}
defer gz.Close()
body = gz
cleanHeaders.Del("Content-Encoding")
}
}
cleanHeaders.Set("Accept-Encoding", "identity")
req, err := http.NewRequest(r.Method, targetURL, body)
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}
req.Header = cleanHeaders
req.Host = r.Host // preserve original host
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, "Upstream request failed", http.StatusBadGateway)
return
}
defer resp.Body.Close()
for name, values := range resp.Header {
// Fix potential internal host leaks in redirect responses
if strings.ToLower(name) == "location" {
for _, v := range values {
if loc, err := url.Parse(v); err == nil {
if loc.Host == "sync:8080" || loc.Host == "sync" {
loc.Scheme = "https"
loc.Host = r.Host
v = loc.String()
}
}
w.Header().Add(name, v)
}
} else {
for _, v := range values {
w.Header().Add(name, v)
}
}
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}