java.lang.Object
edu.iu.IuWebUtils
Provides useful utility methods for low-level web client and server
interactions.
-
Method Summary
Modifier and TypeMethodDescription(package private) static booleanalpha(char c) ALPHA = %x41-5A / %x61-7A(package private) static intBWS = OWS(package private) static intcomment = "(" *( ctext / quoted-pair / comment ) ")"(package private) static booleancookieOctet(char c) cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E (US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash)(package private) static intcookieValue(String cookieHeaderValue, int pos) cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )static StringcreateChallenge(String scheme, Map<String, String> params) Creates an authentication challenge sending to a client via the WWW-Authenticate header.static StringcreateQueryString(Map<String, ? extends Iterable<String>> params) Creates a query string from a map.(package private) static booleanctext(char c) ctext = HTAB / SP / %x21-27 / %x2A-5B / %x5D-7E / obs-text(package private) static intParses a decimal octet, as defined for an IPv4address.static StringdescribeStatus(int statusCode) Describes an HTTP status code.(package private) static booleandigit(char c) DIGIT = %x30-39static InetAddressgetInetAddress(String hostname) Resolves and caches theIP addressfor a host name.(package private) static intParses an h16 (16-bit hexadecimal number).(package private) static booleanhexdig(char c) Determines if a character is a HEXDIG character.static booleanisInetAddressInRange(InetAddress address, String range) Determines whether or not an IP address is included in a CIDR range.static booleanisIPAddress(String nodename) Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IP address.static booleanisIPv4Address(String nodename) Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IPv4 address.static booleanisIPv6Address(String nodename) Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IPv6 address.static booleanstatic booleanisVisibleAscii(String s, int minLength) Determines whether or not a string is composed entirely of non-whitespace visible ASCII characters, and at least minLength characters, but no longer than 1024.(package private) static intParses an ls32 (least significant 32-bits), either as anh16 pairorparseIPv4Address(String, int).static StringnormalizeHeaderName(String headerName) Validates and normalizes case for an HTTP header name.(package private) static intParses an obfnode or obfport forparseNodeIdentifier(String).(package private) static booleanobsText(char c) obs-text = %x80-FFstatic Iterator<IuWebAuthenticationChallenge> parseAuthenticateHeader(String wwwAuthenticate) Parses challenge parameters from a WWW-Authenticate header.static Iterable<HttpCookie> parseCookieHeader(String cookieHeaderValue) Parses the cookie request header, returning anHttpCookiefor each cookie sent with the request.static IuForwardedHeaderparseForwardedHeader(String forwardedHeaderValue) Parses the cookie request header, returning anHttpCookiefor each cookie sent with the request.parseHeader(String headerValue) Parses a header value composed of key/value pairs separated by semicolon ';'.(package private) static intparseIPv4Address(String nodeId, int originalPos) Parses an IPV4Address forparseNodeIdentifier(String).(package private) static intparseIPv6Address(String nodeId, int originalPos) Parses an IPV6Address forparseNodeIdentifier(String).static InetSocketAddressparseNodeIdentifier(String nodeId) Parses a node identifier.static InetSocketAddressparseNodeIdentifier(String nodeId, Function<String, InetAddress> obfuscatedNodeLookup, Function<String, Integer> obfuscatedPortLookup) ImplementsparseNodeIdentifier(String nodeId)with obfuscated node and port lookups.parseQueryString(String queryString) Parses a query string.(package private) static intproduct = token [ "/" product-version ](package private) static booleanqdtext(char c) qdtext = HTAB / SP / "!" / %x23-5B / %x5D-7E / obs-text(package private) static intquotedPair(String s, int pos) quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )(package private) static intquotedString(String s, int start) quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE(package private) static int1*SP(package private) static booleantchar(char c) tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA(package private) static inttoken = 1*tchar(package private) static inttoken68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="static voidvalidateUserAgent(String userAgent) Validates a user-agent header value.(package private) static booleanvchar(char c) VCHAR = %x21-7E
-
Method Details
-
vchar
static boolean vchar(char c) VCHAR = %x21-7E- Parameters:
c- character- Returns:
- true if c matches VCHAR ABNF rule; else false
- See Also:
-
alpha
static boolean alpha(char c) ALPHA = %x41-5A / %x61-7A- Parameters:
c- character- Returns:
- true if c matches ALHPA ABNF rule; else false
- See Also:
-
digit
static boolean digit(char c) DIGIT = %x30-39- Parameters:
c- character- Returns:
- true if c matches DIGIT ABNF rule; else false
- See Also:
-
ctext
static boolean ctext(char c) ctext = HTAB / SP / %x21-27 / %x2A-5B / %x5D-7E / obs-text- Parameters:
c- character- Returns:
- true if c matches ctext ABNF rule; else false
- See Also:
-
obsText
static boolean obsText(char c) obs-text = %x80-FF- Parameters:
c- character- Returns:
- true if c matches obs-text ABNF rule; else false
- See Also:
-
comment
comment = "(" *( ctext / quoted-pair / comment ) ")"- Parameters:
s- input stringpos- position at start of comment- Returns:
- end position after matching comment ABNF rule; returns pos if token68 was not matched
- See Also:
-
token68
token68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="- Parameters:
s- input stringpos- position at start of token68 character- Returns:
- end position after matching token68 ABNF rule; returns pos if token68 was not matched
- See Also:
-
tchar
static boolean tchar(char c) tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA- Parameters:
c- character- Returns:
- true if c matches obs-text ABNF rule; else false
- See Also:
-
token
token = 1*tchar- Parameters:
s- input stringpos- position at start of token character- Returns:
- end position of a matching token ABNF rule; returns pos if token was not matched
- See Also:
-
bws
BWS = OWSOWS = *( SP / HTAB )
RWS = 1*( SP / HTAB )
- Parameters:
s- input stringpos- position at start of token character- Returns:
- end position of a matching BWS ABNF rule
- See Also:
-
sp
1*SP- Parameters:
s- input stringpos- position at start of token character- Returns:
- end position of a matching sp ABNF rule
- See Also:
-
product
product = token [ "/" product-version ]product-version = token
- Parameters:
s- input stringpos- position at start of product expression- Returns:
- end position of a matching product ABNF rule; pos if not matched
-
qdtext
static boolean qdtext(char c) qdtext = HTAB / SP / "!" / %x23-5B / %x5D-7E / obs-text- Parameters:
c- character- Returns:
- true if c matches qdtext ABNF rule; else false
- See Also:
-
quotedPair
quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )- Parameters:
s- input stringpos- position at start of token character- Returns:
- end position of a matching quoted-pair ABNF rule; pos if the rule was not matched
- See Also:
-
quotedString
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE- Parameters:
s- input stringstart- position at start of token character- Returns:
- end position of a matching BWS ABNF rule
- See Also:
-
isVisibleAscii
Determines whether or not a string is composed entirely of non-whitespace visible ASCII characters, and at least minLength characters, but no longer than 1024.- Parameters:
s- string to checkminLength- minimum length- Returns:
- true if all characters are visible ASCII
-
isRootOf
-
createChallenge
Creates an authentication challenge sending to a client via the WWW-Authenticate header.- Parameters:
scheme- authentication scheme to requestparams- challenge attributes for informing the client of how to authenticate- Returns:
- authentication challenge
- See Also:
-
parseAuthenticateHeader
public static Iterator<IuWebAuthenticationChallenge> parseAuthenticateHeader(String wwwAuthenticate) Parses challenge parameters from a WWW-Authenticate header.- Parameters:
wwwAuthenticate- WWW-Authenticate header challenge value- Returns:
- Parsed authentication challenge parameters
-
validateUserAgent
Validates a user-agent header value.User-Agent = product *( RWS ( product / comment ) )
- Parameters:
userAgent- user-agent header value- See Also:
-
parseQueryString
Parses a query string.- Parameters:
queryString- query string- Returns:
Map
-
createQueryString
Creates a query string from a map.- Parameters:
params-Mapof parameter values- Returns:
- query string
-
parseHeader
Parses a header value composed of key/value pairs separated by semicolon ';'.- Parameters:
headerValue- header value- Returns:
Mapof header elements
-
cookieOctet
static boolean cookieOctet(char c) cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E (US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash)- Parameters:
c- character- Returns:
- true if the character is in the cookie-octet character set.
- See Also:
-
cookieValue
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E (US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon, and backslash)
- Parameters:
cookieHeaderValue- Cookie header valuepos- position at start of the cookie-value token- Returns:
- end position of the cookie value
- See Also:
-
parseCookieHeader
Parses the cookie request header, returning anHttpCookiefor each cookie sent with the request.- Parameters:
cookieHeaderValue-Cookie:header value. This value MUST match the syntax defined forcookie-stringRFC-6265 HTTP State Management, Section 4.2.1- Returns:
- Iterable of parsed cookies
-
parseForwardedHeader
Parses the cookie request header, returning anHttpCookiefor each cookie sent with the request.- Parameters:
forwardedHeaderValue-Forwarded:header value. This value MUST match the syntax defined forForwardedat RFC-7238 Forwarded HTTP Extension, Section 4- Returns:
- forwarded header value
-
decOctet
Parses a decimal octet, as defined for an IPv4address.- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
parseIPv4Address
Parses an IPV4Address forparseNodeIdentifier(String).- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
hexdig
static boolean hexdig(char c) Determines if a character is a HEXDIG character.HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
- Parameters:
c- character- Returns:
- true if the character is matches the HEXDIG ABNF rules from RFC-5234; else false
-
h16
Parses an h16 (16-bit hexadecimal number).- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
ls32
Parses an ls32 (least significant 32-bits), either as anh16 pairorparseIPv4Address(String, int).- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
parseIPv6Address
Parses an IPV6Address forparseNodeIdentifier(String).- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
obf
Parses an obfnode or obfport forparseNodeIdentifier(String).- Parameters:
nodeId-node identifieroriginalPos- start position- Returns:
- end position if matching; else returns start position
-
isIPv4Address
Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IPv4 address.- Parameters:
nodename- nodename string- Returns:
- true if nodename is a string that contains a valid IPv4 address; else false
- See Also:
-
isIPv6Address
Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IPv6 address.- Parameters:
nodename- nodename string- Returns:
- true if nodename is a string that contains a valid IPv6 address; else false
- See Also:
-
isIPAddress
Determines whether or not a nodename string, i.e., from an HTTP header, contains a valid IP address.- Parameters:
nodename- nodename string- Returns:
- true if nodename is a string that contains a valid IP address; else false
- See Also:
-
parseNodeIdentifier
Parses a node identifier.The node identifier is defined by the ABNF syntax as:
node = nodename [ ":" node-port ] nodename = IPv4address / "[" IPv6address "]" / "unknown" / obfnode IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet dec-octet = DIGIT ; 0-9 / %x31-39 DIGIT ; 10-99 / "1" 2DIGIT ; 100-199 / "2" %x30-34 DIGIT ; 200-249 / "25" %x30-35 ; 250-255 IPv6address = 6( h16 ":" ) ls32 / "::" 5( h16 ":" ) ls32 / [ h16 ] "::" 4( h16 ":" ) ls32 / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 / [ *4( h16 ":" ) h16 ] "::" ls32 / [ *5( h16 ":" ) h16 ] "::" h16 / [ *6( h16 ":" ) h16 ] "::" ls32 = ( h16 ":" h16 ) / IPv4address ; least-significant 32 bits of address h16 = 1*4HEXDIG ; 16 bits of address represented in hexadecimal obfnode = "_" 1*(ALPHA / DIGIT / "." / "_" / "-") node-port = port / obfport port = 1*5DIGIT obfport = "_" 1*(ALPHA / DIGIT / "." / "_" / "-")- Parameters:
nodeId- Node identifier- Returns:
InetSocketAddress- See Also:
-
parseNodeIdentifier
public static InetSocketAddress parseNodeIdentifier(String nodeId, Function<String, InetAddress> obfuscatedNodeLookup, Function<String, Integer> obfuscatedPortLookup) ImplementsparseNodeIdentifier(String nodeId)with obfuscated node and port lookups.- Parameters:
nodeId- Node identifierobfuscatedNodeLookup- Resovles an obfuscated node name (obfnode) toInetAddressobfuscatedPortLookup- Resovles an obfuscated node port (obfport) number- Returns:
InetSocketAddress- See Also:
-
normalizeHeaderName
Validates and normalizes case for an HTTP header name.Follows each hyphen '-' character with an upper case character; converts other characters
to lower case- Parameters:
headerName- HTTP header name- Returns:
String- Throws:
IllegalArgumentException- If the name contains non-alphabetic characters other than hyphen '-', or if the name begins or ends with a hyphen.
-
describeStatus
Describes an HTTP status code.- Parameters:
statusCode- HTTP status code- Returns:
String
-
getInetAddress
Resolves and caches theIP addressfor a host name.- Parameters:
hostname- host name- Returns:
- resolved
InetAddress
-
isInetAddressInRange
Determines whether or not an IP address is included in a CIDR range.- Parameters:
address- addressrange- CIDR range- Returns:
- true if the range includes the address; else false
-