-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
serve-standalone.go
177 lines (172 loc) · 4.72 KB
/
serve-standalone.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
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
"time"
)
var validator_js = flag.String("validator_js", "https://cdn.ampproject.org/"+
"v0/validator.js", "The Validator Javascript. Latest published version "+
"by default, or dist/validator_minified.js (built with build.py) for "+
"development.")
var ext_to_mime = map[string]string{
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".png": "image/png",
".svg": "image/svg+xml",
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
//
// Handle '/'.
//
if r.URL.Path == "/" {
bytes, err := ioutil.ReadFile("index.html")
if err != nil {
http.Error(w, "File not found.", http.StatusNotFound)
return
}
w.Header().Set("Content-type", "text/html")
if *validator_js != "https://cdn.ampproject.org/v0/validator.js" {
bytes = []byte(strings.Replace(string(bytes),
"https://cdn.ampproject.org/v0/validator.js", "/validator.js", -1))
}
w.Write(bytes)
return
}
//
// Handle '/webui.js'
//
if r.RequestURI == "/webui.js" {
bytes, err := ioutil.ReadFile("webui.js")
if err != nil {
http.Error(w, "File not found.", http.StatusNotFound)
return
}
w.Header().Set("Content-type", "text/javascript")
w.Write(bytes)
return
}
//
// Handle '/validator.js'
//
if r.RequestURI == "/validator.js" {
bytes, err := ioutil.ReadFile(*validator_js)
if err != nil {
http.Error(w, "File not found.", http.StatusNotFound)
return
}
w.Header().Set("Content-type", "text/javascript")
w.Write(bytes)
return
}
// Look up any other resources relative to node_modules or webui.
relative_path := r.RequestURI[1:] // Strip leading "/".
roots := []string{".", "node_modules"}
for i := range roots {
root := roots[i]
// Only serve files below the current working directory.
abs_path, err := filepath.Abs(
path.Join(".", root, relative_path))
if err != nil {
continue
}
cwd, err := filepath.Abs(".")
if err != nil || !strings.HasPrefix(abs_path, cwd) {
continue
}
if mime, ok := ext_to_mime[filepath.Ext(abs_path)]; ok {
bytes, err := ioutil.ReadFile(abs_path)
if err != nil {
continue
}
w.Header().Set("Content-type", mime)
w.Write(bytes)
return
}
}
http.Error(w, "File not found.", http.StatusNotFound)
return
}
//
// Handle /fetch?, a request to fetch an arbitrary doc from the
// internet. It presents the results as JSON.
//
if r.Method == "POST" && r.RequestURI == "/fetch" &&
r.Header.Get("X-Requested-By") == "validator webui" {
param := r.FormValue("url")
u, err := url.Parse(param)
if param == "" || err != nil ||
(u.Scheme != "http" && u.Scheme != "https") {
http.Error(w, "Bad request.", http.StatusBadRequest)
return
}
var netClient = &http.Client{
Timeout: time.Second * 20,
}
req, err := http.NewRequest("GET", param, nil)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
req.Header.Add("User-Agent",
"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MTC19V) "+
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile "+
"Safari/537.36 (compatible; validator.amp.dev)")
resp, err := netClient.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
type WebPage struct {
Contents string
}
p := WebPage{Contents: string(data)}
bytes, err := json.Marshal(p)
if err != nil {
http.Error(w, fmt.Sprintf("Problem formatting json (%v)",
err.Error()),
http.StatusInternalServerError)
}
w.Header().Set("Content-type", "application/json")
w.Write(bytes)
return
}
http.Error(w, "Bad request.", http.StatusBadRequest)
}
func main() {
flag.Parse()
http.HandleFunc("/", handler)
http.ListenAndServe(":8765", nil)
}