-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookie handling #104
Open
kennethjor
wants to merge
8
commits into
cdapio:develop
Choose a base branch
from
kennethjor:kennethjor/cookie-param
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cookie handling #104
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad4911b
Implemented String-based cookie handling.
kennethjor 5a55b5c
Added support for DefaultValue on cookies.
kennethjor d4005f3
Added support for Netty Cookie objects.
kennethjor 425b08e
Readme.
kennethjor eb68a86
Simplified cookie testing.
kennethjor 0e9f971
Merge branch 'cdapio:develop' into kennethjor/cookie-param
kennethjor a5662a4
Code style.
kennethjor a0418c9
Moved cookie parsing to getCookieParamValue.
kennethjor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright © 2017-2019 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.http.internal; | ||
|
||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.cookie.Cookie; | ||
import io.netty.handler.codec.http.cookie.CookieDecoder; | ||
import io.netty.handler.codec.http.cookie.ServerCookieDecoder; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Parses cookies from a request. | ||
*/ | ||
public class CookieParser { | ||
private final boolean strict; | ||
|
||
public CookieParser(boolean strict) { | ||
this.strict = strict; | ||
} | ||
|
||
public Map<String, Cookie> parseCookies(HttpRequest request) { | ||
List<String> headers = request.headers().getAll(HttpHeaderNames.COOKIE); | ||
if (headers == null || headers.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
ServerCookieDecoder decoder = getCookieDecoder(); | ||
Map<String, Cookie> cookies = new LinkedHashMap<>(); | ||
for (String value : headers) { | ||
for (Cookie cookie : decoder.decode(value)) { | ||
cookies.put(cookie.name(), cookie); | ||
} | ||
} | ||
return cookies; | ||
} | ||
|
||
private ServerCookieDecoder getCookieDecoder() { | ||
return strict ? ServerCookieDecoder.STRICT : ServerCookieDecoder.LAX; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the problems with parsing here is that an invalid cookie will cause the request to fail, even if the method doesn't care about it. It could potentially cause backwards incompatibilities, as somebody could upgrade this library and then see their requests failing due to some headers they never looked at before.
instead of parsing all the cookies here, it would be better to only lookup the relevant header in the getCookieParamValue() method and decode it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have moved it to the method so it will only be run on-demand. It does mean though that every cookie will result in parsing all the cookies. Is there a way to cache this? All the members on the
HttpResourceModel
are final and set in the constructor, so I wasn't sure if it would be ok to add a cookie parsing cache there, which is the approach I'd normally take.