-
Notifications
You must be signed in to change notification settings - Fork 19
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
Cookies #20
Open
kevinresol
wants to merge
1
commit into
haxetink:master
Choose a base branch
from
kevinresol:cookie
base: master
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
Cookies #20
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package tink.http; | ||
|
||
using DateTools; | ||
using StringTools; | ||
using Std; | ||
|
||
@:forward | ||
abstract Cookie(CookieObject) from CookieObject { | ||
|
||
public inline function new(name:String, value:String, ?expires:Date, ?domain:String, ?path:String, secure = false, httpOnly = false) | ||
this = { | ||
name: name, | ||
value: value, | ||
expires: expires, | ||
domain: domain, | ||
path: path, | ||
secure: secure, | ||
httpOnly: httpOnly, | ||
} | ||
|
||
public static function parse(s:String):Array<Cookie> { | ||
return [ | ||
for(pair in s.split('; ')) { | ||
var v = pair.split('='); | ||
{ | ||
name: v[0], | ||
value: v[1], | ||
} | ||
} | ||
]; | ||
} | ||
|
||
@:to | ||
public function toString():String { | ||
var buf = new StringBuf(); | ||
buf.add(this.name); | ||
if(this.value != null) buf.add('=${this.value}'); | ||
if(this.expires != null) buf.add('; expires=${formatDate(this.expires)}'); | ||
if(this.domain != null) buf.add('; domain=${this.domain}'); | ||
if(this.path != null) buf.add('; path=${this.path}'); | ||
if(this.secure) buf.add('; secure'); | ||
if(this.httpOnly) buf.add('; httponly'); | ||
return buf.toString(); | ||
} | ||
|
||
function formatDate(date:Date) { | ||
var timezoneOffset = | ||
#if php | ||
untyped __php__("intval(date('Z', {0}->__t));", date); | ||
#else | ||
Date.fromString('1970-01-01 00:00:00').getTime(); | ||
#end | ||
var d = date.delta(timezoneOffset); | ||
|
||
var day = switch d.getDay() { | ||
case 0: 'Sun'; | ||
case 1: 'Mon'; | ||
case 2: 'Tue'; | ||
case 3: 'Wed'; | ||
case 4: 'Thu'; | ||
case 5: 'Fri'; | ||
case 6: 'Sat'; | ||
default: throw 'assert'; | ||
} | ||
var date = d.getDate().string().lpad('0', 2); | ||
var month = switch d.getMonth() { | ||
case 0: 'Jan'; | ||
case 1: 'Feb'; | ||
case 2: 'Mar'; | ||
case 3: 'Apr'; | ||
case 4: 'May'; | ||
case 5: 'Jun'; | ||
case 6: 'Jul'; | ||
case 7: 'Aug'; | ||
case 8: 'Sep'; | ||
case 9: 'Oct'; | ||
case 10: 'Nov'; | ||
case 11: 'Dec'; | ||
default: throw 'assert'; | ||
} | ||
var year = d.getFullYear(); | ||
var hour = d.getHours().string().lpad('0', 2); | ||
var minute = d.getMinutes().string().lpad('0', 2); | ||
var second = d.getSeconds().string().lpad('0', 2); | ||
return '$day, $date-$month-$year $hour:$minute:$second GMT'; | ||
} | ||
} | ||
|
||
typedef CookieObject = { | ||
name:String, | ||
value:String, | ||
?expires:Date, | ||
?domain:String, | ||
?path:String, | ||
?secure:Bool, | ||
?httpOnly:Bool, | ||
} |
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,32 @@ | ||
package; | ||
|
||
import haxe.unit.TestCase; | ||
import tink.http.Cookie; | ||
|
||
class TestCookie extends TestCase { | ||
function testCookie() { | ||
var cookies = Cookie.parse('name=value'); | ||
assertEquals(1, cookies.length); | ||
assertEquals('name', cookies[0].name); | ||
assertEquals('value', cookies[0].value); | ||
|
||
var cookies = Cookie.parse('name=value; name1=value1'); | ||
assertEquals(2, cookies.length); | ||
assertEquals('name', cookies[0].name); | ||
assertEquals('value', cookies[0].value); | ||
assertEquals('name1', cookies[1].name); | ||
assertEquals('value1', cookies[1].value); | ||
|
||
var cookie = new Cookie('name', 'value'); | ||
assertEquals('name=value', cookie); | ||
|
||
var cookie = new Cookie('name', 'value', DateTools.delta(Date.now(), 1000)); | ||
trace((cookie:String)); // TODO: assert time zone | ||
|
||
var cookie = new Cookie('name', 'value', 'domain.com', '/path'); | ||
assertEquals('name=value; domain=domain.com; path=/path', cookie); | ||
|
||
var cookie = new Cookie('name', 'value', 'domain.com', '/path', true, true); | ||
assertEquals('name=value; domain=domain.com; path=/path; secure; httponly', cookie); | ||
} | ||
} |
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.
A good idea, but unfortunately won't work during DST :(
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 don't live in a place with DST so I don't know much about it.
Does the computer even know DST is in effect currently?
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.
Oh, lucky you :)
Yeah, I think it's handled all the way down to the OS (or so it seems). And it's a PITA to deal with. I suppose if we could do all of this in UTC itself, it would be a lot easier.