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.WriteHeader(resp.StatusCode) | |
75 w.Header().Set("Content-Type", "text/html") | |
Mark Mentovai
2015/10/08 21:46:17
If you’re going to copy the response: carry the Co
| |
76 io.Copy(w, resp.Body) | |
Mark Mentovai
2015/10/08 21:46:17
Is it actually kosher to do this? Might leak somet
| |
77 return | |
78 } | |
79 decoder := base64.NewDecoder(base64.StdEncoding, resp.Body) | |
80 b, err := ioutil.ReadAll(decoder) | |
81 if err != nil { | |
82 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
83 return | |
84 } | |
85 item = &memcache.Item{ | |
86 Key: urlStr, | |
87 Value: b, | |
88 Expiration: 24 * time.Hour, | |
89 } | |
90 if err := memcache.Set(ctx, item); err != nil { | |
91 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
92 return | |
93 } | |
94 } else if err != nil { | |
95 http.Error(w, err.Error(), http.StatusInternalServerError) | |
96 return | |
97 } | |
98 | |
99 w.Header().Set("Content-Type", contentType(path.Base(u.Path))) | |
100 fmt.Fprintf(w, "%s", item.Value) | |
101 } | |
102 | |
103 var contentTypes = map[string]string{ | |
104 ".html": "text/html", | |
105 ".css": "text/css", | |
106 ".js": "text/javascript", | |
107 ".png": "image/png", | |
108 } | |
109 | |
110 // contentType returns the appropriate content type header for file. | |
111 func contentType(file string) string { | |
112 for suffix, typ := range contentTypes { | |
113 if strings.HasSuffix(file, suffix) { | |
114 return typ + "; charset=UTF-8" | |
115 } | |
116 } | |
117 return "text/plain; charset=UTF-8" | |
118 } | |
OLD | NEW |