Class HttpMessage

  • All Implemented Interfaces:
    Message

    public class HttpMessage
    extends java.lang.Object
    implements Message
    Representation of a HTTP message request (header and body) and response (header and body) pair.
    • Field Detail

      • EVENT_DATA_REQUEST_HEADER

        public static final java.lang.String EVENT_DATA_REQUEST_HEADER
        See Also:
        Constant Field Values
      • EVENT_DATA_REQUEST_BODY

        public static final java.lang.String EVENT_DATA_REQUEST_BODY
        See Also:
        Constant Field Values
      • EVENT_DATA_RESPONSE_HEADER

        public static final java.lang.String EVENT_DATA_RESPONSE_HEADER
        See Also:
        Constant Field Values
      • EVENT_DATA_RESPONSE_BODY

        public static final java.lang.String EVENT_DATA_RESPONSE_BODY
        See Also:
        Constant Field Values
    • Constructor Detail

      • HttpMessage

        public HttpMessage()
        Constructor for a empty HTTP message.
      • HttpMessage

        public HttpMessage​(org.apache.commons.httpclient.URI uri)
                    throws HttpMalformedHeaderException
        Constructs a HttpMessage with a HTTP/1.1 GET request to the given URI.

        The following headers are automatically added:

        • Host, with the domain and port from the given URI.
        • User-Agent, using the HttpRequestHeader.getDefaultUserAgent().
        • Pragma: no-cache
        • Cache-Control: no-cache, if version is HTTP/1.1
        • Content-Type: application/x-www-form-urlencoded, if the method is POST or PUT.
        Parameters:
        uri - the request target.
        Throws:
        HttpMalformedHeaderException - if the resulting HTTP header is malformed.
      • HttpMessage

        @Deprecated
        public HttpMessage​(org.apache.commons.httpclient.URI uri,
                           ConnectionParam params)
                    throws HttpMalformedHeaderException
        Deprecated.
        (2.8.0) Use HttpMessage(URI) instead.
        Constructs a HttpMessage with a HTTP/1.1 GET request to the given URI.

        The following headers are automatically added:

        • Host, with the domain and port from the given URI.
        • User-Agent, using the HttpRequestHeader.getDefaultUserAgent().
        • Pragma: no-cache
        • Cache-Control: no-cache, if version is HTTP/1.1
        • Content-Type: application/x-www-form-urlencoded, if the method is POST or PUT.
        Parameters:
        uri - the request target.
        params - unused.
        Throws:
        HttpMalformedHeaderException - if the resulting HTTP header is malformed.
        Since:
        2.4.2
      • HttpMessage

        public HttpMessage​(HttpRequestHeader reqHeader)
        Constructs an HTTP message with the given request header.
        Parameters:
        reqHeader - the request header
        Throws:
        java.lang.IllegalArgumentException - if the parameter reqHeader is null.
      • HttpMessage

        public HttpMessage​(HttpRequestHeader reqHeader,
                           HttpRequestBody reqBody)
        Constructs an HTTP message with the given request header and request body.
        Parameters:
        reqHeader - the request header
        reqBody - the request body
        Throws:
        java.lang.IllegalArgumentException - if the parameter reqHeader or reqBody are null.
      • HttpMessage

        public HttpMessage​(HttpRequestHeader reqHeader,
                           HttpRequestBody reqBody,
                           HttpResponseHeader resHeader,
                           HttpResponseBody resBody)
        Constructor for a HTTP message with given request and response pair.
        Parameters:
        reqHeader - the request header
        reqBody - the request body
        resHeader - the response header
        resBody - the response body
        Throws:
        java.lang.IllegalArgumentException - if one of the parameters is null.
    • Method Detail

      • getHttpSession

        public HttpSession getHttpSession()
        Gets the http session associated with this message.
        Returns:
        the http session
      • setHttpSession

        public void setHttpSession​(HttpSession session)
        Sets the http session associated with this message.
        Parameters:
        session - the new http session
      • getRequestHeader

        public HttpRequestHeader getRequestHeader()
        Gets the request header of this message.
        Returns:
        the request header, never null
      • setRequestHeader

        public void setRequestHeader​(HttpRequestHeader reqHeader)
        Sets the request header of this message.
        Parameters:
        reqHeader - the new request header
        Throws:
        java.lang.IllegalArgumentException - if parameter reqHeader is null.
      • getResponseHeader

        public HttpResponseHeader getResponseHeader()
        Gets the response header of this message.

        To know if a response has been set call the method HttpResponseHeader#isEmpty() on the returned response header. The response header is initially empty.

        Returns:
        the response header, never null
        See Also:
        HttpHeader.isEmpty()
      • setResponseHeader

        public void setResponseHeader​(HttpResponseHeader resHeader)
        Sets the response header of this message.
        Parameters:
        resHeader - the new response header
        Throws:
        java.lang.IllegalArgumentException - if parameter resHeader is null.
      • getRequestBody

        public HttpRequestBody getRequestBody()
        Gets the request body of this message.
        Returns:
        the request body, never null
      • setRequestBody

        public void setRequestBody​(HttpRequestBody reqBody)
        Sets the request body of this message.

        Note: No encodings are set to the request body to match the header.

        Parameters:
        reqBody - the new request body
        Throws:
        java.lang.IllegalArgumentException - if parameter reqBody is null.
      • getResponseBody

        public HttpResponseBody getResponseBody()
        Gets the response body of this message.
        Returns:
        the response body, never null
      • setResponseBody

        public void setResponseBody​(HttpResponseBody resBody)
        Sets the response body of this message.

        Note: No encodings are set to the response body to match the header.

        Parameters:
        resBody - the new response body
        Throws:
        java.lang.IllegalArgumentException - if parameter resBody is null.
      • setContentEncodings

        public static void setContentEncodings​(HttpHeader header,
                                               HttpBody body)
        Sets the content encodings defined in the header into the body.

        Note: Supports only gzip and deflate.

        Parameters:
        header - the header.
        body - the body.
      • setRequestBody

        public void setRequestBody​(java.lang.String body)
        Sets the given string body as the request body.

        The defined request header content encodings are set to the body, the string body will be encoded accordingly.

        Parameters:
        body - the new body.
        See Also:
        HttpBody.setContentEncodings(List)
      • setRequestBody

        public void setRequestBody​(byte[] body)
        Sets the given byte body as the request body.

        The defined request header content encodings are set to the body, the byte body is assumed to be properly encoded.

        Parameters:
        body - the new body.
        See Also:
        HttpBody.setContentEncodings(List)
      • setResponseBody

        public void setResponseBody​(java.lang.String body)
        Sets the given string body as the response body.

        The defined response header content encodings are set to the body, the string body will be encoded accordingly.

        Parameters:
        body - the new body.
        See Also:
        HttpBody.setContentEncodings(List)
      • setResponseBody

        public void setResponseBody​(byte[] body)
        Sets the given byte body as the response body.

        The defined response header content encodings are set to the body, the byte body is assumed to be properly encoded.

        Parameters:
        body - the new body.
        See Also:
        HttpBody.setContentEncodings(List)
      • equals

        public boolean equals​(java.lang.Object object)
        Compare if 2 message is the same. 2 messages are the same if: Host, port, path and query param and VALUEs are the same. For POST request, the body must be the same.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        object -
        Returns:
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equalType

        public boolean equalType​(HttpMessage msg)
        Compares this HttpMessage against another. Messages are equal type if the host, port, path and parameter names are equal. Even though the query values may differ. For POST this assumes x-www-form-urlencoded, for other types (such as JSON) this means that parameter names and values (the full request body) could be included.
        Parameters:
        msg - the message against which this HttpMessage is being compared.
        Returns:
        true if the messages are considered equal, false otherwise
      • getParamNameSet

        public java.util.TreeSet<java.lang.String> getParamNameSet​(HtmlParameter.Type type)
        Returns the names of the parameters of the given type. As a Set is used no names will be duplicated.
        Parameters:
        type - the type of the parameters that will be extracted from the message
        Returns:
        a TreeSet with the names of the parameters of the given type, never null
        Since:
        2.4.2
      • getParameterNames

        public java.util.List<java.lang.String> getParameterNames​(HtmlParameter.Type type)
        Returns the names of the parameters of the given type in a List. The List can return duplicated names.
        Parameters:
        type - the type of the parameters that will be extracted from the message
        Returns:
        a List with the names of the parameters of the given type, never null
        Since:
        2.10.0
      • getParameters

        public java.util.List<HtmlParameter> getParameters​(HtmlParameter.Type type)
        Returns the parameters of the given type in a List. The List can return duplicated parameter names.
        Parameters:
        type - the type of the parameters that will be extracted from the message
        Returns:
        a List with the parameters of the given type, never null
        Since:
        2.10.0
      • getParamNames

        public java.lang.String[] getParamNames()
      • getUrlParams

        public java.util.TreeSet<HtmlParameter> getUrlParams()
      • getFormParams

        public java.util.TreeSet<HtmlParameter> getFormParams()
      • setCookieParamsAsString

        public void setCookieParamsAsString​(java.lang.String data)
      • getCookieParamsAsString

        public java.lang.String getCookieParamsAsString()
      • getCookieParams

        public java.util.TreeSet<HtmlParameter> getCookieParams()
      • getUserObject

        public java.lang.Object getUserObject()
        Returns:
        Returns the userObject.
      • setUserObject

        public void setUserObject​(java.lang.Object userObject)
        Parameters:
        userObject - The userObject to set.
      • cloneAll

        public HttpMessage cloneAll()
        Clones this message.

        It returns a new HttpMessage with a copy of the request/response headers and bodies, no other state is copied.

        Returns:
        a new HttpMessage with the same (contents) request/response headers and bodies as this one.
        See Also:
        HttpMessage(HttpMessage)
      • cloneRequest

        public HttpMessage cloneRequest()
        Clones the request of this message.

        It returns a new HttpMessage with a copy of the request header and body, no other state is copied.

        Returns:
        a new HttpMessage with the same (contents) request header and body as this one.
        See Also:
        HttpMessage(HttpMessage)
      • getTimeElapsedMillis

        public int getTimeElapsedMillis()
        Returns:
        Get the elapsed time (time difference) between the request is sent and all response is received. In millis. The value is zero if the response is not received.
      • setTimeElapsedMillis

        public void setTimeElapsedMillis​(int timeElapsed)
        Set the elapsed time (time difference) between the request is sent and all response is received. In millis.
        Parameters:
        timeElapsed -
      • getTimeSentMillis

        public long getTimeSentMillis()
        Get the starting time when the request is going to be sent. This is the System.currentTimeMillis before the message is sent. The value is zero if the request is not sent yet.
      • setTimeSentMillis

        public void setTimeSentMillis​(long timeSent)
        Set the time when the request is sent.
        Parameters:
        timeSent - The timeSent to set.
      • getNote

        public java.lang.String getNote()
        Returns:
        Returns the note.
      • setNote

        public void setNote​(java.lang.String note)
        Parameters:
        note - The note to set.
      • mutateHttpMethod

        public void mutateHttpMethod​(java.lang.String method)
      • setFormParams

        public void setFormParams​(java.util.TreeSet<HtmlParameter> postParams)
      • setGetParams

        public void setGetParams​(java.util.TreeSet<HtmlParameter> getParams)
      • setCookieParams

        public void setCookieParams​(java.util.TreeSet<HtmlParameter> cookieParams)
      • isWebSocketUpgrade

        public boolean isWebSocketUpgrade()
        ZAP: New method checking for connection upgrade.
        Returns:
        True if this connection should be upgraded to WebSockets.
      • setCookies

        public void setCookies​(java.util.List<java.net.HttpCookie> cookies)
      • isInScope

        public boolean isInScope()
        Specified by:
        isInScope in interface Message
      • isEventStream

        public boolean isEventStream()
        ZAP: New method checking if connection is a Server-Sent Events stream.
        Returns:
      • setForceIntercept

        public void setForceIntercept​(boolean force)
      • getRequestingUser

        public User getRequestingUser()
        Gets the request user.
        Returns:
        the request user
      • setRequestingUser

        public void setRequestingUser​(User requestUser)
        Sets the requesting user. When sending the message, if a requesting user has been set, the message will be modified so that it will be sent as from the point of view of this particular user.
        Parameters:
        requestUser - the new request user
      • isResponseFromTargetHost

        public boolean isResponseFromTargetHost()
        Tells whether or not the response has been received from the target host.

        Note: No distinction is done between responses from intermediate proxy servers (if any) and the target host.

        Returns:
        true if the response has been received from the target host, false otherwise.
      • setResponseFromTargetHost

        public void setResponseFromTargetHost​(boolean responseFromTargetHost)
        Sets if the response has been received or not from the target host.
        Parameters:
        responseFromTargetHost - true if the response has been received from the target host, false otherwise.
      • toEventData

        public java.util.Map<java.lang.String,​java.lang.String> toEventData()
        Returns a map of data suitable for including in an Event
        Specified by:
        toEventData in interface Message
        Since:
        2.8.0
      • getType

        public java.lang.String getType()
        Returns "HTTP"
        Specified by:
        getType in interface Message
        Returns:
        the type of the message
        Since:
        2.8.0