@Test public void testResponseParsingWithSomeGarbage() throws Exception { final String s = "garbage\r\n" + "garbage\r\n" + "more garbage\r\n" + "HTTP/1.1 200 OK\r\n" + "header1: value1\r\n" + "header2: value2\r\n" + "\r\n"; final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII); final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer); final HttpResponse response = parser.parse(); Assert.assertNotNull(response); Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion()); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); final Header[] headers = response.getAllHeaders(); Assert.assertNotNull(headers); Assert.assertEquals(2, headers.length); Assert.assertEquals("header1", headers[0].getName()); Assert.assertEquals("header2", headers[1].getName()); }
@Test(expected=ProtocolException.class) public void testResponseParsingWithTooMuchGarbage() throws Exception { final String s = "garbage\r\n" + "garbage\r\n" + "more garbage\r\n" + "HTTP/1.1 200 OK\r\n" + "header1: value1\r\n" + "header2: value2\r\n" + "\r\n"; final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII); final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer) { @Override protected boolean reject(final CharArrayBuffer line, final int count) { return count >= 2; } }; parser.parse(); }
private void acceptClient(@NotNull Socket client) throws IOException { final SessionInputBuffer inputBuffer = wrapInputStream(client.getInputStream()); final HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inputBuffer, new BasicLineParser(), new DefaultHttpRequestFactory(), MessageConstraints.DEFAULT ); final SessionOutputBuffer outputBuffer = wrapOutputStream(client.getOutputStream()); final HttpMessageWriter<HttpResponse> writer = new DefaultHttpResponseWriter(outputBuffer); while (!socket.isClosed()) { try { service(inputBuffer, outputBuffer, parser, writer); } catch (ConnectionClosedException ignored) { break; } catch (HttpException e) { log.error(e.getMessage(), e); break; } } }
@Override protected HttpMessageParser<HttpResponse> createResponseParser( final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { // override in derived class to specify a line parser return new DefaultHttpResponseParser (buffer, null, responseFactory, params); }
@Override protected HttpMessageParser<HttpResponse> createResponseParser(final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { // override in derived class to specify a line parser return new DefaultHttpResponseParser(buffer, null, responseFactory, params); }
@Test(expected=ProtocolException.class) public void testResponseParsingOnlyGarbage() throws Exception { final String s = "garbage\r\n" + "garbage\r\n" + "more garbage\r\n" + "a lot more garbage\r\n"; final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII); final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer); parser.parse(); }
@Override protected HttpMessageParser createResponseParser( final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { // override in derived class to specify a line parser return new DefaultResponseParser (buffer, null, responseFactory, params); }
@Override protected HttpMessageParser<HttpResponse> createResponseParser( final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { return new DefaultHttpResponseParser( buffer, new MyLineParser(), responseFactory, params); }
@SuppressWarnings("deprecation") protected void service(@NotNull SessionInputBuffer inputBuffer, @NotNull SessionOutputBuffer outputBuffer, @NotNull HttpMessageParser<HttpRequest> parser, @NotNull HttpMessageWriter<HttpResponse> writer) throws IOException, HttpException { try { final HttpRequest request = parser.parse(); final HttpEntity entity; if (request instanceof HttpEntityEnclosingRequest) { final EntityDeserializer deserializer = new EntityDeserializer(new LaxContentLengthStrategy()); entity = deserializer.deserialize(inputBuffer, request); ((HttpEntityEnclosingRequest) request).setEntity(entity); } else { entity = null; } final HttpResponse response = service(request); if (entity != null) { entity.getContent().close(); } if (response.getEntity() != null) { response.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(response.getEntity().getContentLength())); response.addHeader(response.getEntity().getContentType()); response.addHeader(response.getEntity().getContentEncoding()); } response.setHeader(HttpHeaders.SERVER, "Protobuf RPC"); writer.write(response); if (response.getEntity() != null) { final EntitySerializer serializer = new EntitySerializer(new LaxContentLengthStrategy()); serializer.serialize(outputBuffer, response, response.getEntity()); } } finally { outputBuffer.flush(); } }
public HttpMessageParser create(final SessionInputBuffer buffer, final MessageConstraints constraints) { return new DefaultHttpResponseParser(buffer, lineParser, responseFactory, constraints); }
public HttpMessageParser create(final SessionInputBuffer buffer, final MessageConstraints constraints) { return new DefaultHttpRequestParser(buffer, lineParser, requestFactory, constraints); }
@Override public HttpMessageParser<HttpResponse> create(final SessionInputBuffer buffer, final MessageConstraints constraints) { return new DefaultHttpResponseParser(buffer, lineParser, responseFactory, constraints); }
@Test(expected=NoHttpResponseException.class) public void testResponseParsingNoResponse() throws Exception { final SessionInputBuffer inbuffer = new SessionInputBufferMock("", Consts.ASCII); final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer); parser.parse(); }
@Override protected HttpMessageParser createResponseParser(final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { return new DefaultResponseParser(buffer, new IcyLineParser(), responseFactory, params); }
/** * Creates an instance of {@link HttpMessageParser} to be used for parsing * HTTP responses received over this connection. * <p> * This method can be overridden in a super class in order to provide * a different implementation of the {@link HttpMessageParser} interface or * to pass a different implementation of the {@link LineParser} to the * the {@link DefaultHttpResponseParser} constructor. * * @param buffer the session input buffer. * @param responseFactory the HTTP response factory. * @param params HTTP parameters. * @return HTTP message parser. */ protected HttpMessageParser<HttpResponse> createResponseParser( final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params) { return new DefaultHttpResponseParser(buffer, null, responseFactory, params); }
/** * Creates an instance of {@link HttpMessageParser} to be used for parsing * HTTP requests received over this connection. * <p> * This method can be overridden in a super class in order to provide * a different implementation of the {@link HttpMessageParser} interface or * to pass a different implementation of the {@link LineParser} to the * the {@link DefaultHttpRequestParser} constructor. * * @param buffer the session input buffer. * @param requestFactory the HTTP request factory. * @param params HTTP parameters. * @return HTTP message parser. */ protected HttpMessageParser<HttpRequest> createRequestParser( final SessionInputBuffer buffer, final HttpRequestFactory requestFactory, final HttpParams params) { return new DefaultHttpRequestParser(buffer, null, requestFactory, params); }