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.

GitblitParamUrlCodingStrategy.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket;
  17. import java.text.MessageFormat;
  18. import org.apache.wicket.IRequestTarget;
  19. import org.apache.wicket.Page;
  20. import org.apache.wicket.PageParameters;
  21. import org.apache.wicket.request.RequestParameters;
  22. import org.apache.wicket.request.target.coding.MixedParamUrlCodingStrategy;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import com.gitblit.GitBlit;
  26. import com.gitblit.Keys;
  27. /**
  28. * Simple subclass of mixed parameter url coding strategy that works around the
  29. * encoded forward-slash issue that is present in some servlet containers.
  30. *
  31. * https://issues.apache.org/jira/browse/WICKET-1303
  32. * http://tomcat.apache.org/security-6.html
  33. *
  34. * @author James Moger
  35. *
  36. */
  37. public class GitblitParamUrlCodingStrategy extends MixedParamUrlCodingStrategy {
  38. private Logger logger = LoggerFactory.getLogger(GitblitParamUrlCodingStrategy.class);
  39. /**
  40. * Construct.
  41. *
  42. * @param <C>
  43. * @param mountPath
  44. * mount path (not empty)
  45. * @param bookmarkablePageClass
  46. * class of mounted page (not null)
  47. * @param parameterNames
  48. * the parameter names (not null)
  49. */
  50. public <C extends Page> GitblitParamUrlCodingStrategy(String mountPath,
  51. Class<C> bookmarkablePageClass, String[] parameterNames) {
  52. super(mountPath, bookmarkablePageClass, parameterNames);
  53. }
  54. /**
  55. * Url encodes a string that is mean for a URL path (e.g., between slashes)
  56. *
  57. * @param string
  58. * string to be encoded
  59. * @return encoded string
  60. */
  61. protected String urlEncodePathComponent(String string) {
  62. char altChar = GitBlit.getChar(Keys.web.forwardSlashCharacter, '/');
  63. if (altChar != '/') {
  64. string = string.replace('/', altChar);
  65. }
  66. return super.urlEncodePathComponent(string);
  67. }
  68. /**
  69. * Returns a decoded value of the given value (taken from a URL path
  70. * section)
  71. *
  72. * @param value
  73. * @return Decodes the value
  74. */
  75. protected String urlDecodePathComponent(String value) {
  76. char altChar = GitBlit.getChar(Keys.web.forwardSlashCharacter, '/');
  77. if (altChar != '/') {
  78. value = value.replace(altChar, '/');
  79. }
  80. return super.urlDecodePathComponent(value);
  81. }
  82. /**
  83. * Gets the decoded request target.
  84. *
  85. * @param requestParameters
  86. * the request parameters
  87. * @return the decoded request target
  88. */
  89. @Override
  90. public IRequestTarget decode(RequestParameters requestParameters) {
  91. final String parametersFragment = requestParameters.getPath().substring(
  92. getMountPath().length());
  93. logger.debug(MessageFormat
  94. .format("REQ: {0} PARAMS {1}", getMountPath(), parametersFragment));
  95. final PageParameters parameters = new PageParameters(decodeParameters(parametersFragment,
  96. requestParameters.getParameters()));
  97. return super.decode(requestParameters);
  98. }
  99. }