Class HttpHeaders

java.lang.Object
org.microproxy.http.HttpHeaders
All Implemented Interfaces:
Iterable<Map.Entry<String,String>>

public final class HttpHeaders extends Object implements Iterable<Map.Entry<String,String>>
An ordered, case-insensitive multimap of header fields. The original spelling and order of header names is preserved when a message is forwarded.

Names must be RFC 9110 tokens and values must not contain CR, LF or NUL; violations throw IllegalArgumentException, which prevents header injection through filters.

  • Constructor Details

    • HttpHeaders

      public HttpHeaders()
      Creates an empty header collection.
  • Method Details

    • copy

      public HttpHeaders copy()
      Returns a deep copy of these headers.
      Returns:
      an independent copy preserving all field names, values, and order
    • size

      public int size()
      Counts the header fields, including repeated names.
      Returns:
      the number of fields
    • isEmpty

      public boolean isEmpty()
      Checks whether any fields are present.
      Returns:
      true if there are no fields
    • add

      public HttpHeaders add(String name, Object value)
      Appends a field after validating its name and value.
      Parameters:
      name - the field name, preserved as supplied
      value - the value, converted with String.valueOf
      Returns:
      these headers
    • add

      public HttpHeaders add(String name, Iterable<?> values)
      Appends one field for each supplied value.
      Parameters:
      name - the field name, preserved as supplied
      values - the values to append in iteration order, converted with String.valueOf
      Returns:
      these headers
    • add

      public HttpHeaders add(HttpHeaders other)
      Appends every field of other.
      Parameters:
      other - the fields to append in their existing order
      Returns:
      these headers
    • set

      public HttpHeaders set(String name, Object value)
      Replaces all fields named name with a single field, keeping the first position.
      Parameters:
      name - the field name, matched without regard to case
      value - the replacement value, converted with String.valueOf
      Returns:
      these headers
    • set

      public HttpHeaders set(String name, Iterable<?> values)
      Replaces all fields named name with one field per element of values.
      Parameters:
      name - the field name, matched without regard to case
      values - the replacement values in iteration order, converted with String.valueOf
      Returns:
      these headers
    • set

      public HttpHeaders set(HttpHeaders other)
      Replaces the contents of these headers with other.
      Parameters:
      other - the fields to copy, preserving their order
      Returns:
      these headers
    • get

      public String get(String name)
      Returns the first value for name, or null.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      the first matching value, or null if absent
    • get

      public String get(String name, String defaultValue)
      Returns the first matching value with a fallback for absent fields.
      Parameters:
      name - the field name, matched without regard to case
      defaultValue - the value to return when no field matches
      Returns:
      the first matching value, or defaultValue if absent
    • getAll

      public List<String> getAll(String name)
      Returns all values for name, in order.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      a new list of matching values in field order, or an empty list
    • count

      public int count(String name)
      The number of fields named name.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      the number of matching fields
    • nameAt

      public String nameAt(int index)
      The name of field index (0 to size() - 1), as received.
      Parameters:
      index - the zero-based field index
      Returns:
      the field name with its original spelling
    • valueAt

      public String valueAt(int index)
      The value of field index.
      Parameters:
      index - the zero-based field index
      Returns:
      the field value at that index
    • removeIf

      public void removeIf(Predicate<String> namePredicate)
      Removes every field whose name matches namePredicate.
      Parameters:
      namePredicate - the predicate tested against each name in its original spelling
    • contains

      public boolean contains(String name)
      Checks whether a field with this name exists.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      whether at least one field matches
    • containsValue

      public boolean containsValue(String name, String value, boolean ignoreCase)
      Returns true if any comma-separated element of any field named name equals value.
      Parameters:
      name - the field name, matched without regard to case
      value - the comma-separated element to find
      ignoreCase - whether element comparison ignores case
      Returns:
      whether any matching field contains the element
    • remove

      public boolean remove(String name)
      Removes every field named name. Returns true if anything was removed.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      whether any fields were removed
    • clear

      public HttpHeaders clear()
      Removes all fields.
      Returns:
      these headers
    • names

      public Set<String> names()
      The distinct field names, in first-seen order and original spelling.
      Returns:
      an unmodifiable snapshot of distinct names in first-seen order
    • entries

      public List<Map.Entry<String,String>> entries()
      All fields as (name, value) pairs in order.
      Returns:
      a new list of immutable name/value entries in field order
    • iterator

      public Iterator<Map.Entry<String,String>> iterator()
      Specified by:
      iterator in interface Iterable<Map.Entry<String,String>>
    • splitList

      public static List<String> splitList(String value)
      Splits a list-valued header (a, b ,c) into its trimmed, non-empty elements. Quoted strings are not specially handled, which is fine for the connection-management headers this is used for.
      Parameters:
      value - the comma-separated header value to split
      Returns:
      the trimmed, nonempty elements in order
    • getAllElements

      public List<String> getAllElements(String name)
      All comma-separated elements across every field named name.
      Parameters:
      name - the field name, matched without regard to case
      Returns:
      the trimmed, nonempty elements in field and element order
    • toString

      public String toString()
      Overrides:
      toString in class Object