Search in sources :

Example 21 with HttpRequestHeader

use of org.parosproxy.paros.network.HttpRequestHeader in project zaproxy by zaproxy.

the class HttpPanelViewModelUtilsUnitTest method shouldUpdateRequestContentLength.

@Test
void shouldUpdateRequestContentLength() {
    // Given
    HttpMessage message = mock(HttpMessage.class);
    HttpRequestHeader requestHeader = spy(HttpRequestHeader.class);
    given(message.getRequestHeader()).willReturn(requestHeader);
    HttpRequestBody requestBody = mock(HttpRequestBody.class);
    given(message.getRequestBody()).willReturn(requestBody);
    int length = 1234;
    given(requestBody.length()).willReturn(length);
    // When
    HttpPanelViewModelUtils.updateRequestContentLength(message);
    // Then
    verify(requestHeader).setContentLength(length);
}
Also used : HttpRequestBody(org.zaproxy.zap.network.HttpRequestBody) HttpMessage(org.parosproxy.paros.network.HttpMessage) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with HttpRequestHeader

use of org.parosproxy.paros.network.HttpRequestHeader in project zaproxy by zaproxy.

the class ProxyThread method run.

@Override
@SuppressWarnings("deprecation")
public void run() {
    proxyThreadList.add(thread);
    boolean isSecure = false;
    HttpRequestHeader firstHeader = null;
    try {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inSocket.getInputStream(), 2048);
        inSocket = new CustomStreamsSocket(inSocket, bufferedInputStream, inSocket.getOutputStream());
        if (isSslTlsHandshake(bufferedInputStream)) {
            isSecure = true;
            beginSSL(null);
        }
        httpIn = new HttpInputStream(inSocket);
        httpOut = new HttpOutputStream(inSocket.getOutputStream());
        firstHeader = httpIn.readRequestHeader(isSecure);
        firstHeader.setSenderAddress(inSocket.getInetAddress());
        if (firstHeader.getMethod().equalsIgnoreCase(HttpRequestHeader.CONNECT)) {
            HttpMessage connectMsg = new HttpMessage(firstHeader);
            connectMsg.setTimeSentMillis(System.currentTimeMillis());
            try {
                httpOut.write(CONNECT_HTTP_200);
                httpOut.flush();
                connectMsg.setResponseHeader(CONNECT_HTTP_200);
                connectMsg.setTimeElapsedMillis((int) (System.currentTimeMillis() - connectMsg.getTimeSentMillis()));
                notifyConnectMessage(connectMsg);
                if (isSslTlsHandshake(bufferedInputStream)) {
                    isSecure = true;
                    beginSSL(firstHeader.getHostName());
                }
                firstHeader = httpIn.readRequestHeader(isSecure);
                firstHeader.setSenderAddress(inSocket.getInetAddress());
                processHttp(firstHeader, isSecure);
            } catch (org.parosproxy.paros.security.MissingRootCertificateException e) {
                // Unluckily Firefox and Internet Explorer will not show this message.
                // We should find a way to let the browsers display this error message.
                // May we can redirect to some kind of ZAP custom error page.
                final HttpMessage errmsg = new HttpMessage(firstHeader);
                setErrorResponse(errmsg, BAD_GATEWAY_RESPONSE_STATUS, e, "ZAP SSL Error");
                writeHttpResponse(errmsg, httpOut);
                throw new IOException(e);
            }
        } else {
            processHttp(firstHeader, isSecure);
        }
    } catch (SocketTimeoutException e) {
        // ZAP: Log the exception
        if (firstHeader != null) {
            if (HttpRequestHeader.CONNECT.equalsIgnoreCase(firstHeader.getMethod())) {
                log.warn("Timeout reading (client) message after CONNECT to " + firstHeader.getURI());
            } else {
                log.warn("Timeout accessing " + firstHeader.getURI());
            }
        } else {
            log.warn("Socket timeout while reading first message.");
            if (log.isDebugEnabled()) {
                log.debug(e, e);
            }
        }
    } catch (HttpMalformedHeaderException e) {
        log.warn("Malformed Header: ", e);
    } catch (HttpException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.debug("IOException: ", e);
    } finally {
        proxyThreadList.remove(thread);
        // ZAP: do only close if flag is false
        if (!keepSocketOpen) {
            disconnect();
        }
    }
}
Also used : HttpOutputStream(org.parosproxy.paros.network.HttpOutputStream) IOException(java.io.IOException) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpException(org.apache.commons.httpclient.HttpException) HttpInputStream(org.parosproxy.paros.network.HttpInputStream) HttpMessage(org.parosproxy.paros.network.HttpMessage)

Example 23 with HttpRequestHeader

use of org.parosproxy.paros.network.HttpRequestHeader in project zaproxy by zaproxy.

the class ManualHttpRequestEditorDialog method setDefaultMessage.

@Override
public void setDefaultMessage() {
    HttpMessage msg = new HttpMessage();
    try {
        URI uri = new URI("http://www.any_domain_name.org/path", true);
        msg.setRequestHeader(new HttpRequestHeader(HttpRequestHeader.GET, uri, HttpHeader.HTTP11));
        setMessage(msg);
    } catch (HttpMalformedHeaderException e) {
        logger.error(e.getMessage(), e);
    } catch (URIException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpMessage(org.parosproxy.paros.network.HttpMessage) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) URI(org.apache.commons.httpclient.URI)

Aggregations

HttpRequestHeader (org.parosproxy.paros.network.HttpRequestHeader)23 HttpMessage (org.parosproxy.paros.network.HttpMessage)13 URI (org.apache.commons.httpclient.URI)11 HttpRequestBody (org.zaproxy.zap.network.HttpRequestBody)4 IOException (java.io.IOException)3 URIException (org.apache.commons.httpclient.URIException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)3 HarPostData (edu.umass.cs.benchlab.har.HarPostData)2 HarPostDataParam (edu.umass.cs.benchlab.har.HarPostDataParam)2 HarQueryString (edu.umass.cs.benchlab.har.HarQueryString)2 Test (org.junit.jupiter.api.Test)2 HttpResponseHeader (org.parosproxy.paros.network.HttpResponseHeader)2 SSLContextManager (ch.csnc.extension.httpclient.SSLContextManager)1 HarCookie (edu.umass.cs.benchlab.har.HarCookie)1 HarCookies (edu.umass.cs.benchlab.har.HarCookies)1 HarHeader (edu.umass.cs.benchlab.har.HarHeader)1 HarPostDataParams (edu.umass.cs.benchlab.har.HarPostDataParams)1 HarQueryParam (edu.umass.cs.benchlab.har.HarQueryParam)1 HarRequest (edu.umass.cs.benchlab.har.HarRequest)1