Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GitSmartHttpTools.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.server;
  44. import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
  45. import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
  46. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.IOException;
  49. import java.util.Arrays;
  50. import java.util.Collections;
  51. import java.util.List;
  52. import javax.servlet.ServletOutputStream;
  53. import javax.servlet.http.HttpServletRequest;
  54. import javax.servlet.http.HttpServletResponse;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.transport.PacketLineOut;
  57. /**
  58. * Utility functions for handling the Git-over-HTTP protocol.
  59. */
  60. public class GitSmartHttpTools {
  61. private static final String INFO_REFS = Constants.INFO_REFS;
  62. /** Name of the git-upload-pack service. */
  63. public static final String UPLOAD_PACK = "git-upload-pack";
  64. /** Name of the git-receive-pack service. */
  65. public static final String RECEIVE_PACK = "git-receive-pack";
  66. /** Content type supplied by the client to the /git-upload-pack handler. */
  67. public static final String UPLOAD_PACK_REQUEST_TYPE =
  68. "application/x-git-upload-pack-request";
  69. /** Content type returned from the /git-upload-pack handler. */
  70. public static final String UPLOAD_PACK_RESULT_TYPE =
  71. "application/x-git-upload-pack-result";
  72. /** Content type supplied by the client to the /git-receive-pack handler. */
  73. public static final String RECEIVE_PACK_REQUEST_TYPE =
  74. "application/x-git-receive-pack-request";
  75. /** Content type returned from the /git-receive-pack handler. */
  76. public static final String RECEIVE_PACK_RESULT_TYPE =
  77. "application/x-git-receive-pack-result";
  78. /** Git service names accepted by the /info/refs?service= handler. */
  79. public static final List<String> VALID_SERVICES =
  80. Collections.unmodifiableList(Arrays.asList(new String[] {
  81. UPLOAD_PACK, RECEIVE_PACK }));
  82. private static final String INFO_REFS_PATH = "/" + INFO_REFS;
  83. private static final String UPLOAD_PACK_PATH = "/" + UPLOAD_PACK;
  84. private static final String RECEIVE_PACK_PATH = "/" + RECEIVE_PACK;
  85. private static final List<String> SERVICE_SUFFIXES =
  86. Collections.unmodifiableList(Arrays.asList(new String[] {
  87. INFO_REFS_PATH, UPLOAD_PACK_PATH, RECEIVE_PACK_PATH }));
  88. /**
  89. * Check a request for Git-over-HTTP indicators.
  90. *
  91. * @param req
  92. * the current HTTP request that may have been made by Git.
  93. * @return true if the request is likely made by a Git client program.
  94. */
  95. public static boolean isGitClient(HttpServletRequest req) {
  96. return isInfoRefs(req) || isUploadPack(req) || isReceivePack(req);
  97. }
  98. /**
  99. * Send an error to the Git client or browser.
  100. * <p>
  101. * Server implementors may use this method to send customized error messages
  102. * to a Git protocol client using an HTTP 200 OK response with the error
  103. * embedded in the payload. If the request was not issued by a Git client,
  104. * an HTTP response code is returned instead.
  105. *
  106. * @param req
  107. * current request.
  108. * @param res
  109. * current response.
  110. * @param httpStatus
  111. * HTTP status code to set if the client is not a Git client.
  112. * @throws IOException
  113. * the response cannot be sent.
  114. */
  115. public static void sendError(HttpServletRequest req,
  116. HttpServletResponse res, int httpStatus) throws IOException {
  117. sendError(req, res, httpStatus, null);
  118. }
  119. /**
  120. * Send an error to the Git client or browser.
  121. * <p>
  122. * Server implementors may use this method to send customized error messages
  123. * to a Git protocol client using an HTTP 200 OK response with the error
  124. * embedded in the payload. If the request was not issued by a Git client,
  125. * an HTTP response code is returned instead.
  126. *
  127. * @param req
  128. * current request.
  129. * @param res
  130. * current response.
  131. * @param httpStatus
  132. * HTTP status code to set if the client is not a Git client.
  133. * @param textForGit
  134. * plain text message to display on the user's console. This is
  135. * shown only if the client is likely to be a Git client. If null
  136. * or the empty string a default text is chosen based on the HTTP
  137. * response code.
  138. * @throws IOException
  139. * the response cannot be sent.
  140. */
  141. public static void sendError(HttpServletRequest req,
  142. HttpServletResponse res, int httpStatus, String textForGit)
  143. throws IOException {
  144. if (textForGit == null || textForGit.length() == 0) {
  145. switch (httpStatus) {
  146. case SC_FORBIDDEN:
  147. textForGit = HttpServerText.get().repositoryAccessForbidden;
  148. break;
  149. case SC_NOT_FOUND:
  150. textForGit = HttpServerText.get().repositoryNotFound;
  151. break;
  152. case SC_INTERNAL_SERVER_ERROR:
  153. textForGit = HttpServerText.get().internalServerError;
  154. break;
  155. default:
  156. textForGit = "HTTP " + httpStatus;
  157. break;
  158. }
  159. }
  160. ByteArrayOutputStream buf = new ByteArrayOutputStream(128);
  161. PacketLineOut pck = new PacketLineOut(buf);
  162. if (isInfoRefs(req)) {
  163. String svc = req.getParameter("service");
  164. pck.writeString("# service=" + svc + "\n");
  165. pck.end();
  166. pck.writeString("ERR " + textForGit);
  167. send(res, infoRefsResultType(svc), buf.toByteArray());
  168. } else if (isUploadPack(req)) {
  169. pck.writeString("ERR " + textForGit);
  170. send(res, UPLOAD_PACK_RESULT_TYPE, buf.toByteArray());
  171. } else if (isReceivePack(req)) {
  172. pck.writeString("ERR " + textForGit);
  173. send(res, RECEIVE_PACK_RESULT_TYPE, buf.toByteArray());
  174. } else {
  175. res.sendError(httpStatus);
  176. }
  177. }
  178. private static void send(HttpServletResponse res, String type, byte[] buf)
  179. throws IOException {
  180. res.setStatus(HttpServletResponse.SC_OK);
  181. res.setContentType(type);
  182. res.setContentLength(buf.length);
  183. ServletOutputStream os = res.getOutputStream();
  184. try {
  185. os.write(buf);
  186. } finally {
  187. os.close();
  188. }
  189. }
  190. /**
  191. * Get the response Content-Type a client expects for the request.
  192. * <p>
  193. * This method should only be invoked if
  194. * {@link #isGitClient(HttpServletRequest)} is true.
  195. *
  196. * @param req
  197. * current request.
  198. * @return the Content-Type the client expects.
  199. * @throws IllegalArgumentException
  200. * the request is not a Git client request. See
  201. * {@link #isGitClient(HttpServletRequest)}.
  202. */
  203. public static String getResponseContentType(HttpServletRequest req) {
  204. if (isInfoRefs(req))
  205. return infoRefsResultType(req.getParameter("service"));
  206. else if (isUploadPack(req))
  207. return UPLOAD_PACK_RESULT_TYPE;
  208. else if (isReceivePack(req))
  209. return RECEIVE_PACK_RESULT_TYPE;
  210. else
  211. throw new IllegalArgumentException();
  212. }
  213. static String infoRefsResultType(String svc) {
  214. return "application/x-" + svc + "-advertisement";
  215. }
  216. /**
  217. * Strip the Git service suffix from a request path.
  218. *
  219. * Generally the suffix is stripped by the {@code SuffixPipeline} handling
  220. * the request, so this method is rarely needed.
  221. *
  222. * @param path
  223. * the path of the request.
  224. * @return the path up to the last path component before the service suffix;
  225. * the path as-is if it contains no service suffix.
  226. */
  227. public static String stripServiceSuffix(String path) {
  228. for (String suffix : SERVICE_SUFFIXES) {
  229. if (path.endsWith(suffix))
  230. return path.substring(0, path.length() - suffix.length());
  231. }
  232. return path;
  233. }
  234. /**
  235. * Check if the HTTP request was for the /info/refs?service= Git handler.
  236. *
  237. * @param req
  238. * current request.
  239. * @return true if the request is for the /info/refs service.
  240. */
  241. public static boolean isInfoRefs(HttpServletRequest req) {
  242. return req.getRequestURI().endsWith(INFO_REFS_PATH)
  243. && VALID_SERVICES.contains(req.getParameter("service"));
  244. }
  245. /**
  246. * Check if the HTTP request path ends with the /git-upload-pack handler.
  247. *
  248. * @param pathOrUri
  249. * path or URI of the request.
  250. * @return true if the request is for the /git-upload-pack handler.
  251. */
  252. public static boolean isUploadPack(String pathOrUri) {
  253. return pathOrUri != null && pathOrUri.endsWith(UPLOAD_PACK_PATH);
  254. }
  255. /**
  256. * Check if the HTTP request was for the /git-upload-pack Git handler.
  257. *
  258. * @param req
  259. * current request.
  260. * @return true if the request is for the /git-upload-pack handler.
  261. */
  262. public static boolean isUploadPack(HttpServletRequest req) {
  263. return isUploadPack(req.getRequestURI())
  264. && UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType());
  265. }
  266. /**
  267. * Check if the HTTP request was for the /git-receive-pack Git handler.
  268. *
  269. * @param req
  270. * current request.
  271. * @return true if the request is for the /git-receive-pack handler.
  272. */
  273. public static boolean isReceivePack(HttpServletRequest req) {
  274. String uri = req.getRequestURI();
  275. return uri != null && uri.endsWith(RECEIVE_PACK_PATH)
  276. && RECEIVE_PACK_REQUEST_TYPE.equals(req.getContentType());
  277. }
  278. private GitSmartHttpTools() {
  279. }
  280. }