public EagerHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), statusLine, getHeaders(), getBody().orElse(null)); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers) { return withHeaders(headers, true); }
rawHttpStatusCode = rawResponse.getStatusCode(); rawHttpContentType = rawResponse.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE).orElse(""); rawHttpResponseBody = rawResponse.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")); } catch (IOException e) {
@Test public void frontPageExample() throws IOException { RawHttp rawHttp = new RawHttp(); RawHttpRequest request = rawHttp.parseRequest( "GET /hello.txt HTTP/1.1\r\n" + "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n" + "Host: www.example.com\r\n" + "Accept-Language: en, mi"); Socket socket = new Socket("www.example.com", 80); request.writeTo(socket.getOutputStream()); EagerHttpResponse<?> response = rawHttp.parseResponse(socket.getInputStream()).eagerly(); // call "eagerly()" in order to download the body System.out.println(response.eagerly()); assertThat(response.getStatusCode(), equalTo(404)); assertTrue(response.getBody().isPresent()); File responseFile = Files.createTempFile("rawhttp", ".http").toFile(); try (OutputStream out = Files.newOutputStream(responseFile.toPath())) { response.writeTo(out); } System.out.printf("Response parsed from file (%s):", responseFile); System.out.println(rawHttp.parseResponse(responseFile).eagerly()); }
@Test public void goingRawWithoutFancyClient() throws IOException { RawHttp rawHttp = new RawHttp(); RawHttpRequest request = rawHttp.parseRequest(String.format("GET localhost:%d/hello HTTP/1.0", PORT)); Socket socket = new Socket("localhost", PORT); request.writeTo(socket.getOutputStream()); EagerHttpResponse<?> response = rawHttp.parseResponse(socket.getInputStream()).eagerly(); assertThat(response.getStatusCode(), is(200)); assertThat(response.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")), equalTo("Hello")); }
/** * Ensure that this response is read eagerly, downloading the full body if necessary. * <p> * The returned object can be safely passed around after the connection used to receive * this response has been closed. * * @param keepAlive whether to keep the connection alive. If false, the {@link BodyReader} * associated with this response is closed after by the time this method returns. * @return this response, after eagerly downloading all of its contents. * @throws IOException if an error occurs while reading this response */ public EagerHttpResponse<Response> eagerly(boolean keepAlive) throws IOException { try { return EagerHttpResponse.from(this); } finally { if (!keepAlive) { getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } } }
return new EagerHttpResponse<>(response.getLibResponse().orElse(null), response.getRequest().orElse(null), response.getStartLine(),
@Test public void usingRawHttpWithHttpComponents() throws IOException { RawHttpClient<?> client = new RawHttpComponentsClient(); RawHttpRequest request = new RawHttp().parseRequest(String.format("GET localhost:%d/hello HTTP/1.0", PORT)); EagerHttpResponse<?> response = client.send(request).eagerly(); assertThat(response.getStatusCode(), is(200)); assertThat(response.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")), equalTo("Hello")); }
/** * Ensure that this response is read eagerly, downloading the full body if necessary. * <p> * The returned object can be safely passed around after the connection used to receive * this response has been closed. * * @param keepAlive whether to keep the connection alive. If false, the {@link BodyReader} * associated with this response is closed after by the time this method returns. * @return this response, after eagerly downloading all of its contents. * @throws IOException if an error occurs while reading this response */ public EagerHttpResponse<Response> eagerly(boolean keepAlive) throws IOException { try { return EagerHttpResponse.from(this); } finally { if (!keepAlive) { getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } } }
return new EagerHttpResponse<>(response.getLibResponse().orElse(null), response.getRequest().orElse(null), response.getStartLine(),
public EagerHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), statusLine, getHeaders(), getBody().orElse(null)); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers) { return withHeaders(headers, true); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), getStartLine(), append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), getStartLine(), append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }