Chromium Code Reviews| 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/log" | |
| 31 "google.golang.org/appengine/memcache" | |
| 32 "google.golang.org/appengine/urlfetch" | |
| 33 ) | |
| 34 | |
| 35 const baseURL = "https://chromium.googlesource.com/crashpad/crashpad/+/doc/doc/g enerated/?format=TEXT" | |
| 36 | |
| 37 func init() { | |
| 38 http.HandleFunc("/", handler) | |
| 39 } | |
| 40 | |
| 41 func handler(w http.ResponseWriter, r *http.Request) { | |
| 42 ctx := appengine.NewContext(r) | |
| 43 client := urlfetch.Client(ctx) | |
| 44 | |
| 45 // Don’t show dotfiles. | |
| 46 if strings.HasPrefix(path.Base(r.URL.Path), ".") { | |
| 47 http.Error(w, http.StatusText(http.StatusNotFound), http.StatusN otFound) | |
| 48 return | |
| 49 } | |
| 50 | |
| 51 u, err := url.Parse(baseURL) | |
| 52 if err != nil { | |
| 53 http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 54 return | |
| 55 } | |
| 56 | |
| 57 // Redirect directories to their index pages (/doc/ -> /doc/index.html). | |
| 58 if strings.HasSuffix(r.URL.Path, "/") { | |
|
Mark Mentovai
2015/10/09 16:26:05
Will we be able to detect that something was a dir
Bons
2015/10/09 16:31:34
We can do a check to see if it looks like this: ht
Mark Mentovai
2015/10/09 16:41:19
Less hacky than that if possible.
I did a couple
| |
| 59 http.Redirect(w, r, r.URL.Path+"index.html", http.StatusFound) | |
| 60 return | |
| 61 } | |
| 62 | |
| 63 u.Path = path.Join(u.Path, r.URL.Path) | |
| 64 urlStr := u.String() | |
| 65 | |
| 66 item, err := memcache.Get(ctx, urlStr) | |
| 67 if err == memcache.ErrCacheMiss { | |
| 68 log.Debugf(ctx, "Fetching %s", urlStr) | |
| 69 resp, err := client.Get(urlStr) | |
| 70 if err != nil { | |
| 71 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
| 72 return | |
| 73 } | |
| 74 defer resp.Body.Close() | |
| 75 if resp.StatusCode != http.StatusOK { | |
| 76 w.WriteHeader(resp.StatusCode) | |
| 77 for k, v := range w.Header() { | |
| 78 w.Header()[k] = v | |
| 79 } | |
| 80 io.Copy(w, resp.Body) | |
| 81 return | |
| 82 } | |
| 83 decoder := base64.NewDecoder(base64.StdEncoding, resp.Body) | |
| 84 b, err := ioutil.ReadAll(decoder) | |
| 85 if err != nil { | |
| 86 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
| 87 return | |
| 88 } | |
| 89 item = &memcache.Item{ | |
| 90 Key: urlStr, | |
| 91 Value: b, | |
| 92 Expiration: 1 * time.Hour, | |
| 93 } | |
| 94 if err := memcache.Set(ctx, item); err != nil { | |
| 95 http.Error(w, err.Error(), http.StatusInternalServerErro r) | |
| 96 return | |
| 97 } | |
| 98 } else if err != nil { | |
| 99 http.Error(w, err.Error(), http.StatusInternalServerError) | |
| 100 return | |
| 101 } | |
| 102 | |
| 103 w.Header().Set("Content-Type", contentType(path.Base(u.Path))) | |
| 104 fmt.Fprintf(w, "%s", item.Value) | |
| 105 } | |
| 106 | |
| 107 // contentType returns the appropriate content type header for file. | |
| 108 func contentType(file string) string { | |
| 109 contentTypes := map[string]string{ | |
| 110 ".html": "text/html; charset=UTF-8", | |
| 111 ".css": "text/css; charset=UTF-8", | |
| 112 ".js": "text/javascript; charset=UTF-8", | |
| 113 ".png": "image/png", | |
| 114 ".ico": "image/x-icon", | |
| 115 } | |
| 116 for suffix, typ := range contentTypes { | |
| 117 if strings.HasSuffix(file, suffix) { | |
| 118 return typ | |
| 119 } | |
| 120 } | |
| 121 return "text/plain; charset=UTF-8" | |
| 122 } | |
| OLD | NEW |