Search in sources :

Example 6 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement in project httpcomponents-core by apache.

the class TestHeaderElement method testParamByName.

@Test
public void testParamByName() throws Exception {
    final String s = "name = value; param1 = value1; param2 = value2";
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.append(s);
    final ParserCursor cursor = new ParserCursor(0, buf.length());
    final HeaderElement element = BasicHeaderValueParser.INSTANCE.parseHeaderElement(buf, cursor);
    Assertions.assertEquals("value1", element.getParameterByName("param1").getValue());
    Assertions.assertEquals("value2", element.getParameterByName("param2").getValue());
    Assertions.assertNull(element.getParameterByName("param3"));
    Assertions.assertThrows(NullPointerException.class, () -> element.getParameterByName(null));
}
Also used : HeaderElement(org.apache.hc.core5.http.HeaderElement) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement in project httpcomponents-core by apache.

the class ContentType method parse.

private static ContentType parse(final CharSequence s, final boolean strict) throws UnsupportedCharsetException {
    if (TextUtils.isBlank(s)) {
        return null;
    }
    final ParserCursor cursor = new ParserCursor(0, s.length());
    final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(s, cursor);
    if (elements.length > 0) {
        return create(elements[0], strict);
    }
    return null;
}
Also used : ParserCursor(org.apache.hc.core5.http.message.ParserCursor)

Example 8 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement in project httpcomponents-core by apache.

the class ResponseConnControl method process.

@Override
public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");
    // Always drop connection after certain type of responses
    final int status = response.getCode();
    if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_REQUEST_TIMEOUT || status == HttpStatus.SC_LENGTH_REQUIRED || status == HttpStatus.SC_REQUEST_TOO_LONG || status == HttpStatus.SC_REQUEST_URI_TOO_LONG || status == HttpStatus.SC_SERVICE_UNAVAILABLE || status == HttpStatus.SC_NOT_IMPLEMENTED) {
        response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
        return;
    }
    if (!response.containsHeader(HttpHeaders.CONNECTION)) {
        // Always drop connection for HTTP/1.0 responses and below
        // if the content body cannot be correctly delimited
        final ProtocolVersion ver = context.getProtocolVersion();
        if (entity != null && entity.getContentLength() < 0 && ver.lessEquals(HttpVersion.HTTP_1_0)) {
            response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
        } else {
            final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
            final HttpRequest request = coreContext.getRequest();
            boolean closeRequested = false;
            boolean keepAliveRequested = false;
            if (request != null) {
                final Iterator<HeaderElement> it = MessageSupport.iterate(request, HttpHeaders.CONNECTION);
                while (it.hasNext()) {
                    final HeaderElement he = it.next();
                    if (he.getName().equalsIgnoreCase(HeaderElements.CLOSE)) {
                        closeRequested = true;
                        break;
                    } else if (he.getName().equalsIgnoreCase(HeaderElements.KEEP_ALIVE)) {
                        keepAliveRequested = true;
                    }
                }
            }
            if (closeRequested) {
                response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
            } else {
                if (response.containsHeader(HttpHeaders.UPGRADE)) {
                    response.addHeader(HttpHeaders.CONNECTION, HeaderElements.UPGRADE);
                } else {
                    if (keepAliveRequested) {
                        response.addHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE);
                    } else {
                        if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                            response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
                        }
                    }
                }
            }
        }
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HeaderElement(org.apache.hc.core5.http.HeaderElement) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 9 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement in project httpcomponents-core by apache.

the class BasicHeaderValueParser method parseElements.

@Override
public HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor) {
    Args.notNull(buffer, "Char sequence");
    Args.notNull(cursor, "Parser cursor");
    final List<HeaderElement> elements = new ArrayList<>();
    while (!cursor.atEnd()) {
        final HeaderElement element = parseHeaderElement(buffer, cursor);
        if (!(element.getName().isEmpty() && element.getValue() == null)) {
            elements.add(element);
        }
    }
    return elements.toArray(EMPTY_HEADER_ELEMENT_ARRAY);
}
Also used : HeaderElement(org.apache.hc.core5.http.HeaderElement) ArrayList(java.util.ArrayList)

Example 10 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement in project httpcomponents-core by apache.

the class BasicHeaderValueParser method parseHeaderElement.

@Override
public HeaderElement parseHeaderElement(final CharSequence buffer, final ParserCursor cursor) {
    Args.notNull(buffer, "Char sequence");
    Args.notNull(cursor, "Parser cursor");
    final NameValuePair nvp = parseNameValuePair(buffer, cursor);
    NameValuePair[] params = null;
    if (!cursor.atEnd()) {
        final char ch = buffer.charAt(cursor.getPos() - 1);
        if (ch != ELEM_DELIMITER) {
            params = parseParameters(buffer, cursor);
        }
    }
    return new BasicHeaderElement(nvp.getName(), nvp.getValue(), params);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair)

Aggregations

HeaderElement (org.apache.hc.core5.http.HeaderElement)17 Test (org.junit.jupiter.api.Test)13 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)5 Header (org.apache.hc.core5.http.Header)3 ParserCursor (org.apache.hc.core5.http.message.ParserCursor)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 TimeValue (org.apache.hc.core5.util.TimeValue)1 Option (org.eclipse.californium.core.coap.Option)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1