Skip to content

Commit

Permalink
Use DateTime::Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Altai-man committed Mar 12, 2018
1 parent 937d8f2 commit b015334
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 100 deletions.
2 changes: 1 addition & 1 deletion META6.json
Expand Up @@ -8,7 +8,7 @@
"license" : "Artistic-2.0",
"depends" : [ "IO::Socket::Async::SSL", "OO::Monitors", "IO::Path::ChildSecure",
"Base64", "HTTP::HPACK", "Cro::Core", "Cro::TLS", "JSON::Fast",
"Crypt::Random", "JSON::JWT" ],
"Crypt::Random", "JSON::JWT", "DateTime::Parse" ],
"provides" : {
"Cro::HTTP::Auth": "lib/Cro/HTTP/Auth.pm6",
"Cro::HTTP::Auth::WebToken": "lib/Cro/HTTP/Auth/WebToken.pm6",
Expand Down
33 changes: 28 additions & 5 deletions lib/Cro/HTTP/Cookie.pm6
@@ -1,4 +1,4 @@
use Cro::HTTP::DateTime;
use DateTime::Parse;

class X::Cro::HTTP::Cookie::Unrecognized is Exception {
has $.what;
Expand Down Expand Up @@ -31,7 +31,10 @@ grammar CookieString {
token TOP { <cookie-pair> ['; ' <cookie-av> ]* }
token cookie-pair { <cookie-name> '=' <cookie-value> }
proto token cookie-av {*}
token cookie-av:sym<expires> { :i 'Expires=' <HTTP-date> }
token cookie-av:sym<expires> { :i 'Expires=' [ <dt=DateTime::Parse::Grammar::rfc1123-date> |
<dt=DateTime::Parse::Grammar::rfc850-date> |
<dt=DateTime::Parse::Grammar::rfc850-var-date> |
<dt=DateTime::Parse::Grammar::asctime-date> ] }
token cookie-av:sym<max-age> { :i 'Max-Age=' '-'? <[1..9]> <[0..9]>* }
token cookie-av:sym<domain> { :i 'Domain=' <domain> }
token cookie-av:sym<path> { :i 'Path=' <path> }
Expand All @@ -58,12 +61,12 @@ class CookieBuilder {
}

method !data-deal($str) {
# XXX: can throw
DateTimeGrammar.parse($str, actions => DateTimeActions.new).made;
DateTime::Parse.new($str);
}

method cookie-av:sym<expires> ($/) {
make ('expires', self!data-deal(~$/<HTTP-date>));
my $res = self!data-deal(~$/<dt>);
make ('expires', $res);
}
method cookie-av:sym<max-age> ($/) {
make ('max-age', Duration.new: (~$/).split('=')[1].Int);
Expand Down Expand Up @@ -93,6 +96,26 @@ class Cro::HTTP::Cookie {
has Bool $.secure;
has Bool $.http-only;

sub rfc1123-formatter(DateTime $_ --> DateTime) is export {
my %month-names = 1 => 'Jan', 2 => 'Feb', 3 => 'Mar',
4 => 'Apr', 5 => 'May', 6 => 'Jun',
7 => 'Jul', 8 => 'Aug', 9 => 'Sep',
10 => 'Oct', 11 => 'Nov', 12 => 'Dec';
my %amonth-names = %month-names.antipairs;

my %weekdays = 1 => 'Mon', 2 => 'Tue',
3 => 'Wed', 4 => 'Thu',
5 => 'Fri', 6 => 'Sat',
7 => 'Sun';
my %aweekdays = %weekdays.antipairs;

my $rfc1123-format = sub ($self) { sprintf "%s, %02d %s %04d %02d:%02d:%02d GMT",
%weekdays{.day-of-week}, .day,
%month-names{.month}, .year,
.hour, .minute, .second given $self; }
DateTime.new(.Str, formatter => $rfc1123-format);
}

submethod BUILD(:$!name, :$!value,
:$!expires=Nil, :$!max-age=Nil,
:$!domain=Nil,:$!path=Nil,
Expand Down
87 changes: 0 additions & 87 deletions lib/Cro/HTTP/DateTime.pm6

This file was deleted.

7 changes: 0 additions & 7 deletions t/http-cookie.t
@@ -1,4 +1,3 @@
use Cro::HTTP::DateTime;
use Cro::HTTP::Cookie;
use Test;

Expand Down Expand Up @@ -39,12 +38,6 @@ dies-ok { my Domain $d = "\n"; }, 'Incorrect domain name with bad character';
dies-ok { my Domain $d = ""; }, 'Empty domain name cannot be created';
dies-ok { my Domain $d = ' '; }, 'Domain name cannot contain spaces';


# Time-related tests
like 'Sun, 06 Nov 1994 08:49:37 GMT', /<HTTP-date>/, 'RFC 1123';
like 'Sunday, 06-Nov-94 08:49:37 GMT', /<HTTP-date>/, 'RFC 850';
like 'Sun Nov 6 08:49:37 1994', /<HTTP-date>/, 'ANSI C\'s asctime() format';

isnt CookieString.parse('SID=31d4d86e407aad42; Path=/; Domain=example.com'), Nil, 'Set-Cookie string 1 parses';
isnt CookieString.parse('lang=en-US; Path=/; Domain=example.com'), Nil, 'Set-Cookie string 2 parses';
isnt CookieString.parse('lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT'), Nil, 'Set-Cookie string 3 parses';
Expand Down

0 comments on commit b015334

Please sign in to comment.