@Test(expected = UnsupportedProtocolException.class) public void shouldThrowUnsuportedProtocolExceptionIllegalProtocol() throws Exception { when(socket.getInputStream()).thenReturn(new ByteArrayInputStream("GET / MALF/1.0\r\nHeader1: someValue\r\n\r\n".getBytes())); factory.createFromSocket(socket); }
private String getHeadersString(final InputStream in) throws IOException { StringBuilder headersString = new StringBuilder(); byte[] buffer; buffer = new byte[1]; int headersEndSymbolLength = HEADERS_END_DELIMINATOR.length(); while (in.read(buffer, 0, buffer.length) != -1) { headersString.append((char) buffer[0]); if (headersString.length() > headersEndSymbolLength) { String endChars = getEndChars(headersString, headersEndSymbolLength); if (endChars.equals(HEADERS_END_DELIMINATOR)) { headersString.setLength(headersString.length() - headersEndSymbolLength); break; } } } Statistics.addBytesReceived(headersString.length() + headersEndSymbolLength); return headersString.toString(); }
/** * Reads and computes headers. * * @param in * @return * @throws IOException */ private Headers computeHeaders(final InputStream in) throws IOException { String headersString = getHeadersString(in); if (headersString.length() > MINUMUM_HEADER_LINE_LENGTH) { try { return headersParser.parse(headersString); } catch (MalformedInputException e) { throw new ProtocolException("Malformed request headers"); } } // TODO Write a test that sends a request containing status line only return new Headers(); }
private void handlePostRequest(final HttpServletRequestImpl.Builder builder, final InputStream in, final Headers headers) throws IOException, MalformedInputException { int postLength; if (headers.containsHeader(Headers.HEADER_CONTENT_LENGTH)) { try { postLength = Integer.parseInt(headers.getHeader(Headers.HEADER_CONTENT_LENGTH)); } catch (NumberFormatException e) { throw new MalformedInputException(e.getMessage()); } } else { throw new LengthRequiredException(); } // Only if post length is greater than 0 // Keep 0 value - makes no sense to parse the data if (postLength < 1) { return; } if (postLength > POST_MAX_LENGTH) { throw new PayloadTooLargeProtocolException("Payload of " + postLength + "b exceeds the limit of " + POST_MAX_LENGTH + "b"); } if (isMultipartRequest(headers)) { handlePostMultipartRequest(builder, headers, in, postLength); } else { handlePostPlainRequest(builder, in, postLength); } }
status = statusParser.parse(getStatusLine(in)); } catch (MalformedInputException e) { throw new MalformedStatusLineException("Malformed status line " + e.getMessage()); if (!isValidProtocol(status.getProtocol())) { throw new UnsupportedProtocolException("Protocol " + status.getProtocol() + " is not supported"); assignSocketMetadata(socket, builder); Headers headers = computeHeaders(in); builder.withServletContext(getImplicitServletContext()) .withInputStream(in) .withStatus(status) .withPrincipal(null) .withHeaders(headers) .withCookies(getCookies(headers)); handlePostRequest(builder, in, headers); } catch (MalformedInputException e) { throw new ProtocolException("Malformed post input");
@Before public void setUp() throws Exception { headers = new Headers(); headersParser = mock(Parser.class); when(headersParser.parse(any(String.class))).thenReturn(headers); cookieParser = mock(Parser.class); factory = new HttpServletRequestImplFactory( headersParser, mock(Parser.class), new RequestStatusParser(), cookieParser, mock(Parser.class), "" ); InputStream inputStream = new ByteArrayInputStream("GET / HTTP/1.0\r\nHeader1: someValue\r\n\r\n".getBytes()); socket = mock(Socket.class); when(socket.getInputStream()).thenReturn(inputStream); when(socket.getInetAddress()).thenReturn(mock(InetAddress.class)); when(socket.getLocalAddress()).thenReturn(mock(InetAddress.class)); when(socket.getRemoteSocketAddress()).thenReturn(new InetSocketAddress(mock(InetAddress.class), 1)); }
public ServiceContainer(final ServerConfig serverConfig) { HeadersParser headersParser = new HeadersParser(); requestWrapperFactory = new HttpServletRequestImplFactory(headersParser, new QueryStringParser(), new RequestStatusParser(), new CookieParser(), new MultipartHeadersPartParser(headersParser), serverConfig.getTempPath() ); responseFactory = new HttpServletResponseImplFactory( new HeadersSerializer(), new CookieHeaderSerializer(new DateProvider()), new StreamHelper( new RangeHelper(), new RangePartHeaderSerializer() ) ); threadPoolExecutor = new ThreadPoolExecutor(1, serverConfig.getMaxServerThreads(), DEFAULT_TIME_UNITS, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(serverConfig.getMaxServerThreads() * MAX_THREADS_MULTIPLIER), Executors.defaultThreadFactory(), new ServiceUnavailableHandler(responseFactory) ); httpErrorHandlerResolver = new HttpErrorHandlerResolverImpl(serverConfig); pathHelper = new PathHelper(); }
@Test public void shouldAssignNoCookieAndNoHeadersOnNoHeadersString() throws Exception { when(socket.getInputStream()).thenReturn(new ByteArrayInputStream("GET / HTTP/1.0\r\n\r\n".getBytes())); HttpServletRequestImpl request = factory.createFromSocket(socket); assertThat(request.getCookies().length, is(0)); assertThat(request.getHeaders().keySet().size(), is(0)); } }
@Test(expected = ProtocolException.class) public void shouldThrowProtocolExceptionOnMalformedHeaders() throws Exception { when(headersParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); factory.createFromSocket(socket); }
@Test public void shouldAssignNoCookieOnMalformedCookieString() throws Exception { headers.setHeader(Headers.HEADER_COOKIE, "ANYTHING"); when(cookieParser.parse(any(String.class))).thenThrow(new MalformedInputException("ANY")); HttpServletRequestImpl request = factory.createFromSocket(socket); assertThat(request.getCookies().length, is(0)); verify(cookieParser, times(1)).parse(any(String.class)); }
try { response = responseFactory.createFromSocket(socket); HttpServletRequestImpl request = requestFactory.createFromSocket(socket);