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