/** * Parses the HTTP request produced by the given stream. * * @param inputStream producing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpRequest parseRequest(InputStream inputStream) throws IOException { return parseRequest(inputStream, null); }
/** * Parses the HTTP request produced by the given stream. * * @param inputStream producing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpRequest parseRequest(InputStream inputStream) throws IOException { return parseRequest(inputStream, null); }
/** * Parses the given HTTP request. * * @param request in text form * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid */ public final RawHttpRequest parseRequest(String request) { try { return parseRequest(new ByteArrayInputStream(request.getBytes(UTF_8))); } catch (IOException e) { // IOException should be impossible throw new RuntimeException(e); } }
/** * Parses the given HTTP request. * * @param request in text form * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid */ public final RawHttpRequest parseRequest(String request) { try { return parseRequest(new ByteArrayInputStream(request.getBytes(UTF_8))); } catch (IOException e) { // IOException should be impossible throw new RuntimeException(e); } }
/** * Parses the HTTP request contained in the given file. * * @param file containing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs reading the file */ public final RawHttpRequest parseRequest(File file) throws IOException { try (InputStream stream = Files.newInputStream(file.toPath())) { return parseRequest(stream).eagerly(); } }
/** * Parses the HTTP request contained in the given file. * * @param file containing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs reading the file */ public final RawHttpRequest parseRequest(File file) throws IOException { try (InputStream stream = Files.newInputStream(file.toPath())) { return parseRequest(stream).eagerly(); } }
private static CliError sendRequestFromText(String request, RequestRunOptions options) { return sendRequest(HTTP.parseRequest(request), options); }
private static CliError sendRequestFromFile(File file, RequestRunOptions options) { try (InputStream fileStream = Files.newInputStream(file.toPath())) { return sendRequest(HTTP.parseRequest(fileStream), options); } catch (IOException e) { return new CliError(ErrorCode.IO_EXCEPTION, e.toString()); } }
private static CliError sendRequestFromSysIn(RequestRunOptions options) { try { return sendRequest(HTTP.parseRequest(System.in), options); } catch (IOException e) { return new CliError(ErrorCode.IO_EXCEPTION, e.toString()); } }
@Test(expected = FileNotFoundException.class) public void replacingBodyWithFile() throws Throwable { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); try { RawHttpRequest requestWithBody = request.withBody( new FileBody(new File("hello.request"), "text/plain")); System.out.println(requestWithBody.eagerly()); } catch (RuntimeException e) { throw e.getCause(); } }
@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")); }
@Test public void replacingBodyWithChunkedEncodedMessage() throws IOException { InputStream stream = new ByteArrayInputStream("Hello RawHTTTP".getBytes()); int chunkSize = 4; RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody( new ChunkedBody(stream, "text/plain", chunkSize)); System.out.println(requestWithBody.eagerly()); }
@Test public void replacingBodyWithBytes() throws IOException { byte[] bytes = "Hello RawHTTP".getBytes(); RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody( new BytesBody(bytes, "text/plain")); System.out.println(requestWithBody.eagerly()); }
@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")); }
@Test public void replacingBodyWithString() throws IOException { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody(new StringBody("Hello RawHTTP", "text/plain")); System.out.println(requestWithBody.eagerly()); }
@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 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\"")); }
@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(); }
String jsonBody = "{ \"hello\": true, \"raw_http\": \"2.0\" }"; RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest( "POST /post HTTP/1.1\r\n" + "Host: httpbin.org\r\n" +