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  
40      /**
41       * Creates a new cookie with the specified name and value.
42       */
43      public DefaultCookie(String name, String value) {
44          this.name = checkNonEmptyAfterTrim(name, "name");
45          setValue(value);
46      }
47  
48      @Override
49      public String name() {
50          return name;
51      }
52  
53      @Override
54      public String value() {
55          return value;
56      }
57  
58      @Override
59      public void setValue(String value) {
60          this.value = checkNotNull(value, "value");
61      }
62  
63      @Override
64      public boolean wrap() {
65          return wrap;
66      }
67  
68      @Override
69      public void setWrap(boolean wrap) {
70          this.wrap = wrap;
71      }
72  
73      @Override
74      public String domain() {
75          return domain;
76      }
77  
78      @Override
79      public void setDomain(String domain) {
80          this.domain = validateAttributeValue("domain", domain);
81      }
82  
83      @Override
84      public String path() {
85          return path;
86      }
87  
88      @Override
89      public void setPath(String path) {
90          this.path = validateAttributeValue("path", path);
91      }
92  
93      @Override
94      public long maxAge() {
95          return maxAge;
96      }
97  
98      @Override
99      public void setMaxAge(long maxAge) {
100         this.maxAge = maxAge;
101     }
102 
103     @Override
104     public boolean isSecure() {
105         return secure;
106     }
107 
108     @Override
109     public void setSecure(boolean secure) {
110         this.secure = secure;
111     }
112 
113     @Override
114     public boolean isHttpOnly() {
115         return httpOnly;
116     }
117 
118     @Override
119     public void setHttpOnly(boolean httpOnly) {
120         this.httpOnly = httpOnly;
121     }
122 
123     /**
124      * Checks to see if this {@link Cookie} can be sent along cross-site requests.
125      * For more information, please look
126      * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
127      * @return <b>same-site-flag</b> value
128      */
129     public SameSite sameSite() {
130         return sameSite;
131     }
132 
133     /**
134      * Determines if this this {@link Cookie} can be sent along cross-site requests.
135      * For more information, please look
136      *  <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
137      * @param sameSite <b>same-site-flag</b> value
138      */
139     public void setSameSite(SameSite sameSite) {
140         this.sameSite = sameSite;
141     }
142 
143     @Override
144     public int hashCode() {
145         return name().hashCode();
146     }
147 
148     @Override
149     public boolean equals(Object o) {
150         if (this == o) {
151             return true;
152         }
153 
154         if (!(o instanceof Cookie)) {
155             return false;
156         }
157 
158         Cookie that = (Cookie) o;
159         if (!name().equals(that.name())) {
160             return false;
161         }
162 
163         if (path() == null) {
164             if (that.path() != null) {
165                 return false;
166             }
167         } else if (that.path() == null) {
168             return false;
169         } else if (!path().equals(that.path())) {
170             return false;
171         }
172 
173         if (domain() == null) {
174             if (that.domain() != null) {
175                 return false;
176             }
177         } else {
178             return domain().equalsIgnoreCase(that.domain());
179         }
180 
181         return true;
182     }
183 
184     @Override
185     public int compareTo(Cookie c) {
186         int v = name().compareTo(c.name());
187         if (v != 0) {
188             return v;
189         }
190 
191         if (path() == null) {
192             if (c.path() != null) {
193                 return -1;
194             }
195         } else if (c.path() == null) {
196             return 1;
197         } else {
198             v = path().compareTo(c.path());
199             if (v != 0) {
200                 return v;
201             }
202         }
203 
204         if (domain() == null) {
205             if (c.domain() != null) {
206                 return -1;
207             }
208         } else if (c.domain() == null) {
209             return 1;
210         } else {
211             v = domain().compareToIgnoreCase(c.domain());
212             return v;
213         }
214 
215         return 0;
216     }
217 
218     /**
219      * Validate a cookie attribute value, throws a {@link IllegalArgumentException} otherwise.
220      * Only intended to be used by {@link io.netty.handler.codec.http.DefaultCookie}.
221      * @param name attribute name
222      * @param value attribute value
223      * @return the trimmed, validated attribute value
224      * @deprecated CookieUtil is package private, will be removed once old Cookie API is dropped
225      */
226     @Deprecated
227     protected String validateValue(String name, String value) {
228         return validateAttributeValue(name, value);
229     }
230 
231     @Override
232     public String toString() {
233         StringBuilder buf = stringBuilder()
234             .append(name())
235             .append('=')
236             .append(value());
237         if (domain() != null) {
238             buf.append(", domain=")
239                .append(domain());
240         }
241         if (path() != null) {
242             buf.append(", path=")
243                .append(path());
244         }
245         if (maxAge() >= 0) {
246             buf.append(", maxAge=")
247                .append(maxAge())
248                .append('s');
249         }
250         if (isSecure()) {
251             buf.append(", secure");
252         }
253         if (isHttpOnly()) {
254             buf.append(", HTTPOnly");
255         }
256         if (sameSite() != null) {
257             buf.append(", SameSite=").append(sameSite());
258         }
259         return buf.toString();
260     }
261 }