use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-core by apache.
the class DefaultConnectionReuseStrategy method keepAlive.
// see interface ConnectionReuseStrategy
@Override
public boolean keepAlive(final HttpRequest request, final HttpResponse response, final HttpContext context) {
Args.notNull(response, "HTTP response");
if (request != null) {
final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION));
while (ti.hasNext()) {
final String token = ti.next();
if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
return false;
}
}
}
// returns content as part of a HTTP 204 response.
if (response.getCode() == HttpStatus.SC_NO_CONTENT) {
final Header clh = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
if (clh != null) {
try {
final long contentLen = Long.parseLong(clh.getValue());
if (contentLen > 0) {
return false;
}
} catch (final NumberFormatException ex) {
// fall through
}
}
if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
return false;
}
}
// Check for a self-terminating entity. If the end of the entity will
// be indicated by closing the connection, there is no keep-alive.
final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
if (teh != null) {
if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) {
return false;
}
} else {
final String method = request != null ? request.getMethod() : null;
if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) {
return false;
}
}
// Check for the "Connection" header. If that is absent, check for
// the "Proxy-Connection" header. The latter is an unspecified and
// broken but unfortunately common extension of HTTP.
Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION);
if (!headerIterator.hasNext()) {
headerIterator = response.headerIterator("Proxy-Connection");
}
final ProtocolVersion ver = response.getVersion() != null ? response.getVersion() : context.getProtocolVersion();
if (headerIterator.hasNext()) {
if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
final Iterator<String> it = new BasicTokenIterator(headerIterator);
while (it.hasNext()) {
final String token = it.next();
if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
return false;
}
}
return true;
}
final Iterator<String> it = new BasicTokenIterator(headerIterator);
while (it.hasNext()) {
final String token = it.next();
if (HeaderElements.KEEP_ALIVE.equalsIgnoreCase(token)) {
return true;
}
}
return false;
}
return ver.greaterEquals(HttpVersion.HTTP_1_1);
}
use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-core by apache.
the class DefaultHttpRequestWriter method writeHeadLine.
@Override
protected void writeHeadLine(final ClassicHttpRequest message, final CharArrayBuffer lineBuf) throws IOException {
final ProtocolVersion transportVersion = message.getVersion();
getLineFormatter().formatRequestLine(lineBuf, new RequestLine(message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1));
}
use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-core by apache.
the class RequestTargetHost method process.
@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
Args.notNull(request, "HTTP request");
Args.notNull(context, "HTTP context");
final ProtocolVersion ver = context.getProtocolVersion();
final String method = request.getMethod();
if (Method.CONNECT.isSame(method) && ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
}
if (!request.containsHeader(HttpHeaders.HOST)) {
URIAuthority authority = request.getAuthority();
if (authority == null) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
}
throw new ProtocolException("Target host is unknown");
}
if (authority.getUserInfo() != null) {
authority = new URIAuthority(authority.getHostName(), authority.getPort());
}
request.addHeader(HttpHeaders.HOST, authority);
}
}
use of org.apache.hc.core5.http.ProtocolVersion 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.ProtocolVersion in project httpcomponents-core by apache.
the class ResponseContent method process.
/**
* Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
* @param response The HttpResponse to modify.
* @param context Unused.
* @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
* @throws IllegalArgumentException If the response is null.
*/
@Override
public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
Args.notNull(response, "HTTP response");
if (this.overwrite) {
response.removeHeaders(HttpHeaders.TRANSFER_ENCODING);
response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
} else {
if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (response.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
throw new ProtocolException("Content-Length header already present");
}
}
final ProtocolVersion ver = context.getProtocolVersion();
if (entity != null) {
final long len = entity.getContentLength();
if (len >= 0 && !entity.isChunked()) {
response.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(entity.getContentLength()));
} else if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
response.addHeader(HttpHeaders.TRANSFER_ENCODING, HeaderElements.CHUNKED_ENCODING);
MessageSupport.addTrailerHeader(response, entity);
}
MessageSupport.addContentTypeHeader(response, entity);
MessageSupport.addContentEncodingHeader(response, entity);
} else {
final int status = response.getCode();
if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_NOT_MODIFIED) {
response.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
}
}
}
Aggregations