OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 // Package crashpad mirrors crashpad documentation from Chromium’s git repo. | |
16 package crashpad | |
17 | |
18 import ( | |
19 "encoding/base64" | |
20 "fmt" | |
21 "io" | |
22 "io/ioutil" | |
23 "net/http" | |
24 "net/url" | |
25 "path" | |
26 "strings" | |
27 "time" | |
28 | |
29 "google.golang.org/appengine" | |
30 "google.golang.org/appengine/memcache" | |
31 "google.golang.org/appengine/urlfetch" | |
32 ) | |
33 | |
34 const baseURL = "https://chromium.googlesource.com/crashpad/crashpad/+/doc/doc/g enerated/?format=TEXT" | |
35 | |
36 func init() { | |
37 http.HandleFunc("/", handler) | |
38 } | |
39 | |
40 func handler(w http.ResponseWriter, r *http.Request) { | |
41 ctx := appengine.NewContext(r) | |
42 client := urlfetch.Client(ctx) | |
43 | |
44 // Don’t show dotfiles. | |
45 if strings.HasPrefix(path.Base(r.URL.Path), ".") { | |
46 http.Error(w, http.StatusText(http.StatusNotFound), http.StatusN otFound) | |
47 return | |
48 } | |
49 | |
50 u, err := url.Parse(baseURL) | |
51 if err != nil { | |
52 http.Error(w, err.Error(), http.StatusInternalServerError) | |
53 return | |
54 } | |
55 | |
56 // Redirect directories to their index pages (/doc/ -> /doc/index.html). | |
57 if strings.HasSuffix(r.URL.Path, "/") { | |
58 http.Redirect(w, r, r.URL.Path+"index.html", http.StatusFound) | |
59 return | |
60 } | |
61 | |
62 u.Path = path.Join(u.Path, r.URL.Path) | |
63 urlStr := u.String() | |
64 | |
65 item, err := memcache.Get(ctx, urlStr) | |
66 if err == memcache.ErrCacheMiss { | |
67 resp, err := client.Get(urlStr) | |
68 if err != nil { | |
69 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
70 return | |
71 } | |
72 defer resp.Body.Close() | |
73 if resp.StatusCode != http.StatusOK { | |
74 w.Header().Set("Content-Type", "text/html") | |
Mark Mentovai
2015/10/08 21:46:16
If you’re going to copy the response: carry the st
Bons
2015/10/08 22:31:16
Done.
| |
75 io.Copy(w, resp.Body) | |
76 return | |
77 } | |
78 decoder := base64.NewDecoder(base64.StdEncoding, resp.Body) | |
79 b, err := ioutil.ReadAll(decoder) | |
80 if err != nil { | |
81 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
82 return | |
83 } | |
84 item = &memcache.Item{ | |
85 Key: urlStr, | |
86 Value: b, | |
87 Expiration: 24 * time.Hour, | |
Mark Mentovai
2015/10/08 21:46:17
Maybe that’s a little long, is one hour OK? Robert
Bons
2015/10/08 22:31:17
Done.
| |
88 } | |
89 if err := memcache.Set(ctx, item); err != nil { | |
90 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
91 return | |
92 } | |
93 } else if err != nil { | |
94 http.Error(w, err.Error(), http.StatusInternalServerError) | |
95 return | |
96 } | |
97 | |
98 w.Header().Set("Content-Type", contentType(path.Base(u.Path))) | |
Mark Mentovai
2015/10/08 21:46:16
Sensible to set Content-Length too, since I think
Bons
2015/10/08 22:31:16
that’s done automatically.
| |
99 fmt.Fprintf(w, "%s", item.Value) | |
100 } | |
101 | |
102 var contentTypes = map[string]string{ | |
Mark Mentovai
2015/10/08 21:46:16
I don’t know anything about Go, but should this be
Bons
2015/10/08 22:31:17
Yeah. Moved.
| |
103 ".html": "text/html", | |
104 ".css": "text/css", | |
105 ".js": "text/javascript", | |
106 ".png": "image/png", | |
107 } | |
108 | |
109 // contentType returns the appropriate content type header for file. | |
110 func contentType(file string) string { | |
111 for suffix, typ := range contentTypes { | |
112 if strings.HasSuffix(file, suffix) { | |
113 return typ + "; charset=UTF-8" | |
Mark Mentovai
2015/10/08 21:46:16
charset doesn’t make sense for png.
Bons
2015/10/08 22:31:17
Done.
| |
114 } | |
115 } | |
116 return "text/plain; charset=UTF-8" | |
117 } | |
OLD | NEW |