Search in sources :

Example 16 with ParserCursor

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

the class AbstractHeaderElementIterator method bufferHeaderValue.

private void bufferHeaderValue() {
    this.cursor = null;
    this.buffer = null;
    while (this.headerIt.hasNext()) {
        final Header h = this.headerIt.next();
        if (h instanceof FormattedHeader) {
            this.buffer = ((FormattedHeader) h).getBuffer();
            this.cursor = new ParserCursor(0, this.buffer.length());
            this.cursor.updatePos(((FormattedHeader) h).getValuePos());
            break;
        }
        final String value = h.getValue();
        if (value != null) {
            this.buffer = value;
            this.cursor = new ParserCursor(0, value.length());
            break;
        }
    }
}
Also used : FormattedHeader(org.apache.hc.core5.http.FormattedHeader) Header(org.apache.hc.core5.http.Header) FormattedHeader(org.apache.hc.core5.http.FormattedHeader)

Example 17 with ParserCursor

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

the class BasicLineParser method parseRequestLine.

/**
 * Parses a request line.
 *
 * @param buffer    a buffer holding the line to parse
 *
 * @return  the parsed request line
 *
 * @throws ParseException        in case of a parse error
 */
@Override
public RequestLine parseRequestLine(final CharArrayBuffer buffer) throws ParseException {
    Args.notNull(buffer, "Char array buffer");
    final ParserCursor cursor = new ParserCursor(0, buffer.length());
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final String method = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    if (TextUtils.isEmpty(method)) {
        throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final String uri = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    if (TextUtils.isEmpty(uri)) {
        throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    if (!cursor.atEnd()) {
        throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    return new RequestLine(method, uri, ver);
}
Also used : ParseException(org.apache.hc.core5.http.ParseException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 18 with ParserCursor

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

the class URIBuilder method parseQuery.

static List<NameValuePair> parseQuery(final CharSequence s, final Charset charset, final boolean plusAsBlank) {
    if (s == null) {
        return null;
    }
    final Tokenizer tokenParser = Tokenizer.INSTANCE;
    final ParserCursor cursor = new ParserCursor(0, s.length());
    final List<NameValuePair> list = new ArrayList<>();
    while (!cursor.atEnd()) {
        final String name = tokenParser.parseToken(s, cursor, QUERY_PARAM_SEPARATORS);
        String value = null;
        if (!cursor.atEnd()) {
            final int delim = s.charAt(cursor.getPos());
            cursor.updatePos(cursor.getPos() + 1);
            if (delim == PARAM_VALUE_SEPARATOR) {
                value = tokenParser.parseToken(s, cursor, QUERY_VALUE_SEPARATORS);
                if (!cursor.atEnd()) {
                    cursor.updatePos(cursor.getPos() + 1);
                }
            }
        }
        if (!name.isEmpty()) {
            list.add(new BasicNameValuePair(PercentCodec.decode(name, charset, plusAsBlank), PercentCodec.decode(value, charset, plusAsBlank)));
        }
    }
    return list;
}
Also used : ParserCursor(org.apache.hc.core5.http.message.ParserCursor) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) Tokenizer(org.apache.hc.core5.util.Tokenizer)

Example 19 with ParserCursor

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

the class URIBuilder method splitPath.

static List<String> splitPath(final CharSequence s) {
    if (s == null) {
        return null;
    }
    final ParserCursor cursor = new ParserCursor(0, s.length());
    // Skip leading separator
    if (cursor.atEnd()) {
        return new ArrayList<>(0);
    }
    if (PATH_SEPARATORS.get(s.charAt(cursor.getPos()))) {
        cursor.updatePos(cursor.getPos() + 1);
    }
    final List<String> list = new ArrayList<>();
    final StringBuilder buf = new StringBuilder();
    for (; ; ) {
        if (cursor.atEnd()) {
            list.add(buf.toString());
            break;
        }
        final char current = s.charAt(cursor.getPos());
        if (PATH_SEPARATORS.get(current)) {
            list.add(buf.toString());
            buf.setLength(0);
        } else {
            buf.append(current);
        }
        cursor.updatePos(cursor.getPos() + 1);
    }
    return list;
}
Also used : ParserCursor(org.apache.hc.core5.http.message.ParserCursor) ArrayList(java.util.ArrayList)

Example 20 with ParserCursor

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

the class TestBasicLineParser method testHttpVersionParsing.

@Test
public void testHttpVersionParsing() throws Exception {
    final CharArrayBuffer buffer = new CharArrayBuffer(16);
    buffer.append("HTTP/1.1");
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    HttpVersion version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
    Assertions.assertEquals("HTTP", version.getProtocol(), "HTTP protocol name");
    Assertions.assertEquals(1, version.getMajor(), "HTTP major version number");
    Assertions.assertEquals(1, version.getMinor(), "HTTP minor version number");
    Assertions.assertEquals("HTTP/1.1", version.toString(), "HTTP version number");
    Assertions.assertEquals(buffer.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    buffer.clear();
    buffer.append("HTTP/1.123 123");
    cursor = new ParserCursor(0, buffer.length());
    version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
    Assertions.assertEquals("HTTP", version.getProtocol(), "HTTP protocol name");
    Assertions.assertEquals(1, version.getMajor(), "HTTP major version number");
    Assertions.assertEquals(123, version.getMinor(), "HTTP minor version number");
    Assertions.assertEquals("HTTP/1.123", version.toString(), "HTTP version number");
    Assertions.assertEquals(' ', buffer.charAt(cursor.getPos()));
    Assertions.assertEquals(buffer.length() - 4, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) HttpVersion(org.apache.hc.core5.http.HttpVersion) Test(org.junit.jupiter.api.Test)

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