Search in sources :

Example 1 with HttpVersion

use of org.eclipse.jetty.http.HttpVersion in project jetty.project by eclipse.

the class HttpConnection method normalizeRequest.

protected void normalizeRequest(Request request) {
    HttpVersion version = request.getVersion();
    HttpFields headers = request.getHeaders();
    ContentProvider content = request.getContent();
    ProxyConfiguration.Proxy proxy = destination.getProxy();
    // Make sure the path is there
    String path = request.getPath();
    if (path.trim().length() == 0) {
        path = "/";
        request.path(path);
    }
    URI uri = request.getURI();
    if (proxy instanceof HttpProxy && !HttpClient.isSchemeSecure(request.getScheme()) && uri != null) {
        path = uri.toString();
        request.path(path);
    }
    // If we are HTTP 1.1, add the Host header
    if (version.getVersion() <= 11) {
        if (!headers.containsKey(HttpHeader.HOST.asString()))
            headers.put(getHttpDestination().getHostField());
    }
    // Add content headers
    if (content != null) {
        if (!headers.containsKey(HttpHeader.CONTENT_TYPE.asString())) {
            String contentType = null;
            if (content instanceof ContentProvider.Typed)
                contentType = ((ContentProvider.Typed) content).getContentType();
            if (contentType != null)
                headers.put(HttpHeader.CONTENT_TYPE, contentType);
            else
                headers.put(HttpHeader.CONTENT_TYPE, "application/octet-stream");
        }
        long contentLength = content.getLength();
        if (contentLength >= 0) {
            if (!headers.containsKey(HttpHeader.CONTENT_LENGTH.asString()))
                headers.put(HttpHeader.CONTENT_LENGTH, String.valueOf(contentLength));
        }
    }
    // Cookies
    CookieStore cookieStore = getHttpClient().getCookieStore();
    if (cookieStore != null) {
        StringBuilder cookies = null;
        if (uri != null)
            cookies = convertCookies(cookieStore.get(uri), null);
        cookies = convertCookies(request.getCookies(), cookies);
        if (cookies != null)
            request.header(HttpHeader.COOKIE.asString(), cookies.toString());
    }
    // Authentication
    applyAuthentication(request, proxy != null ? proxy.getURI() : null);
    applyAuthentication(request, uri);
}
Also used : CookieStore(java.net.CookieStore) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) HttpFields(org.eclipse.jetty.http.HttpFields) HttpVersion(org.eclipse.jetty.http.HttpVersion) URI(java.net.URI)

Example 2 with HttpVersion

use of org.eclipse.jetty.http.HttpVersion in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testRequestHasHTTP2Version.

@Test
public void testRequestHasHTTP2Version() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            HttpVersion version = HttpVersion.fromString(request.getProtocol());
            response.setStatus(version == HttpVersion.HTTP_2 ? HttpStatus.OK_200 : HttpStatus.INTERNAL_SERVER_ERROR_500);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).onRequestBegin(request -> {
        if (request.getVersion() != HttpVersion.HTTP_2)
            request.abort(new Exception("Not a HTTP/2 request"));
    }).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpVersion(org.eclipse.jetty.http.HttpVersion) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with HttpVersion

use of org.eclipse.jetty.http.HttpVersion in project jetty.project by eclipse.

the class Request method getProtocol.

/* ------------------------------------------------------------ */
/*
     * @see javax.servlet.ServletRequest#getProtocol()
     */
@Override
public String getProtocol() {
    MetaData.Request metadata = _metaData;
    if (metadata == null)
        return null;
    HttpVersion version = metadata.getHttpVersion();
    if (version == null)
        return null;
    return version.toString();
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) HttpVersion(org.eclipse.jetty.http.HttpVersion)

Aggregations

HttpVersion (org.eclipse.jetty.http.HttpVersion)3 HttpFields (org.eclipse.jetty.http.HttpFields)2 MetaData (org.eclipse.jetty.http.MetaData)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 CookieStore (java.net.CookieStore)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1 TimeUnit (java.util.concurrent.TimeUnit)1