Search in sources :

Example 51 with Header

use of org.apache.commons.httpclient.Header in project zaproxy by zaproxy.

the class ZapHttpParser method parseHeaders.

/*
     * Implementation copied from HttpParser#parseHeaders(InputStream, String) except that no exception is thrown in case of
     * malformed HTTP header lines.
     */
@SuppressWarnings({ "rawtypes", "unchecked", "null" })
public static Header[] parseHeaders(InputStream is, String charset) throws IOException, HttpException {
    ArrayList headers = new ArrayList();
    String name = null;
    StringBuffer value = null;
    for (; ; ) {
        String line = HttpParser.readLine(is, charset);
        if ((line == null) || (line.trim().length() < 1)) {
            break;
        }
        // discussion on folded headers
        if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) {
            // so append value
            if (value != null) {
                value.append(' ');
                value.append(line.trim());
            }
        } else {
            // make sure we save the previous name,value pair if present
            if (name != null) {
                headers.add(new Header(name, value.toString()));
            }
            // Otherwise we should have normal HTTP header line
            // Parse the header name and value
            int colon = line.indexOf(":");
            if (colon < 0) {
                // Do not thrown the exception ignore it instead
                // throw new ProtocolException("Unable to parse header: " + line);
                logger.warn("Ignoring malformed HTTP header line: \"" + line + "\"");
                name = null;
                value = null;
            } else {
                name = line.substring(0, colon).trim();
                value = new StringBuffer(line.substring(colon + 1).trim());
            }
        }
    }
    // make sure we save the last name,value pair if present
    if (name != null) {
        headers.add(new Header(name, value.toString()));
    }
    return (Header[]) headers.toArray(new Header[headers.size()]);
}
Also used : Header(org.apache.commons.httpclient.Header) ArrayList(java.util.ArrayList)

Example 52 with Header

use of org.apache.commons.httpclient.Header in project hadoop by apache.

the class SwiftRestClient method execWithDebugOutput.

/**
   * Execute the request with the request and response logged at debug level
   * @param method method to execute
   * @param client client to use
   * @param <M> method type
   * @return the status code
   * @throws IOException any failure reported by the HTTP client.
   */
private <M extends HttpMethod> int execWithDebugOutput(M method, HttpClient client) throws IOException {
    if (LOG.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder(method.getName() + " " + method.getURI() + "\n");
        for (Header header : method.getRequestHeaders()) {
            builder.append(header.toString());
        }
        LOG.debug(builder);
    }
    int statusCode = client.executeMethod(method);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Status code = " + statusCode);
    }
    return statusCode;
}
Also used : Header(org.apache.commons.httpclient.Header) Endpoint(org.apache.hadoop.fs.swift.auth.entities.Endpoint)

Example 53 with Header

use of org.apache.commons.httpclient.Header in project hadoop by apache.

the class TestSwiftRestClient method testPutAndDelete.

@Test(timeout = SWIFT_TEST_TIMEOUT)
public void testPutAndDelete() throws Throwable {
    assumeEnabled();
    SwiftRestClient client = createClient();
    client.authenticate();
    Path path = new Path("restTestPutAndDelete");
    SwiftObjectPath sobject = SwiftObjectPath.fromPath(serviceURI, path);
    byte[] stuff = new byte[1];
    stuff[0] = 'a';
    client.upload(sobject, new ByteArrayInputStream(stuff), stuff.length);
    //check file exists
    Duration head = new Duration();
    Header[] responseHeaders = client.headRequest("expect success", sobject, SwiftRestClient.NEWEST);
    head.finished();
    LOG.info("head request duration " + head);
    for (Header header : responseHeaders) {
        LOG.info(header.toString());
    }
    //delete the file
    client.delete(sobject);
    //check file is gone
    try {
        Header[] headers = client.headRequest("expect fail", sobject, SwiftRestClient.NEWEST);
        Assert.fail("Expected deleted file, but object is still present: " + sobject);
    } catch (FileNotFoundException e) {
    //expected
    }
    for (DurationStats stats : client.getOperationStatistics()) {
        LOG.info(stats);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) DurationStats(org.apache.hadoop.fs.swift.util.DurationStats) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) FileNotFoundException(java.io.FileNotFoundException) Duration(org.apache.hadoop.fs.swift.util.Duration) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Test(org.junit.Test)

Example 54 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class WebDavClient method logRequestInfo.

private void logRequestInfo(HttpMethod method, String body) throws IOException {
    if (!mDebugEnabled) {
        return;
    }
    StringBuilder reqLog = new StringBuilder();
    reqLog.append("WebDAV request:\n").append(method.getName()).append(" ").append(method.getURI().toString());
    reqLog.append('\n');
    Header[] headers = method.getRequestHeaders();
    if (headers != null && headers.length > 0) {
        for (Header hdr : headers) {
            String hdrName = hdr.getName();
            reqLog.append(hdrName).append('=');
            if (hdrName.contains("Auth") || (hdrName.contains(HttpHeaders.COOKIE))) {
                reqLog.append("*** REPLACED ***\n");
            } else {
                reqLog.append(hdr.getValue()).append('\n');
            }
        }
    }
    if (Strings.isNullOrEmpty(body) || !ZimbraLog.dav.isTraceEnabled()) {
        ZimbraLog.dav.debug(reqLog.toString());
    } else {
        ZimbraLog.dav.debug("%s\n%s", reqLog.toString(), body);
    }
}
Also used : Header(org.apache.commons.httpclient.Header)

Example 55 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class WebDavClient method executeFollowRedirect.

protected HttpMethod executeFollowRedirect(DavRequest req) throws IOException {
    HttpMethod method = null;
    boolean done = false;
    while (!done) {
        method = execute(req);
        int ret = method.getStatusCode();
        if (ret == HttpStatus.SC_MOVED_PERMANENTLY || ret == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header newLocation = method.getResponseHeader("Location");
            if (newLocation != null) {
                String uri = newLocation.getValue();
                ZimbraLog.dav.debug("redirect to new url = " + uri);
                method.releaseConnection();
                req.setRedirectUrl(uri);
                continue;
            }
        }
        done = true;
    }
    return method;
}
Also used : Header(org.apache.commons.httpclient.Header) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5