Skip to content

Commit ddf750a

Browse files
authored
Merge pull request #1 from marconi1992/fix/gzip-encoding
Remove Accept-Encoding for proxied origins
2 parents 192723f + 4fe8d64 commit ddf750a

File tree

3 files changed

+138
-124
lines changed

3 files changed

+138
-124
lines changed

.editorconfig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[*]
22
indent_size = 2
3-
indent_style = space
3+
indent_style = space
4+
5+
[*.go]
6+
indent_size = 4
7+
indent_style = tab

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Hypernova Proxy needs a configuration file in order to setup the location for th
3131
## Using Hypernova Proxy with Docker
3232

3333
```Dockerfile
34-
FROM marconi1992/hypernova-proxy:1.0.0
34+
FROM marconi1992/hypernova-proxy:1.0.1
3535

3636
COPY config.json config.json
3737
```

main.go

Lines changed: 132 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,191 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
"net/http/httputil"
10+
"net/url"
411
"os"
5-
"fmt"
6-
"log"
7-
"strings"
8-
"strconv"
9-
"io/ioutil"
10-
"encoding/json"
11-
"net/url"
12-
"net/http"
13-
"net/http/httputil"
14-
"github.com/PuerkitoBio/goquery"
12+
"strconv"
13+
"strings"
14+
15+
"github.com/PuerkitoBio/goquery"
1516
)
1617

1718
type HypernovaResult struct {
18-
Success bool
19-
Html string
20-
Name string
19+
Success bool
20+
Html string
21+
Name string
2122
}
2223

2324
type HypernovaResponse struct {
24-
Results map[string]HypernovaResult
25+
Results map[string]HypernovaResult
2526
}
2627

2728
type Location struct {
28-
Path string
29-
Host string
30-
ModifyResponse bool
29+
Path string
30+
Host string
31+
ModifyResponse bool
3132
}
3233

3334
type Configuration struct {
34-
Locations []Location
35+
Locations []Location
3536
}
3637

3738
func createQuery(tag string, uuid string, name string) string {
38-
query := fmt.Sprintf("%s[data-hypernova-id=\"%s\"][data-hypernova-key=\"%s\"]", tag, uuid, name)
39+
query := fmt.Sprintf("%s[data-hypernova-id=\"%s\"][data-hypernova-key=\"%s\"]", tag, uuid, name)
3940

40-
return query
41+
return query
4142
}
4243

4344
func modifyBody(html string) string {
44-
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
45-
45+
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
46+
4647
if err != nil {
4748
log.Fatal(err)
48-
}
49+
}
4950

50-
batch := make(map[string]map[string]interface{})
51-
52-
doc.Find("div[data-hypernova-key]").Each(func(i int, s *goquery.Selection) {
53-
uuid, uuidOk := s.Attr("data-hypernova-id")
54-
name, nameOk := s.Attr("data-hypernova-key")
55-
if !uuidOk || !nameOk {
56-
return
57-
}
51+
batch := make(map[string]map[string]interface{})
5852

59-
scriptQuery := createQuery("script", uuid, name)
53+
doc.Find("div[data-hypernova-key]").Each(func(i int, s *goquery.Selection) {
54+
uuid, uuidOk := s.Attr("data-hypernova-id")
55+
name, nameOk := s.Attr("data-hypernova-key")
56+
if !uuidOk || !nameOk {
57+
return
58+
}
6059

61-
script := doc.Find(scriptQuery).First()
60+
scriptQuery := createQuery("script", uuid, name)
6261

63-
if script == nil {
64-
return
65-
}
62+
script := doc.Find(scriptQuery).First()
6663

67-
content := script.Text()
68-
content = content[4:(len(content) - 3)]
69-
70-
var data interface{}
64+
if script == nil {
65+
return
66+
}
7167

72-
json.Unmarshal([]byte(content), &data)
68+
content := script.Text()
69+
content = content[4:(len(content) - 3)]
7370

74-
batch[uuid] = make(map[string]interface{})
75-
batch[uuid]["name"] = name
76-
batch[uuid]["data"] = data
77-
})
71+
var data interface{}
7872

79-
b, encodeErr := json.Marshal(batch)
73+
json.Unmarshal([]byte(content), &data)
8074

81-
if encodeErr != nil {
82-
log.Fatal(encodeErr)
83-
}
75+
batch[uuid] = make(map[string]interface{})
76+
batch[uuid]["name"] = name
77+
batch[uuid]["data"] = data
78+
})
8479

85-
payload := string(b)
80+
b, encodeErr := json.Marshal(batch)
8681

87-
resp, reqErr := http.Post(os.Getenv("HYPERNOVA_BATCH"), "application/json", strings.NewReader(payload))
82+
if encodeErr != nil {
83+
log.Fatal(encodeErr)
84+
}
8885

89-
if reqErr != nil {
90-
log.Fatal(reqErr)
91-
}
86+
payload := string(b)
9287

93-
defer resp.Body.Close()
88+
resp, reqErr := http.Post(os.Getenv("HYPERNOVA_BATCH"), "application/json", strings.NewReader(payload))
9489

95-
body, bodyErr := ioutil.ReadAll(resp.Body)
90+
if reqErr != nil {
91+
log.Fatal(reqErr)
92+
}
9693

97-
if bodyErr != nil {
98-
log.Fatal(bodyErr)
99-
}
100-
101-
var hypernovaResponse HypernovaResponse
94+
defer resp.Body.Close()
10295

103-
json.Unmarshal(body, &hypernovaResponse)
96+
body, bodyErr := ioutil.ReadAll(resp.Body)
10497

105-
for uuid, result := range hypernovaResponse.Results {
106-
if !result.Success {
107-
break
108-
}
109-
110-
scriptQuery := createQuery("script", uuid, result.Name)
111-
doc.Find(scriptQuery).Remove()
98+
if bodyErr != nil {
99+
log.Fatal(bodyErr)
100+
}
112101

113-
divQuery := createQuery("div", uuid, result.Name)
114-
doc.Find(divQuery).ReplaceWithHtml(result.Html)
115-
}
102+
var hypernovaResponse HypernovaResponse
116103

117-
html, htmlError := doc.Html()
104+
json.Unmarshal(body, &hypernovaResponse)
118105

119-
if htmlError != nil {
120-
log.Fatal(htmlError)
121-
}
106+
for uuid, result := range hypernovaResponse.Results {
107+
if !result.Success {
108+
break
109+
}
122110

123-
return html
124-
}
111+
scriptQuery := createQuery("script", uuid, result.Name)
112+
doc.Find(scriptQuery).Remove()
125113

126-
func main() {
127-
setUpLocations();
114+
divQuery := createQuery("div", uuid, result.Name)
115+
doc.Find(divQuery).ReplaceWithHtml(result.Html)
116+
}
128117

129-
log.Fatal(http.ListenAndServe(":8080", nil))
118+
html, htmlError := doc.Html()
119+
120+
if htmlError != nil {
121+
log.Fatal(htmlError)
122+
}
123+
124+
return html
130125
}
131126

132-
func setUpLocations () error {
133-
b, err := ioutil.ReadFile(os.Getenv("CONFIG_FILE"));
127+
func main() {
128+
setUpLocations()
134129

135-
if err != nil {
136-
fmt.Println("Config file not found");
137-
return err
138-
}
130+
log.Fatal(http.ListenAndServe(":8080", nil))
131+
}
139132

140-
var configuration Configuration
133+
func setUpLocations() error {
134+
b, err := ioutil.ReadFile(os.Getenv("CONFIG_FILE"))
141135

142-
json.Unmarshal(b, &configuration)
136+
if err != nil {
137+
fmt.Println("Config file not found")
138+
return err
139+
}
140+
141+
var configuration Configuration
142+
143+
json.Unmarshal(b, &configuration)
144+
145+
fmt.Println(configuration)
146+
147+
for _, location := range configuration.Locations {
148+
origin, err := url.Parse(location.Host)
149+
if err != nil {
150+
log.Fatal(err)
151+
} else {
152+
proxy := httputil.NewSingleHostReverseProxy(origin)
153+
154+
if location.ModifyResponse {
155+
proxy.ModifyResponse = ModifyResponse
156+
proxy.Director = func(req *http.Request) {
157+
req.Header.Add("X-Forwarded-Host", req.Host)
158+
req.Header.Add("X-Origin-Host", origin.Host)
159+
req.Header.Del("Accept-Encoding")
160+
req.URL.Scheme = "http"
161+
req.URL.Host = origin.Host
162+
}
163+
}
164+
165+
http.Handle(location.Path, proxy)
166+
}
167+
}
168+
169+
return nil
170+
}
143171

144-
fmt.Println(configuration)
172+
func ModifyResponse(r *http.Response) error {
173+
contentType := r.Header.Get("Content-Type")
174+
if !strings.HasPrefix(contentType, "text/html") {
175+
return nil
176+
}
145177

146-
for _, location := range configuration.Locations {
147-
host, err := url.Parse(location.Host)
148-
if err != nil {
149-
log.Fatal(err)
150-
} else {
151-
proxy := httputil.NewSingleHostReverseProxy(host)
152-
153-
if (location.ModifyResponse) {
154-
proxy.ModifyResponse = ModifyResponse
155-
}
178+
html, err := ioutil.ReadAll(r.Body)
156179

157-
http.Handle(location.Path, proxy)
158-
}
159-
}
180+
if err != nil {
181+
log.Fatal(err)
182+
return err
183+
}
160184

161-
return nil
162-
}
185+
newHtml := modifyBody(string(html))
163186

164-
func ModifyResponse(r *http.Response) error {
165-
contentType := r.Header.Get("Content-Type")
166-
if !strings.HasPrefix(contentType, "text/html") {
167-
return nil
168-
}
169-
170-
html, err := ioutil.ReadAll(r.Body)
171-
172-
if err != nil {
173-
return err
174-
}
175-
176-
newHtml := modifyBody(string(html));
177-
r.Body = ioutil.NopCloser(strings.NewReader(newHtml))
178-
r.ContentLength = int64(len(newHtml))
179-
r.Header.Set("Content-Length", strconv.Itoa(len(newHtml)))
180-
return nil
187+
r.Body = ioutil.NopCloser(strings.NewReader(newHtml))
188+
r.ContentLength = int64(len(newHtml))
189+
r.Header.Set("Content-Length", strconv.Itoa(len(newHtml)))
190+
return nil
181191
}

0 commit comments

Comments
 (0)