| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 20 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 25 | import com.google.common.hash.Funnels; |
| 26 | import com.google.common.hash.HashCode; |
| 27 | import com.google.common.hash.Hasher; |
| 28 | import com.google.common.hash.Hashing; |
| Jakub Vrana | 21e70b7 | 2019-05-20 16:33:54 +0200 | [diff] [blame] | 29 | import com.google.common.html.types.LegacyConversions; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 30 | import com.google.common.io.ByteStreams; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 31 | import com.google.common.net.HttpHeaders; |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 32 | import com.google.template.soy.jbcsrc.api.SoySauce; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | import java.io.File; |
| 34 | import java.io.IOException; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 35 | import java.io.InputStream; |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 36 | import java.io.OutputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | import java.net.MalformedURLException; |
| 38 | import java.net.URL; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | import java.util.Map; |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 40 | import java.util.Optional; |
| Kurt Alfred Kluever | c1f6cfc | 2018-04-30 20:16:43 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentHashMap; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 42 | import java.util.concurrent.ConcurrentMap; |
| David Pursehouse | b40361f | 2017-05-30 10:41:53 +0900 | [diff] [blame] | 43 | import java.util.function.Function; |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 44 | import java.util.zip.GZIPOutputStream; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 45 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | import javax.servlet.http.HttpServletResponse; |
| 47 | |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Renderer for Soy templates used by Gitiles. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 50 | * |
| 51 | * <p>Most callers should not use the methods in this class directly, and instead use one of the |
| 52 | * HTML methods in {@link BaseServlet}. |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame] | 53 | */ |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | public abstract class Renderer { |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 55 | // Must match .streamingPlaceholder. |
| Sven Selberg | 958f5bf | 2022-01-31 10:33:24 +0100 | [diff] [blame] | 56 | private static final String PLACEHOLDER = "id=\"STREAMED-OUTPUT-BLOCK\""; |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 57 | |
| Dave Borowitz | 3b685ab | 2017-03-09 09:24:54 -0500 | [diff] [blame] | 58 | private static final ImmutableList<String> SOY_FILENAMES = |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 59 | ImmutableList.of( |
| 60 | "BlameDetail.soy", |
| 61 | "Common.soy", |
| 62 | "DiffDetail.soy", |
| 63 | "Doc.soy", |
| Alon Bar-Lev | cf9e71d | 2019-01-23 15:23:19 +0200 | [diff] [blame] | 64 | "Error.soy", |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 65 | "HostIndex.soy", |
| 66 | "LogDetail.soy", |
| 67 | "ObjectDetail.soy", |
| 68 | "PathDetail.soy", |
| 69 | "RefList.soy", |
| 70 | "RevisionDetail.soy", |
| 71 | "RepositoryIndex.soy"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | |
| Dave Borowitz | 3b685ab | 2017-03-09 09:24:54 -0500 | [diff] [blame] | 73 | public static final ImmutableMap<String, String> STATIC_URL_GLOBALS = |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 74 | ImmutableMap.of( |
| 75 | "gitiles.BASE_CSS_URL", "base.css", |
| 76 | "gitiles.DOC_CSS_URL", "doc.css", |
| 77 | "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 78 | |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 79 | protected static Function<String, URL> fileUrlMapper() { |
| 80 | return fileUrlMapper(""); |
| 81 | } |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 82 | |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 83 | protected static Function<String, URL> fileUrlMapper(String prefix) { |
| 84 | checkNotNull(prefix); |
| 85 | return filename -> { |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 86 | if (filename == null) { |
| 87 | return null; |
| 88 | } |
| 89 | try { |
| 90 | return new File(prefix + filename).toURI().toURL(); |
| 91 | } catch (MalformedURLException e) { |
| 92 | throw new IllegalArgumentException(e); |
| 93 | } |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 94 | }; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 97 | protected ImmutableMap<String, URL> templates; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 98 | protected ImmutableMap<String, String> globals; |
| Jesse Costello-Good | f65ff3b | 2021-05-17 13:18:09 -0700 | [diff] [blame] | 99 | protected final String siteTitle; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 100 | private final ConcurrentMap<String, HashCode> hashes = |
| Kurt Alfred Kluever | c1f6cfc | 2018-04-30 20:16:43 -0700 | [diff] [blame] | 101 | new ConcurrentHashMap<>(SOY_FILENAMES.size()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 102 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 103 | protected Renderer( |
| 104 | Function<String, URL> resourceMapper, |
| 105 | Map<String, String> globals, |
| 106 | String staticPrefix, |
| 107 | Iterable<URL> customTemplates, |
| 108 | String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | checkNotNull(staticPrefix, "staticPrefix"); |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 110 | |
| 111 | ImmutableMap.Builder<String, URL> b = ImmutableMap.builder(); |
| 112 | for (String name : SOY_FILENAMES) { |
| 113 | b.put(name, resourceMapper.apply(name)); |
| 114 | } |
| 115 | for (URL u : customTemplates) { |
| 116 | b.put(u.toString(), u); |
| 117 | } |
| 118 | templates = b.build(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 119 | |
| 120 | Map<String, String> allGlobals = Maps.newHashMap(); |
| 121 | for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) { |
| 122 | allGlobals.put(e.getKey(), staticPrefix + e.getValue()); |
| 123 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 124 | allGlobals.putAll(globals); |
| 125 | this.globals = ImmutableMap.copyOf(allGlobals); |
| Jesse Costello-Good | f65ff3b | 2021-05-17 13:18:09 -0700 | [diff] [blame] | 126 | this.siteTitle = siteTitle; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 129 | public HashCode getTemplateHash(String soyFile) { |
| 130 | HashCode h = hashes.get(soyFile); |
| 131 | if (h == null) { |
| 132 | h = computeTemplateHash(soyFile); |
| 133 | hashes.put(soyFile, h); |
| 134 | } |
| 135 | return h; |
| 136 | } |
| 137 | |
| 138 | HashCode computeTemplateHash(String soyFile) { |
| 139 | URL u = templates.get(soyFile); |
| 140 | checkState(u != null, "Missing Soy template %s", soyFile); |
| 141 | |
| David Pursehouse | d591449 | 2017-05-31 13:08:22 +0900 | [diff] [blame] | 142 | Hasher h = Hashing.murmur3_128().newHasher(); |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 143 | try (InputStream is = u.openStream(); |
| 144 | OutputStream os = Funnels.asOutputStream(h)) { |
| 145 | ByteStreams.copy(is, os); |
| 146 | } catch (IOException e) { |
| 147 | throw new IllegalStateException("Missing Soy template " + soyFile, e); |
| 148 | } |
| 149 | return h.hash(); |
| 150 | } |
| 151 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 152 | void renderHtml( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 153 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 154 | throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 155 | res.setContentType("text/html"); |
| 156 | res.setCharacterEncoding("UTF-8"); |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 157 | byte[] data = |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 158 | newRenderer(templateName, Optional.of(req)) |
| 159 | .setData(soyData) |
| 160 | .renderHtml() |
| 161 | .get() |
| 162 | .toString() |
| 163 | .getBytes(UTF_8); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 164 | if (BaseServlet.acceptsGzipEncoding(req)) { |
| Shawn Pearce | ed3c2d1 | 2016-05-30 15:59:02 -0700 | [diff] [blame] | 165 | res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 166 | res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); |
| 167 | data = BaseServlet.gzip(data); |
| 168 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 169 | res.setContentLength(data.length); |
| 170 | res.getOutputStream().write(data); |
| 171 | } |
| 172 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 173 | OutputStream renderHtmlStreaming( |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 174 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 175 | throws IOException { |
| 176 | return renderHtmlStreaming(req, res, false, templateName, soyData); |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 179 | OutputStream renderHtmlStreaming( |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 180 | HttpServletRequest req, |
| 181 | HttpServletResponse res, |
| 182 | boolean gzip, |
| 183 | String templateName, |
| 184 | Map<String, ?> soyData) |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 185 | throws IOException { |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 186 | String html = |
| 187 | newRenderer(templateName, Optional.of(req)).setData(soyData).renderHtml().get().toString(); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 188 | int id = html.indexOf(PLACEHOLDER); |
| 189 | checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER); |
| 190 | |
| 191 | int lt = html.lastIndexOf('<', id); |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 192 | int gt = html.indexOf('>', id + PLACEHOLDER.length()); |
| 193 | |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 194 | OutputStream out = gzip ? new GZIPOutputStream(res.getOutputStream()) : res.getOutputStream(); |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 195 | out.write(html.substring(0, lt).getBytes(UTF_8)); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 196 | out.flush(); |
| 197 | |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 198 | byte[] tail = html.substring(gt + 1).getBytes(UTF_8); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 199 | return new OutputStream() { |
| 200 | @Override |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 201 | public void write(byte[] b, int off, int len) throws IOException { |
| 202 | out.write(b, off, len); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void write(int b) throws IOException { |
| 207 | out.write(b); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void flush() throws IOException { |
| 212 | out.flush(); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void close() throws IOException { |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 217 | try (OutputStream o = out) { |
| 218 | o.write(tail); |
| 219 | } |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | } |
| 223 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 224 | SoySauce.Renderer newRenderer(String templateName) { |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 225 | return newRenderer(templateName, Optional.empty()); |
| 226 | } |
| 227 | |
| 228 | SoySauce.Renderer newRenderer(String templateName, Optional<HttpServletRequest> req) { |
| Jakub Vrana | 21e70b7 | 2019-05-20 16:33:54 +0200 | [diff] [blame] | 229 | ImmutableMap.Builder<String, Object> staticUrls = ImmutableMap.builder(); |
| 230 | for (String key : STATIC_URL_GLOBALS.keySet()) { |
| 231 | staticUrls.put( |
| 232 | key.replaceFirst("^gitiles\\.", ""), |
| 233 | LegacyConversions.riskilyAssumeTrustedResourceUrl(globals.get(key))); |
| 234 | } |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 235 | ImmutableMap.Builder<String, Object> ij = |
| 236 | ImmutableMap.<String, Object>builder() |
| 237 | .put("staticUrls", staticUrls.build()) |
| 238 | .put("SITE_TITLE", siteTitle); |
| 239 | Optional<String> nonce = req.map((r) -> (String) r.getAttribute("nonce")); |
| Matthias Sohn | 30ea267 | 2023-09-30 22:18:32 +0200 | [diff] [blame] | 240 | if (nonce.isPresent()) { |
| Jonathan Nieder | 9707668 | 2022-12-12 15:54:33 -0800 | [diff] [blame] | 241 | ij.put("csp_nonce", nonce.get()); |
| Kamil Musin | 34d5008 | 2022-12-09 15:48:47 +0100 | [diff] [blame] | 242 | } |
| 243 | return getSauce().renderTemplate(templateName).setIj(ij.build()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 246 | protected abstract SoySauce getSauce(); |
| Jesse Costello-Good | 6f62c5d | 2021-02-02 12:05:06 -0800 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Give a resource URL of a soy template file, returns the import path for use in a Soy import |
| 250 | * statement. |
| 251 | */ |
| 252 | protected String toSoySrcPath(URL templateUrl) { |
| 253 | String filePath = templateUrl.getPath(); |
| 254 | String fileName = filePath.substring(filePath.lastIndexOf('/') + 1); |
| 255 | return "com/google/gitiles/templates/" + fileName; |
| 256 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 257 | } |