blob: 4def1764fde54df92c31870f45337d700e01eb42 [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
Shawn Pearce374f1842015-02-10 15:36:54 -080017import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
18import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
19import static org.eclipse.jgit.lib.FileMode.TYPE_FILE;
20import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
21import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
22
23import com.google.common.base.MoreObjects;
24import com.google.common.base.Strings;
Shawn Pearce5c34e092017-06-29 21:18:30 -070025import com.google.common.base.Throwables;
Shawn Pearce374f1842015-02-10 15:36:54 -080026import com.google.common.hash.Hasher;
27import com.google.common.hash.Hashing;
28import com.google.common.net.HttpHeaders;
29import com.google.gitiles.BaseServlet;
Shawn Pearce374f1842015-02-10 15:36:54 -080030import com.google.gitiles.GitilesAccess;
David Pursehouse27cf26d2019-05-13 06:48:07 +020031import com.google.gitiles.GitilesRequestFailureException;
32import com.google.gitiles.GitilesRequestFailureException.FailureReason;
Shawn Pearce374f1842015-02-10 15:36:54 -080033import com.google.gitiles.GitilesView;
34import com.google.gitiles.Renderer;
35import com.google.gitiles.ViewFilter;
Shawn Pearce5c34e092017-06-29 21:18:30 -070036import com.google.gitiles.doc.html.StreamHtmlBuilder;
Dave Borowitz3b744b12016-08-19 16:11:10 -040037import java.io.IOException;
Shawn Pearce5c34e092017-06-29 21:18:30 -070038import java.io.OutputStream;
39import java.io.Writer;
Dave Borowitz3b744b12016-08-19 16:11:10 -040040import java.util.HashMap;
41import java.util.Map;
42import javax.annotation.Nullable;
43import javax.servlet.http.HttpServletRequest;
44import javax.servlet.http.HttpServletResponse;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070045import org.commonmark.node.Node;
Shawn Pearce374f1842015-02-10 15:36:54 -080046import org.eclipse.jgit.errors.IncorrectObjectTypeException;
Shawn Pearce47fd6562016-05-28 14:15:15 -070047import org.eclipse.jgit.errors.LargeObjectException;
Shawn Pearce374f1842015-02-10 15:36:54 -080048import org.eclipse.jgit.http.server.ServletUtils;
Shawn Pearce374f1842015-02-10 15:36:54 -080049import org.eclipse.jgit.lib.Constants;
50import org.eclipse.jgit.lib.ObjectId;
Shawn Pearce374f1842015-02-10 15:36:54 -080051import org.eclipse.jgit.lib.ObjectReader;
52import org.eclipse.jgit.lib.Repository;
53import org.eclipse.jgit.revwalk.RevTree;
54import org.eclipse.jgit.revwalk.RevWalk;
55import org.eclipse.jgit.treewalk.TreeWalk;
Shawn Pearce374f1842015-02-10 15:36:54 -080056
Shawn Pearce374f1842015-02-10 15:36:54 -080057public class DocServlet extends BaseServlet {
58 private static final long serialVersionUID = 1L;
59
60 private static final String INDEX_MD = "index.md";
61 private static final String NAVBAR_MD = "navbar.md";
62 private static final String SOY_FILE = "Doc.soy";
Sven Selbergd99004c2022-01-31 10:24:08 +010063 private static final String SOY_TEMPLATE = "com.google.gitiles.templates.Doc.markdownDoc";
Shawn Pearce374f1842015-02-10 15:36:54 -080064
65 // Generation of ETag logic. Bump this only if DocServlet logic changes
66 // significantly enough to impact cached pages. Soy template and source
67 // files are automatically hashed as part of the ETag.
Shawn Pearce12c8fab2016-05-15 16:55:21 -070068 private static final int ETAG_GEN = 5;
Shawn Pearce374f1842015-02-10 15:36:54 -080069
Shawn Pearce3739fcd2017-06-30 15:38:17 -070070 private final HtmlSanitizer.Factory htmlSanitizer;
71
Shawn Pearce374f1842015-02-10 15:36:54 -080072 public DocServlet(GitilesAccess.Factory accessFactory, Renderer renderer) {
Shawn Pearce3739fcd2017-06-30 15:38:17 -070073 this(accessFactory, renderer, HtmlSanitizer.DISABLED_FACTORY);
74 }
75
76 public DocServlet(
77 GitilesAccess.Factory accessFactory, Renderer renderer, HtmlSanitizer.Factory htmlSanitizer) {
Shawn Pearce374f1842015-02-10 15:36:54 -080078 super(renderer, accessFactory);
Shawn Pearce3739fcd2017-06-30 15:38:17 -070079 this.htmlSanitizer = htmlSanitizer;
Shawn Pearce374f1842015-02-10 15:36:54 -080080 }
81
82 @Override
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020083 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Shawn Pearce47fd6562016-05-28 14:15:15 -070084 MarkdownConfig cfg = MarkdownConfig.get(getAccess(req).getConfig());
85 if (!cfg.render) {
Masaya Suzuki5cecb862019-03-25 17:35:44 -070086 throw new GitilesRequestFailureException(FailureReason.MARKDOWN_NOT_ENABLED);
Shawn Pearce374f1842015-02-10 15:36:54 -080087 }
88
89 GitilesView view = ViewFilter.getView(req);
90 Repository repo = ServletUtils.getRepository(req);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070091 try (RevWalk rw = new RevWalk(repo)) {
Shawn Pearce47fd6562016-05-28 14:15:15 -070092 ObjectReader reader = rw.getObjectReader();
Shawn Pearce374f1842015-02-10 15:36:54 -080093 String path = view.getPathPart();
94 RevTree root;
95 try {
96 root = rw.parseTree(view.getRevision().getId());
97 } catch (IncorrectObjectTypeException e) {
David Ostrovsky1ae7c382020-01-24 08:17:24 +010098 throw new GitilesRequestFailureException(FailureReason.INCORRECT_OBJECT_TYPE, e);
Shawn Pearce374f1842015-02-10 15:36:54 -080099 }
100
Shawn Pearce47fd6562016-05-28 14:15:15 -0700101 MarkdownFile srcmd = findFile(rw, root, path);
Shawn Pearce374f1842015-02-10 15:36:54 -0800102 if (srcmd == null) {
Masaya Suzuki5cecb862019-03-25 17:35:44 -0700103 throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
Shawn Pearce374f1842015-02-10 15:36:54 -0800104 }
105
Robert Liaoe8f74112018-10-24 18:10:51 -0700106 MarkdownFile navmd = findNavbar(rw, root, path);
Shawn Pearce374f1842015-02-10 15:36:54 -0800107 String curEtag = etag(srcmd, navmd);
Shawn Pearce47fd6562016-05-28 14:15:15 -0700108 if (etagMatch(req, curEtag)) {
Shawn Pearce374f1842015-02-10 15:36:54 -0800109 res.setStatus(SC_NOT_MODIFIED);
110 return;
111 }
112
113 view = view.toBuilder().setPathPart(srcmd.path).build();
Shawn Pearce47fd6562016-05-28 14:15:15 -0700114 try {
115 srcmd.read(reader, cfg);
116 if (navmd != null) {
117 navmd.read(reader, cfg);
118 }
David Pursehouse6dec8372019-05-13 06:51:32 +0200119 } catch (LargeObjectException.ExceedsLimit e) {
120 fileTooBig(res, view);
Shawn Pearce47fd6562016-05-28 14:15:15 -0700121 return;
Shawn Pearce374f1842015-02-10 15:36:54 -0800122 }
123
Shawn Pearce47fd6562016-05-28 14:15:15 -0700124 MarkdownToHtml.Builder fmt =
125 MarkdownToHtml.builder()
126 .setConfig(cfg)
127 .setGitilesView(view)
Shawn Pearcec68ad0b2016-05-28 16:52:47 -0700128 .setRequestUri(req.getRequestURI())
Shawn Pearce47fd6562016-05-28 14:15:15 -0700129 .setReader(reader)
Shawn Pearce3739fcd2017-06-30 15:38:17 -0700130 .setRootTree(root)
131 .setHtmlSanitizer(htmlSanitizer.create(req));
Shawn Pearcef485a312017-07-01 12:10:27 -0700132 Navbar navbar = createNavbar(cfg, fmt, navmd);
Shawn Pearce374f1842015-02-10 15:36:54 -0800133 res.setHeader(HttpHeaders.ETAG, curEtag);
Shawn Pearcef485a312017-07-01 12:10:27 -0700134 showDoc(req, res, view, fmt, navbar, srcmd);
Shawn Pearce374f1842015-02-10 15:36:54 -0800135 }
136 }
137
Shawn Pearce47fd6562016-05-28 14:15:15 -0700138 private static boolean etagMatch(HttpServletRequest req, String etag) {
139 String reqEtag = req.getHeader(HttpHeaders.IF_NONE_MATCH);
140 return reqEtag != null && reqEtag.equals(etag);
141 }
142
143 private String etag(MarkdownFile srcmd, @Nullable MarkdownFile navmd) {
Shawn Pearce374f1842015-02-10 15:36:54 -0800144 byte[] b = new byte[Constants.OBJECT_ID_LENGTH];
David Pursehoused5914492017-05-31 13:08:22 +0900145 Hasher h = Hashing.murmur3_128().newHasher();
Shawn Pearce374f1842015-02-10 15:36:54 -0800146 h.putInt(ETAG_GEN);
147
148 renderer.getTemplateHash(SOY_FILE).writeBytesTo(b, 0, b.length);
149 h.putBytes(b);
150
151 if (navmd != null) {
152 navmd.id.copyRawTo(b, 0);
153 h.putBytes(b);
154 }
155
156 srcmd.id.copyRawTo(b, 0);
157 h.putBytes(b);
158 return h.hash().toString();
159 }
160
David Pursehouse3bce3562018-11-08 16:23:38 +0900161 private MarkdownFile findNavbar(RevWalk rw, RevTree root, String path) throws IOException {
Robert Liaoe8f74112018-10-24 18:10:51 -0700162 if (!Strings.isNullOrEmpty(path)) {
163 // Traverse up the path until we find a NAVBAR_MD.
164 StringBuilder pathRemaining = new StringBuilder(path);
165 while (pathRemaining.length() > 0) {
166 int lastPathSeparatorIndex = pathRemaining.lastIndexOf("/");
167 pathRemaining.setLength(lastPathSeparatorIndex + 1);
168 MarkdownFile navmd = findFile(rw, root, pathRemaining.toString() + NAVBAR_MD);
169 if (navmd != null) {
170 return navmd;
171 }
172 pathRemaining.setLength(Math.max(lastPathSeparatorIndex, 0));
173 }
174 return null;
175 }
176
177 return findFile(rw, root, NAVBAR_MD);
178 }
179
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200180 private void showDoc(
181 HttpServletRequest req,
182 HttpServletResponse res,
183 GitilesView view,
Shawn Pearce47fd6562016-05-28 14:15:15 -0700184 MarkdownToHtml.Builder fmt,
Shawn Pearcef485a312017-07-01 12:10:27 -0700185 Navbar navbar,
Shawn Pearce47fd6562016-05-28 14:15:15 -0700186 MarkdownFile srcFile)
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200187 throws IOException {
Shawn Pearce374f1842015-02-10 15:36:54 -0800188 Map<String, Object> data = new HashMap<>();
Shawn Pearcef485a312017-07-01 12:10:27 -0700189 data.putAll(navbar.toSoyData());
Shawn Pearce12c8fab2016-05-15 16:55:21 -0700190
Shawn Pearcef485a312017-07-01 12:10:27 -0700191 MarkdownConfig cfg = navbar.getConfig();
Shawn Pearceb55cf2b2017-06-29 21:56:37 -0700192 Node doc = GitilesMarkdown.parse(cfg, srcFile.consumeContent());
Shawn Pearce47fd6562016-05-28 14:15:15 -0700193 data.put("pageTitle", pageTitle(doc, srcFile));
Shawn Pearce68311c72015-06-09 17:01:34 -0700194 if (view.getType() != GitilesView.Type.ROOTED_DOC) {
195 data.put("sourceUrl", GitilesView.show().copyFrom(view).toUrl());
196 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
197 data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
198 }
Shawn Pearce47fd6562016-05-28 14:15:15 -0700199 if (cfg.analyticsId != null) {
200 data.put("analyticsId", cfg.analyticsId);
Shawn Pearcebc381a42015-06-22 12:17:43 -0700201 }
202
Shawn Pearce5c34e092017-06-29 21:18:30 -0700203 try (OutputStream out = startRenderCompressedStreamingHtml(req, res, SOY_TEMPLATE, data)) {
204 Writer w = newWriter(out, res);
Shawn Pearcef485a312017-07-01 12:10:27 -0700205 fmt.setConfig(cfg)
206 .setFilePath(srcFile.path)
207 .build()
208 .renderToHtml(new StreamHtmlBuilder(w), doc);
Shawn Pearce5c34e092017-06-29 21:18:30 -0700209 w.flush();
210 } catch (RuntimeIOException e) {
211 Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
212 throw e;
Shawn Pearce374f1842015-02-10 15:36:54 -0800213 }
Shawn Pearce5c34e092017-06-29 21:18:30 -0700214 }
215
Shawn Pearcef485a312017-07-01 12:10:27 -0700216 private static Navbar createNavbar(
217 MarkdownConfig cfg, MarkdownToHtml.Builder fmt, @Nullable MarkdownFile navFile) {
218 Navbar navbar = new Navbar().setConfig(cfg);
Shawn Pearce5c34e092017-06-29 21:18:30 -0700219 if (navFile != null) {
Shawn Pearcef485a312017-07-01 12:10:27 -0700220 navbar
221 .setFormatter(fmt.setFilePath(navFile.path).build())
222 .setMarkdown(navFile.consumeContent());
Shawn Pearce5c34e092017-06-29 21:18:30 -0700223 }
Shawn Pearcef485a312017-07-01 12:10:27 -0700224 return navbar;
Shawn Pearce374f1842015-02-10 15:36:54 -0800225 }
226
Shawn Pearce47fd6562016-05-28 14:15:15 -0700227 private static String pageTitle(Node doc, MarkdownFile srcFile) {
228 String title = MarkdownUtil.getTitle(doc);
229 return MoreObjects.firstNonNull(title, srcFile.path);
230 }
231
232 @Nullable
233 private static MarkdownFile findFile(RevWalk rw, RevTree root, String path) throws IOException {
Shawn Pearce374f1842015-02-10 15:36:54 -0800234 if (Strings.isNullOrEmpty(path)) {
235 path = INDEX_MD;
236 }
237
238 ObjectReader reader = rw.getObjectReader();
David Pursehousec3e772a2016-06-15 21:49:35 +0900239 try (TreeWalk tw = TreeWalk.forPath(reader, path, root)) {
240 if (tw == null) {
241 return null;
242 }
243 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_TREE) {
244 if (findIndexFile(tw)) {
245 path = tw.getPathString();
246 } else {
247 return null;
248 }
249 }
250 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE) {
251 if (!path.endsWith(".md")) {
252 return null;
253 }
254 return new MarkdownFile(path, tw.getObjectId(0));
255 }
Shawn Pearce374f1842015-02-10 15:36:54 -0800256 return null;
257 }
Shawn Pearce374f1842015-02-10 15:36:54 -0800258 }
259
260 private static boolean findIndexFile(TreeWalk tw) throws IOException {
261 tw.enterSubtree();
262 while (tw.next()) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200263 if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE && INDEX_MD.equals(tw.getNameString())) {
Shawn Pearce374f1842015-02-10 15:36:54 -0800264 return true;
265 }
266 }
267 return false;
268 }
269
David Pursehouse6dec8372019-05-13 06:51:32 +0200270 private static void fileTooBig(HttpServletResponse res, GitilesView view) throws IOException {
Shawn Pearce47fd6562016-05-28 14:15:15 -0700271 if (view.getType() == GitilesView.Type.ROOTED_DOC) {
Masaya Suzuki5cecb862019-03-25 17:35:44 -0700272 throw new GitilesRequestFailureException(FailureReason.OBJECT_TOO_LARGE);
Shawn Pearce47fd6562016-05-28 14:15:15 -0700273 }
David Pursehouseb9061262019-05-13 06:52:07 +0200274 res.sendRedirect(GitilesView.show().copyFrom(view).toUrl());
Shawn Pearce47fd6562016-05-28 14:15:15 -0700275 }
276
Shawn Pearce47fd6562016-05-28 14:15:15 -0700277 private static class MarkdownFile {
Shawn Pearce374f1842015-02-10 15:36:54 -0800278 final String path;
279 final ObjectId id;
Shawn Pearce47fd6562016-05-28 14:15:15 -0700280 byte[] content;
Shawn Pearce374f1842015-02-10 15:36:54 -0800281
Shawn Pearce47fd6562016-05-28 14:15:15 -0700282 MarkdownFile(String path, ObjectId id) {
Shawn Pearce374f1842015-02-10 15:36:54 -0800283 this.path = path;
284 this.id = id;
285 }
286
Shawn Pearce47fd6562016-05-28 14:15:15 -0700287 void read(ObjectReader reader, MarkdownConfig cfg) throws IOException {
288 content = reader.open(id, OBJ_BLOB).getCachedBytes(cfg.inputLimit);
Shawn Pearce374f1842015-02-10 15:36:54 -0800289 }
Shawn Pearce5c34e092017-06-29 21:18:30 -0700290
291 byte[] consumeContent() {
292 byte[] c = content;
293 content = null;
294 return c;
295 }
Shawn Pearce374f1842015-02-10 15:36:54 -0800296 }
297}