Class DefaultHttpHeaders

    • Constructor Detail

      • DefaultHttpHeaders

        public DefaultHttpHeaders()
        Create a new, empty HTTP headers object.

        Header names and values are validated as they are added, to ensure they are compliant with the HTTP protocol.

      • DefaultHttpHeaders

        @Deprecated
        public DefaultHttpHeaders​(boolean validate)
        Deprecated.
        Prefer using the DefaultHttpHeaders() constructor instead, to always have validation enabled.
        Warning! Setting validate to false will mean that Netty won't validate & protect against user-supplied header values that are malicious. This can leave your server implementation vulnerable to CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting') . When disabling this validation, it is the responsibility of the caller to ensure that the values supplied do not contain a non-url-escaped carriage return (CR) and/or line feed (LF) characters.
        Parameters:
        validate - Should Netty validate header values to ensure they aren't malicious.
      • DefaultHttpHeaders

        protected DefaultHttpHeaders​(boolean validateValues,
                                     io.netty.handler.codec.DefaultHeaders.NameValidator<CharSequence> nameValidator)
        Create an HTTP headers object with the given name validator.

        Warning! It is strongly recommended that the name validator implement validation that is at least as strict as HttpHeaderValidationUtil.validateToken(CharSequence). It is also strongly recommended that validateValues is enabled.

        Without these validations in place, your code can be susceptible to CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting') . It is the responsibility of the caller to ensure that the values supplied do not contain a non-url-escaped carriage return (CR) and/or line feed (LF) characters.

        Parameters:
        validateValues - Should Netty validate header values to ensure they aren't malicious.
        nameValidator - The DefaultHeaders.NameValidator to use, never {@code null.
      • DefaultHttpHeaders

        protected DefaultHttpHeaders​(io.netty.handler.codec.DefaultHeaders<CharSequence,​CharSequence,​?> headers)
    • Method Detail

      • set

        public HttpHeaders set​(HttpHeaders headers)
        Description copied from class: HttpHeaders
        Cleans the current header entries and copies all header entries of the specified headers.
        Overrides:
        set in class HttpHeaders
        Returns:
        this
      • add

        public HttpHeaders add​(CharSequence name,
                               Iterable<?> values)
        Description copied from class: HttpHeaders
        Adds a new header with the specified name and values. This getMethod can be represented approximately as the following code:
         for (Object v: values) {
             if (v == null) {
                 break;
             }
             headers.add(name, v);
         }
         
        Overrides:
        add in class HttpHeaders
        Parameters:
        name - The name of the headers being set
        values - The values of the headers being set
        Returns:
        this
      • set

        public HttpHeaders set​(CharSequence name,
                               Object value)
        Description copied from class: HttpHeaders
        Sets a header with the specified name and value. If there is an existing header with the same name, it is removed. If the specified value is not a String, it is converted into a String by Object.toString(), except for Date and Calendar, which are formatted to the date format defined in RFC2616.
        Overrides:
        set in class HttpHeaders
        Parameters:
        name - The name of the header being set
        value - The value of the header being set
        Returns:
        this
      • set

        public HttpHeaders set​(CharSequence name,
                               Iterable<?> values)
        Description copied from class: HttpHeaders
        Sets a header with the specified name and values. If there is an existing header with the same name, it is removed. This getMethod can be represented approximately as the following code:
         headers.remove(name);
         for (Object v: values) {
             if (v == null) {
                 break;
             }
             headers.add(name, v);
         }
         
        Overrides:
        set in class HttpHeaders
        Parameters:
        name - The name of the headers being set
        values - The values of the headers being set
        Returns:
        this
      • setInt

        public HttpHeaders setInt​(CharSequence name,
                                  int value)
        Description copied from class: HttpHeaders
        Set the name to value. This will remove all previous values associated with name.
        Specified by:
        setInt in class HttpHeaders
        Parameters:
        name - The name to modify
        value - The value
        Returns:
        this
      • setShort

        public HttpHeaders setShort​(CharSequence name,
                                    short value)
        Description copied from class: HttpHeaders
        Set the name to value. This will remove all previous values associated with name.
        Specified by:
        setShort in class HttpHeaders
        Parameters:
        name - The name to modify
        value - The value
        Returns:
        this
      • get

        public String get​(CharSequence name)
        Description copied from class: HttpHeaders
        Returns the value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Overrides:
        get in class HttpHeaders
        Parameters:
        name - The name of the header to search
        Returns:
        The first header value or null if there is no such header
        See Also:
        HttpHeaders.getAsString(CharSequence)
      • getInt

        public Integer getInt​(CharSequence name)
        Description copied from class: HttpHeaders
        Returns the integer value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getInt in class HttpHeaders
        Parameters:
        name - the name of the header to search
        Returns:
        the first header value if the header is found and its value is an integer. null if there's no such header or its value is not an integer.
      • getInt

        public int getInt​(CharSequence name,
                          int defaultValue)
        Description copied from class: HttpHeaders
        Returns the integer value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getInt in class HttpHeaders
        Parameters:
        name - the name of the header to search
        defaultValue - the default value
        Returns:
        the first header value if the header is found and its value is an integer. defaultValue if there's no such header or its value is not an integer.
      • getShort

        public Short getShort​(CharSequence name)
        Description copied from class: HttpHeaders
        Returns the short value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getShort in class HttpHeaders
        Parameters:
        name - the name of the header to search
        Returns:
        the first header value if the header is found and its value is a short. null if there's no such header or its value is not a short.
      • getShort

        public short getShort​(CharSequence name,
                              short defaultValue)
        Description copied from class: HttpHeaders
        Returns the short value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getShort in class HttpHeaders
        Parameters:
        name - the name of the header to search
        defaultValue - the default value
        Returns:
        the first header value if the header is found and its value is a short. defaultValue if there's no such header or its value is not a short.
      • getTimeMillis

        public Long getTimeMillis​(CharSequence name)
        Description copied from class: HttpHeaders
        Returns the date value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getTimeMillis in class HttpHeaders
        Parameters:
        name - the name of the header to search
        Returns:
        the first header value if the header is found and its value is a date. null if there's no such header or its value is not a date.
      • getTimeMillis

        public long getTimeMillis​(CharSequence name,
                                  long defaultValue)
        Description copied from class: HttpHeaders
        Returns the date value of a header with the specified name. If there are more than one values for the specified name, the first value is returned.
        Specified by:
        getTimeMillis in class HttpHeaders
        Parameters:
        name - the name of the header to search
        defaultValue - the default value
        Returns:
        the first header value if the header is found and its value is a date. defaultValue if there's no such header or its value is not a date.
      • containsValue

        public boolean containsValue​(CharSequence name,
                                     CharSequence value,
                                     boolean ignoreCase)
        Description copied from class: HttpHeaders
        Returns true if a header with the name and value exists, false otherwise. This also handles multiple values that are separated with a ,.

        If ignoreCase is true then a case insensitive compare is done on the value.

        Overrides:
        containsValue in class HttpHeaders
        Parameters:
        name - the name of the header to find
        value - the value of the header to find
        ignoreCase - true then a case insensitive compare is run to compare values. otherwise a case sensitive compare is run to compare values.
      • contains

        public boolean contains​(CharSequence name)
        Description copied from class: HttpHeaders
        Checks to see if there is a header with the specified name
        Overrides:
        contains in class HttpHeaders
        Parameters:
        name - The name of the header to search for
        Returns:
        True if at least one header is found
      • isEmpty

        public boolean isEmpty()
        Description copied from class: HttpHeaders
        Checks if no header exists.
        Specified by:
        isEmpty in class HttpHeaders
      • size

        public int size()
        Description copied from class: HttpHeaders
        Returns the number of headers in this object.
        Specified by:
        size in class HttpHeaders
      • contains

        public boolean contains​(CharSequence name,
                                CharSequence value,
                                boolean ignoreCase)
        Description copied from class: HttpHeaders
        Returns true if a header with the name and value exists, false otherwise.

        If ignoreCase is true then a case insensitive compare is done on the value.

        Overrides:
        contains in class HttpHeaders
        Parameters:
        name - the name of the header to find
        value - the value of the header to find
        ignoreCase - true then a case insensitive compare is run to compare values. otherwise a case sensitive compare is run to compare values.
      • names

        public Set<String> names()
        Description copied from class: HttpHeaders
        Returns a new Set that contains the names of all headers in this object. Note that modifying the returned Set will not affect the state of this object. If you intend to enumerate over the header entries only, use HttpHeaders.iterator() instead, which has much less overhead.
        Specified by:
        names in class HttpHeaders
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object