Search in sources :

Example 6 with Cookie

use of io.netty.handler.codec.http.cookie.Cookie in project ratpack by ratpack.

the class ClientSideSessionStore method addCookie.

private void addCookie(String name, String value) {
    Cookie cookie = response.get().cookie(name, value);
    if (cookieConfig.getPath() != null) {
        cookie.setPath(cookieConfig.getPath());
    }
    if (cookieConfig.getDomain() != null) {
        cookie.setDomain(cookieConfig.getDomain());
    }
    long expirySeconds = cookieConfig.getExpires() == null ? 0 : cookieConfig.getExpires().getSeconds();
    if (expirySeconds > 0) {
        cookie.setMaxAge(expirySeconds);
    }
    cookie.setHttpOnly(cookieConfig.isHttpOnly());
    cookie.setSecure(cookieConfig.isSecure());
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie)

Example 7 with Cookie

use of io.netty.handler.codec.http.cookie.Cookie in project ratpack by ratpack.

the class ClientSideSessionStore method deserialize.

private ByteBuf deserialize(ImmutableList<Cookie> sessionCookies) throws Exception {
    if (sessionCookies.isEmpty()) {
        return Unpooled.EMPTY_BUFFER;
    }
    StringBuilder sessionCookie = new StringBuilder();
    for (Cookie cookie : sessionCookies) {
        sessionCookie.append(cookie.value());
    }
    String[] parts = sessionCookie.toString().split(SESSION_SEPARATOR);
    if (parts.length != 2) {
        return Unpooled.buffer(0, 0);
    }
    ByteBuf payload = null;
    ByteBuf digest = null;
    ByteBuf expectedDigest = null;
    ByteBuf decryptedPayload = null;
    try {
        payload = fromBase64(bufferAllocator, parts[0]);
        digest = fromBase64(bufferAllocator, parts[1]);
        expectedDigest = signer.sign(payload, bufferAllocator);
        if (ByteBufUtil.equals(digest, expectedDigest)) {
            decryptedPayload = crypto.decrypt(payload.resetReaderIndex(), bufferAllocator);
        } else {
            decryptedPayload = Unpooled.buffer(0, 0);
        }
    } finally {
        if (payload != null) {
            payload.touch().release();
        }
        if (digest != null) {
            digest.release();
        }
        if (expectedDigest != null) {
            expectedDigest.release();
        }
    }
    return decryptedPayload.touch();
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with Cookie

use of io.netty.handler.codec.http.cookie.Cookie in project ratpack by ratpack.

the class CookieBasedSessionId method getCookieSessionId.

private Optional<AsciiString> getCookieSessionId() {
    if (cookieSessionId == null) {
        if (terminated) {
            return Optional.empty();
        } else {
            Cookie match = null;
            for (Cookie cookie : request.getCookies()) {
                if (cookie.name().equals(cookieConfig.getIdName())) {
                    match = cookie;
                    break;
                }
            }
            cookieSessionId = match == null ? Optional.empty() : Optional.of(AsciiString.of(match.value()));
        }
    }
    return cookieSessionId;
}
Also used : DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Cookie(io.netty.handler.codec.http.cookie.Cookie)

Example 9 with Cookie

use of io.netty.handler.codec.http.cookie.Cookie in project spring-framework by spring-projects.

the class RxNettyServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (String name : this.request.getCookies().keySet()) {
        for (Cookie cookie : this.request.getCookies().get(name)) {
            HttpCookie httpCookie = new HttpCookie(name, cookie.value());
            cookies.add(name, httpCookie);
        }
    }
    return cookies;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Example 10 with Cookie

use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.

the class ResponseSender method synchronizeAndSetupResponseInfoAndFirstChunk.

protected void synchronizeAndSetupResponseInfoAndFirstChunk(ResponseInfo<?> responseInfo, HttpResponse actualResponseObject, RequestInfo requestInfo, ChannelHandlerContext ctx) {
    // Set the content type header.
    //      NOTE: This is ok even if the response doesn't have a body - in the case of chunked messages we don't
    //            know whether body content will be coming later or not so we have to be proactive here in case
    //            there *is* body content later.
    //      ALSO NOTE: The responseInfo may have already had a Content-Type header specified (e.g. reverse proxied
    //            response), but the way we build the header this will be ok. If responseInfo wanted to override it
    //            we allow that, and if not then the Content-Type in the headers will be honored, and if both of
    //            those are unspecified then the default mime type and charset are used.
    responseInfo.getHeaders().set(CONTENT_TYPE, buildContentTypeHeader(responseInfo));
    // Set the HTTP status code on the ResponseInfo object from the actualResponseObject if necessary.
    if (responseInfo.getHttpStatusCode() == null)
        responseInfo.setHttpStatusCode(actualResponseObject.getStatus().code());
    // Make sure a trace ID is in the headers.
    if (!responseInfo.getHeaders().contains(TraceHeaders.TRACE_ID)) {
        // All responses must contain a trace ID. Try to get it from the request
        //      since it wasn't already in the response.
        String traceId = extractDistributedTraceId(requestInfo, ctx);
        if (traceId == null) {
            // Couldn't find a valid trace ID anywhere, so just create a dummy one, and log what happened so if
            //      someone searches for that ID they'll find something explaining what happened.
            traceId = TraceAndSpanIdGenerator.generateId();
            String warningMsg = "Generating a dummy Trace ID for response header because a real Trace ID did not exist. This " + "probably happened because the request was not processed by the channel pipeline. dummy_trace_id=" + traceId;
            runnableWithTracingAndMdc(() -> logger.warn(warningMsg), ctx).run();
        }
        responseInfo.getHeaders().set(TraceHeaders.TRACE_ID, traceId);
    }
    // Handle any keep-alive stuff
    if (responseInfo.isForceConnectionCloseAfterResponseSent()) {
        // We'll be closing the connection after this response is sent, so send the appropriate Connection header.
        responseInfo.getHeaders().set(CONNECTION, HttpHeaders.Values.CLOSE);
    } else if (requestInfo.isKeepAliveRequested()) {
        //      what the content length will be.
        if (actualResponseObject instanceof LastHttpContent) {
            responseInfo.getHeaders().set(CONTENT_LENGTH, ((LastHttpContent) actualResponseObject).content().readableBytes());
        } else {
            //      If we have one of those responses, we mark it with content-length 0
            if (isContentAlwaysEmpty(responseInfo)) {
                responseInfo.getHeaders().remove(TRANSFER_ENCODING);
                responseInfo.getHeaders().set(CONTENT_LENGTH, 0);
            } else {
                // Not a must-be-empty-payload status code. For these there might be a payload and we can't know the
                //      content length since it's being sent to us in chunks, so we have to set the
                //      Transfer-Encoding header to chunked in order for the response sending to be successful
                //      (otherwise the receiving client would just hang waiting for the connection to be closed).
                // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6
                // and http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 for the technical explanation.
                // See http://en.wikipedia.org/wiki/Chunked_transfer_encoding for a more straightforward explanation
                responseInfo.getHeaders().remove(CONTENT_LENGTH);
                responseInfo.getHeaders().set(TRANSFER_ENCODING, CHUNKED);
            }
        }
        // Add keep alive header as per
        //      http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        responseInfo.getHeaders().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }
    // Synchronize the ResponseInfo headers with the actualResponseObject
    //      (copy from responseInfo into actualResponseObject)
    actualResponseObject.headers().add(responseInfo.getHeaders());
    // Add cookies (if any)
    if (responseInfo.getCookies() != null) {
        for (Cookie cookie : responseInfo.getCookies()) {
            actualResponseObject.headers().add(HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie.name(), cookie.value()));
        }
    }
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

Cookie (io.netty.handler.codec.http.cookie.Cookie)45 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)21 Test (org.junit.Test)19 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)15 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 Charset (java.nio.charset.Charset)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)6 Test (org.testng.annotations.Test)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 ByteBuf (io.netty.buffer.ByteBuf)3 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)3 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Uri (org.asynchttpclient.uri.Uri)2