Package sonia.scm.util
Class HttpUtil
java.lang.Object
sonia.scm.util.HttpUtil
Util method for the http protocol.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
authentication realm for basic authenticationstatic final String
Basic authorization schemestatic final String
Bearer authorization schemestatic final String
Name of bearer authentication cookie.static final String
static final String
authorization headerstatic final String
content-length headerstatic final String
location headerstatic final String
header for identifying the scm-manager clientstatic final String
static final String
authentication headerstatic final String
The original host requested by the client in the Host HTTP request header.static final String
The original port requested by the client.static final String
The original protocol (http or https) requested by the client.static final int
Default http portstatic final int
Default https portstatic final String
Default http schemestatic final String
Default https schemestatic final String
Possible value of the X-SCM-Client http header.static final String
Url hash separatorstatic final String
Url parameter separatorstatic final String
Url parameter value separatorstatic final String
Url parameters separatorstatic final String
Url folder separatorstatic final String
Url port separatorstatic final String
Url scheme separatorstatic final String
message for unauthorized request -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Appends the suffix to given uri.static String
appendHash
(String uri, String hash) Appends the hash to the given uri.static String
appendParameter
(String uri, String name, String value) Appends the parameter to the given uri.static void
checkForCRLFInjection
(String parameter) Throws anIllegalArgumentException
if the parameter contains illegal characters which could imply a CRLF injection attack.static String
concatenate
(String... pathElements) Joins all path elements together separated by{@link #SEPARATOR_PATH}
.static String
Creates the value for the content-disposition attachment header.static String
Url decode.static void
drainBody
(jakarta.servlet.http.HttpServletRequest request) Skips to complete body of a request.static String
Url encode.static String
getCompleteUrl
(jakarta.servlet.http.HttpServletRequest request, String... pathSegments) Returns an absolute url with context path.static String
getCompleteUrl
(ScmConfiguration configuration, String path) Return the complete url of the given path.static String
getHeaderOrGetParameter
(jakarta.servlet.http.HttpServletRequest request, String parameter) Returns header value or query parameter if the request is a get request.static int
getPortFromUrl
(String url) static int
getServerPort
(ScmConfiguration configuration, jakarta.servlet.http.HttpServletRequest request) static String
getStrippedURI
(jakarta.servlet.http.HttpServletRequest request) Return the request uri without the context path.static String
getStrippedURI
(jakarta.servlet.http.HttpServletRequest request, String uri) Returns the given uri without the context path.static String
Returns the given uri without ending separator.static String
Returns the given uri without leading separator.static boolean
isChunked
(jakarta.servlet.http.HttpServletRequest request) Returns true if the body of the request is chunked.static boolean
isForwarded
(jakarta.servlet.http.HttpServletRequest request) Returnstrue
if the request is forwarded by a reverse proxy.static boolean
isWUIRequest
(jakarta.servlet.http.HttpServletRequest request) Returns true if the http request is send by the scm-manager web interface.static String
normalizeUrl
(String url) static String
removeCRLFInjectionChars
(String parameter) Remove all chars from the given parameter, which could be used for CRLF injection attack.static String
Remove matrix parameters from the given uri.static void
sendUnauthorized
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Send an unauthorized header back to the clientstatic void
sendUnauthorized
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String realmDescription) Send an unauthorized header back to the clientstatic void
sendUnauthorized
(jakarta.servlet.http.HttpServletResponse response) Send an unauthorized header back to the clientstatic void
sendUnauthorized
(jakarta.servlet.http.HttpServletResponse response, String realmDescription) Send an unauthorized header back to the clientstatic boolean
userAgentStartsWith
(jakarta.servlet.http.HttpServletRequest request, String userAgent) Returns true if the User-Agent header of the current request starts with the given string.
-
Field Details
-
AUTHENTICATION_REALM
authentication realm for basic authentication- See Also:
-
AUTHORIZATION_SCHEME_BASIC
Basic authorization scheme- Since:
- 2.0.0
- See Also:
-
AUTHORIZATION_SCHEME_BEARER
Bearer authorization scheme- Since:
- 2.0.0
- See Also:
-
COOKIE_BEARER_AUTHENTICATION
Name of bearer authentication cookie. TODO find a better place- Since:
- 2.0.0
- See Also:
-
ENCODING
- See Also:
-
HEADER_AUTHORIZATION
authorization header- Since:
- 2.0.0
- See Also:
-
HEADER_CONTENT_LENGTH
content-length header- Since:
- 1.46
- See Also:
-
HEADER_LOCATION
location header- Since:
- 1.43
- See Also:
-
HEADER_SCM_CLIENT
header for identifying the scm-manager client- Since:
- 1.19
- See Also:
-
HEADER_USERAGENT
- See Also:
-
HEADER_WWW_AUTHENTICATE
authentication header- See Also:
-
HEADER_X_FORWARDED_HOST
The original host requested by the client in the Host HTTP request header.- Since:
- 1.47
- See Also:
-
HEADER_X_FORWARDED_PORT
The original port requested by the client.- Since:
- 1.47
- See Also:
-
HEADER_X_FORWARDED_PROTO
The original protocol (http or https) requested by the client.- Since:
- 1.47
- See Also:
-
PORT_HTTP
public static final int PORT_HTTPDefault http port- Since:
- 1.5
- See Also:
-
PORT_HTTPS
public static final int PORT_HTTPSDefault https port- Since:
- 1.5
- See Also:
-
SCHEME_HTTP
Default http scheme- Since:
- 1.5
- See Also:
-
SCHEME_HTTPS
Default https scheme- Since:
- 1.5
- See Also:
-
SCM_CLIENT_WUI
Possible value of the X-SCM-Client http header. Identifies the scm-manager web interface.- Since:
- 1.19
- See Also:
-
SEPARATOR_HASH
Url hash separator- Since:
- 1.9
- See Also:
-
SEPARATOR_PARAMETER
Url parameter separator- Since:
- 1.9
- See Also:
-
SEPARATOR_PARAMETERS
Url parameters separator- Since:
- 1.9
- See Also:
-
SEPARATOR_PARAMETER_VALUE
Url parameter value separator- Since:
- 1.9
- See Also:
-
SEPARATOR_PATH
Url folder separator- Since:
- 1.5
- See Also:
-
SEPARATOR_PORT
Url port separator- Since:
- 1.5
- See Also:
-
SEPARATOR_SCHEME
Url scheme separator- Since:
- 1.5
- See Also:
-
STATUS_UNAUTHORIZED_MESSAGE
message for unauthorized request- See Also:
-
-
Method Details
-
concatenate
Joins all path elements together separated by{@link #SEPARATOR_PATH}
.- Parameters:
pathElements
- path elements- Returns:
- concatenated path
- Since:
- 2.0.0
-
append
Appends the suffix to given uri.- Parameters:
uri
- urisuffix
- suffix- Returns:
- Since:
- 1.9
-
appendHash
Appends the hash to the given uri.- Parameters:
uri
- urihash
- hash- Returns:
- uri with hash
- Since:
- 1.9
-
appendParameter
Appends the parameter to the given uri.- Parameters:
uri
- uriname
- parameter namevalue
- parameter value- Returns:
- uri with parameter
- Since:
- 1.9
-
checkForCRLFInjection
Throws anIllegalArgumentException
if the parameter contains illegal characters which could imply a CRLF injection attack. Note: the current implementation throws theIllegalArgumentException
also if the parameter contains a "%". So you have to decode your parameters before the check,- Parameters:
parameter
- value- Since:
- 1.28
-
createContentDispositionAttachmentHeader
Creates the value for the content-disposition attachment header. The method creates the filename as specified in rfc6266.- Parameters:
name
- attachment name- Returns:
- value of content-disposition header
- Since:
- 1.46
- See Also:
-
decode
Url decode.- Parameters:
value
- value to decode- Returns:
- decoded value
- Since:
- 1.9
-
drainBody
public static void drainBody(jakarta.servlet.http.HttpServletRequest request) Skips to complete body of a request.- Parameters:
request
- http request- Since:
- 1.37
-
encode
Url encode.- Parameters:
value
- value to encode- Returns:
- encoded value
- Since:
- 1.9
-
normalizeUrl
- Since:
- 1.26
-
removeCRLFInjectionChars
Remove all chars from the given parameter, which could be used for CRLF injection attack. Note: the current implementation the "%" char is also removed from the source parameter.- Parameters:
parameter
- value- Returns:
- the parameter value without crlf chars
- Since:
- 1.28
-
removeMatrixParameter
Remove matrix parameters from the given uri.- Parameters:
uri
- uri- Returns:
- uri without matrix parameter
- Since:
- 1.10
-
sendUnauthorized
public static void sendUnauthorized(jakarta.servlet.http.HttpServletResponse response) throws IOException Send an unauthorized header back to the client- Parameters:
response
- - the http response- Throws:
IOException
-
sendUnauthorized
public static void sendUnauthorized(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException Send an unauthorized header back to the client- Parameters:
request
- http requestresponse
- http response- Throws:
IOException
-
sendUnauthorized
public static void sendUnauthorized(jakarta.servlet.http.HttpServletResponse response, String realmDescription) throws IOException Send an unauthorized header back to the client- Parameters:
response
- - the http responserealmDescription
- - realm description- Throws:
IOException
- Since:
- 1.36
-
sendUnauthorized
public static void sendUnauthorized(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String realmDescription) throws IOException Send an unauthorized header back to the client- Parameters:
request
- http requestresponse
- http responserealmDescription
- realm description- Throws:
IOException
- Since:
- 1.19
-
userAgentStartsWith
public static boolean userAgentStartsWith(jakarta.servlet.http.HttpServletRequest request, String userAgent) Returns true if the User-Agent header of the current request starts with the given string.- Parameters:
request
- http requestuserAgent
- string to test against the header- Returns:
- true if the header starts with the given string
- Since:
- 1.37
-
getCompleteUrl
public static String getCompleteUrl(jakarta.servlet.http.HttpServletRequest request, String... pathSegments) Returns an absolute url with context path. The method creates the url from forwarding request headers, if they are available.- Parameters:
request
- http client requestpathSegments
-- Returns:
- absolute url with context path
- Since:
- 1.16
- See Also:
-
getCompleteUrl
Return the complete url of the given path.- Parameters:
configuration
- - main SCM-Manager configurationpath
- to get the url from- Returns:
- the complete url of the given path
- Since:
- 1.5
-
getHeader
-
getPortFromUrl
-
getServerPort
public static int getServerPort(ScmConfiguration configuration, jakarta.servlet.http.HttpServletRequest request) -
getStrippedURI
Return the request uri without the context path.- Parameters:
request
- - the http client request
-
getStrippedURI
Returns the given uri without the context path.- Parameters:
request
- - the http client requesturi
- - the uri to get the stripped uri from- Returns:
- uri without context path
-
getUriWithoutEndSeperator
Returns the given uri without ending separator.- Parameters:
uri
- - to strip ending separator- Returns:
- the given uri without an ending separator
- Since:
- 1.7
-
getHeaderOrGetParameter
public static Optional<String> getHeaderOrGetParameter(jakarta.servlet.http.HttpServletRequest request, String parameter) Returns header value or query parameter if the request is a get request.- Parameters:
request
- http requestparameter
- name of header/parameter- Returns:
- header value or query parameter
- Since:
- 2.0.0
-
getUriWithoutStartSeperator
Returns the given uri without leading separator.- Parameters:
uri
- - to strip leading separator- Since:
- 1.7
-
isChunked
public static boolean isChunked(jakarta.servlet.http.HttpServletRequest request) Returns true if the body of the request is chunked.- Since:
- 1.37
-
isForwarded
public static boolean isForwarded(jakarta.servlet.http.HttpServletRequest request) Returnstrue
if the request is forwarded by a reverse proxy. The method uses the X-Forwarded-Host header to identify a forwarded request.- Parameters:
request
- servlet request- Returns:
true
if the request is forwarded- Since:
- 1.47
-
isWUIRequest
public static boolean isWUIRequest(jakarta.servlet.http.HttpServletRequest request) Returns true if the http request is send by the scm-manager web interface.- Parameters:
request
- http request- Returns:
- true if the request comes from the web interface.
- Since:
- 1.19
-