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

Function handler

validator/js/webui/serve-standalone.go:29–159  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

27}
28
29func handler(w http.ResponseWriter, r *http.Request) {
30 if r.Method == "GET" {
31 //
32 // Handle '/'.
33 //
34 if r.URL.Path == "/" {
35 bytes, err := ioutil.ReadFile("index.html")
36 if err != nil {
37 http.Error(w, "File not found.", http.StatusNotFound)
38 return
39 }
40 w.Header().Set("Content-type", "text/html")
41 if r.FormValue("experimental_wasm") == "1" {
42 bytes = []byte(strings.Replace(string(bytes),
43 "https://cdn.ampproject.org/v0/validator.js", "https://cdn.ampproject.org/v0/validator_wasm.js", -1))
44 } else if *validator_js != "https://cdn.ampproject.org/v0/validator.js" {
45 bytes = []byte(strings.Replace(string(bytes),
46 "https://cdn.ampproject.org/v0/validator.js", "/validator.js", -1))
47 }
48 w.Write(bytes)
49 return
50 }
51 //
52 // Handle '/webui.js'
53 //
54 if r.RequestURI == "/webui.js" {
55 bytes, err := ioutil.ReadFile("webui.js")
56 if err != nil {
57 http.Error(w, "File not found.", http.StatusNotFound)
58 return
59 }
60 w.Header().Set("Content-type", "text/javascript")
61 w.Write(bytes)
62 return
63 }
64 //
65 // Handle '/validator.js'
66 //
67 if r.RequestURI == "/validator.js" {
68 bytes, err := ioutil.ReadFile(*validator_js)
69 if err != nil {
70 http.Error(w, "File not found.", http.StatusNotFound)
71 return
72 }
73 w.Header().Set("Content-type", "text/javascript")
74 w.Write(bytes)
75 return
76 }
77 // Look up any other resources relative to node_modules or webui.
78 relative_path := r.RequestURI[1:] // Strip leading "/".
79 roots := []string{".", "node_modules"}
80 for i := range roots {
81 root := roots[i]
82 // Only serve files below the current working directory.
83 abs_path, err := filepath.Abs(
84 path.Join(".", root, relative_path))
85 if err != nil {
86 continue

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected