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(); }
private void handlePostPlainRequest(final HttpServletRequestImpl.Builder builder, final InputStream in, final int postLength) throws IOException, MalformedInputException { byte[] buffer; buffer = new byte[1]; StringBuilder postLine = new StringBuilder(); while (in.read(buffer, 0, buffer.length) != -1) { postLine.append((char) buffer[0]); if (postLine.length() == postLength) { break; } } Statistics.addBytesReceived(postLine.length()); builder.withPostParameters(queryStringParser.parse(postLine.toString())); }
private void skipToTheFirstPart() throws IOException { byte[] smallBuffer = new byte[1]; // Used for reading the input stream character by character int charPosition = 0; while (true) { int numberOfBytesRead = in.read(smallBuffer); if (numberOfBytesRead == -1) { Statistics.addBytesReceived(allBytesRead); throw new IOException("Premature end of stream before reaching the end of the first boundary"); } allBytesRead += numberOfBytesRead; if (allBytesRead > expectedPostLength) { throw new PayloadTooLargeProtocolException("Payload of too large"); } if (beginBoundary.charAt(charPosition) == smallBuffer[0]) { if (++charPosition == beginBoundary.length()) { break; } } else { charPosition = 0; } } }
String method = statusLine.substring(0, statusLine.length() - 1).toUpperCase(); if (!RECOGNIZED_METHODS_LIST.contains(method)) { Statistics.addBytesReceived(length); throw new MalformedOrUnsupportedMethodProtocolException("Method " + method + " is not supported"); Statistics.addBytesReceived(length); throw new MalformedOrUnsupportedMethodProtocolException("Method name is longer than expected"); Statistics.addBytesReceived(length); throw new StatusLineTooLongProtocolException("Exceeded max size of " + STATUS_MAX_LENGTH); Statistics.addBytesReceived(length);
Statistics.addBytesReceived(allBytesRead); throw new PayloadTooLargeProtocolException("Payload of too large"); Statistics.addBytesReceived(allBytesRead);
@Test public void shouldIncrementAllByteCounters() { assertThat(Statistics.getBytesReceived(), is(equalTo(0l))); assertThat(Statistics.getBytesSent(), is(equalTo(0l))); Statistics.addBytesReceived(3); assertThat(Statistics.getBytesReceived(), is(equalTo(3l))); Statistics.addBytesReceived(5); assertThat(Statistics.getBytesReceived(), is(equalTo(8l))); Statistics.addBytesSent(2); assertThat(Statistics.getBytesSent(), is(equalTo(2l))); Statistics.addBytesSent(11); assertThat(Statistics.getBytesSent(), is(equalTo(13l))); Statistics.reset(); assertThat(Statistics.getBytesReceived(), is(equalTo(0l))); assertThat(Statistics.getBytesSent(), is(equalTo(0l))); }