| 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 | |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | import java.io.UnsupportedEncodingException; |
| 20 | import java.net.URLEncoder; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | import javax.servlet.http.HttpServletRequest; |
| 22 | |
| 23 | /** Interface for URLs displayed on source browsing pages. */ |
| 24 | public interface GitilesUrls { |
| 25 | /** |
| 26 | * Escapes repository or path names to be safely embedded into a URL. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 27 | * |
| 28 | * <p>This escape implementation escapes a repository or path name such as "foo/bar</child" to |
| 29 | * appear as "foo/bar%3C/child". Spaces are escaped as "%20". Its purpose is to escape a |
| 30 | * repository name to be safe for inclusion in the path component of the URL, where "/" is a valid |
| 31 | * character that should not be encoded, while almost any other non-alpha, non-numeric character |
| 32 | * will be encoded using URL style encoding. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | */ |
| David Pursehouse | fa84572 | 2016-10-04 16:26:17 +0900 | [diff] [blame] | 34 | static String escapeName(String name) { |
| 35 | try { |
| 36 | return URLEncoder.encode(name, UTF_8.name()) |
| 37 | .replace("%2F", "/") |
| 38 | .replace("%2f", "/") |
| 39 | .replace("+", "%20") |
| 40 | .replace("%2B", "+") |
| 41 | .replace("%2b", "+"); |
| 42 | } catch (UnsupportedEncodingException e) { |
| 43 | throw new IllegalStateException(e); |
| 44 | } |
| 45 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Return the name of the host from the request. |
| 49 | * |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 50 | * <p>Used in various user-visible text, like "MyHost Git Repositories". |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | * |
| 52 | * @param req request. |
| 53 | * @return host name; may be null. |
| 54 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 55 | String getHostName(HttpServletRequest req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Return the base URL for git repositories on this host. |
| 59 | * |
| 60 | * @param req request. |
| 61 | * @return base URL for git repositories. |
| 62 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 63 | String getBaseGitUrl(HttpServletRequest req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Return the base URL for Gerrit projects on this host. |
| 67 | * |
| 68 | * @param req request. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 69 | * @return base URL for Gerrit Code Review, or null if Gerrit is not configured. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | */ |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 71 | String getBaseGerritUrl(HttpServletRequest req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | } |