use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientURITest method testAsteriskFormTarget.
@Test
public void testAsteriskFormTarget() throws Exception {
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("*", target);
Assert.assertEquals("*", request.getPathInfo());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.OPTIONS).scheme(scheme).path("*").timeout(5, TimeUnit.SECONDS);
Assert.assertEquals("*", request.getPath());
Assert.assertNull(request.getQuery());
Fields params = request.getParams();
Assert.assertEquals(0, params.getSize());
Assert.assertNull(request.getURI());
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 TerminatingRegexRuleTest method init.
@Before
public void init() throws Exception {
rewriteHandler = new RewriteHandler();
rewriteHandler.setServer(_server);
rewriteHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setStatus(HttpStatus.CREATED_201);
request.setAttribute("target", target);
request.setAttribute("URI", request.getRequestURI());
request.setAttribute("info", request.getPathInfo());
}
});
rewriteHandler.start();
TerminatingRegexRule rule1 = new TerminatingRegexRule();
rule1.setRegex("^/login.jsp$");
rewriteHandler.addRule(rule1);
RedirectRegexRule rule2 = new RedirectRegexRule();
rule2.setRegex("^/login.*$");
rule2.setReplacement("http://login.company.com/");
rewriteHandler.addRule(rule2);
start(false);
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class AbstractRuleTestCase method start.
protected void start(final boolean isSecure) throws Exception {
_connector = new LocalConnector(_server);
_connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().addCustomizer(new HttpConfiguration.Customizer() {
@Override
public void customize(Connector connector, HttpConfiguration channelConfig, Request request) {
request.setSecure(isSecure);
}
});
_server.setConnectors(new Connector[] { _connector });
_server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
_request = baseRequest;
_response = _request.getResponse();
try {
_latch.await();
} catch (InterruptedException e) {
throw new ServletException(e);
}
}
});
_server.start();
_latch = new CountDownLatch(1);
_connector.executeRequest("GET / HTTP/1.0\nCookie: set=already\n\n");
while (_response == null) Thread.sleep(1);
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class RewriteHandlerTest method init.
@Before
public void init() throws Exception {
_handler = new RewriteHandler();
_handler.setServer(_server);
_handler.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setStatus(201);
request.setAttribute("target", target);
request.setAttribute("URI", request.getRequestURI());
request.setAttribute("info", request.getPathInfo());
}
});
_handler.start();
_rule1 = new RewritePatternRule();
_rule1.setPattern("/aaa/*");
_rule1.setReplacement("/bbb");
_rule2 = new RewritePatternRule();
_rule2.setPattern("/bbb/*");
_rule2.setReplacement("/ccc");
_rule3 = new RewritePatternRule();
_rule3.setPattern("/ccc/*");
_rule3.setReplacement("/ddd");
_rule4 = new RewriteRegexRule();
_rule4.setRegex("/xxx/(.*)");
_rule4.setReplacement("/$1/zzz");
_handler.setRules(new Rule[] { _rule1, _rule2, _rule3, _rule4 });
start(false);
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class ServerConnectorTimeoutTest method testHttpWriteIdleTimeout.
@Test
public void testHttpWriteIdleTimeout() throws Exception {
_httpConfiguration.setBlockingTimeout(500);
configureServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
client.setSoTimeout(10000);
Assert.assertFalse(client.isClosed());
final OutputStream os = client.getOutputStream();
final InputStream is = client.getInputStream();
final StringBuilder response = new StringBuilder();
CompletableFuture<Void> responseFuture = CompletableFuture.runAsync(() -> {
try (InputStreamReader reader = new InputStreamReader(is, UTF_8)) {
int c;
while ((c = reader.read()) != -1) {
response.append((char) c);
}
} catch (IOException e) {
// Valid path (as connection is forcibly closed)
// t.printStackTrace(System.err);
}
});
CompletableFuture<Void> requestFuture = CompletableFuture.runAsync(() -> {
try {
os.write(("POST /echo HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 20\r\n" + "\r\n").getBytes("utf-8"));
os.flush();
os.write("123456789\n".getBytes("utf-8"));
os.flush();
TimeUnit.SECONDS.sleep(1);
os.write("=========\n".getBytes("utf-8"));
os.flush();
} catch (InterruptedException | IOException e) {
// Valid path, as write of second half of content can fail
// e.printStackTrace(System.err);
}
});
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
requestFuture.get(2, TimeUnit.SECONDS);
responseFuture.get(3, TimeUnit.SECONDS);
Assert.assertThat(response.toString(), containsString(" 500 "));
Assert.assertThat(response.toString(), Matchers.not(containsString("=========")));
}
}
Aggregations