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())); }
/** * Serves all input stream to the output stream. * * @param inputStream * @param outputStream * @throws IOException */ public void serveMultiRangeStream(final InputStream inputStream, final OutputStream outputStream) throws IOException { int numberOfBufferReadBytes; byte[] buffer = new byte[BUFFER_SIZE]; while ((numberOfBufferReadBytes = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, numberOfBufferReadBytes); outputStream.flush(); Statistics.addBytesSent(numberOfBufferReadBytes); } }
/** * Default constructor. */ private HttpServletRequestImpl() { Statistics.incrementRequestHandled(); postParameters = new HashMap<>(); getParameters = new HashMap<>(); uploadedFiles = new HashSet<>(); attributes = new HashMap<>(); }
@Test public void shouldIncrementAllCountersAndResetThem() { assertThat(Statistics.getError404s(), is(equalTo(0l))); assertThat(Statistics.getError500s(), is(equalTo(0l))); assertThat(Statistics.getRequestsHandled(), is(equalTo(0l))); Statistics.incrementError404(); assertThat(Statistics.getError404s(), is(equalTo(1l))); Statistics.incrementError500(); assertThat(Statistics.getError500s(), is(equalTo(1l))); Statistics.incrementRequestHandled(); assertThat(Statistics.getRequestsHandled(), is(equalTo(1l))); Statistics.incrementError404(); assertThat(Statistics.getError404s(), is(equalTo(2l))); Statistics.incrementError500(); assertThat(Statistics.getError500s(), is(equalTo(2l))); Statistics.incrementRequestHandled(); assertThat(Statistics.getRequestsHandled(), is(equalTo(2l))); Statistics.reset(); assertThat(Statistics.getError404s(), is(equalTo(0l))); assertThat(Statistics.getError500s(), is(equalTo(0l))); assertThat(Statistics.getRequestsHandled(), is(equalTo(0l))); }
private HTMLDocument renderDocument() { HTMLDocument doc = new HTMLDocument("Statistics"); doc.setOwnerClass(getClass().getSimpleName()); doc.writeln("<div class=\"page-header\"><h1>Server statistics</h1></div>"); doc.writeln("<p class=\"bg-info\">Please refresh the page to update the statistics."); doc.writeln("<table class=\"table\">"); doc.writeln("<tr>"); doc.writeln(" <td>Data received</td><td>" + FileUtilities.fileSizeUnits(ro.polak.http.Statistics.getBytesReceived()) + "</td>"); doc.writeln("</tr>"); doc.writeln("<tr>"); doc.writeln(" <td>Data sent</td><td>" + FileUtilities.fileSizeUnits(ro.polak.http.Statistics.getBytesSent()) + "</td>"); doc.writeln("</tr>"); doc.writeln("<tr>"); doc.writeln(" <td>Requests handled</td><td>" + ro.polak.http.Statistics.getRequestsHandled() + "</td>"); doc.writeln("</tr>"); doc.writeln("<tr>"); doc.writeln(" <td>404 errors</td><td>" + ro.polak.http.Statistics.getError404s() + "</td>"); doc.writeln("</tr>"); doc.writeln("<tr>"); doc.writeln(" <td>500 errors</td><td>" + ro.polak.http.Statistics.getError500s() + "</td>"); doc.writeln("</tr>"); doc.writeln("</table"); return doc; } }
@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))); }
/** * Returns resolved handler for given exception. * * @param e * @return */ @Override public HttpErrorHandler getHandler(final RuntimeException e) { Throwable fallbackException; try { if (e instanceof ProtocolException) { return getProtocolExceptionHandler((ProtocolException) e); } else if (e instanceof AccessDeniedException) { return new HttpError403Handler(serverConfig.getErrorDocument403Path()); } else if (e instanceof NotFoundException) { Statistics.incrementError404(); return new HttpError404Handler(serverConfig.getErrorDocument404Path()); } else if (e instanceof MethodNotAllowedException) { return new HttpError405Handler(getAllowedMethods()); } else { fallbackException = e; } } catch (Throwable handlingException) { fallbackException = handlingException; } Statistics.incrementError500(); return new HttpError500Handler().setReason(fallbackException); }
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(); }
Statistics.addBytesSent(numberOfBytesToServe);
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);