You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegexPipeline.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.http.server.glue;
  11. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  12. import static org.eclipse.jgit.http.server.glue.MetaFilter.REGEX_GROUPS;
  13. import java.io.IOException;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. import javax.servlet.Filter;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. /**
  22. * Selects requests by matching the URI against a regular expression.
  23. * <p>
  24. * The pattern is bound and matched against the path info of the servlet
  25. * request, as this class assumes it is invoked by {@link MetaServlet}.
  26. * <p>
  27. * If there are capture groups in the regular expression, the matched ranges of
  28. * the capture groups are stored as an array of modified HttpServetRequests,
  29. * into the request attribute {@link MetaFilter#REGEX_GROUPS}. Using a capture
  30. * group that may not capture, e.g. {@code "(/foo)?"}, will cause an error at
  31. * request handling time.
  32. * <p>
  33. * Each servlet request has been altered to have its {@code getServletPath()}
  34. * method return the original path info up to the beginning of the corresponding
  35. * capture group, and its {@code getPathInfo()} method return the matched text.
  36. * A {@link RegexGroupFilter} can be applied in the pipeline to switch the
  37. * current HttpServletRequest to reference a different capture group before
  38. * running additional filters, or the final servlet.
  39. * <p>
  40. * Note that for {@code getPathInfo()} to start with a leading "/" as described
  41. * in the servlet documentation, capture groups must actually capture the
  42. * leading "/".
  43. * <p>
  44. * This class dispatches the remainder of the pipeline using the first capture
  45. * group as the current request, making {@code RegexGroupFilter} required only
  46. * to access capture groups beyond the first.
  47. */
  48. class RegexPipeline extends UrlPipeline {
  49. static class Binder extends ServletBinderImpl {
  50. private final Pattern pattern;
  51. Binder(String p) {
  52. pattern = Pattern.compile(p);
  53. }
  54. Binder(Pattern p) {
  55. pattern = p;
  56. }
  57. @Override
  58. UrlPipeline create() {
  59. return new RegexPipeline(pattern, getFilters(), getServlet());
  60. }
  61. }
  62. private final Pattern pattern;
  63. RegexPipeline(final Pattern pattern, final Filter[] filters,
  64. final HttpServlet servlet) {
  65. super(filters, servlet);
  66. this.pattern = pattern;
  67. }
  68. @Override
  69. boolean match(HttpServletRequest req) {
  70. final String pathInfo = req.getPathInfo();
  71. return pathInfo != null && pattern.matcher(pathInfo).matches();
  72. }
  73. @Override
  74. void service(HttpServletRequest req, HttpServletResponse rsp)
  75. throws ServletException, IOException {
  76. final String reqInfo = req.getPathInfo();
  77. if (reqInfo == null) {
  78. rsp.sendError(SC_NOT_FOUND);
  79. return;
  80. }
  81. final Matcher cur = pattern.matcher(reqInfo);
  82. if (!cur.matches()) {
  83. rsp.sendError(SC_NOT_FOUND);
  84. return;
  85. }
  86. final String reqPath = req.getServletPath();
  87. final Object old = req.getAttribute(REGEX_GROUPS);
  88. try {
  89. if (1 <= cur.groupCount()) {
  90. // If there are groups extract every capture group and
  91. // build a request for them so RegexGroupFilter can pick
  92. // a different capture group later. Continue using the
  93. // first capture group as the path info.
  94. WrappedRequest groups[] = new WrappedRequest[cur.groupCount()];
  95. for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
  96. final int s = cur.start(groupId);
  97. final String path, info;
  98. path = reqPath + reqInfo.substring(0, s);
  99. info = cur.group(groupId);
  100. groups[groupId - 1] = new WrappedRequest(req, path, info);
  101. }
  102. req.setAttribute(REGEX_GROUPS, groups);
  103. super.service(groups[0], rsp);
  104. } else {
  105. // No capture groups were present, service the whole request.
  106. final String path = reqPath + reqInfo;
  107. final String info = null;
  108. super.service(new WrappedRequest(req, path, info), rsp);
  109. }
  110. } finally {
  111. if (old != null)
  112. req.setAttribute(REGEX_GROUPS, old);
  113. else
  114. req.removeAttribute(REGEX_GROUPS);
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public String toString() {
  120. return "Pipeline[regex: " + pattern + " ]";
  121. }
  122. }