Package org.microproxy.http
Class HttpHeaders
java.lang.Object
org.microproxy.http.HttpHeaders
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppends one field for each supplied value.Appends a field after validating its name and value.add(HttpHeaders other) Appends every field ofother.clear()Removes all fields.booleanChecks whether a field with this name exists.booleancontainsValue(String name, String value, boolean ignoreCase) Returns true if any comma-separated element of any field namednameequalsvalue.copy()Returns a deep copy of these headers.intThe number of fields namedname.entries()All fields as (name, value) pairs in order.Returns the first value forname, ornull.Returns the first matching value with a fallback for absent fields.Returns all values forname, in order.getAllElements(String name) All comma-separated elements across every field namedname.booleanisEmpty()Checks whether any fields are present.iterator()nameAt(int index) The name of fieldindex(0 tosize()- 1), as received.names()The distinct field names, in first-seen order and original spelling.booleanRemoves every field namedname.voidRemoves every field whose name matchesnamePredicate.Replaces all fields namednamewith one field per element ofvalues.Replaces all fields namednamewith a single field, keeping the first position.set(HttpHeaders other) Replaces the contents of these headers withother.intsize()Counts the header fields, including repeated names.Splits a list-valued header (a, b ,c) into its trimmed, non-empty elements.toString()valueAt(int index) The value of fieldindex.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
HttpHeaders
public HttpHeaders()Creates an empty header collection.
-
-
Method Details
-
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
Appends a field after validating its name and value.- Parameters:
name- the field name, preserved as suppliedvalue- the value, converted with String.valueOf- Returns:
- these headers
-
add
Appends one field for each supplied value.- Parameters:
name- the field name, preserved as suppliedvalues- the values to append in iteration order, converted with String.valueOf- Returns:
- these headers
-
add
Appends every field ofother.- Parameters:
other- the fields to append in their existing order- Returns:
- these headers
-
set
Replaces all fields namednamewith a single field, keeping the first position.- Parameters:
name- the field name, matched without regard to casevalue- the replacement value, converted with String.valueOf- Returns:
- these headers
-
set
Replaces all fields namednamewith one field per element ofvalues.- Parameters:
name- the field name, matched without regard to casevalues- the replacement values in iteration order, converted with String.valueOf- Returns:
- these headers
-
set
Replaces the contents of these headers withother.- Parameters:
other- the fields to copy, preserving their order- Returns:
- these headers
-
get
Returns the first value forname, ornull.- Parameters:
name- the field name, matched without regard to case- Returns:
- the first matching value, or null if absent
-
get
Returns the first matching value with a fallback for absent fields.- Parameters:
name- the field name, matched without regard to casedefaultValue- the value to return when no field matches- Returns:
- the first matching value, or defaultValue if absent
-
getAll
Returns all values forname, 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
The number of fields namedname.- Parameters:
name- the field name, matched without regard to case- Returns:
- the number of matching fields
-
nameAt
The name of fieldindex(0 tosize()- 1), as received.- Parameters:
index- the zero-based field index- Returns:
- the field name with its original spelling
-
valueAt
The value of fieldindex.- Parameters:
index- the zero-based field index- Returns:
- the field value at that index
-
removeIf
Removes every field whose name matchesnamePredicate.- Parameters:
namePredicate- the predicate tested against each name in its original spelling
-
contains
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
Returns true if any comma-separated element of any field namednameequalsvalue.- Parameters:
name- the field name, matched without regard to casevalue- the comma-separated element to findignoreCase- whether element comparison ignores case- Returns:
- whether any matching field contains the element
-
remove
Removes every field namedname. Returns true if anything was removed.- Parameters:
name- the field name, matched without regard to case- Returns:
- whether any fields were removed
-
clear
Removes all fields.- Returns:
- these headers
-
names
The distinct field names, in first-seen order and original spelling.- Returns:
- an unmodifiable snapshot of distinct names in first-seen order
-
entries
All fields as (name, value) pairs in order.- Returns:
- a new list of immutable name/value entries in field order
-
iterator
-
splitList
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
All comma-separated elements across every field namedname.- Parameters:
name- the field name, matched without regard to case- Returns:
- the trimmed, nonempty elements in field and element order
-
toString
-