/** * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response * handler object. * * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler object. * @throws IOException If there were any problems getting any response information from the * HttpClient method object. */ private SdkHttpFullResponse createResponse(org.apache.http.HttpResponse apacheHttpResponse, HttpRequestBase apacheRequest) throws IOException { return SdkHttpFullResponse.builder() .statusCode(apacheHttpResponse.getStatusLine().getStatusCode()) .statusText(apacheHttpResponse.getStatusLine().getReasonPhrase()) .content(apacheHttpResponse.getEntity() != null ? toAbortableInputStream(apacheHttpResponse, apacheRequest) : null) .headers(transformHeaders(apacheHttpResponse)) .build(); }
/** * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response * handler object. * * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler object. * @throws IOException If there were any problems getting any response information from the * HttpClient method object. */ private HttpExecuteResponse createResponse(org.apache.http.HttpResponse apacheHttpResponse, HttpRequestBase apacheRequest) throws IOException { SdkHttpResponse response = SdkHttpResponse.builder() .statusCode(apacheHttpResponse.getStatusLine().getStatusCode()) .statusText(apacheHttpResponse.getStatusLine().getReasonPhrase()) .headers(transformHeaders(apacheHttpResponse)) .build(); AbortableInputStream responseBody = apacheHttpResponse.getEntity() != null ? toAbortableInputStream(apacheHttpResponse, apacheRequest) : null; return HttpExecuteResponse.builder().response(response).responseBody(responseBody).build(); }
@Override public HttpExecuteResponse call() throws IOException { connection.connect(); request.contentStreamProvider().ifPresent(provider -> invokeSafely(() -> IoUtils.copy(provider.newStream(), connection.getOutputStream()))); int responseCode = connection.getResponseCode(); boolean isErrorResponse = HttpStatusFamily.of(responseCode).isOneOf(CLIENT_ERROR, SERVER_ERROR); InputStream content = !isErrorResponse ? connection.getInputStream() : connection.getErrorStream(); AbortableInputStream responseBody = content != null ? AbortableInputStream.create(content) : null; return HttpExecuteResponse.builder() .response(SdkHttpResponse.builder() .statusCode(responseCode) .statusText(connection.getResponseMessage()) // TODO: Don't ignore abort? .headers(extractHeaders(connection)) .build()) .responseBody(responseBody) .build(); }
@Override protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg) throws Exception { RequestContext requestContext = channelContext.channel().attr(REQUEST_CONTEXT_KEY).get(); if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; SdkHttpResponse sdkResponse = SdkHttpFullResponse.builder() .headers(fromNettyHeaders(response.headers())) .statusCode(response.status().code()) .statusText(response.status().reasonPhrase()) .build(); channelContext.channel().attr(KEEP_ALIVE).set(HttpUtil.isKeepAlive(response)); requestContext.handler().onHeaders(sdkResponse); } CompletableFuture<Void> ef = executeFuture(channelContext); if (msg instanceof StreamedHttpResponse) { requestContext.handler().onStream( new PublisherAdapter((StreamedHttpResponse) msg, channelContext, requestContext, ef)); } else if (msg instanceof FullHttpResponse) { // Be prepared to take care of (ignore) a trailing LastHttpResponse // from the HttpClientCodec if there is one. channelContext.pipeline().replace(HttpStreamsClientHandler.class, channelContext.name() + "-LastHttpContentSwallower", LastHttpContentSwallower.getInstance()); ByteBuf fullContent = ((FullHttpResponse) msg).content(); ByteBuffer bb = copyToByteBuffer(fullContent); fullContent.release(); requestContext.handler().onStream(new FullResponseContentPublisher(channelContext, bb, ef)); finalizeResponse(requestContext, channelContext); } }
@Override protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg) throws Exception { RequestContext requestContext = channelContext.channel().attr(REQUEST_CONTEXT_KEY).get(); if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; SdkHttpResponse sdkResponse = SdkHttpFullResponse.builder() .headers(fromNettyHeaders(response.headers())) .statusCode(response.status().code()) .statusText(response.status().reasonPhrase()) .build(); channelContext.channel().attr(KEEP_ALIVE).set(HttpUtil.isKeepAlive(response)); requestContext.handler().onHeaders(sdkResponse); } CompletableFuture<Void> ef = executeFuture(channelContext); if (msg instanceof StreamedHttpResponse) { requestContext.handler().onStream( new PublisherAdapter((StreamedHttpResponse) msg, channelContext, requestContext, ef)); } else if (msg instanceof FullHttpResponse) { // Be prepared to take care of (ignore) a trailing LastHttpResponse // from the HttpClientCodec if there is one. channelContext.pipeline().replace(HttpStreamsClientHandler.class, channelContext.name() + "-LastHttpContentSwallower", new LastHttpContentSwallower()); ByteBuf fullContent = ((FullHttpResponse) msg).content(); ByteBuffer bb = copyToByteBuffer(fullContent); fullContent.release(); requestContext.handler().onStream(new FullResponseContentPublisher(channelContext, bb, ef)); finalizeResponse(requestContext, channelContext); } }