@Override public Optional<EagerBodyReader> getBody() { Optional<? extends BodyReader> body = super.getBody(); return body.map(b -> (EagerBodyReader) b); }
@Override public Optional<EagerBodyReader> getBody() { Optional<? extends BodyReader> body = super.getBody(); return body.map(b -> (EagerBodyReader) b); }
private static void closeBodyOf(RawHttpResponse<?> response) { if (response != null) { response.getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } }
private static void closeBodyOf(RawHttpResponse<?> response) { if (response != null) { response.getBody().ifPresent(b -> { try { b.close(); } catch (IOException e) { e.printStackTrace(); } }); } }
/** * Create a copy of this HTTP response, replacing its statusLine with the provided one. * * @param statusLine to replace * @return copy of this HTTP message with the provided statusLine */ public RawHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new RawHttpResponse<>(libResponse, request, statusLine, getHeaders(), getBody().orElse(null)); }
/** * Create a copy of this HTTP response, replacing its statusLine with the provided one. * * @param statusLine to replace * @return copy of this HTTP message with the provided statusLine */ public RawHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new RawHttpResponse<>(libResponse, request, statusLine, getHeaders(), getBody().orElse(null)); }
@Override public void logRequest(RawHttpRequest request, RawHttpResponse<?> response) { executor.submit(() -> { if (request.getSenderAddress().isPresent()) { InetAddress senderAddress = request.getSenderAddress().get(); System.out.print(senderAddress.getHostAddress() + " "); } Long bytes = response.getBody() .map(b -> b.getLengthIfKnown().orElse(-1L)) .orElse(-1L); System.out.println("[" + LocalDateTime.now().format(dateFormat) + "] \"" + request.getStartLine() + "\" " + response.getStatusCode() + " " + bytes); }); }
/** * 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(); } }); } } }
/** * 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(); } }); } } }
@Override public RawHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new RawHttpResponse<>(libResponse, request, statusLine, append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }
@Override public RawHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new RawHttpResponse<>(libResponse, request, statusLine, append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }
@Test public void sendRawRequest() throws IOException { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest( "GET / HTTP/1.1\r\n" + "Host: headers.jsontest.com\r\n" + "User-Agent: RawHTTP\r\n" + "Accept: application/json"); Socket socket = new Socket("headers.jsontest.com", 80); request.writeTo(socket.getOutputStream()); // get the response RawHttpResponse<?> response = http.parseResponse(socket.getInputStream()); assertEquals(200, response.getStatusCode()); assertTrue(response.getBody().isPresent()); String textBody = response.getBody().get().decodeBodyToString(UTF_8); assertTrue(textBody.contains("\"User-Agent\": \"RawHTTP\"")); }
InputStream responseStream = response.getBody().map(b -> b.isChunked() ? b.asRawStream() : null) .orElseThrow(() -> new IllegalStateException("HTTP response does not contain a chunked body"));
@Test public void useHttpServer() throws InterruptedException, IOException { RawHttpServer server = new TcpRawHttpServer(8086); RawHttp http = new RawHttp(); server.start(request -> { System.out.println("Got Request:\n" + request); String body = "Hello RawHTTP!"; String dateString = RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)); RawHttpResponse<?> response = http.parseResponse("HTTP/1.1 200 OK\r\n" + "Content-Type: plain/text\r\n" + "Content-Length: " + body.length() + "\r\n" + "Server: RawHTTP\r\n" + "Date: " + dateString + "\r\n" + "\r\n" + body); return Optional.of(response); }); // wait for the socket get bound Thread.sleep(150L); RawHttpRequest request = http.parseRequest("GET /\r\nHost: localhost"); Socket socket = new Socket(InetAddress.getLoopbackAddress(), 8086); request.writeTo(socket.getOutputStream()); // get the response RawHttpResponse<?> response = http.parseResponse(socket.getInputStream()).eagerly(); System.out.println("RESPONSE:\n" + response); assertEquals(200, response.getStatusCode()); assertTrue(response.getBody().isPresent()); assertEquals("Hello RawHTTP!", response.getBody().get().decodeBodyToString(UTF_8)); server.stop(); }
@Test public void requestWithTcpRawHttpClient() throws IOException { TcpRawHttpClient client = new TcpRawHttpClient(); RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest( "GET / HTTP/1.1\r\n" + "Host: headers.jsontest.com\r\n" + "User-Agent: RawHTTP\r\n" + "Accept: application/json"); RawHttpResponse<?> response = client.send(request); assertEquals(200, response.getStatusCode()); assertTrue(response.getBody().isPresent()); String textBody = response.getBody().get().decodeBodyToString(UTF_8); assertTrue(textBody.contains("\"User-Agent\": \"RawHTTP\"")); client.close(); }