Search in sources :

Example 16 with HeaderElement

use of org.apache.http.HeaderElement in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenCustomizingKeepAliveStrategy_thenNoExceptions.

// 5
@Test
public final // 5.1
void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
    final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
            final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                final HeaderElement he = it.nextElement();
                final String param = he.getName();
                final String value = he.getValue();
                if ((value != null) && param.equalsIgnoreCase("timeout")) {
                    return Long.parseLong(value) * 1000;
                }
            }
            final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            if ("localhost".equalsIgnoreCase(target.getHostName())) {
                return 10 * 1000;
            } else {
                return 5 * 1000;
            }
        }
    };
    client = HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(poolingConnManager).build();
    client.execute(get1);
    client.execute(get2);
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HeaderElement(org.apache.http.HeaderElement) HttpHost(org.apache.http.HttpHost) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) Test(org.junit.Test)

Example 17 with HeaderElement

use of org.apache.http.HeaderElement in project ovirt-engine by oVirt.

the class CSRFProtectionFilter method isProtectionRequested.

/**
 * Checks if the headers contained in the given request indicate that the user wants to enable protection. This
 * means checking if the {@code Prefer} header exists and has at least one {@code csrf-protection} element. For
 * example:
 *
 * <pre>
 * GET /ovirt-engine/api HTTP/1.1
 * Host: ovirt.example.com
 * Prefer: persistent-auth, csrf-protection
 * </pre>
 *
 * @param request the HTTP request to check
 * @return {@code true} if the request contains headers that indicate that protection should be enabled,
 *   {@code false} otherwise
 */
private boolean isProtectionRequested(HttpServletRequest request) {
    Enumeration<String> headerValues = request.getHeaders(PREFER_HEADER);
    while (headerValues.hasMoreElements()) {
        String headerValue = headerValues.nextElement();
        HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null);
        for (HeaderElement headerElement : headerElements) {
            String elementName = headerElement.getName();
            if (PREFER_ELEMENT.equalsIgnoreCase(elementName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : HeaderElement(org.apache.http.HeaderElement)

Example 18 with HeaderElement

use of org.apache.http.HeaderElement in project ovirt-engine by oVirt.

the class FiltersHelper method getPrefer.

public static int getPrefer(HttpServletRequest req) {
    int ret = 0;
    Enumeration<String> headerValues = req.getHeaders(Constants.HEADER_PREFER);
    while (headerValues.hasMoreElements()) {
        String headerValue = headerValues.nextElement();
        HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null);
        if (headerElements != null) {
            for (HeaderElement headerElement : headerElements) {
                String elementName = headerElement.getName();
                if ("new-auth".equalsIgnoreCase(elementName)) {
                    ret |= PREFER_NEW_AUTH;
                }
                if ("persistent-auth".equalsIgnoreCase(elementName)) {
                    ret |= PREFER_PERSISTENCE_AUTH;
                }
            }
        }
    }
    return ret;
}
Also used : HeaderElement(org.apache.http.HeaderElement)

Example 19 with HeaderElement

use of org.apache.http.HeaderElement in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method addMockedHeader.

private void addMockedHeader(final HttpResponse httpResponseMock, final String name, final String value, HeaderElement[] elements) {
    final Header header = new Header() {

        @Override
        public String getName() {
            return name;
        }

        @Override
        public String getValue() {
            return value;
        }

        @Override
        public HeaderElement[] getElements() throws ParseException {
            return elements;
        }
    };
    when(httpResponseMock.getFirstHeader(name)).thenReturn(header);
}
Also used : Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement)

Example 20 with HeaderElement

use of org.apache.http.HeaderElement in project jmeter by apache.

the class MultipartUrlConfig method parseArguments.

/**
 * This method allows a proxy server to send over the raw text from a
 * browser's output stream to be parsed and stored correctly into the
 * UrlConfig object.
 *
 * @param queryString text to parse
 */
public void parseArguments(String queryString) {
    // $NON-NLS-1$
    String[] parts = JOrphanUtils.split(queryString, "--" + getBoundary());
    for (String part : parts) {
        // $NON-NLS-1$
        String contentDisposition = getHeaderValue("Content-disposition", part);
        // $NON-NLS-1$
        String contentType = getHeaderValue("Content-type", part);
        // Check if it is form data
        if (contentDisposition != null && contentDisposition.contains("form-data")) {
            // $NON-NLS-1$
            // Get the form field name
            HeaderElement[] headerElements = null;
            try {
                headerElements = BasicHeaderValueParser.parseElements(contentDisposition, BasicHeaderValueParser.INSTANCE);
            } catch (ParseException e) {
                log.info("Can't parse header {}", contentDisposition, e);
            }
            String name = "";
            String path = null;
            if (headerElements != null) {
                for (HeaderElement element : headerElements) {
                    name = getParameterValue(element, "name", "");
                    path = getParameterValue(element, "filename", null);
                }
            }
            if (path != null && !path.isEmpty()) {
                // Set the values retrieved for the file upload
                files.addHTTPFileArg(path, name, contentType);
            } else {
                // Find the first empty line of the multipart, it signals end of headers for multipart
                // Agents are supposed to terminate lines in CRLF:
                // $NON-NLS-1$
                int indexEmptyCrLfCrLfLinePos = part.indexOf(CRLFCRLF);
                // $NON-NLS-1$
                int indexEmptyLfLfLinePos = part.indexOf(LFLF);
                String value = null;
                if (indexEmptyCrLfCrLfLinePos > -1) {
                    // CRLF blank line found
                    value = part.substring(indexEmptyCrLfCrLfLinePos + CRLFCRLF.length(), part.lastIndexOf(CRLF));
                } else if (indexEmptyLfLfLinePos > -1) {
                    // LF blank line found
                    value = part.substring(indexEmptyLfLfLinePos + LFLF.length(), part.lastIndexOf(LF));
                }
                this.addNonEncodedArgument(name, value, contentType);
            }
        }
    }
}
Also used : HeaderElement(org.apache.http.HeaderElement) ParseException(org.apache.http.ParseException)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3