Skip to content

Commit

Permalink
Remove Rfc6265 prefix from cookie encoders and decoders
Browse files Browse the repository at this point in the history
Motivation:

Rfc6265Client/ServerCookieEncoder is a better replacement of the old
Client/ServerCookieEncoder, and thus there's no point of keeping both.

Modifications:

- Remove the old Client/ServerCookieEncoder
- Remove the 'Rfc6265' prefix from the new cookie encoder/decoder
  classes
- Deprecate CookieDecoder

Result:

We have much better cookie encoder/decoder implementation now.
  • Loading branch information
trustin committed Jan 21, 2015
1 parent 5692c1e commit 6cf93c8
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 1,044 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package io.netty.handler.codec.http;

import static io.netty.handler.codec.http.CookieEncoderUtil.stringBuilder;

import java.text.ParsePosition;
import java.util.Date;

import static io.netty.handler.codec.http.CookieEncoderUtil.*;

/**
* A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie decoder to be used client side.
*
* It will store the raw value in {@link Cookie#setRawValue(String)} so it can be
* eventually sent back to the Origin server as is.
*
* @see Rfc6265ClientCookieEncoder
* @see ClientCookieEncoder
*/
public final class Rfc6265ClientCookieDecoder {
public final class ClientCookieDecoder {

/**
* Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}.
Expand Down Expand Up @@ -307,7 +307,7 @@ private void parse8(String header, int nameStart, String value) {
}
}

private Rfc6265ClientCookieDecoder() {
private ClientCookieDecoder() {
// unused
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 The Netty Project
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -18,25 +18,41 @@
import static io.netty.handler.codec.http.CookieEncoderUtil.*;

/**
* Encodes client-side {@link Cookie}s into an HTTP header value. This encoder can encode
* the HTTP cookie version 0, 1, and 2.
* A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used client side,
* so only name=value pairs are sent.
*
* User-Agents are not supposed to interpret cookies, so, if present, {@link Cookie#rawValue()} will be used.
* Otherwise, {@link Cookie#value()} will be used unquoted.
*
* Note that multiple cookies are supposed to be sent at once in a single "Cookie" header.
*
* <pre>
* // Example
* {@link HttpRequest} req = ...;
* res.setHeader("Cookie", {@link ClientCookieEncoder}.encode("JSESSIONID", "1234"));
* </pre>
*
* @see CookieDecoder
* @see ClientCookieDecoder
*/
public final class ClientCookieEncoder {

/**
* Encodes the specified cookie into an HTTP header value.
* Encodes the specified cookie into a Cookie header value.
*
* @param name the cookie name
* @param value the cookie value
* @return a Rfc6265 style Cookie header value
*/
public static String encode(String name, String value) {
return encode(new DefaultCookie(name, value));
}

/**
* Encodes the specified cookie into a Cookie header value.
*
* @param specified the cookie
* @return a Rfc6265 style Cookie header value
*/
public static String encode(Cookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie");
Expand All @@ -47,71 +63,66 @@ public static String encode(Cookie cookie) {
return stripTrailingSeparator(buf);
}

/**
* Encodes the specified cookies into a single Cookie header value.
*
* @param cookies some cookies
* @return a Rfc6265 style Cookie header value, null if no cookies are passed.
*/
public static String encode(Cookie... cookies) {
if (cookies == null) {
throw new NullPointerException("cookies");
}

if (cookies.length == 0) {
return null;
}

StringBuilder buf = stringBuilder();
for (Cookie c: cookies) {
for (Cookie c : cookies) {
if (c == null) {
break;
}

encode(buf, c);
}
return stripTrailingSeparator(buf);
return stripTrailingSeparatorOrNull(buf);
}

/**
* Encodes the specified cookies into a single Cookie header value.
*
* @param cookies some cookies
* @return a Rfc6265 style Cookie header value, null if no cookies are passed.
*/
public static String encode(Iterable<Cookie> cookies) {
if (cookies == null) {
throw new NullPointerException("cookies");
}

if (!cookies.iterator().hasNext()) {
return null;
}

StringBuilder buf = stringBuilder();
for (Cookie c: cookies) {
for (Cookie c : cookies) {
if (c == null) {
break;
}

encode(buf, c);
}
return stripTrailingSeparator(buf);
return stripTrailingSeparatorOrNull(buf);
}

private static void encode(StringBuilder buf, Cookie c) {
if (c.version() >= 1) {
add(buf, '$' + CookieHeaderNames.VERSION, 1);
}

add(buf, c.name(), c.value());

if (c.path() != null) {
add(buf, '$' + CookieHeaderNames.PATH, c.path());
}

if (c.domain() != null) {
add(buf, '$' + CookieHeaderNames.DOMAIN, c.domain());
}

if (c.version() >= 1) {
if (!c.ports().isEmpty()) {
buf.append('$');
buf.append(CookieHeaderNames.PORT);
buf.append((char) HttpConstants.EQUALS);
buf.append((char) HttpConstants.DOUBLE_QUOTE);
for (int port: c.ports()) {
buf.append(port);
buf.append((char) HttpConstants.COMMA);
}
buf.setCharAt(buf.length() - 1, (char) HttpConstants.DOUBLE_QUOTE);
buf.append((char) HttpConstants.SEMICOLON);
buf.append((char) HttpConstants.SP);
}
}
// rawValue > value > ""
String value = c.rawValue() != null ? c.rawValue()
: c.value() != null ? c.value() : "";
addUnquoted(buf, c.name(), value);
}

private ClientCookieEncoder() {
// Unused
// unused
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.TreeSet;

/**
* @deprecated Use {@link ClientCookieDecoder} or {@link ServerCookieDecoder} instead.
*
* Decodes an HTTP header value into {@link Cookie}s. This decoder can decode
* the HTTP cookie version 0, 1, and 2.
*
Expand All @@ -37,6 +39,7 @@
* @see ClientCookieEncoder
* @see ServerCookieEncoder
*/
@Deprecated
public final class CookieDecoder {

private static final char COMMA = ',';
Expand Down

This file was deleted.

Loading

0 comments on commit 6cf93c8

Please sign in to comment.