use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncMiddleManServlet method service.
@Override
protected void service(HttpServletRequest clientRequest, HttpServletResponse proxyResponse) throws ServletException, IOException {
String rewrittenTarget = rewriteTarget(clientRequest);
if (_log.isDebugEnabled()) {
StringBuffer target = clientRequest.getRequestURL();
if (clientRequest.getQueryString() != null)
target.append("?").append(clientRequest.getQueryString());
_log.debug("{} rewriting: {} -> {}", getRequestId(clientRequest), target, rewrittenTarget);
}
if (rewrittenTarget == null) {
onProxyRewriteFailed(clientRequest, proxyResponse);
return;
}
final Request proxyRequest = getHttpClient().newRequest(rewrittenTarget).method(clientRequest.getMethod()).version(HttpVersion.fromString(clientRequest.getProtocol()));
copyRequestHeaders(clientRequest, proxyRequest);
addProxyHeaders(clientRequest, proxyRequest);
final AsyncContext asyncContext = clientRequest.startAsync();
// We do not timeout the continuation, but the proxy request.
asyncContext.setTimeout(0);
proxyRequest.timeout(getTimeout(), TimeUnit.MILLISECONDS);
// to allow optimization of the Content-Length header.
if (hasContent(clientRequest)) {
DeferredContentProvider provider = newProxyContentProvider(clientRequest, proxyResponse, proxyRequest);
proxyRequest.content(provider);
if (expects100Continue(clientRequest)) {
proxyRequest.attribute(CLIENT_REQUEST_ATTRIBUTE, clientRequest);
proxyRequest.attribute(CONTINUE_ACTION_ATTRIBUTE, (Runnable) () -> {
try {
ServletInputStream input = clientRequest.getInputStream();
input.setReadListener(newProxyReadListener(clientRequest, proxyResponse, proxyRequest, provider));
} catch (Throwable failure) {
onClientRequestFailure(clientRequest, proxyRequest, proxyResponse, failure);
}
});
sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
} else {
ServletInputStream input = clientRequest.getInputStream();
input.setReadListener(newProxyReadListener(clientRequest, proxyResponse, proxyRequest, provider));
}
} else {
sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncProxyServlet method proxyRequestContent.
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
ServletInputStream input = request.getInputStream();
DeferredContentProvider provider = new DeferredContentProvider();
input.setReadListener(newReadListener(request, response, proxyRequest, provider));
return provider;
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testClientRequestReadFailsOnFirstRead.
@Test
public void testClientRequestReadFailsOnFirstRead() throws Exception {
startServer(new EchoHttpServlet());
startProxy(new AsyncMiddleManServlet() {
@Override
protected int readClientRequestContent(ServletInputStream input, byte[] buffer) throws IOException {
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) {
System.err.println(result);
if (result.getResponse().getStatus() == 500)
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 DispatcherForwardTest method testContentCanBeReadViaInputStreamAfterForwardWithoutQuery.
@Test
public void testContentCanBeReadViaInputStreamAfterForwardWithoutQuery() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final String query1 = "a=1%20one";
final String form = "c=3%20three";
servlet1 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
req.getRequestDispatcher("/two").forward(req, resp);
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("c"), Matchers.nullValue());
latch.countDown();
}
};
servlet2 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
ServletInputStream input = req.getInputStream();
for (int i = 0; i < form.length(); ++i) checkThat(form.charAt(i) & 0xFFFF, Matchers.equalTo(input.read()));
}
};
prepare();
String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
String response = connector.getResponses(request);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class DispatcherForwardTest method testContentCanBeReadViaInputStreamAfterForwardWithQuery.
@Test
public void testContentCanBeReadViaInputStreamAfterForwardWithQuery() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final String query1 = "a=1%20one";
final String query2 = "b=2%20two";
final String query3 = "b=2%20two&a=1%20one";
final String form = "c=3%20three";
servlet1 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
req.getRequestDispatcher("/two?" + query2).forward(req, resp);
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("c"), Matchers.nullValue());
latch.countDown();
}
};
servlet2 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query3));
ServletInputStream input = req.getInputStream();
for (int i = 0; i < form.length(); ++i) checkThat(form.charAt(i) & 0xFFFF, Matchers.equalTo(input.read()));
checkThat(-1, Matchers.equalTo(input.read()));
}
};
prepare();
String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
String response = connector.getResponses(request);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Aggregations