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, 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 <a href="https://datatracker.ietf.org/doc/html/rfc822' rfc 822.

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 form 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 is not 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 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.
    • uriNoParametersNorFragment

      public String uriNoParametersNorFragment()
      Strip the fragment part and all parameters (part 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 the empty string.
    • uriParameters

      public String uriParameters()
      Return only the parameters part (= 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./". If not, uri must start with "/" or "*".
      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 .
      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#ask?a=b?c=d HTTP/1.0  
      param1:value1«
      param2:«
      param2:a very long value«
         extending over more than«
         one line«
      «
      DATA ---
        
      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).

      (This method gets the next character read by readC() via the buffer c.)

      Throws:
      IOException