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