diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java index 64f1b22dfdd..28a3198d3b3 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java @@ -205,7 +205,7 @@ public class HttpConnection extends AbstractConnection implements Connection } case POST: { - request.header(HttpHeader.CONTENT_TYPE.asString(), MimeTypes.Type.FORM_ENCODED.asString()); + request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()); request.content(new StringContentProvider(params.toString())); break; } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java index 75d474e0d41..3bbbaa2ddea 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java @@ -485,7 +485,7 @@ public class HttpDestination implements Destination, AutoCloseable, Dumpable .scheme(HttpScheme.HTTP.asString()) .method(HttpMethod.CONNECT) .path(target) - .header(HttpHeader.HOST.asString(), target) + .header(HttpHeader.HOST, target) .timeout(client.getConnectTimeout(), TimeUnit.MILLISECONDS); connection.send(connect, new Response.CompleteListener() { diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java index a6646be194e..6841f941f0f 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java @@ -223,6 +223,16 @@ public class HttpRequest implements Request return this; } + @Override + public Request header(HttpHeader header, String value) + { + if (value == null) + headers.remove(header); + else + headers.add(header, value); + return this; + } + @Override public Request attribute(String name, Object value) { @@ -371,7 +381,7 @@ public class HttpRequest implements Request public Request content(ContentProvider content, String contentType) { if (contentType != null) - header(HttpHeader.CONTENT_TYPE.asString(), contentType); + header(HttpHeader.CONTENT_TYPE, contentType); this.content = content; return this; } @@ -386,7 +396,7 @@ public class HttpRequest implements Request public Request file(Path file, String contentType) throws IOException { if (contentType != null) - header(HttpHeader.CONTENT_TYPE.asString(), contentType); + header(HttpHeader.CONTENT_TYPE, contentType); return content(new PathContentProvider(file)); } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/api/Request.java b/jetty-client/src/main/java/org/eclipse/jetty/client/api/Request.java index f5c6705d54e..f78447cfc5f 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/api/Request.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/api/Request.java @@ -32,6 +32,7 @@ import java.util.concurrent.TimeoutException; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.util.InputStreamResponseListener; import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.util.Fields; @@ -135,6 +136,13 @@ public interface Request */ Request header(String name, String value); + /** + * @param header the header name + * @param value the value of the header + * @return this request object + */ + Request header(HttpHeader header, String value); + /** * @param name the name of the attribute * @param value the value of the attribute diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java index 33e97580242..6769e85b23d 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java @@ -99,7 +99,7 @@ public class BasicAuthentication implements Authentication public void apply(Request request) { if (request.getURI().toString().startsWith(uri.toString())) - request.header(HttpHeader.AUTHORIZATION.asString(), value); + request.header(HttpHeader.AUTHORIZATION, value); } @Override diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java index 7f03c2b2ff6..61604f62425 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java @@ -262,7 +262,7 @@ public class DigestAuthentication implements Authentication } value.append(", response=\"").append(hashA3).append("\""); - request.header(HttpHeader.AUTHORIZATION.asString(), value.toString()); + request.header(HttpHeader.AUTHORIZATION, value.toString()); } private String nextNonceCount() diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java index b6d83e3bb7a..e281ac43ff8 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java @@ -84,7 +84,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(contents)) .timeout(5, TimeUnit.SECONDS) .send(); @@ -124,7 +124,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest byte[] content2 = new byte[16384]; ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content1, content2) { @Override @@ -176,7 +176,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content1, content2)) .send(new BufferingResponseListener() { @@ -227,7 +227,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest .scheme(scheme) .method(HttpMethod.POST) .path("/continue") - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content)) .send(new BufferingResponseListener() { @@ -276,7 +276,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest .scheme(scheme) .method(HttpMethod.POST) .path("/redirect") - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content)) .send(new BufferingResponseListener() { @@ -322,7 +322,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content)) .send(new BufferingResponseListener() { @@ -369,7 +369,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content)) .send(new BufferingResponseListener() { @@ -433,7 +433,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(new BytesContentProvider(content)) .send(new BufferingResponseListener() { @@ -480,7 +480,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest DeferredContentProvider content = new DeferredContentProvider(); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(content) .send(new BufferingResponseListener() { @@ -529,7 +529,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1)); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(content) .send(new BufferingResponseListener() { @@ -602,7 +602,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .onRequestHeaders(new org.eclipse.jetty.client.api.Request.HeadersListener() { @Override @@ -676,7 +676,7 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest final CountDownLatch latch = new CountDownLatch(1); client.newRequest("localhost", connector.getLocalPort()) .scheme(scheme) - .header(HttpHeader.EXPECT.asString(), HttpHeaderValue.CONTINUE.asString()) + .header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()) .content(content) .send(new BufferingResponseListener() { diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java index 5e26a9ab740..601fd1c49ce 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientLoadTest.java @@ -38,6 +38,7 @@ import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.BytesContentProvider; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.server.handler.AbstractHandler; @@ -132,7 +133,7 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest // Choose randomly whether to close the connection on the client or on the server if (!ssl && random.nextBoolean()) - request.header("Connection", "close"); + request.header(HttpHeader.CONNECTION, "close"); else if (!ssl && random.nextBoolean()) request.header("X-Close", "true"); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java index 0dfed4d0295..4d2aa2f4381 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpConnectionLifecycleTest.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; - import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -185,7 +184,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest public void onBegin(Request request) { // Remove the host header, this will make the request invalid - request.header(HttpHeader.HOST.asString(), null); + request.header(HttpHeader.HOST, null); } @Override @@ -245,7 +244,7 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest public void onBegin(Request request) { // Remove the host header, this will make the request invalid - request.header(HttpHeader.HOST.asString(), null); + request.header(HttpHeader.HOST, null); } @Override diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java index b39f3af18b5..84999dea5af 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ProxyServlet.java @@ -44,6 +44,7 @@ import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.InputStreamContentProvider; import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.server.handler.ContextHandler; @@ -397,14 +398,14 @@ public class ProxyServlet extends HttpServlet // Force the Host header if configured if (_hostHeader != null) - proxyRequest.header("Host", _hostHeader); + proxyRequest.header(HttpHeader.HOST, _hostHeader); // Add proxy headers - proxyRequest.header("Via", "http/1.1 " + _viaHost); - proxyRequest.header("X-Forwarded-For", request.getRemoteAddr()); - proxyRequest.header("X-Forwarded-Proto", request.getScheme()); - proxyRequest.header("X-Forwarded-Host", request.getHeader("Host")); - proxyRequest.header("X-Forwarded-Server", request.getLocalName()); + proxyRequest.header(HttpHeader.VIA, "http/1.1 " + _viaHost); + proxyRequest.header(HttpHeader.X_FORWARDED_FOR, request.getRemoteAddr()); + proxyRequest.header(HttpHeader.X_FORWARDED_PROTO, request.getScheme()); + proxyRequest.header(HttpHeader.X_FORWARDED_HOST, request.getHeader(HttpHeader.HOST.asString())); + proxyRequest.header(HttpHeader.X_FORWARDED_SERVER, request.getLocalName()); proxyRequest.content(new InputStreamContentProvider(request.getInputStream()) { diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/PushStrategyBenchmarkTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/PushStrategyBenchmarkTest.java index 6c29ca02cbb..17f0e17d8df 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/PushStrategyBenchmarkTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/PushStrategyBenchmarkTest.java @@ -29,7 +29,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -38,6 +37,7 @@ import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -153,7 +153,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest ++result; httpClient.newRequest("localhost", connector.getLocalPort()) .path(path) - .header("Referer", referrer) + .header(HttpHeader.REFERER, referrer) .send(new TestListener()); } for (int i = 0; i < jsResources.length; ++i) @@ -162,7 +162,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest ++result; httpClient.newRequest("localhost", connector.getLocalPort()) .path(path) - .header("Referer", referrer) + .header(HttpHeader.REFERER, referrer) .send(new TestListener()); } for (int i = 0; i < pngResources.length; ++i) @@ -171,7 +171,7 @@ public class PushStrategyBenchmarkTest extends AbstractHTTPSPDYTest ++result; httpClient.newRequest("localhost", connector.getLocalPort()) .path(path) - .header("Referer", referrer) + .header(HttpHeader.REFERER, referrer) .send(new TestListener()); }