use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testClientRequestReadFailsOnSecondRead.
@Test
public void testClientRequestReadFailsOnSecondRead() throws Exception {
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
startServer(new EchoHttpServlet());
startProxy(new AsyncMiddleManServlet() {
private int count;
@Override
protected int readClientRequestContent(ServletInputStream input, byte[] buffer) throws IOException {
if (++count < 2)
return super.readClientRequestContent(input, buffer);
else
throw new IOException("explicitly_thrown_by_test");
}
});
startClient();
final CountDownLatch latch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider();
client.newRequest("localhost", serverConnector.getLocalPort()).content(content).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
if (result.getResponse().getStatus() == 502)
latch.countDown();
}
});
content.offer(ByteBuffer.allocate(512));
sleep(1000);
content.offer(ByteBuffer.allocate(512));
content.close();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testInterruptedRequest.
@Test
public void testInterruptedRequest() throws Exception {
final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
configureServer(new AbstractHandler.ErrorDispatchHandler() {
@Override
public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
int contentLength = request.getContentLength();
ServletInputStream inputStream = request.getInputStream();
for (int i = 0; i < contentLength; i++) {
try {
inputStream.read();
} catch (EofException e) {
earlyEOFException.set(true);
throw new QuietServletException(e);
}
if (i == 3)
fourBytesRead.set(true);
}
}
});
StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
request.append("Host: localhost\n");
request.append("Content-length: 6\n\n");
request.append("foo");
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
OutputStream os = client.getOutputStream();
os.write(request.toString().getBytes());
os.flush();
client.shutdownOutput();
String response = readResponse(client);
client.close();
assertThat("response contains 500", response, Matchers.containsString(" 500 "));
assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class HttpTrailersTest method testHugeTrailer.
@Test
public void testHugeTrailer() throws Exception {
start(new AbstractHandler.ErrorDispatchHandler() {
@Override
protected void doNonErrorHandle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
jettyRequest.setHandled(true);
try {
// EOF will not be reached because of the huge trailer.
ServletInputStream input = jettyRequest.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
Assert.fail();
} catch (IOException x) {
// Expected.
}
}
});
char[] huge = new char[1024 * 1024];
Arrays.fill(huge, 'X');
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
client.setSoTimeout(5000);
try {
String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "0\r\n" + "Trailer: " + new String(huge) + "\r\n" + "\r\n";
OutputStream output = client.getOutputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
HttpTester.Response response = HttpTester.parseResponse(HttpTester.from(client.getInputStream()));
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
} catch (Exception e) {
// May be thrown if write fails and error handling is aborted
}
}
}
use of javax.servlet.ServletInputStream in project javaee7-samples by javaee-samples.
the class ReadTestServlet method processRequest.
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter output = response.getWriter()) {
output.println("<html>");
output.println("<head>");
output.println("<title>Reading asynchronously</title>");
output.println("</head>");
output.println("<body>");
output.println("<h1>Reading asynchronously</h1>");
AsyncContext context = request.startAsync();
ServletInputStream input = request.getInputStream();
input.setReadListener(new MyReadListener(input, context));
output.println("</body>");
output.println("</html>");
}
}
use of javax.servlet.ServletInputStream in project ninja by ninjaframework.
the class NinjaServletContextTest method testGetWindows1250ParameterInMultipart.
@Test
public void testGetWindows1250ParameterInMultipart() throws Exception {
String body = "------Ninja\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "content-type: text/plain; charset=windows-1250\r\n" + "content-transfer-encoding: quoted-printable\r\n" + "\r\n" + "Joe owes €100.\r\n" + "------Ninja--\r\n";
ServletInputStream sis = createHttpServletRequestInputStream(body.getBytes("windows-1250"));
when(httpServletRequest.getContentType()).thenReturn("multipart/form-data; boundary=----Ninja");
when(httpServletRequest.getMethod()).thenReturn("POST");
when(ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_FILE_SIZE, -1)).thenReturn(1024);
when(ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_TOTAL_SIZE, -1)).thenReturn(1024);
when(httpServletRequest.getInputStream()).thenReturn(sis);
context.init(servletContext, httpServletRequest, httpServletResponse);
assertEquals("Joe owes €100.", context.getParameter("field1"));
}
Aggregations