Package org.glassfish.grizzly.http

Examples of org.glassfish.grizzly.http.HttpRequestPacket$Builder


                return true;
            }
        }

        try {
            final HttpRequestPacket httpRequestPacket = request.getRequest();
            final RequestURIRef requestURIRef = httpRequestPacket.getRequestURIRef();
            requestURIRef.setDefaultURIEncoding(requestURIEncoding);

            if (decodeURL) {
                // URI decoding
                try {
View Full Code Here


            final HttpContext context = HttpContext.get(ctx);
            Request handlerRequest = httpRequestInProgress.get(context);

            if (handlerRequest == null) {
                // It's a new HTTP request
                final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader();
                final HttpResponsePacket response = request.getResponse();
               
                activeRequestsCounter.incrementAndGet();
               
                handlerRequest = Request.create();
                handlerRequest.parameters.setLimit(config.getMaxRequestParameters());
                httpRequestInProgress.set(context, handlerRequest);
                final Response handlerResponse = handlerRequest.getResponse();

                handlerRequest.initialize(request, ctx, this);
                handlerResponse.initialize(handlerRequest, response,
                        ctx, suspendedResponseQueue, this);

                handlerRequest.addAfterServiceListener(flushResponseHandler);
               
                HttpServerProbeNotifier.notifyRequestReceive(this, connection,
                        handlerRequest);

                boolean wasSuspended = false;
               
                try {
                    ctx.setMessage(handlerResponse);

                    if (!isShuttingDown) {
                        if (!config.isPassTraceRequest()
                                && request.getMethod() == Method.TRACE) {
                            onTraceRequest(handlerRequest, handlerResponse);
                        } else {
                            final HttpHandler httpHandlerLocal = httpHandler;
                            if (httpHandlerLocal != null) {
                                wasSuspended = !httpHandlerLocal.doHandle(
                                        handlerRequest, handlerResponse);
                            }
                        }
                    } else { // if we're in the shutting down phase - serve shutdown page and exit
                        handlerResponse.getResponse().getProcessingState().setError(true);
                        HtmlHelper.setErrorAndSendErrorPage(
                                handlerRequest, handlerResponse,
                                config.getDefaultErrorPageGenerator(),
                                503, HttpStatus.SERVICE_UNAVAILABLE_503.getReasonPhrase(),
                                "The server is being shutting down...", null);
                    }
                } catch (Exception t) {
                    LOGGER.log(Level.WARNING, "Exception during HttpHandler invocation", t);
                   
                    request.getProcessingState().setError(true);
                   
                    if (!response.isCommitted()) {
                            HtmlHelper.setErrorAndSendErrorPage(
                                    handlerRequest, handlerResponse,
                                    config.getDefaultErrorPageGenerator(),
View Full Code Here

        response.finish();
        request.onAfterService();
       
        HttpServerProbeNotifier.notifyRequestComplete(this, connection, response);
       
        final HttpRequestPacket httpRequest = request.getRequest();
        final boolean isBroken = httpRequest.isContentBroken();
       
        // Suspend state is cancelled - it means normal processing might have
        // been broken. We don't want to reuse Request and Response in this state,
        // cause there still might be threads referencing them.
        if (response.suspendState != Response.SuspendState.CANCELLED) {
View Full Code Here

    @Override
    public NextAction handleRead(final FilterChainContext ctx) throws IOException {

        final HttpContent requestContent = ctx.getMessage();
        final HttpRequestPacket request = (HttpRequestPacket) requestContent.getHttpHeader();

        if (fileCache.isEnabled() && Method.GET.equals(request.getMethod())) {
            final FileCacheEntry cacheEntry = fileCache.get(request);
            if (cacheEntry != null) {
                final HttpResponsePacket response = request.getResponse();
                prepareResponse(cacheEntry, response);
               
                if (response.getStatus() != 200) {
                    // The cache hit - return empty response
                    ctx.write(HttpContent.builder(response)
                            .content(Buffers.EMPTY_BUFFER)
                            .last(true)
                            .build());
                   
                    return flush(ctx);
                }

                // check if we can send plain or compressed data back.
                // depends on client request headers and file cache entry
                final boolean isServeCompressed =
                        cacheEntry.canServeCompressed(request);
               
                // The client doesn't have this resource cached, so
                // we have to send entire payload
                prepareResponseWithPayload(cacheEntry, response,
                        isServeCompressed);

                if (cacheEntry.type != CacheType.FILE) {
                    // the payload is available in a ByteBuffer
                    final Buffer buffer = Buffers.wrap(ctx.getMemoryManager(),
                            cacheEntry.getByteBuffer(isServeCompressed));

                    ctx.write(HttpContent.builder(response)
                            .content(buffer)
                            .last(true)
                            .build());

                    return flush(ctx);
                }
               
                if (fileCache.isFileSendEnabled() && !request.isSecure()) {
                    return sendFileZeroCopy(ctx, response, cacheEntry,
                            isServeCompressed);
                } else {
                    return sendFileUsingBuffers(ctx, response, cacheEntry,
                            isServeCompressed);
View Full Code Here

        ctx.write(HttpContent.builder(responsePacket).build());
    }

    private static UpgradeRequest createWebSocketRequest(final HttpContent requestContent) {

        final HttpRequestPacket requestPacket = (HttpRequestPacket) requestContent.getHttpHeader();

        final RequestContext requestContext = RequestContext.Builder.create()
                .requestURI(URI.create(requestPacket.getRequestURI()))
                .queryString(requestPacket.getQueryString())
                .secure(requestPacket.isSecure())
                .build();

        for (String name : requestPacket.getHeaders().names()) {
            final List<String> values = requestContext.getHeaders().get(name);
            if (values == null) {
                requestContext.getHeaders().put(name, Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            } else {
                values.addAll(Utils.parseHeaderValue(requestPacket.getHeader(name).trim()));
            }
        }

        return requestContext;
    }
View Full Code Here

        @Override
        public void onErrorEvent(Connection connection, HttpPacket packet, Throwable error) {
            if (accessLoggingEnabled) {
                if (packet instanceof HttpRequestPacket) {

                    final HttpRequestPacket requestPacket = (HttpRequestPacket) packet;
                    final HttpResponsePacket responsePacket = requestPacket.getResponse();

                    // 400 should be hardcoded since the response status isn't available for bad requests
                    responsePacket.setStatus(HttpStatus.BAD_REQUEST_400);

                    org.glassfish.grizzly.http.server.Request request = org.glassfish.grizzly.http.server.Request.create();
View Full Code Here

    // ----------------------------------------------------- Private Methods

    private static void recycleRequestResponsePackets(final Connection c, final HttpResponsePacket response) {
        if (!Utils.isSpdyConnection(c)) {
            HttpRequestPacket request = response.getRequest();
            request.setExpectContent(false);
            response.recycle();
            request.recycle();
        }
    }
View Full Code Here

        if (isUpgradeRequest(httpTxContext.getHandler()) && isWebSocket) {
            httpTxContext.setWSRequest(true);
            convertToUpgradeRequest(httpTxContext);
        }

        HttpRequestPacket requestPacket = requestCache.poll();
        if (requestPacket == null) {
            requestPacket = new HttpRequestPacketImpl();
        }
       
        final Method method = Method.valueOf(request.getMethod());
       
        requestPacket.setMethod(method);
        requestPacket.setProtocol(Protocol.HTTP_1_1);

        // Special handling for CONNECT.
        if (method == Method.CONNECT) {
            final int port = uri.getPort();
            requestPacket.setRequestURI(uri.getHost() + ':' + (port == -1 ? 443 : port));
        } else if ((secure || isWebSocket) && config.isUseRelativeURIsWithConnectProxies()) {
            requestPacket.setRequestURI(getNonEmptyPath(uri));
        } else {
            requestPacket.setRequestURI(uri.toUrl());
        }

        final BodyHandler bodyHandler = isPayloadAllowed(method) ?
                bodyHandlerFactory.getBodyHandler(request) :
                null;
       
        if (bodyHandler != null) {
            final long contentLength = request.getContentLength();
            if (contentLength >= 0) {
                requestPacket.setContentLengthLong(contentLength);
                requestPacket.setChunked(false);
            } else {
                requestPacket.setChunked(true);
            }
        }

        if (httpTxContext.isWSRequest()) {
            try {
                final URI wsURI = httpTxContext.getWsRequestURI().toJavaNetURI();
                httpTxContext.setProtocolHandler(Version.RFC6455.createHandler(true));
                httpTxContext.setHandshake(httpTxContext.getProtocolHandler().createHandShake(wsURI));
                requestPacket = (HttpRequestPacket) httpTxContext.getHandshake().composeHeaders().getHttpHeader();
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException("Invalid WS URI: " + httpTxContext.getWsRequestURI());
            }
        }

        requestPacket.setSecure(secure);
        addQueryString(request, requestPacket);
        addHostHeader(request, uri, requestPacket);
        addGeneralHeaders(request, requestPacket);
        addCookies(request, requestPacket);
        addAuthorizationHeader(request, requestPacket);

        initTransferCompletionHandler(request, httpTxContext.getHandler());

        final HttpRequestPacket requestPacketLocal = requestPacket;
        FilterChainContext sendingCtx = ctx;

        if (secure) {
            // Check to see if the ProtocolNegotiator has given
            // us a different FilterChain to use. If so, we need
            // use a different FilterChainContext when invoking sendRequest().
            sendingCtx = checkAndHandleFilterChainUpdate(ctx, sendingCtx);
        }
        final Connection c = ctx.getConnection();
        final HttpContext httpCtx;
        if (!Utils.isSpdyConnection(c)) {
            httpCtx = HttpContext.newInstance(c, c, c, requestPacketLocal);
        } else {
            SpdySession session = SpdySession.get(c);
            final Lock lock = session.getNewClientStreamLock();
            try {
                lock.lock();
                SpdyStream stream = session.openStream(requestPacketLocal, session.getNextLocalStreamId(), 0, 0, 0, false,
                        !requestPacketLocal.isExpectContent());
                httpCtx = HttpContext.newInstance(stream, stream, stream, requestPacketLocal);
            } finally {
                lock.unlock();
            }
        }
        httpCtx.attach(ctx);
        HttpTxContext.set(ctx, httpTxContext);
        requestPacketLocal.getProcessingState().setHttpContext(httpCtx);
        requestPacketLocal.setConnection(c);
       
        return sendRequest(sendingCtx, request, requestPacketLocal,
                wrapWithExpectHandlerIfNeeded(bodyHandler, requestPacket));
    }
View Full Code Here

    @Override
    public NextAction handleWrite(FilterChainContext ctx) throws IOException {
        final Object msg = ctx.getMessage();
        if (HttpPacket.isHttp(msg)) {
            HttpPacket httpPacket = (HttpPacket) msg;
            final HttpRequestPacket request = (HttpRequestPacket) httpPacket.getHttpHeader();
            if (!request.isCommitted()) {
                HttpTxContext context = HttpTxContext.get(ctx);
                assert (context != null);
                Request req = context.getRequest();
                if (!secure) {
                    request.setRequestURI(req.getUrl());
                }
                addProxyHeaders(getRealm(req), request);
            }
        }
       
View Full Code Here

    }

    public boolean onHttpHeaderParsed(final HttpHeader httpHeader,
            final Buffer buffer, final FilterChainContext ctx) {
        final HttpRequestPacket request = ((HttpResponsePacket) httpHeader).getRequest();
        if (Method.CONNECT.equals(request.getMethod())) {
            // finish request/response processing, because Grizzly itself
            // treats CONNECT traffic as part of request-response processing
            // and we don't want it be treated like that
            httpHeader.setExpectContent(false);
        }
View Full Code Here

TOP

Related Classes of org.glassfish.grizzly.http.HttpRequestPacket$Builder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.