Module iu.util
Package edu.iu

Class IuWebUtils

java.lang.Object
edu.iu.IuWebUtils

public final class IuWebUtils extends Object
Provides useful utility methods for low-level web client and server interactions.
  • 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

      static int comment(String s, int pos)
      comment = "(" *( ctext / quoted-pair / comment ) ")"
      Parameters:
      s - input string
      pos - position at start of comment
      Returns:
      end position after matching comment ABNF rule; returns pos if token68 was not matched
      See Also:
    • token68

      static int token68(String s, int pos)
      token68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
      Parameters:
      s - input string
      pos - 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

      static int token(String s, int pos)
      token = 1*tchar
      Parameters:
      s - input string
      pos - 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

      static int bws(String s, int pos)
      BWS = OWS

      OWS = *( SP / HTAB )

      RWS = 1*( SP / HTAB )

      Parameters:
      s - input string
      pos - position at start of token character
      Returns:
      end position of a matching BWS ABNF rule
      See Also:
    • sp

      static int sp(String s, int pos)
      1*SP
      Parameters:
      s - input string
      pos - position at start of token character
      Returns:
      end position of a matching sp ABNF rule
      See Also:
    • product

      static int product(String s, int pos)
      product = token [ "/" product-version ]

      product-version = token

      Parameters:
      s - input string
      pos - 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

      static int quotedPair(String s, int pos)
      quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
      Parameters:
      s - input string
      pos - 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

      static int quotedString(String s, int start)
      quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
      Parameters:
      s - input string
      start - position at start of token character
      Returns:
      end position of a matching BWS ABNF rule
      See Also:
    • isVisibleAscii

      public static boolean isVisibleAscii(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.
      Parameters:
      s - string to check
      minLength - minimum length
      Returns:
      true if all characters are visible ASCII
    • isRootOf

      public static boolean isRootOf(URI rootUri, URI resourceUri)
      Determines if a root URI encompasses a resource URI.
      Parameters:
      rootUri - root URI
      resourceUri - resource URI
      Returns:
      URI
    • createChallenge

      public static String createChallenge(String scheme, Map<String,String> params)
      Creates an authentication challenge sending to a client via the WWW-Authenticate header.
      Parameters:
      scheme - authentication scheme to request
      params - 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

      public static void validateUserAgent(String userAgent)
      Validates a user-agent header value.

      User-Agent = product *( RWS ( product / comment ) )

      Parameters:
      userAgent - user-agent header value
      See Also:
    • parseQueryString

      public static Map<String,? extends Iterable<String>> parseQueryString(String queryString)
      Parses a query string.
      Parameters:
      queryString - query string
      Returns:
      Map
    • createQueryString

      public static String createQueryString(Map<String,? extends Iterable<String>> params)
      Creates a query string from a map.
      Parameters:
      params - Map of parameter values
      Returns:
      query string
    • parseHeader

      public static Map<String,String> parseHeader(String headerValue)
      Parses a header value composed of key/value pairs separated by semicolon ';'.
      Parameters:
      headerValue - header value
      Returns:
      Map of 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

      static int cookieValue(String cookieHeaderValue, int pos)
      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 value
      pos - position at start of the cookie-value token
      Returns:
      end position of the cookie value
      See Also:
    • parseCookieHeader

      public static Iterable<HttpCookie> parseCookieHeader(String cookieHeaderValue)
      Parses the cookie request header, returning an HttpCookie for each cookie sent with the request.
      Parameters:
      cookieHeaderValue - Cookie: header value. This value MUST match the syntax defined for cookie-string RFC-6265 HTTP State Management, Section 4.2.1
      Returns:
      Iterable of parsed cookies
    • parseForwardedHeader

      public static IuForwardedHeader parseForwardedHeader(String forwardedHeaderValue)
      Parses the cookie request header, returning an HttpCookie for each cookie sent with the request.
      Parameters:
      forwardedHeaderValue - Forwarded: header value. This value MUST match the syntax defined for Forwarded at RFC-7238 Forwarded HTTP Extension, Section 4
      Returns:
      forwarded header value
    • decOctet

      static int decOctet(String nodeId, int originalPos)
      Parses a decimal octet, as defined for an IPv4address.
      Parameters:
      nodeId - node identifier
      originalPos - start position
      Returns:
      end position if matching; else returns start position
    • parseIPv4Address

      static int parseIPv4Address(String nodeId, int originalPos)
      Parses an IPV4Address for parseNodeIdentifier(String).
      Parameters:
      nodeId - node identifier
      originalPos - 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

      static int h16(String nodeId, int originalPos)
      Parses an h16 (16-bit hexadecimal number).
      Parameters:
      nodeId - node identifier
      originalPos - start position
      Returns:
      end position if matching; else returns start position
    • ls32

      static int ls32(String nodeId, int originalPos)
      Parses an ls32 (least significant 32-bits), either as an h16 pair or parseIPv4Address(String, int).
      Parameters:
      nodeId - node identifier
      originalPos - start position
      Returns:
      end position if matching; else returns start position
    • parseIPv6Address

      static int parseIPv6Address(String nodeId, int originalPos)
      Parses an IPV6Address for parseNodeIdentifier(String).
      Parameters:
      nodeId - node identifier
      originalPos - start position
      Returns:
      end position if matching; else returns start position
    • obf

      static int obf(String nodeId, int originalPos)
      Parses an obfnode or obfport for parseNodeIdentifier(String).
      Parameters:
      nodeId - node identifier
      originalPos - start position
      Returns:
      end position if matching; else returns start position
    • isIPv4Address

      public static boolean isIPv4Address(String nodename)
      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

      public static boolean isIPv6Address(String nodename)
      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

      public static boolean isIPAddress(String nodename)
      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

      public static InetSocketAddress parseNodeIdentifier(String nodeId)
      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)
      Implements parseNodeIdentifier(String nodeId) with obfuscated node and port lookups.
      Parameters:
      nodeId - Node identifier
      obfuscatedNodeLookup - Resovles an obfuscated node name (obfnode) to InetAddress
      obfuscatedPortLookup - Resovles an obfuscated node port (obfport) number
      Returns:
      InetSocketAddress
      See Also:
    • normalizeHeaderName

      public static String normalizeHeaderName(String headerName) throws IllegalArgumentException
      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

      public static String describeStatus(int statusCode)
      Describes an HTTP status code.
      Parameters:
      statusCode - HTTP status code
      Returns:
      String
    • getInetAddress

      public static InetAddress getInetAddress(String hostname)
      Resolves and caches the IP address for a host name.
      Parameters:
      hostname - host name
      Returns:
      resolved InetAddress
    • isInetAddressInRange

      public static boolean isInetAddressInRange(InetAddress address, String range)
      Determines whether or not an IP address is included in a CIDR range.
      Parameters:
      address - address
      range - CIDR range
      Returns:
      true if the range includes the address; else false