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 if strings.HasSuffix(r.URL.Path, "/") { |
| 57 http.Redirect(w, r, r.URL.Path+"index.html", http.StatusFound) |
| 58 return |
| 59 } |
| 60 |
| 61 u.Path = path.Join(u.Path, r.URL.Path) |
| 62 urlStr := u.String() |
| 63 |
| 64 item, err := memcache.Get(ctx, urlStr) |
| 65 if err == memcache.ErrCacheMiss { |
| 66 resp, err := client.Get(urlStr) |
| 67 if err != nil { |
| 68 http.Error(w, err.Error(), http.StatusInternalServerErro
r) |
| 69 return |
| 70 } |
| 71 defer resp.Body.Close() |
| 72 if resp.StatusCode != http.StatusOK { |
| 73 w.Header().Set("Content-Type", "text/html") |
| 74 io.Copy(w, resp.Body) |
| 75 return |
| 76 } |
| 77 decoder := base64.NewDecoder(base64.StdEncoding, resp.Body) |
| 78 b, err := ioutil.ReadAll(decoder) |
| 79 if err != nil { |
| 80 http.Error(w, err.Error(), http.StatusInternalServerErro
r) |
| 81 return |
| 82 } |
| 83 item = &memcache.Item{ |
| 84 Key: urlStr, |
| 85 Value: b, |
| 86 Expiration: 24 * time.Hour, |
| 87 } |
| 88 if err := memcache.Set(ctx, item); err != nil { |
| 89 http.Error(w, err.Error(), http.StatusInternalServerErro
r) |
| 90 return |
| 91 } |
| 92 } else if err != nil { |
| 93 http.Error(w, err.Error(), http.StatusInternalServerError) |
| 94 return |
| 95 } |
| 96 |
| 97 w.Header().Set("Content-Type", contentType(path.Base(u.Path))) |
| 98 fmt.Fprintf(w, "%s", item.Value) |
| 99 } |
| 100 |
| 101 var contentTypes = map[string]string{ |
| 102 ".html": "text/html", |
| 103 ".css": "text/css", |
| 104 ".js": "text/javascript", |
| 105 ".png": "image/png", |
| 106 } |
| 107 |
| 108 // contentType returns the appropriate content type header for file. |
| 109 func contentType(file string) string { |
| 110 for suffix, typ := range contentTypes { |
| 111 if strings.HasSuffix(file, suffix) { |
| 112 return typ + "; charset=UTF-8" |
| 113 } |
| 114 } |
| 115 return "text/plain; charset=UTF-8" |
| 116 } |
OLD | NEW |