Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Added JSESSIONID
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Aug 21, 2012
1 parent 059df39 commit a66deec
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 58 deletions.
4 changes: 3 additions & 1 deletion README.pod
Expand Up @@ -6,7 +6,7 @@ SockJS-perl - a SockJS Perl Plack/PSGI implementation

=head1 DESCRIPTION

=head2 Supported transports
=head2 Supported features

Work's still in progress.

Expand All @@ -17,6 +17,8 @@ Work's still in progress.
HtmlFile
IFrame XHR Polling/EvenSource/HtmlFile

JSessionID

=head2 The Client-Side Part

SockJS client is required. You can get it from
Expand Down
8 changes: 6 additions & 2 deletions lib/SockJS.pm
Expand Up @@ -12,6 +12,7 @@ use Digest::MD5 ();
use Scalar::Util ();

use Plack::Middleware::ContentLength;
use SockJS::Middleware::JSessionID;
use SockJS::Transport;
use SockJS::Session;

Expand All @@ -31,8 +32,9 @@ sub new {
sub to_app {
my $self = shift;

return Plack::Middleware::ContentLength->new->wrap(sub { $self->call(@_) }
);
return SockJS::Middleware::JSessionID->new(cookie => $self->{cookie})
->wrap(
Plack::Middleware::ContentLength->new->wrap(sub { $self->call(@_) }));
}

sub call {
Expand Down Expand Up @@ -116,6 +118,8 @@ sub _dispatch_transport {
return [404, ['Content-Type' => 'text/plain'], ['Not found']]
unless $transport;

$env->{'sockjs.transport'} = $transport->name;

my $response;
eval { $response = $transport->dispatch($env, $session, $path) } || do {
my $e = $@;
Expand Down
35 changes: 35 additions & 0 deletions lib/SockJS/Middleware/JSessionID.pm
@@ -0,0 +1,35 @@
package SockJS::Middleware::JSessionID;

use strict;
use warnings;

use parent 'Plack::Middleware';

use Plack::Util;

sub call {
my $self = shift;
my ($env) = @_;

my $res = $self->app->(@_);
return $res unless $env->{'sockjs.transport'};

return $self->response_cb(
$res => sub {
my $res = shift;

my $h = Plack::Util::headers($res->[1]);

if (my $cookie = $env->{HTTP_COOKIE}) {
if ($cookie =~ m/(?:^|;|\s)JSESSIONID\s*=\s*(.+?)(?:\s|;|$)/) {
$h->push('Set-Cookie' => "JSESSIONID=$1; Path=/");
}
}
elsif ($self->{cookie}) {
$h->push('Set-Cookie' => 'JSESSIONID=dummy; Path=/');
}
}
);
}

1;
2 changes: 1 addition & 1 deletion lib/SockJS/Transport.pm
Expand Up @@ -47,7 +47,7 @@ sub build {

$class = "SockJS::Transport::$class";

return $class->new(@_);
return $class->new(name => $path, @_);
}

1;
17 changes: 17 additions & 0 deletions lib/SockJS/Transport/Base.pm
@@ -0,0 +1,17 @@
package SockJS::Transport::Base;

use strict;
use warnings;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

return $self;
}

sub name {shift->{name}}

1;
7 changes: 3 additions & 4 deletions lib/SockJS/Transport/EventSource.pm
Expand Up @@ -3,11 +3,10 @@ package SockJS::Transport::EventSource;
use strict;
use warnings;

sub new {
my $class = shift;
use base 'SockJS::Transport::Base';

my $self = {@_};
bless $self, $class;
sub new {
my $self = shift->SUPER::new(@_);

$self->{response_limit} ||= 128 * 1024;

Expand Down
7 changes: 3 additions & 4 deletions lib/SockJS/Transport/HtmlFile.pm
Expand Up @@ -3,11 +3,10 @@ package SockJS::Transport::HtmlFile;
use strict;
use warnings;

sub new {
my $class = shift;
use base 'SockJS::Transport::Base';

my $self = {@_};
bless $self, $class;
sub new {
my $self = shift->SUPER::new(@_);

$self->{response_limit} ||= 128 * 1024;

Expand Down
9 changes: 1 addition & 8 deletions lib/SockJS/Transport/JSONPPolling.pm
Expand Up @@ -3,14 +3,7 @@ package SockJS::Transport::JSONPPolling;
use strict;
use warnings;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

return $self;
}
use base 'SockJS::Transport::Base';

sub dispatch {
my $self = shift;
Expand Down
9 changes: 1 addition & 8 deletions lib/SockJS/Transport/JSONPSend.pm
Expand Up @@ -3,14 +3,7 @@ package SockJS::Transport::JSONPSend;
use strict;
use warnings;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

return $self;
}
use base 'SockJS::Transport::Base';

sub dispatch {
my $self = shift;
Expand Down
11 changes: 2 additions & 9 deletions lib/SockJS/Transport/WebSocket.pm
Expand Up @@ -3,20 +3,13 @@ package SockJS::Transport::WebSocket;
use strict;
use warnings;

use base 'SockJS::Transport::Base';

use Protocol::WebSocket::Handshake::Server;

use SockJS::Handle;
use SockJS::Exception;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

return $self;
}

sub dispatch {
my $self = shift;
my ($env, $session) = @_;
Expand Down
11 changes: 2 additions & 9 deletions lib/SockJS/Transport/XHRPolling.pm
Expand Up @@ -3,16 +3,9 @@ package SockJS::Transport::XHRPolling;
use strict;
use warnings;

use SockJS::Exception;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;
use base 'SockJS::Transport::Base';

return $self;
}
use SockJS::Exception;

sub dispatch {
my $self = shift;
Expand Down
9 changes: 1 addition & 8 deletions lib/SockJS/Transport/XHRSend.pm
Expand Up @@ -3,14 +3,7 @@ package SockJS::Transport::XHRSend;
use strict;
use warnings;

sub new {
my $class = shift;

my $self = {@_};
bless $self, $class;

return $self;
}
use base 'SockJS::Transport::Base';

sub dispatch {
my $self = shift;
Expand Down
7 changes: 3 additions & 4 deletions lib/SockJS/Transport/XHRStreaming.pm
Expand Up @@ -3,11 +3,10 @@ package SockJS::Transport::XHRStreaming;
use strict;
use warnings;

sub new {
my $class = shift;
use base 'SockJS::Transport::Base';

my $self = {@_};
bless $self, $class;
sub new {
my $self = shift->SUPER::new(@_);

$self->{response_limit} ||= 128 * 1024;

Expand Down
66 changes: 66 additions & 0 deletions t/core/MiddlewareJSessionIDTest.pm
@@ -0,0 +1,66 @@
package MiddlewareJSessionIDTest;

use strict;
use warnings;

use base 'TestBase';

use Test::More;

use SockJS::Middleware::JSessionID;

sub echo_cookie : Test {
my $self = shift;

my $mw = $self->_build_middleware;

$mw->wrap(sub { [200, [], []] });

my $env = {
'HTTP_COOKIE' => 'JSESSIONID=abcde',
'sockjs.transport' => 'xhr_polling'
};

my $response = $mw->call($env);

is_deeply($response->[1], ['Set-Cookie' => 'JSESSIONID=abcde; Path=/']);
}

sub echo_cookie_smart : Test {
my $self = shift;

my $mw = $self->_build_middleware;

$mw->wrap(sub { [200, [], []] });

my $env = {
'HTTP_COOKIE' => 'foo=bar;JSESSIONID = abcde;bar=baz',
'sockjs.transport' => 'xhr_polling'
};

my $response = $mw->call($env);

is_deeply($response->[1], ['Set-Cookie' => 'JSESSIONID=abcde; Path=/']);
}

sub set_default_cookie : Test {
my $self = shift;

my $mw = $self->_build_middleware(cookie => 1);

$mw->wrap(sub { [200, [], []] });

my $env = {'sockjs.transport' => 'xhr_polling'};

my $response = $mw->call($env);

is_deeply($response->[1], ['Set-Cookie' => 'JSESSIONID=dummy; Path=/']);
}

sub _build_middleware {
my $self = shift;

return SockJS::Middleware::JSessionID->new(@_);
}

1;

0 comments on commit a66deec

Please sign in to comment.