View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.cookie;
17  
18  import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
19  
20  import static io.netty.handler.codec.http.cookie.CookieUtil.stringBuilder;
21  import static io.netty.handler.codec.http.cookie.CookieUtil.validateAttributeValue;
22  import static io.netty.util.internal.ObjectUtil.checkNotNull;
23  import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
24  
25  /**
26   * The default {@link Cookie} implementation.
27   */
28  public class DefaultCookie implements Cookie {
29  
30      private final String name;
31      private String value;
32      private boolean wrap;
33      private String domain;
34      private String path;
35      private long maxAge = UNDEFINED_MAX_AGE;
36      private boolean secure;
37      private boolean httpOnly;
38      private SameSite sameSite;
39      private boolean partitioned;
40  
41      /**
42       * Creates a new cookie with the specified name and value.
43       */
44      public DefaultCookie(String name, String value) {
45          this.name = checkNonEmptyAfterTrim(name, "name");
46          setValue(value);
47      }
48  
49      @Override
50      public String name() {
51          return name;
52      }
53  
54      @Override
55      public String value() {
56          return value;
57      }
58  
59      @Override
60      public void setValue(String value) {
61          this.value = checkNotNull(value, "value");
62      }
63  
64      @Override
65      public boolean wrap() {
66          return wrap;
67      }
68  
69      @Override
70      public void setWrap(boolean wrap) {
71          this.wrap = wrap;
72      }
73  
74      @Override
75      public String domain() {
76          return domain;
77      }
78  
79      @Override
80      public void setDomain(String domain) {
81          this.domain = validateAttributeValue("domain", domain);
82      }
83  
84      @Override
85      public String path() {
86          return path;
87      }
88  
89      @Override
90      public void setPath(String path) {
91          this.path = validateAttributeValue("path", path);
92      }
93  
94      @Override
95      public long maxAge() {
96          return maxAge;
97      }
98  
99      @Override
100     public void setMaxAge(long maxAge) {
101         this.maxAge = maxAge;
102     }
103 
104     @Override
105     public boolean isSecure() {
106         return secure;
107     }
108 
109     @Override
110     public void setSecure(boolean secure) {
111         this.secure = secure;
112     }
113 
114     @Override
115     public boolean isHttpOnly() {
116         return httpOnly;
117     }
118 
119     @Override
120     public void setHttpOnly(boolean httpOnly) {
121         this.httpOnly = httpOnly;
122     }
123 
124     /**
125      * Checks to see if this {@link Cookie} can be sent along cross-site requests.
126      * For more information, please look
127      * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
128      * @return <b>same-site-flag</b> value
129      */
130     public SameSite sameSite() {
131         return sameSite;
132     }
133 
134     /**
135      * Determines if this this {@link Cookie} can be sent along cross-site requests.
136      * For more information, please look
137      *  <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
138      * @param sameSite <b>same-site-flag</b> value
139      */
140     public void setSameSite(SameSite sameSite) {
141         this.sameSite = sameSite;
142     }
143 
144     /**
145      * Checks to see if this {@link Cookie} is partitioned
146      *
147      * @return True if this {@link Cookie} is partitioned, otherwise false
148      */
149     public boolean isPartitioned() {
150         return partitioned;
151     }
152 
153     /**
154      * Sets the {@code Partitioned} attribute of this {@link Cookie}
155      *
156      * @param partitioned True if this {@link Cookie} is to be partitioned, otherwise false
157      */
158     public void setPartitioned(boolean partitioned) {
159         this.partitioned = partitioned;
160     }
161 
162     @Override
163     public int hashCode() {
164         return name().hashCode();
165     }
166 
167     @Override
168     public boolean equals(Object o) {
169         if (this == o) {
170             return true;
171         }
172 
173         if (!(o instanceof Cookie)) {
174             return false;
175         }
176 
177         Cookie that = (Cookie) o;
178         if (!name().equals(that.name())) {
179             return false;
180         }
181 
182         if (path() == null) {
183             if (that.path() != null) {
184                 return false;
185             }
186         } else if (that.path() == null) {
187             return false;
188         } else if (!path().equals(that.path())) {
189             return false;
190         }
191 
192         if (domain() == null) {
193             if (that.domain() != null) {
194                 return false;
195             }
196         } else {
197             return domain().equalsIgnoreCase(that.domain());
198         }
199 
200         return true;
201     }
202 
203     @Override
204     public int compareTo(Cookie c) {
205         int v = name().compareTo(c.name());
206         if (v != 0) {
207             return v;
208         }
209 
210         if (path() == null) {
211             if (c.path() != null) {
212                 return -1;
213             }
214         } else if (c.path() == null) {
215             return 1;
216         } else {
217             v = path().compareTo(c.path());
218             if (v != 0) {
219                 return v;
220             }
221         }
222 
223         if (domain() == null) {
224             if (c.domain() != null) {
225                 return -1;
226             }
227         } else if (c.domain() == null) {
228             return 1;
229         } else {
230             v = domain().compareToIgnoreCase(c.domain());
231             return v;
232         }
233 
234         return 0;
235     }
236 
237     /**
238      * Validate a cookie attribute value, throws a {@link IllegalArgumentException} otherwise.
239      * Only intended to be used by {@link io.netty.handler.codec.http.DefaultCookie}.
240      * @param name attribute name
241      * @param value attribute value
242      * @return the trimmed, validated attribute value
243      * @deprecated CookieUtil is package private, will be removed once old Cookie API is dropped
244      */
245     @Deprecated
246     protected String validateValue(String name, String value) {
247         return validateAttributeValue(name, value);
248     }
249 
250     @Override
251     public String toString() {
252         StringBuilder buf = stringBuilder()
253             .append(name())
254             .append('=')
255             .append(value());
256         if (domain() != null) {
257             buf.append(", domain=")
258                .append(domain());
259         }
260         if (path() != null) {
261             buf.append(", path=")
262                .append(path());
263         }
264         if (maxAge() >= 0) {
265             buf.append(", maxAge=")
266                .append(maxAge())
267                .append('s');
268         }
269         if (isSecure()) {
270             buf.append(", secure");
271         }
272         if (isHttpOnly()) {
273             buf.append(", HTTPOnly");
274         }
275         if (sameSite() != null) {
276             buf.append(", SameSite=").append(sameSite());
277         }
278         if (isPartitioned()) {
279             buf.append(", Partitioned");
280         }
281         return buf.toString();
282     }
283 }