MCPcopy Create free account
hub / github.com/ampproject/amphtml / handler

Function handler

validator/js/webui/main/serve.go:20–69  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

18)
19
20func handler(w http.ResponseWriter, r *http.Request) {
21 if r.Method != "POST" ||
22 r.Header.Get("X-Requested-By") != "validator webui" {
23 http.Error(w, "Bad request.", http.StatusBadRequest)
24 return
25 }
26 param := r.FormValue("url")
27 u, err := url.Parse(param)
28 if param == "" || err != nil || (u.Scheme != "http" && u.Scheme != "https") {
29 http.Error(w, "Bad request.", http.StatusBadRequest)
30 return
31 }
32
33 client := &http.Client {}
34 req, err := http.NewRequest("GET", u.String(), nil)
35 if err != nil {
36 http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
37 http.StatusBadGateway)
38 return
39 }
40 req.Header.Add("User-Agent",
41 "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MTC19V) "+
42 "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile "+
43 "Safari/537.36 (compatible; validator.amp.dev)")
44 resp, err := client.Do(req)
45 if err != nil {
46 http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
47 http.StatusBadGateway)
48 return
49 }
50 defer resp.Body.Close()
51 data, err := ioutil.ReadAll(resp.Body)
52 if err != nil {
53 http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
54 http.StatusBadGateway)
55 return
56 }
57 type WebPage struct {
58 Contents string
59 }
60 p := WebPage{Contents: string(data)}
61 bytes, err := json.Marshal(p)
62 if err != nil {
63 http.Error(w, fmt.Sprintf("Problem formatting json (%v)",
64 err.Error()),
65 http.StatusInternalServerError)
66 }
67 w.Header().Set("Content-type", "application/json")
68 w.Write(bytes)
69}
70
71func main() {
72 http.HandleFunc("/", handler)

Callers

nothing calls this directly

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected