blob: 8c30e26356b24f4b48347a486cb2ac6ce0e2934f [file] [log] [blame]
Shawn Pearce374f1842015-02-10 15:36:54 -08001// Copyright 2015 Google Inc. 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
15package com.google.gitiles.doc;
16
17import static java.nio.charset.StandardCharsets.UTF_8;
18import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
19import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
20import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
21import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
22import static org.eclipse.jgit.lib.FileMode.TYPE_FILE;
23import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
24import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
25
26import com.google.common.base.MoreObjects;
27import com.google.common.base.Strings;
28import com.google.common.hash.Hasher;
29import com.google.common.hash.Hashing;
30import com.google.common.net.HttpHeaders;
31import com.google.gitiles.BaseServlet;
32import com.google.gitiles.FormatType;
33import com.google.gitiles.GitilesAccess;
34import com.google.gitiles.GitilesView;
35import com.google.gitiles.Renderer;
36import com.google.gitiles.ViewFilter;
37
Shawn Pearce12c8fab2016-05-15 16:55:21 -070038import org.commonmark.node.Node;
Shawn Pearce374f1842015-02-10 15:36:54 -080039import org.eclipse.jgit.errors.IncorrectObjectTypeException;
40import org.eclipse.jgit.http.server.ServletUtils;
41import org.eclipse.jgit.lib.Config;
42import org.eclipse.jgit.lib.Constants;
43import org.eclipse.jgit.lib.ObjectId;
44import org.eclipse.jgit.lib.ObjectLoader;
45import org.eclipse.jgit.lib.ObjectReader;
46import org.eclipse.jgit.lib.Repository;
47import org.eclipse.jgit.revwalk.RevTree;
48import org.eclipse.jgit.revwalk.RevWalk;
49import org.eclipse.jgit.treewalk.TreeWalk;
50import org.eclipse.jgit.util.RawParseUtils;
Shawn Pearce374f1842015-02-10 15:36:54 -080051
52import java.io.IOException;
53import java.util.HashMap;
54import java.util.Map;
55
56import javax.servlet.http.HttpServletRequest;
57import javax.servlet.http.HttpServletResponse;
58
59public class DocServlet extends BaseServlet {
60 private static final long serialVersionUID = 1L;
61
62 private static final String INDEX_MD = "index.md";
63 private static final String NAVBAR_MD = "navbar.md";
64 private static final String SOY_FILE = "Doc.soy";
65 private static final String SOY_TEMPLATE = "gitiles.markdownDoc";
66
67 // Generation of ETag logic. Bump this only if DocServlet logic changes
68 // significantly enough to impact cached pages. Soy template and source
69 // files are automatically hashed as part of the ETag.
Shawn Pearce12c8fab2016-05-15 16:55:21 -070070 private static final int ETAG_GEN = 5;
Shawn Pearce374f1842015-02-10 15:36:54 -080071
72 public DocServlet(GitilesAccess.Factory accessFactory, Renderer renderer) {
73 super(renderer, accessFactory);
74 }
75
76 @Override
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020077 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Shawn Pearce374f1842015-02-10 15:36:54 -080078 Config cfg = getAccess(req).getConfig();
79 if (!cfg.getBoolean("markdown", "render", true)) {
80 res.setStatus(SC_NOT_FOUND);
81 return;
82 }
83
84 GitilesView view = ViewFilter.getView(req);
85 Repository repo = ServletUtils.getRepository(req);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070086 try (RevWalk rw = new RevWalk(repo)) {
Shawn Pearce374f1842015-02-10 15:36:54 -080087 String path = view.getPathPart();
88 RevTree root;
89 try {
90 root = rw.parseTree(view.getRevision().getId());
91 } catch (IncorrectObjectTypeException e) {
92 res.setStatus(SC_NOT_FOUND);
93 return;
94 }
95
96 SourceFile srcmd = findFile(rw, root, path);
97 if (srcmd == null) {
98 res.setStatus(SC_NOT_FOUND);
99 return;
100 }
101
102 SourceFile navmd = findFile(rw, root, NAVBAR_MD);
103 String reqEtag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
104 String curEtag = etag(srcmd, navmd);
105 if (reqEtag != null && reqEtag.equals(curEtag)) {
106 res.setStatus(SC_NOT_MODIFIED);
107 return;
108 }
109
110 view = view.toBuilder().setPathPart(srcmd.path).build();
111 int inputLimit = cfg.getInt("markdown", "inputLimit", 5 << 20);
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700112 Node doc = GitilesMarkdown.parse(srcmd.read(rw.getObjectReader(), inputLimit));
Shawn Pearce374f1842015-02-10 15:36:54 -0800113 if (doc == null) {
Shawn Pearce576d7b62015-03-30 09:16:19 -0700114 res.sendRedirect(GitilesView.show().copyFrom(view).toUrl());
Shawn Pearce374f1842015-02-10 15:36:54 -0800115 return;
116 }
117
Shawn Pearcec32894e2016-05-17 15:25:20 -0400118 String navPath = null;
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700119 String navMarkdown = null;
120 Node nav = null;
Shawn Pearce374f1842015-02-10 15:36:54 -0800121 if (navmd != null) {
Shawn Pearcec32894e2016-05-17 15:25:20 -0400122 navPath = navmd.path;
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700123 navMarkdown = navmd.read(rw.getObjectReader(), inputLimit);
124 nav = GitilesMarkdown.parse(navMarkdown);
Shawn Pearce374f1842015-02-10 15:36:54 -0800125 if (nav == null) {
126 res.setStatus(SC_INTERNAL_SERVER_ERROR);
127 return;
128 }
129 }
130
Shawn Pearcef75a2c62015-02-12 21:39:00 -0800131 int imageLimit = cfg.getInt("markdown", "imageLimit", 256 << 10);
132 ImageLoader img = null;
133 if (imageLimit > 0) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200134 img = new ImageLoader(rw.getObjectReader(), view, root, srcmd.path, imageLimit);
Shawn Pearcef75a2c62015-02-12 21:39:00 -0800135 }
136
Shawn Pearce374f1842015-02-10 15:36:54 -0800137 res.setHeader(HttpHeaders.ETAG, curEtag);
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700138 showDoc(req, res, view, cfg, img, navPath, navMarkdown, nav, srcmd.path, doc);
Shawn Pearce374f1842015-02-10 15:36:54 -0800139 }
140 }
141
142 private String etag(SourceFile srcmd, SourceFile navmd) {
143 byte[] b = new byte[Constants.OBJECT_ID_LENGTH];
144 Hasher h = Hashing.sha1().newHasher();
145 h.putInt(ETAG_GEN);
146
147 renderer.getTemplateHash(SOY_FILE).writeBytesTo(b, 0, b.length);
148 h.putBytes(b);
149
150 if (navmd != null) {
151 navmd.id.copyRawTo(b, 0);
152 h.putBytes(b);
153 }
154
155 srcmd.id.copyRawTo(b, 0);
156 h.putBytes(b);
157 return h.hash().toString();
158 }
159
160 @Override
161 protected void setCacheHeaders(HttpServletResponse res) {
162 long now = System.currentTimeMillis();
163 res.setDateHeader(HttpHeaders.EXPIRES, now);
164 res.setDateHeader(HttpHeaders.DATE, now);
165 res.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=0, must-revalidate");
166 }
167
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200168 private void showDoc(
169 HttpServletRequest req,
170 HttpServletResponse res,
171 GitilesView view,
172 Config cfg,
173 ImageLoader img,
Shawn Pearcec32894e2016-05-17 15:25:20 -0400174 String navPath,
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700175 String navMarkdown,
176 Node nav,
Shawn Pearcec32894e2016-05-17 15:25:20 -0400177 String docPath,
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700178 Node doc)
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200179 throws IOException {
Shawn Pearce374f1842015-02-10 15:36:54 -0800180 Map<String, Object> data = new HashMap<>();
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700181
182 MarkdownToHtml navHtml = new MarkdownToHtml(view, cfg, navPath);
183 data.putAll(Navbar.bannerSoyData(img, navHtml, navMarkdown, nav));
184 data.put("navbarHtml", navHtml.toSoyHtml(nav));
185
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200186 data.put("pageTitle", MoreObjects.firstNonNull(MarkdownUtil.getTitle(doc), view.getPathPart()));
Shawn Pearce68311c72015-06-09 17:01:34 -0700187 if (view.getType() != GitilesView.Type.ROOTED_DOC) {
188 data.put("sourceUrl", GitilesView.show().copyFrom(view).toUrl());
189 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
190 data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
191 }
Shawn Pearcec32894e2016-05-17 15:25:20 -0400192 data.put("bodyHtml", new MarkdownToHtml(view, cfg, docPath).setImageLoader(img).toSoyHtml(doc));
Shawn Pearce374f1842015-02-10 15:36:54 -0800193
Shawn Pearcebc381a42015-06-22 12:17:43 -0700194 String analyticsId = cfg.getString("google", null, "analyticsId");
195 if (!Strings.isNullOrEmpty(analyticsId)) {
196 data.put("analyticsId", analyticsId);
197 }
198
Shawn Pearce374f1842015-02-10 15:36:54 -0800199 String page = renderer.render(SOY_TEMPLATE, data);
200 byte[] raw = page.getBytes(UTF_8);
201 res.setContentType(FormatType.HTML.getMimeType());
202 res.setCharacterEncoding(UTF_8.name());
203 setCacheHeaders(res);
204 if (acceptsGzipEncoding(req)) {
205 res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
206 raw = gzip(raw);
207 }
208 res.setContentLength(raw.length);
209 res.setStatus(HttpServletResponse.SC_OK);
210 res.getOutputStream().write(raw);
211 }
212
213 private static SourceFile findFile(RevWalk rw, RevTree root, String path) throws IOException {
214 if (Strings.isNullOrEmpty(path)) {
215 path = INDEX_MD;
216 }
217
218 ObjectReader reader = rw.getObjectReader();
219 TreeWalk tw = TreeWalk.forPath(reader, path, root);
220 if (tw == null) {
221 return null;
222 }
223 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_TREE) {
224 if (findIndexFile(tw)) {
225 path = tw.getPathString();
226 } else {
227 return null;
228 }
229 }
230 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE) {
231 if (!path.endsWith(".md")) {
232 return null;
233 }
234 return new SourceFile(path, tw.getObjectId(0));
235 }
236 return null;
237 }
238
239 private static boolean findIndexFile(TreeWalk tw) throws IOException {
240 tw.enterSubtree();
241 while (tw.next()) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200242 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE && INDEX_MD.equals(tw.getNameString())) {
Shawn Pearce374f1842015-02-10 15:36:54 -0800243 return true;
244 }
245 }
246 return false;
247 }
248
249 private static class SourceFile {
250 final String path;
251 final ObjectId id;
252
253 SourceFile(String path, ObjectId id) {
254 this.path = path;
255 this.id = id;
256 }
257
258 String read(ObjectReader reader, int inputLimit) throws IOException {
259 ObjectLoader obj = reader.open(id, OBJ_BLOB);
260 byte[] raw = obj.getCachedBytes(inputLimit);
261 return RawParseUtils.decode(raw);
262 }
263 }
264}