use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.
the class ClientGeneratorTest method testGenerateRequestHeaders.
@Test
public void testGenerateRequestHeaders() throws Exception {
HttpFields fields = new HttpFields();
// Short name, short value
final String shortShortName = "REQUEST_METHOD";
final String shortShortValue = "GET";
fields.put(new HttpField(shortShortName, shortShortValue));
// Short name, long value
final String shortLongName = "REQUEST_URI";
// Be sure it's longer than 127 chars to test the large value
final String shortLongValue = "/api/0.6/map?bbox=-64.217736,-31.456810,-64.187736,-31.432322,filler=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
fields.put(new HttpField(shortLongName, shortLongValue));
// Long name, short value
// Be sure it's longer than 127 chars to test the large name
final String longShortName = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
final String longShortValue = "api.openstreetmap.org";
fields.put(new HttpField(longShortName, longShortValue));
// Long name, long value
char[] chars = new char[ClientGenerator.MAX_PARAM_LENGTH];
Arrays.fill(chars, 'z');
final String longLongName = new String(chars);
final String longLongValue = new String(chars);
fields.put(new HttpField(longLongName, longLongValue));
ByteBufferPool byteBufferPool = new MappedByteBufferPool();
ClientGenerator generator = new ClientGenerator(byteBufferPool);
final int id = 13;
Generator.Result result = generator.generateRequestHeaders(id, fields, null);
// Use the fundamental theorem of arithmetic to test the results.
// This way we know onHeader() has been called the right number of
// times with the right arguments, and so onHeaders().
final int[] primes = new int[] { 2, 3, 5, 7, 11 };
int value = 1;
for (int prime : primes) value *= prime;
final AtomicInteger params = new AtomicInteger(1);
ServerParser parser = new ServerParser(new ServerParser.Listener.Adapter() {
@Override
public void onHeader(int request, HttpField field) {
Assert.assertEquals(id, request);
switch(field.getName()) {
case shortShortName:
Assert.assertEquals(shortShortValue, field.getValue());
params.set(params.get() * primes[0]);
break;
case shortLongName:
Assert.assertEquals(shortLongValue, field.getValue());
params.set(params.get() * primes[1]);
break;
case longShortName:
Assert.assertEquals(longShortValue, field.getValue());
params.set(params.get() * primes[2]);
break;
default:
Assert.assertEquals(longLongName, field.getName());
Assert.assertEquals(longLongValue, field.getValue());
params.set(params.get() * primes[3]);
break;
}
}
@Override
public void onHeaders(int request) {
Assert.assertEquals(id, request);
params.set(params.get() * primes[4]);
}
});
for (ByteBuffer buffer : result.getByteBuffers()) {
parser.parse(buffer);
Assert.assertFalse(buffer.hasRemaining());
}
Assert.assertEquals(value, params.get());
// Parse again byte by byte
params.set(1);
for (ByteBuffer buffer : result.getByteBuffers()) {
buffer.flip();
while (buffer.hasRemaining()) parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
Assert.assertFalse(buffer.hasRemaining());
}
Assert.assertEquals(value, params.get());
}
use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.
the class Request method findServerPort.
/* ------------------------------------------------------------ */
private int findServerPort() {
MetaData.Request metadata = _metaData;
// Return host from header field
HttpField host = metadata == null ? null : metadata.getFields().getField(HttpHeader.HOST);
if (host != null) {
// TODO is this needed now?
HostPortHttpField authority = (host instanceof HostPortHttpField) ? ((HostPortHttpField) host) : new HostPortHttpField(host.getValue());
metadata.getURI().setAuthority(authority.getHost(), authority.getPort());
return authority.getPort();
}
// Return host from connection
if (_channel != null)
return getLocalPort();
return -1;
}
use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.
the class Response method reset.
public void reset(boolean preserveCookies) {
resetForForward();
_status = 200;
_reason = null;
_contentLength = -1;
List<HttpField> cookies = preserveCookies ? _fields.stream().filter(f -> f.getHeader() == HttpHeader.SET_COOKIE).collect(Collectors.toList()) : null;
_fields.clear();
String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
if (connection != null) {
for (String value : StringUtil.csvSplit(null, connection, 0, connection.length())) {
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
if (cb != null) {
switch(cb) {
case CLOSE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
break;
case KEEP_ALIVE:
if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
break;
case TE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
break;
default:
}
}
}
}
if (preserveCookies)
cookies.forEach(f -> _fields.add(f));
else {
Request request = getHttpChannel().getRequest();
HttpSession session = request.getSession(false);
if (session != null && session.isNew()) {
SessionHandler sh = request.getSessionHandler();
if (sh != null) {
HttpCookie c = sh.getSessionCookie(session, request.getContextPath(), request.isSecure());
if (c != null)
addCookie(c);
}
}
}
}
use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.
the class HttpClient method copyRequest.
protected Request copyRequest(HttpRequest oldRequest, URI newURI) {
Request newRequest = newHttpRequest(oldRequest.getConversation(), newURI);
newRequest.method(oldRequest.getMethod()).version(oldRequest.getVersion()).content(oldRequest.getContent()).idleTimeout(oldRequest.getIdleTimeout(), TimeUnit.MILLISECONDS).timeout(oldRequest.getTimeout(), TimeUnit.MILLISECONDS).followRedirects(oldRequest.isFollowRedirects());
for (HttpField field : oldRequest.getHeaders()) {
HttpHeader header = field.getHeader();
// We have a new URI, so skip the host header if present.
if (HttpHeader.HOST == header)
continue;
// Remove expectation headers.
if (HttpHeader.EXPECT == header)
continue;
// Remove cookies.
if (HttpHeader.COOKIE == header)
continue;
// Remove authorization headers.
if (HttpHeader.AUTHORIZATION == header || HttpHeader.PROXY_AUTHORIZATION == header)
continue;
String name = field.getName();
String value = field.getValue();
if (!newRequest.getHeaders().contains(name, value))
newRequest.header(name, value);
}
return newRequest;
}
use of org.eclipse.jetty.http.HttpField in project jetty.project by eclipse.
the class ResponseNotifier method forwardFailure.
public void forwardFailure(List<Response.ResponseListener> listeners, Response response, Throwable failure) {
notifyBegin(listeners, response);
for (Iterator<HttpField> iterator = response.getHeaders().iterator(); iterator.hasNext(); ) {
HttpField field = iterator.next();
if (!notifyHeader(listeners, response, field))
iterator.remove();
}
notifyHeaders(listeners, response);
if (response instanceof ContentResponse)
notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse) response).getContent()), Callback.NOOP);
notifyFailure(listeners, response, failure);
}
Aggregations