Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/tink/http/Cookie.hx
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 =
Copy link
Member

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 :(

Copy link
Member Author

@kevinresol kevinresol May 24, 2016

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?

Copy link
Member

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.

#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,
}
7 changes: 7 additions & 0 deletions tests/RunTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package;
import tink.http.Container;
import tink.http.Client;
import tink.http.Multipart;
import haxe.unit.TestRunner;

class RunTests {

Expand All @@ -16,6 +17,12 @@ class RunTests {
var c = new NodeClient();
var s = new NodeContainer(2000);
#end

var t = new TestRunner();
t.add(new TestCookie());
if(!t.run()) {
#if sys Sys.exit(500); #end
}
}

}
32 changes: 32 additions & 0 deletions tests/TestCookie.hx
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);
}
}