aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/Cookie.java
blob: a43310aff51677cf92802f8d3cdbe310c9e3a7b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http;

import org.eclipse.jetty.server.CookieCutter;
import org.eclipse.jetty.server.Response;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * A RFC 6265 compliant cookie.
 *
 * Note: RFC 2109 and RFC 2965 is no longer supported. All fields that are not part of RFC 6265 are deprecated.
 *
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 * @author bjorncs
 */
public class Cookie {

    private final static Logger log = Logger.getLogger(Cookie.class.getName());

    private final Set<Integer> ports = new HashSet<>();
    private String name;
    private String value;
    private String domain;
    private String path;
    private String comment;
    private String commentUrl;
    private long maxAgeSeconds = Integer.MIN_VALUE;
    private int version;
    private boolean secure;
    private boolean httpOnly;
    private boolean discard;

    public Cookie() {
    }

    public Cookie(Cookie cookie) {
        ports.addAll(cookie.ports);
        name = cookie.name;
        value = cookie.value;
        domain = cookie.domain;
        path = cookie.path;
        comment = cookie.comment;
        commentUrl = cookie.commentUrl;
        maxAgeSeconds = cookie.maxAgeSeconds;
        version = cookie.version;
        secure = cookie.secure;
        httpOnly = cookie.httpOnly;
        discard = cookie.discard;
    }

    public Cookie(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public Cookie setName(String name) {
        this.name = name;
        return this;
    }

    public String getValue() {
        return value;
    }

    public Cookie setValue(String value) {
        this.value = value;
        return this;
    }

    public String getDomain() {
        return domain;
    }

    public Cookie setDomain(String domain) {
        this.domain = domain;
        return this;
    }

    public String getPath() {
        return path;
    }

    public Cookie setPath(String path) {
        this.path = path;
        return this;
    }

    @Deprecated
    public String getComment() {
        return comment;
    }

    @Deprecated
    public Cookie setComment(String comment) {
        this.comment = comment;
        return this;
    }

    @Deprecated
    public String getCommentURL() {
        return getCommentUrl();
    }

    @Deprecated
    public Cookie setCommentURL(String commentUrl) {
        return setCommentUrl(commentUrl);
    }

    @Deprecated
    public String getCommentUrl() {
        return commentUrl;
    }

    @Deprecated
    public Cookie setCommentUrl(String commentUrl) {
        this.commentUrl = commentUrl;
        return this;
    }

    public int getMaxAge(TimeUnit unit) {
        return (int)unit.convert(maxAgeSeconds, TimeUnit.SECONDS);
    }

    public Cookie setMaxAge(int maxAge, TimeUnit unit) {
        this.maxAgeSeconds = maxAge >= 0 ? unit.toSeconds(maxAge) : Integer.MIN_VALUE;
        return this;
    }

    @Deprecated
    public int getVersion() {
        return version;
    }

    @Deprecated
    public Cookie setVersion(int version) {
        this.version = version;
        return this;
    }

    public boolean isSecure() {
        return secure;
    }

    public Cookie setSecure(boolean secure) {
        this.secure = secure;
        return this;
    }

    public boolean isHttpOnly() {
        return httpOnly;
    }

    public Cookie setHttpOnly(boolean httpOnly) {
        this.httpOnly = httpOnly;
        return this;
    }

    @Deprecated
    public boolean isDiscard() {
        return discard;
    }

    @Deprecated
    public Cookie setDiscard(boolean discard) {
        this.discard = discard;
        return this;
    }

    @Deprecated
    public Set<Integer> ports() {
        return ports;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cookie cookie = (Cookie) o;
        return maxAgeSeconds == cookie.maxAgeSeconds &&
                version == cookie.version &&
                secure == cookie.secure &&
                httpOnly == cookie.httpOnly &&
                discard == cookie.discard &&
                Objects.equals(ports, cookie.ports) &&
                Objects.equals(name, cookie.name) &&
                Objects.equals(value, cookie.value) &&
                Objects.equals(domain, cookie.domain) &&
                Objects.equals(path, cookie.path) &&
                Objects.equals(comment, cookie.comment) &&
                Objects.equals(commentUrl, cookie.commentUrl);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ports, name, value, domain, path, comment, commentUrl, maxAgeSeconds, version, secure, httpOnly, discard);
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append(name).append("=").append(value);
        return ret.toString();
    }
    // NOTE cookie encoding and decoding:
    //      The implementation uses Jetty for server-side (encoding of Set-Cookie and decoding of Cookie header),
    //      and java.net.HttpCookie for client-side (encoding of Cookie and decoding of Set-Cookie header).
    //
    // Implementation is RFC-6265 compliant.

