Class HttpFields
This class is not synchronized as it is expected that modifications will only be performed by a single thread.
The cookie handling provided by this class is guided by the Servlet specification and RFC6265.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionInitialize an empty HttpFields.HttpFields
(int capacity) Initialize an empty HttpFields.HttpFields
(HttpFields fields) Initialize HttpFields from copy. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add to or set a field.void
void
add
(HttpFields fields) Deprecated.void
add
(HttpHeader header, String value) Add to or set a field.void
add
(HttpHeader header, HttpHeaderValue value) void
addAll
(HttpFields fields) boolean
Add comma separated values, but only if not already present.boolean
addCSV
(HttpHeader header, String... values) Add comma separated values, but only if not already present.protected String
void
addDateField
(String name, long date) Sets the value of a date field.void
clear()
void
computeField
(String name, BiFunction<String, List<HttpField>, HttpField> computeFn) Computes a single field for the given HTTP header name and for existing fields with the same name.void
computeField
(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the given HttpHeader and for existing fields with the same header.private <T> void
computeField
(T header, BiFunction<T, List<HttpField>, HttpField> computeFn, BiFunction<HttpField, T, Boolean> matcher) boolean
boolean
boolean
contains
(HttpHeader header) boolean
contains
(HttpHeader header, String value) boolean
containsKey
(String name) boolean
get
(HttpHeader header) Get multiple field values of the same name as aQuotedCSV
getCSV
(HttpHeader header, boolean keepQuotes) Get multiple field values of the same name, split as aQuotedCSV
long
getDateField
(String name) Get a header as a date value.getField
(int index) Get a Field by index.getField
(HttpHeader header) Get enumeration of header _names.Get Collection of header names.getFields
(HttpHeader header) long
getLongField
(String name) Get a header as an long value.static Float
getQuality
(String value) Deprecated.getQualityCSV
(String name) Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
getQualityCSV
(HttpHeader header) Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
getQualityCSV
(HttpHeader header, ToIntFunction<String> secondaryOrdering) Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
getStringField
(String name) Deprecated.getStringField
(HttpHeader header) Deprecated.Get multi headersDeprecated.getValuesList
(String name) Get multiple header of the same namegetValuesList
(HttpHeader header) Get multiple header of the same nameint
hashCode()
iterator()
void
Set a field.void
Set a field.void
void
put
(HttpHeader header, String value) Set a field.void
put
(HttpHeader header, HttpHeaderValue value) void
putDateField
(String name, long date) Sets the value of a date field.void
putDateField
(HttpHeader name, long date) Sets the value of a date field.void
putLongField
(String name, long value) Sets the value of an long field.void
putLongField
(HttpHeader name, long value) Sets the value of an long field.Deprecated.private void
remove
(int i) Remove a field.remove
(HttpHeader name) Remove a field.int
size()
stream()
static String
stripParameters
(String value) Get field value without parameters.toString()
static String
valueParameters
(String value, Map<String, String> parameters) Get field value parameters.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
__separators
Deprecated.- See Also:
-
LOG
-
_fields
-
_size
private int _size -
__one
Deprecated. -
__zero
Deprecated. -
__qualities
Deprecated.
-
-
Constructor Details
-
HttpFields
public HttpFields()Initialize an empty HttpFields. -
HttpFields
public HttpFields(int capacity) Initialize an empty HttpFields.- Parameters:
capacity
- the capacity of the http fields
-
HttpFields
Initialize HttpFields from copy.- Parameters:
fields
- the fields to copy data from
-
-
Method Details
-
computeField
public void computeField(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the given HttpHeader and for existing fields with the same header.
The compute function receives the field name and a list of fields with the same name so that their values can be used to compute the value of the field that is returned by the compute function. If the compute function returns
null
, the fields with the given name are removed.This method comes handy when you want to add an HTTP header if it does not exist, or add a value if the HTTP header already exists, similarly to
Map.compute(Object, BiFunction)
.This method can be used to
put
a new field (or blindly replace its value):httpFields.computeField("X-New-Header", (name, fields) -> new HttpField(name, "NewValue"));
This method can be used to coalesce many fields into one:
// Input: GET / HTTP/1.1 Host: localhost Cookie: foo=1 Cookie: bar=2,baz=3 User-Agent: Jetty // Computation: httpFields.computeField("Cookie", (name, fields) -> { // No cookies, nothing to do. if (fields == null) return null; // Coalesces all cookies. String coalesced = fields.stream() .flatMap(field -> Stream.of(field.getValues())) .collect(Collectors.joining(", ")); // Returns a single Cookie header with all cookies. return new HttpField(name, coalesced); } // Output: GET / HTTP/1.1 Host: localhost Cookie: foo=1, bar=2, baz=3 User-Agent: Jetty
This method can be used to replace a field:
httpFields.computeField("X-Length", (name, fields) -> { if (fields == null) return null; // Get any value among the X-Length headers. String length = fields.stream() .map(HttpField::getValue) .findAny() .orElse("0"); // Replace X-Length headers with X-Capacity header. return new HttpField("X-Capacity", length); });
This method can be used to remove a field:
httpFields.computeField("Connection", (name, fields) -> null);
- Parameters:
header
- the HTTP headercomputeFn
- the compute function
-
computeField
Computes a single field for the given HTTP header name and for existing fields with the same name.
- Parameters:
name
- the HTTP header namecomputeFn
- the compute function- See Also:
-
computeField
private <T> void computeField(T header, BiFunction<T, List<HttpField>, HttpField> computeFn, BiFunction<HttpField, T, Boolean> matcher) -
size
public int size() -
iterator
-
listIterator
-
stream
-
getFieldNamesCollection
Get Collection of header names.- Returns:
- the unique set of field names.
-
getFieldNames
Get enumeration of header _names. Returns an enumeration of strings representing the header _names for this request.- Returns:
- an enumeration of field names
-
getField
Get a Field by index.- Parameters:
index
- the field index- Returns:
- A Field value or null if the Field value has not been set
-
getField
-
getField
-
getFields
-
getFields
-
contains
-
contains
-
contains
-
contains
-
containsKey
-
getStringField
Deprecated. -
get
-
getStringField
Deprecated. -
get
-
getValuesList
Get multiple header of the same name- Parameters:
header
- the header- Returns:
- List the values
-
getValuesList
Get multiple header of the same name- Parameters:
name
- the case-insensitive field name- Returns:
- List the header values
-
addCSV
Add comma separated values, but only if not already present.- Parameters:
header
- The header to add the value(s) tovalues
- The value(s) to add- Returns:
- True if headers were modified
-
addCSV
Add comma separated values, but only if not already present.- Parameters:
name
- The header to add the value(s) tovalues
- The value(s) to add- Returns:
- True if headers were modified
-
addCSV
-
getCSV
Get multiple field values of the same name, split as aQuotedCSV
- Parameters:
header
- The headerkeepQuotes
- True if the fields are kept quoted- Returns:
- List the values with OWS stripped
-
getCSV
Get multiple field values of the same name as aQuotedCSV
- Parameters:
name
- the case-insensitive field namekeepQuotes
- True if the fields are kept quoted- Returns:
- List the values with OWS stripped
-
getQualityCSV
Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
- Parameters:
header
- The header- Returns:
- List the values in quality order with the q param and OWS stripped
-
getQualityCSV
Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
- Parameters:
header
- The headersecondaryOrdering
- Function to apply an ordering other than specified by quality- Returns:
- List the values in quality order with the q param and OWS stripped
-
getQualityCSV
Get multiple field values of the same name, split and sorted as aQuotedQualityCSV
- Parameters:
name
- the case-insensitive field name- Returns:
- List the values in quality order with the q param and OWS stripped
-
getValues
Get multi headers- Parameters:
name
- the case-insensitive field name- Returns:
- Enumeration of the values
-
getValues
Deprecated.Get multi field values with separator. The multiple values can be represented as separate headers of the same name, or by a single header using the separator(s), or a combination of both. Separators may be quoted.- Parameters:
name
- the case-insensitive field nameseparators
- String of separators.- Returns:
- Enumeration of the values, or null if no such header.
-
put
-
put
Set a field.- Parameters:
name
- the name of the fieldvalue
- the value of the field. If null the field is cleared.
-
put
-
put
Set a field.- Parameters:
header
- the header name of the fieldvalue
- the value of the field. If null the field is cleared.
-
put
Set a field.- Parameters:
name
- the name of the fieldlist
- the List value of the field. If null the field is cleared.
-
add
Add to or set a field. If the field is allowed to have multiple values, add will add multiple headers of the same name.- Parameters:
name
- the name of the fieldvalue
- the value of the field.
-
add
-
add
Add to or set a field. If the field is allowed to have multiple values, add will add multiple headers of the same name.- Parameters:
header
- the headervalue
- the value of the field.
-
remove
Remove a field.- Parameters:
name
- the field to remove- Returns:
- the header that was removed
-
remove
Remove a field.- Parameters:
name
- the field to remove- Returns:
- the header that was removed
-
remove
private void remove(int i) -
getLongField
Get a header as an long value. Returns the value of an integer field or -1 if not found. The case of the field name is ignored.- Parameters:
name
- the case-insensitive field name- Returns:
- the value of the field as a long
- Throws:
NumberFormatException
- If bad long found
-
getDateField
Get a header as a date value. Returns the value of a date field, or -1 if not found. The case of the field name is ignored.- Parameters:
name
- the case-insensitive field name- Returns:
- the value of the field as a number of milliseconds since unix epoch
-
putLongField
Sets the value of an long field.- Parameters:
name
- the field namevalue
- the field long value
-
putLongField
Sets the value of an long field.- Parameters:
name
- the field namevalue
- the field long value
-
putDateField
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value
-
putDateField
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value
-
addDateField
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value
-
hashCode
public int hashCode() -
equals
-
toString
-
clear
public void clear() -
add
-
addAll
-
add
Deprecated.Add fields from another HttpFields instance. Single valued fields are replaced, while all others are added.- Parameters:
fields
- the fields to add
-
stripParameters
Get field value without parameters. Some field values can have parameters. This method separates the value from the parameters and optionally populates a map with the parameters. For example:FieldName : Value ; param1=val1 ; param2=val2
- Parameters:
value
- The Field value, possibly with parameters.- Returns:
- The value.
-
valueParameters
Get field value parameters. Some field values can have parameters. This method separates the value from the parameters and optionally populates a map with the parameters. For example:FieldName : Value ; param1=val1 ; param2=val2
- Parameters:
value
- The Field value, possibly with parameters.parameters
- A map to populate with the parameters, or null- Returns:
- The value.
-
getQuality
Deprecated. -
qualityList
Deprecated.List values in quality order.- Parameters:
e
- Enumeration of values with quality parameters- Returns:
- values in quality order.
-