use of org.eclipse.jetty.http.HttpURI in project jetty.project by eclipse.
the class ProxyProtocolTest method test_PROXY_GET_v1.
@Test
public void test_PROXY_GET_v1() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
Assert.assertEquals("1.2.3.4", request.getRemoteAddr());
Assert.assertEquals(1111, request.getRemotePort());
Assert.assertEquals("5.6.7.8", request.getLocalAddr());
Assert.assertEquals(2222, request.getLocalPort());
} catch (Throwable th) {
th.printStackTrace();
response.setStatus(500);
}
baseRequest.setHandled(true);
}
});
String request1 = "PROXY TCP4 1.2.3.4 5.6.7.8 1111 2222\r\n";
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
channel.write(ByteBuffer.wrap(request1.getBytes(StandardCharsets.UTF_8)));
FuturePromise<Session> promise = new FuturePromise<>();
client.accept(null, channel, new Session.Listener.Adapter(), promise);
Session session = promise.get(5, TimeUnit.SECONDS);
HttpFields fields = new HttpFields();
String uri = "http://localhost:" + connector.getLocalPort() + "/";
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http.HttpURI in project jetty.project by eclipse.
the class Client method main.
public static void main(String[] args) throws Exception {
HTTP2Client client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
client.addBean(sslContextFactory);
client.start();
String host = "webtide.com";
int port = 443;
FuturePromise<Session> sessionPromise = new FuturePromise<>();
client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
Session session = sessionPromise.get(5, TimeUnit.SECONDS);
HttpFields requestFields = new HttpFields();
requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
final Phaser phaser = new Phaser(2);
session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
System.err.println(frame);
if (frame.isEndStream())
phaser.arrive();
}
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
System.err.println(frame);
callback.succeeded();
if (frame.isEndStream())
phaser.arrive();
}
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
System.err.println(frame);
phaser.register();
return this;
}
});
phaser.awaitAdvanceInterruptibly(phaser.arrive(), 5, TimeUnit.SECONDS);
client.stop();
}
use of org.eclipse.jetty.http.HttpURI in project jetty.project by eclipse.
the class AbstractDoSFilterTest method startServer.
public void startServer(Class<? extends Filter> filter) throws Exception {
_tester = new ServletTester("/ctx");
HttpURI uri = new HttpURI(_tester.createConnector(true));
_host = uri.getHost();
_port = uri.getPort();
_tester.getContext().addServlet(TestServlet.class, "/*");
FilterHolder dosFilter = _tester.getContext().addFilter(filter, "/dos/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
dosFilter.setInitParameter("maxRequestsPerSec", "4");
dosFilter.setInitParameter("delayMs", "200");
dosFilter.setInitParameter("throttledRequests", "1");
dosFilter.setInitParameter("waitMs", "10");
dosFilter.setInitParameter("throttleMs", "4000");
dosFilter.setInitParameter("remotePort", "false");
dosFilter.setInitParameter("insertHeaders", "true");
FilterHolder timeoutFilter = _tester.getContext().addFilter(filter, "/timeout/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
timeoutFilter.setInitParameter("maxRequestsPerSec", "4");
timeoutFilter.setInitParameter("delayMs", "200");
timeoutFilter.setInitParameter("throttledRequests", "1");
timeoutFilter.setInitParameter("waitMs", "10");
timeoutFilter.setInitParameter("throttleMs", "4000");
timeoutFilter.setInitParameter("remotePort", "false");
timeoutFilter.setInitParameter("insertHeaders", "true");
timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + "");
_tester.start();
}
use of org.eclipse.jetty.http.HttpURI in project jetty.project by eclipse.
the class Dispatcher method forward.
protected void forward(ServletRequest request, ServletResponse response, DispatcherType dispatch) throws ServletException, IOException {
Request baseRequest = Request.getBaseRequest(request);
Response base_response = baseRequest.getResponse();
base_response.resetForForward();
if (!(request instanceof HttpServletRequest))
request = new ServletRequestHttpWrapper(request);
if (!(response instanceof HttpServletResponse))
response = new ServletResponseHttpWrapper(response);
final HttpURI old_uri = baseRequest.getHttpURI();
final String old_context_path = baseRequest.getContextPath();
final String old_servlet_path = baseRequest.getServletPath();
final String old_path_info = baseRequest.getPathInfo();
final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
final Attributes old_attr = baseRequest.getAttributes();
final DispatcherType old_type = baseRequest.getDispatcherType();
try {
baseRequest.setDispatcherType(dispatch);
if (_named != null) {
_contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
} else {
ForwardAttributes attr = new ForwardAttributes(old_attr);
//for queryString is allowed to be null, but cannot be null for the other values.
if (old_attr.getAttribute(FORWARD_REQUEST_URI) != null) {
attr._pathInfo = (String) old_attr.getAttribute(FORWARD_PATH_INFO);
attr._query = (String) old_attr.getAttribute(FORWARD_QUERY_STRING);
attr._requestURI = (String) old_attr.getAttribute(FORWARD_REQUEST_URI);
attr._contextPath = (String) old_attr.getAttribute(FORWARD_CONTEXT_PATH);
attr._servletPath = (String) old_attr.getAttribute(FORWARD_SERVLET_PATH);
} else {
attr._pathInfo = old_path_info;
attr._query = old_uri.getQuery();
attr._requestURI = old_uri.getPath();
attr._contextPath = old_context_path;
attr._servletPath = old_servlet_path;
}
HttpURI uri = new HttpURI(old_uri.getScheme(), old_uri.getHost(), old_uri.getPort(), _uri.getPath(), _uri.getParam(), _uri.getQuery(), _uri.getFragment());
baseRequest.setHttpURI(uri);
baseRequest.setContextPath(_contextHandler.getContextPath());
baseRequest.setServletPath(null);
baseRequest.setPathInfo(_pathInContext);
if (_uri.getQuery() != null || old_uri.getQuery() != null)
baseRequest.mergeQueryParameters(old_uri.getQuery(), _uri.getQuery(), true);
baseRequest.setAttributes(attr);
_contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
if (!baseRequest.getHttpChannelState().isAsync())
commitResponse(response, baseRequest);
}
} finally {
baseRequest.setHttpURI(old_uri);
baseRequest.setContextPath(old_context_path);
baseRequest.setServletPath(old_servlet_path);
baseRequest.setPathInfo(old_path_info);
baseRequest.setQueryParameters(old_query_params);
baseRequest.resetParameters();
baseRequest.setAttributes(old_attr);
baseRequest.setDispatcherType(old_type);
}
}
use of org.eclipse.jetty.http.HttpURI in project jetty.project by eclipse.
the class PatternRuleTest method assertMatch.
private void assertMatch(boolean flag, String[] matchCase) throws IOException {
_rule.setPattern(matchCase[0]);
final String uri = matchCase[1];
String result = _rule.matchAndApply(uri, new Request(null, null) {
{
setMetaData(new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_1_0, new HttpFields()));
}
}, null);
assertEquals("pattern: " + matchCase[0] + " uri: " + matchCase[1], flag, result != null);
}
Aggregations