    public static String toCookieHeader(Iterable<? extends Cookie> cookies) {
        return StreamSupport.stream(cookies.spliterator(), false)
                .map(cookie -> {
                    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
                    httpCookie.setComment(cookie.getComment());
                    httpCookie.setCommentURL(cookie.getCommentURL());
                    httpCookie.setDiscard(cookie.isDiscard());
                    httpCookie.setDomain(cookie.getDomain());
                    httpCookie.setHttpOnly(cookie.isHttpOnly());
                    httpCookie.setMaxAge(cookie.getMaxAge(TimeUnit.SECONDS));
                    httpCookie.setPath(cookie.getPath());
                    httpCookie.setSecure(cookie.isSecure());
                    httpCookie.setVersion(cookie.getVersion());
                    String portList = cookie.ports().stream()
                            .map(Number::toString)
                            .collect(Collectors.joining(","));
                    httpCookie.setPortlist(portList);
                    return httpCookie.toString();
                })
                .collect(Collectors.joining(";"));
    }

    public static List<Cookie> fromCookieHeader(String headerVal) {
        CookieCutter cookieCutter = new CookieCutter();
        cookieCutter.addCookieField(headerVal);
        return Arrays.stream(cookieCutter.getCookies())
                .map(servletCookie -> {
                    Cookie cookie = new Cookie();
                    cookie.setName(servletCookie.getName());
                    cookie.setValue(servletCookie.getValue());
                    cookie.setComment(servletCookie.getComment());
                    cookie.setPath(servletCookie.getPath());
                    cookie.setDomain(servletCookie.getDomain());
                    cookie.setMaxAge(servletCookie.getMaxAge(), TimeUnit.SECONDS);
                    cookie.setSecure(servletCookie.getSecure());
                    cookie.setVersion(servletCookie.getVersion());
                    cookie.setHttpOnly(servletCookie.isHttpOnly());
                    return cookie;
                })
                .collect(Collectors.toList());
    }

    /**
     * @deprecated Use {@link #toSetCookieHeaderAll(Iterable)} instead.
     */
    @Deprecated
    public static String toSetCookieHeader(Iterable<? extends Cookie> cookies) {
        List<String> encodedCookies = toSetCookieHeaderAll(cookies);
        return encodedCookies.isEmpty() ? null : encodedCookies.get(0);
    }

    // TODO Rename to toSetCookieHeader for Vespa 7
    public static List<String> toSetCookieHeaderAll(Iterable<? extends Cookie> cookies) {
        // Ugly, bot Jetty does not provide a dedicated cookie parser (will be included in Jetty 10)
        Response response = new Response(null, null);
        for (Cookie cookie : cookies) {
            response.addSetRFC6265Cookie(
                    cookie.getName(),
                    cookie.getValue(),
                    cookie.getDomain(),
                    cookie.getPath(),
                    cookie.getMaxAge(TimeUnit.SECONDS),
                    cookie.isSecure(),
                    cookie.isHttpOnly());
        }
        return new ArrayList<>(response.getHeaders("Set-Cookie"));
    }

    // TODO Change return type to Cookie for Vespa 7
    public static List<Cookie> fromSetCookieHeader(String headerVal) {
        return HttpCookie.parse(headerVal).stream()
                .map(httpCookie -> {
                    Cookie cookie = new Cookie();
                    cookie.setName(httpCookie.getName());
                    cookie.setValue(httpCookie.getValue());
                    cookie.setComment(httpCookie.getComment());
                    cookie.setCommentUrl(httpCookie.getCommentURL());
                    cookie.setDiscard(httpCookie.getDiscard());
                    cookie.setDomain(httpCookie.getDomain());
                    cookie.setHttpOnly(httpCookie.isHttpOnly());
                    cookie.setMaxAge((int)httpCookie.getMaxAge(), TimeUnit.SECONDS);
                    cookie.setPath(httpCookie.getPath());
                    cookie.setSecure(httpCookie.getSecure());
                    cookie.setVersion(httpCookie.getVersion());
                    cookie.ports().addAll(parsePortList(httpCookie.getPortlist()));
                    return cookie;
                })
                .collect(Collectors.toList());
    }


    private static List<Integer> parsePortList(String rawPortList) {
        if (rawPortList == null) return Collections.emptyList();

        List<Integer> ports = new ArrayList<>();
        StringTokenizer tokenizer = new StringTokenizer(rawPortList, ",");
        while (tokenizer.hasMoreTokens()) {
            String rawPort = tokenizer.nextToken().trim();
            if (!rawPort.isEmpty()) {
                try {
                    ports.add(Integer.parseInt(rawPort));
                } catch (NumberFormatException e) {
                    log.log(Level.FINE, "Unable to parse port: " + rawPort, e);
                }
            }
        }
        return ports;
    }

}