Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Side by Side Diff: doc/appengine/main.go

Issue 1393353002: Add app engine app to mirror html docs from chromium.googlesource.com (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « doc/appengine/app.yaml ('k') | doc/favicon.ico » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 u.Path = path.Join(u.Path, r.URL.Path)
57 urlStr := u.String()
58
59 item, err := memcache.Get(ctx, urlStr)
60 if err == memcache.ErrCacheMiss {
61 resp, err := client.Get(urlStr)
62 if err != nil {
63 http.Error(w, err.Error(), http.StatusInternalServerErro r)
64 return
65 }
66 defer resp.Body.Close()
67
68 if resp.StatusCode != http.StatusOK {
69 w.WriteHeader(resp.StatusCode)
70 for k, v := range w.Header() {
71 w.Header()[k] = v
72 }
73 io.Copy(w, resp.Body)
74 return
75 }
76
77 // Redirect directories to their index pages (/doc/ -> /doc/inde x.html).
78 if resp.Header.Get("X-Gitiles-Object-Type") == "tree" {
79 http.Redirect(w, r, path.Join(r.URL.Path, "/index.html") , http.StatusFound)
Mark Mentovai 2015/10/09 19:26:17 If the client requested /doc/, will they be sent t
Bons 2015/10/09 19:31:16 path.Join normalizes the paths so that // won't ha
80 return
81 }
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 }
OLDNEW
« no previous file with comments | « doc/appengine/app.yaml ('k') | doc/favicon.ico » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698