use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientTest method testHTTP10WithKeepAliveAndNoContentLength.
@Test
public void testHTTP10WithKeepAliveAndNoContentLength() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Send the headers at this point, then write the content
response.flushBuffer();
response.getOutputStream().print("TEST");
}
});
FuturePromise<Connection> promise = new FuturePromise<>();
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
destination.newConnection(promise);
try (Connection connection = promise.get(5, TimeUnit.SECONDS)) {
long timeout = 5000;
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(destination.getScheme()).version(HttpVersion.HTTP_1_0).header(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()).timeout(timeout, TimeUnit.MILLISECONDS);
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
ContentResponse response = listener.get(2 * timeout, TimeUnit.MILLISECONDS);
Assert.assertEquals(200, response.getStatus());
// The parser notifies end-of-content and therefore the CompleteListener
// before closing the connection, so we need to wait before checking
// that the connection is closed to avoid races.
Thread.sleep(1000);
Assert.assertTrue(connection.isClosed());
}
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientTest method test_GET_WithParameters_ResponseWithContent.
@Test
public void test_GET_WithParameters_ResponseWithContent() throws Exception {
final String paramName1 = "a";
final String paramName2 = "b";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
ServletOutputStream output = response.getOutputStream();
String paramValue1 = request.getParameter(paramName1);
output.write(paramValue1.getBytes(StandardCharsets.UTF_8));
String paramValue2 = request.getParameter(paramName2);
Assert.assertEquals("", paramValue2);
output.write("empty".getBytes(StandardCharsets.UTF_8));
baseRequest.setHandled(true);
}
});
String value1 = "€";
String paramValue1 = URLEncoder.encode(value1, "UTF-8");
String query = paramName1 + "=" + paramValue1 + "&" + paramName2;
ContentResponse response = client.GET(scheme + "://localhost:" + connector.getLocalPort() + "/?" + query);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
String content = new String(response.getContent(), StandardCharsets.UTF_8);
Assert.assertEquals(value1 + "empty", content);
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class SslBytesServerTest method init.
@Before
public void init() throws Exception {
threadPool = Executors.newCachedThreadPool();
server = new Server();
File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
HttpConnectionFactory httpFactory = new HttpConnectionFactory() {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
return configure(new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {
@Override
protected HttpParser newHttpParser(HttpCompliance compliance) {
return new HttpParser(newRequestHandler(), getHttpConfiguration().getRequestHeaderSize(), compliance) {
@Override
public boolean parseNext(ByteBuffer buffer) {
httpParses.incrementAndGet();
return super.parseNext(buffer);
}
};
}
@Override
protected boolean onReadTimeout() {
final Runnable idleHook = SslBytesServerTest.this.idleHook;
if (idleHook != null)
idleHook.run();
return super.onReadTimeout();
}
}, connector, endPoint);
}
};
httpFactory.getHttpConfiguration().addCustomizer(new SecureRequestCustomizer());
SslConnectionFactory sslFactory = new SslConnectionFactory(sslContextFactory, httpFactory.getProtocol()) {
@Override
protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) {
return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine) {
@Override
protected DecryptedEndPoint newDecryptedEndPoint() {
return new DecryptedEndPoint() {
@Override
public int fill(ByteBuffer buffer) throws IOException {
sslFills.incrementAndGet();
return super.fill(buffer);
}
@Override
public boolean flush(ByteBuffer... appOuts) throws IOException {
sslFlushes.incrementAndGet();
return super.flush(appOuts);
}
};
}
};
}
};
ServerConnector connector = new ServerConnector(server, null, null, null, 1, 1, sslFactory, httpFactory) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
ChannelEndPoint endp = super.newEndPoint(channel, selectSet, key);
serverEndPoint.set(endp);
return endp;
}
};
connector.setIdleTimeout(idleTimeout);
connector.setPort(0);
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
try {
request.setHandled(true);
String contentLength = request.getHeader("Content-Length");
if (contentLength != null) {
int length = Integer.parseInt(contentLength);
ServletInputStream input = httpRequest.getInputStream();
ServletOutputStream output = httpResponse.getOutputStream();
byte[] buffer = new byte[32 * 1024];
while (length > 0) {
int read = input.read(buffer);
if (read < 0)
throw new EOFException();
length -= read;
if (target.startsWith("/echo"))
output.write(buffer, 0, read);
}
}
} catch (IOException x) {
if (!(target.endsWith("suppress_exception")))
throw x;
}
}
});
server.start();
serverPort = connector.getLocalPort();
sslContext = sslContextFactory.getSslContext();
proxy = new SimpleProxy(threadPool, "localhost", serverPort);
proxy.start();
logger.info("proxy:{} <==> server:{}", proxy.getPort(), serverPort);
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientURITest method testPathWithQuery.
@Test
public void testPathWithQuery() throws Exception {
String name = "a";
String value = "1";
final String query = name + "=" + value;
final String path = "/path";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
Assert.assertEquals(query, request.getQueryString());
}
});
String pathQuery = path + "?" + query;
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(pathQuery);
Assert.assertEquals(path, request.getPath());
Assert.assertEquals(query, request.getQuery());
Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
Fields params = request.getParams();
Assert.assertEquals(1, params.getSize());
Assert.assertEquals(value, params.get(name).getValue());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientURITest method testPathWithParam.
@Test
public void testPathWithParam() throws Exception {
String name = "a";
String value = "1";
final String query = name + "=" + value;
final String path = "/path";
String pathQuery = path + "?" + query;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
Assert.assertEquals(query, request.getQueryString());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path).param(name, value);
Assert.assertEquals(path, request.getPath());
Assert.assertEquals(query, request.getQuery());
Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
Fields params = request.getParams();
Assert.assertEquals(1, params.getSize());
Assert.assertEquals(value, params.get(name).getValue());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Aggregations