Class WSHelper
java.lang.Object
com.iizix.jetty.WSHelper
Helper class for common functions for Jetty.
- Author:
- Christopher Mindus
Nested Class Summary
Nested ClassesField Summary
FieldsConstructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic URIappendToPath(URI uri, String suffix) Creates a new URI with the path + suffix.static intgetClientPort(HttpServletRequest request) Gets the port the client used for a request: from the port configuration of the connector (PortConfig.getClientPort(): the external port, else 443 behind a front-end proxy that terminates TLS, else the local port), never from a request header.static StringgetDomain(HttpServletRequest request, int check, String originator) Helper to get the domain name from a servlet request and to optionally check if there is a matching "host" header for it.static StringExtracts the domain name from an HttpServletRequest from the "host" header field (removing the port specification - keeping in mind that it could be an IPv6 address).static StringgetDomainFromURL(HttpServletRequest request) Extracts the domain name from an HttpServletRequest from the URL.static StringgetExternalBaseServerURL(String extServerURL, String bind, int port, int securePort, boolean hasExtSSL) Returns the external base server URL with protocol, host name and potentially port number.static PortConfiggetPortConfig(HttpServletRequest request) Gets the port configuration of the connector that received a request.static PortConfiggetPortConfig(Request request) Gets the port configuration of the connector that received a core request (CC-FIX-APP-DEFINITIONS): the connector of its connection meta data, tested forIServerConnector.static booleanisLoopbackOrigin(String origin) Tells whether anOriginis a loopback one:httporhttpsonlocalhost,127.0.0.1or[::1], any port or none.static booleanisSecure(HttpServletRequest request) Checks if a request arrived on a secure port: TLS is terminated either by this server (request.isSecure()) or by a reverse proxy the operator declared in the port configuration (PortConfig.isFrontEndSecured).static booleanisThroughFrontEnd(HttpServletRequest request) Checks if a request came through the front-end proxy of a port configured as front-end secured (PortConfig.isFrontEndSecured).static StringvalidateExternalHostName(String urlString) Validates an external host URL.
Field Details
DEBUG
public static final boolean DEBUGDebug flag.
Constructor Details
WSHelper
public WSHelper()
Method Details
appendToPath
Creates a new URI with the path + suffix.- Parameters:
uri- The original URI.suffix- The suffix (e.g. file extension ".gz").- Returns:
- The new URI.
- Throws:
URISyntaxException- If the suffix is null, if the URI is opaque and therefore has no path to append to (e.g. a "jar:" URI), or if the resulting URI is invalid (the last should not occur when the suffix is just a normal file extension).
getExternalBaseServerURL
public static String getExternalBaseServerURL(String extServerURL, String bind, int port, int securePort, boolean hasExtSSL) Returns the external base server URL with protocol, host name and potentially port number.The name will be taken from:
- external host name if specified,
- resolved bind address if specified and possible to retrieve,
- bind address if specified,
- resolved local host name if possible to retrieve,
- local host name if possible to retrieve,
- "localhost" last.
If "localhost" is used, a warning message is logged about it.
The protocol to use will be secure HTTPS if possible, otherwise the HTTP, even if it is zero for none.
- Parameters:
extServerURL- The external server URL, null or empty for none.bind- Bind address specified, null or empty for none.port- HTTP port number, zero for none.securePort- HTTPS port number, zero for none.hasExtSSL- HTTP port is externally secured.
validateExternalHostName
getDomain
Helper to get the domain name from a servlet request and to optionally check if there is a matching "host" header for it. In case there is no matching "host" header, logging can be performed.- Parameters:
request- The servlet request.check- How to perform the check:
0 = no warnings, no logging, just return it,
1 = warning if "host" doesn't match, but return it,
2 = error if "host" doesn't match, and return null.originator- String to mark as originator when logging error.- Returns:
- The domain name or "host" header field, null if this fails.
getDomainFromURL
Extracts the domain name from an HttpServletRequest from the URL.- Parameters:
request- The HttpServletRequest request.- Returns:
- The domain name, or "unknown" if the request URL/URI is formatted incorrectly.
isLoopbackOrigin
Tells whether anOriginis a loopback one:httporhttpsonlocalhost,127.0.0.1or[::1], any port or none. A development server and the Designer's internal web server grant them for CORS (CC-FIX-SB-CORS): the pages one development server serves on a developer's machine call another on another port of it.- Parameters:
origin- The origin, may be null.- Returns:
- true for a loopback origin.
getDomainFromHostHeader
Extracts the domain name from an HttpServletRequest from the "host" header field (removing the port specification - keeping in mind that it could be an IPv6 address).If the header field 'host' is missing, the return value will be set to
request.getServerName().- Parameters:
request- The HttpServletRequest request.- Returns:
- The domain name, or
nullfor missing "host" header field and failure to get it usingrequest.getServerName().
isSecure
Checks if a request arrived on a secure port: TLS is terminated either by this server (request.isSecure()) or by a reverse proxy the operator declared in the port configuration (PortConfig.isFrontEndSecured). Behind a reverse proxy (e.g. Caddy) the connection to Jetty is plain HTTP, sorequest.isSecure()is false although the client is on TLS; the port configuration is the only thing a client cannot forge, so no request header (X-Forwarded-Proto,Forwarded) is ever consulted.The connector is reached as
ServletApiRequesttoServletContextRequestto its connection meta data, tested forIServerConnector. When any link does not match, the answer isrequest.isSecure()alone.- Parameters:
request- The servlet request.- Returns:
- true if the request is secure by either rule, false otherwise.
isThroughFrontEnd
Checks if a request came through the front-end proxy of a port configured as front-end secured (PortConfig.isFrontEndSecured). The port configuration alone cannot tell: the same connector may also be reached directly, e.g.http://192.168.10.100:8080on the LAN, and such a client uses this connector's own scheme and port. TheHostheader tells: a request whoseHostnames this connector's own port came directly; aHostwithout a port (the proxy's default port) or with another port came through the proxy.- Parameters:
request- The servlet request.- Returns:
- true if the connector is front-end secured and the request came through the proxy.
getPortConfig
Gets the port configuration of the connector that received a request.The connector is reached as
ServletApiRequesttoServletContextRequestto its connection meta data, tested forIServerConnector.- Parameters:
request- The servlet request.- Returns:
- The port configuration, or
nullwhen any link does not match or the connector has none.
getPortConfig
Gets the port configuration of the connector that received a core request (CC-FIX-APP-DEFINITIONS): the connector of its connection meta data, tested forIServerConnector.- Parameters:
request- The core request, may be null.- Returns:
- The port configuration, or
nullwhen the connector is none of ours or the request is null.
getClientPort
Gets the port the client used for a request: from the port configuration of the connector (PortConfig.getClientPort(): the external port, else 443 behind a front-end proxy that terminates TLS, else the local port), never from a request header. Behind a reverse proxy (e.g. Caddy)request.getServerPort()is the port of the plain HTTP hop (80 when theHostheader has none), not the client's.- Parameters:
request- The servlet request.- Returns:
- The client's port, or
request.getServerPort()when the connector has no port configuration.