Search in sources :

Example 36 with ProtocolVersion

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

the class ServerHttp1StreamHandler method commitResponse.

private void commitResponse(final HttpResponse response, final EntityDetails responseEntityDetails) throws HttpException, IOException {
    if (responseCommitted.compareAndSet(false, true)) {
        final ProtocolVersion transportVersion = response.getVersion();
        if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
            throw new UnsupportedHttpVersionException(transportVersion);
        }
        final int status = response.getCode();
        if (status < HttpStatus.SC_SUCCESS) {
            throw new HttpException("Invalid response: " + status);
        }
        context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
        context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
        httpProcessor.process(response, responseEntityDetails, context);
        final boolean endStream = responseEntityDetails == null || (receivedRequest != null && Method.HEAD.isSame(receivedRequest.getMethod()));
        if (!connectionReuseStrategy.keepAlive(receivedRequest, response, context)) {
            keepAlive = false;
        }
        outputChannel.submit(response, endStream, endStream ? FlushMode.IMMEDIATE : FlushMode.BUFFER);
        if (endStream) {
            if (!keepAlive) {
                outputChannel.close();
            }
            responseState = MessageState.COMPLETE;
        } else {
            responseState = MessageState.BODY;
            exchangeHandler.produce(internalDataChannel);
        }
    } else {
        throw new HttpException("Response already committed");
    }
}
Also used : HttpException(org.apache.hc.core5.http.HttpException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 37 with ProtocolVersion

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

the class DefaultHttpResponseWriter method writeHeadLine.

@Override
protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException {
    lineBuf.clear();
    final ProtocolVersion transportVersion = message.getVersion();
    getLineFormatter().formatStatusLine(lineBuf, new StatusLine(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1, message.getCode(), message.getReasonPhrase()));
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Aggregations

ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)35 ProtocolException (org.apache.hc.core5.http.ProtocolException)8 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)8 Header (org.apache.hc.core5.http.Header)6 HttpException (org.apache.hc.core5.http.HttpException)6 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 ParseException (org.apache.hc.core5.http.ParseException)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)3 HttpHost (org.apache.hc.core5.http.HttpHost)3 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 StatusLine (org.apache.hc.core5.http.message.StatusLine)3 Test (org.junit.jupiter.api.Test)3 URI (java.net.URI)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SSLSession (javax.net.ssl.SSLSession)2