Class HttpHeader

java.lang.Object
eu.bandm.tools.util.http.HttpHeader

public class HttpHeader extends Object
Consumes input from an Inputstream and stores it in key/value pairs, as long as no empty line is found. Typical usage pattern is new HttpHeader().read(socket.getInputStream()). After consuming the empty line that ends the header, the InputStream is left for further reading. The first line is split into the three pseudo-header-parameters: method, uri, and httpVersion.

The code follows the specification in rfc 9112 and rfc 3986.

Additionally this class provides simple uri decoding functions which are restricted as they do not allow the encoding of syntactically relevant characters, but sufficient for our current applications.

  • Field Details

  • Constructor Details

    • HttpHeader

      public HttpHeader()
  • Method Details

    • isBlank

      public static boolean isBlank(char c)
      Returns whether the character is a white space or a tabulator.
    • toString

      public String toString()
      String representation including all key/value pairs from keyValue.
      Overrides:
      toString in class Object
    • get

      @Opt public @Opt String get(String key)
      Return the value of a certain key from the html header, null in case there is no entry.
    • getContentLength

      public int getContentLength() throws IOException
      Returns the value of the parameter with name KEY_contentLength. May only be called if such is present. Throws IOException if not found in the keys, and NumberFormatException if the string value does not correspond to a positive or zero integer.
      Throws:
      IOException
    • beforeOrAlternative

      protected String beforeOrAlternative(String s, String alt, int i)
      Return the prefix of s if i>=0, else the alternative "alt".
    • afterOrAlternative

      protected String afterOrAlternative(String s, String alt, int i)
      Return the suffix of s if i >= 0, else the alternative "alt".
    • beforeFirstOrAll

      protected String beforeFirstOrAll(String s, char c)
      Return the prefix of s up to the first occuring character c, exclusively, or the total string if no c is contained.
    • afterFirstOrEmpty

      protected String afterFirstOrEmpty(String s, char c)
      Return the suffix of s following the first occuring character c, exclusively, or an empty // ???? the total string if no c is contained.
    • beforeLastOrAll

      protected String beforeLastOrAll(String s, char c)
      Return the prefix of s up to the last occuring character c, exclusively, or the total string if no c is contained.
    • afterLastOrAll

      protected String afterLastOrAll(String s, char c)
      Return the suffix of s following the last occuring character c, exclusively, or the total string if no c is contained.
    • afterLastOrEmpty

      protected String afterLastOrEmpty(String s, char c)
      Return the suffix of s following the last occuring character c, exclusively, or an empty string if no c is contained.
    • uriNoFragment

      public String uriNoFragment()
      Strip the fragment part (= location information = part starting with hash mark) from the uri.
    • uriNoQueryNorFragment

      public String uriNoQueryNorFragment()
      Strip the fragment part and the query component (starting with "?") from the uri.
    • uriFragment

      public String uriFragment()
      Return the fragment part (= location information = part starting with hash mark) maybe including paramters from the uri. If no hash mark, then return the empty string.
    • uriQuery

      public String uriQuery()
      Return only the query component (= part starting with "?") of the uri, or empty string.
    • uriPathOnly

      public String uriPathOnly() throws IOException
      Returns only the (local/file system) path component of an uri. Assume a server identification must be thrown away only when the string value starts with "http://a.b.c./" or "https://a.b.c./".
      If not, uri must start with "/" or "*". See https://www.rfc-editor.org/rfc/rfc9112#section-3.2.4 »The "asterisk-form" of request-target is only used for a server-wide OPTIONS request.«
      Throws:
      IOException
    • uriLowestFilename

      public String uriLowestFilename() throws IOException
      Return the last file name of the directory path of the uri.
      Throws:
      IOException
    • uriLowestSuffix

      public String uriLowestSuffix() throws IOException
      Return the suffix of the last file name of the directory path of the uri, or empty string, if there is no dot.
      Throws:
      IOException
    • uriLowestFilenameNoSuffix

      public String uriLowestFilenameNoSuffix() throws IOException
      Return the last file name of the directory path of the uri without its suffix, or completely, if it does not contain a dot.
      Throws:
      IOException
    • uriPathOnlyNoSuffix

      public String uriPathOnlyNoSuffix() throws IOException
      Return the complete directory path from the uri, but without the lowest suffix, if any.
      Throws:
      IOException
    • extractCookie

      @Opt public @Opt String extractCookie(String name)
      Return the value which is assigned to the given key in the set of all cookies. Cookies are separated by semicolon ";". Assumes that cookies come in simple form, no attributes, paths etc. are decoded. Lead-in for cookie parameter is KEY_allcookies.
    • readC

      protected void readC() throws IOException
      Aux method for reading one character. The read character is stored globally in c. Any CR must be followed by a LF, and only the CR is stored.
      Throws:
      IOException
    • readToken

      protected String readToken(String key) throws IOException
      Reads a maximal sequence of non-blanks/non-CRs and stores it with the given key in keyValue. Only used for the very first line.
      Throws:
      IOException - if the sequence is empty
    • read

      public void read(InputStream in) throws IOException
      Decode the HTTP header, store its result in keyValue, and leave the InputStream in the state for further reading. A http REQUEST header looks like
      POST http://server.com/dir/dir/dir/file.html?a=b&c=d#fragment HTTP/1.0«
      param1:value1«
      param2:«
      param2:a very long value«
         extending over more than«
         one line«
      «
      DATA ---
        
      Not yet supported:
      A http STATUS header (in a server response) has a different start-line:
          HTTP/1.1 440 not found 
        
      i.e. version, status code, explanation text for status code.

      (See https://datatracker.ietf.org/doc/html/rfc822#section-3.1 for "folding" = multiple line values).

      (See https://datatracker.ietf.org/doc/html/rfc3986 for URI syntax.)

      (Please note that the format of the query component a=b&c=d is mere convention and not part of a standard. see https://stackoverflow.com/questions/39266970.)

      ("parameters", lead in by a semicolon in the uri, are currently not supported.)

      Throws:
      IOException - on format errors: parameter names not followed by colon; missing empty line; etc.