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;
}
}
}
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);
}
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;
}
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;
}
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());
}
Aggregations