Search in sources :

Example 6 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor 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 ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor 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 ParserCursor

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

the class BasicHeaderValueParser method parseParameters.

@Override
public NameValuePair[] parseParameters(final CharSequence buffer, final ParserCursor cursor) {
    Args.notNull(buffer, "Char sequence");
    Args.notNull(cursor, "Parser cursor");
    tokenizer.skipWhiteSpace(buffer, cursor);
    final List<NameValuePair> params = new ArrayList<>();
    while (!cursor.atEnd()) {
        final NameValuePair param = parseNameValuePair(buffer, cursor);
        params.add(param);
        final char ch = buffer.charAt(cursor.getPos() - 1);
        if (ch == ELEM_DELIMITER) {
            break;
        }
    }
    return params.toArray(EMPTY_NAME_VALUE_ARRAY);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) ArrayList(java.util.ArrayList)

Example 9 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor 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 ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor 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

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)12 Test (org.junit.jupiter.api.Test)11 HeaderElement (org.apache.hc.core5.http.HeaderElement)8 NameValuePair (org.apache.hc.core5.http.NameValuePair)7 ArrayList (java.util.ArrayList)5 ParserCursor (org.apache.hc.core5.http.message.ParserCursor)4 ParseException (org.apache.hc.core5.http.ParseException)3 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)2 Header (org.apache.hc.core5.http.Header)2 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 HttpVersion (org.apache.hc.core5.http.HttpVersion)1 NameValuePair (org.apache.hc.core5.http.copied.NameValuePair)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)1 BasicNameValuePair (org.apache.hc.core5.http.message.copied.BasicNameValuePair)1 ParserCursor (org.apache.hc.core5.http.message.copied.ParserCursor)1 Tokenizer (org.apache.hc.core5.util.Tokenizer)1