All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.netty.http.headers.SetCookieHeader Maven / Gradle / Ivy

package org.zodiac.netty.http.headers;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;

final class SetCookieHeader extends AbstractHeader {

    private final boolean strict;
    private final ServerCookieEncoder encoder;
    private final ClientCookieDecoder decoder;

    SetCookieHeader() {
        this(true);
    }

    SetCookieHeader(boolean strict) {
        super(Cookie.class, HttpHeaderNames.SET_COOKIE);
        this.strict = strict;
        this.encoder = this.strict ? ServerCookieEncoder.STRICT : ServerCookieEncoder.LAX;
        this.decoder = this.strict ? ClientCookieDecoder.STRICT : ClientCookieDecoder.LAX;
    }

    @Override
    public String toString(Cookie value) {
        return encoder.encode(value);
    }

    @Override
    public Cookie toValue(CharSequence value) {
        /*Having multiple cookies in a single Set-Cookie header is deprecated, modern browsers only parse the first one.*/
        Cookie ck = decoder.decode(value.toString());
        if (null == ck) {
            new NullPointerException("Does not decode to cookies: '" + value + "'").printStackTrace();
            return null;
        }
        return ck;
    }

}

//@SuppressWarnings("deprecation")
//final class SetCookieHeader extends AbstractHeader {
//
//    SetCookieHeader() {
//        super(io.netty.handler.codec.http.Cookie.class, HttpHeaderNames.SET_COOKIE);
//    }
//
//    @Override
//    public String toString(io.netty.handler.codec.http.Cookie value) {
//        return io.netty.handler.codec.http.ServerCookieEncoder.encode(value);
//    }
//
//    @Override
//    public io.netty.handler.codec.http.Cookie toValue(CharSequence value) {
//        Set ck = io.netty.handler.codec.http.CookieDecoder.decode(value.toString());
//        if (ck.isEmpty()) {
//            new NullPointerException("Does not decode to cookies: '" + value + "'").printStackTrace();
//            return null;
//        }
//        return ck.iterator().next();
//    }
//
//}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy