| 1 | package MojoX::Session::Store::Couchdb; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'MojoX::Session::Store'; | ||
| 7 | |||
| 8 | use Mojo::UserAgent; | ||
| 9 | use Mojo::JSON; | ||
| 10 | |||
| 11 | __PACKAGE__->attr(is_async => 1); | ||
| 12 | |||
| 13 | __PACKAGE__->attr(client => sub { Mojo::UserAgent->new; } ); | ||
| 14 | __PACKAGE__->attr(address => 'localhost'); | ||
| 15 | __PACKAGE__->attr(port => '5984'); | ||
| 16 | __PACKAGE__->attr(database => 'session'); | ||
| 17 | |||
| 18 | # For internal use | ||
| 19 | __PACKAGE__->attr('_rev'); | ||
| 20 | |||
| 21 | sub _build_url { | ||
| 22 | my $self = shift; | ||
| 23 | my $path = shift; | ||
| 24 | my $query = shift; | ||
| 25 | |||
| 26 | my $url = Mojo::URL->new; | ||
| 27 | $url->scheme('http'); | ||
| 28 | $url->host($self->address); | ||
| 29 | $url->port($self->port); | ||
| 30 | $url->path('/' . $self->database . '/' . $path); | ||
| 31 | $url->query(%$query) if $query; | ||
| 32 | |||
| 33 | return $url; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _encode_json { | ||
| 37 | my $self = shift; | ||
| 38 | my $data = shift; | ||
| 39 | |||
| 40 | my $json = Mojo::JSON->new; | ||
| 41 | |||
| 42 | $data = $json->encode($data); | ||
| 43 | unless ($data && !$json->error) { | ||
| 44 | $self->error("Can't parse incoming data"); | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | return $data; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _decode_json { | ||
| 52 | my $self = shift; | ||
| 53 | my $body = shift; | ||
| 54 | |||
| 55 | my $json = Mojo::JSON->new; | ||
| 56 | |||
| 57 | $body = $json->decode($body); | ||
| 58 | |||
| 59 | unless ($body && !$json->error) { | ||
| 60 | $self->error("Can't parse response"); | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | return $body; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub create { | ||
| 68 | my ($self, $sid, $expires, $data, $cb) = @_; | ||
| 69 | |||
| 70 | $self->error(''); | ||
| 71 | |||
| 72 | $data = $self->_encode_json({data => $data, expires => $expires}); | ||
| 73 | return $cb->($self) unless $data; | ||
| 74 | |||
| 75 | my $url = $self->_build_url($sid); | ||
| 76 | |||
| 77 | my $tx = $self->client->put( | ||
| 78 | $url => $data | ||
| 79 | ); | ||
| 80 | |||
| 81 | unless ($tx->success) { | ||
| 82 | $self->error($tx->error); | ||
| 83 | return $cb->($self); | ||
| 84 | } | ||
| 85 | |||
| 86 | unless ($tx->res->code == 201) { | ||
| 87 | $self->error('Wrong response'); | ||
| 88 | return $cb->($self); | ||
| 89 | } | ||
| 90 | |||
| 91 | my $body = $self->_decode_json($tx->res->body); | ||
| 92 | if ($body->{error}) { | ||
| 93 | $self->error($body->{error}); | ||
| 94 | return $cb->($self); | ||
| 95 | } | ||
| 96 | |||
| 97 | $self->_rev($body->{_rev}); | ||
| 98 | |||
| 99 | return $cb->($self); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub update { | ||
| 103 | my ($self, $sid, $expires, $data, $cb) = @_; | ||
| 104 | |||
| 105 | $self->error(''); | ||
| 106 | |||
| 107 | $data = | ||
| 108 | $self->_encode_json({data => $data, expires => $expires, _rev => $self->_rev}); | ||
| 109 | return $cb->($self) unless $data; | ||
| 110 | |||
| 111 | my $url = $self->_build_url($sid); | ||
| 112 | |||
| 113 | my $tx = $self->client->put( | ||
| 114 | $url => $data | ||
| 115 | ); | ||
| 116 | |||
| 117 | if ($tx->error) { | ||
| 118 | $self->error($tx->error); | ||
| 119 | return $cb->($self); | ||
| 120 | } | ||
| 121 | |||
| 122 | unless ($tx->res->code == 201) { | ||
| 123 | $self->error('Wrong response'); | ||
| 124 | return $cb->($self); | ||
| 125 | } | ||
| 126 | |||
| 127 | my $body = $self->_decode_json($tx->res->body); | ||
| 128 | if ($body->{error}) { | ||
| 129 | $self->error($body->{error}); | ||
| 130 | return $cb->($self); | ||
| 131 | } | ||
| 132 | |||
| 133 | $self->_rev($body->{_rev}); | ||
| 134 | |||
| 135 | return $cb->($self); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub load { | ||
| 139 | my ($self, $sid, $cb) = @_; | ||
| 140 | |||
| 141 | $self->error(''); | ||
| 142 | |||
| 143 | my $url = $self->_build_url($sid); | ||
| 144 | |||
| 145 | my $tx = $self->client->get($url); | ||
| 146 | |||
| 147 | if ($tx->error) { | ||
| 148 | $self->error($tx->error); | ||
| 149 | return $cb->($self); | ||
| 150 | } | ||
| 151 | |||
| 152 | # Session not found | ||
| 153 | if ($tx->res->code == 404) { | ||
| 154 | return $cb->($self); | ||
| 155 | } | ||
| 156 | |||
| 157 | # Wrong response status | ||
| 158 | unless ($tx->res->code == 200) { | ||
| 159 | $self->error('Wrong response'); | ||
| 160 | return $cb->($self); | ||
| 161 | } | ||
| 162 | |||
| 163 | my $body = $self->_decode_json($tx->res->body); | ||
| 164 | |||
| 165 | # CouchDB internal id | ||
| 166 | delete $body->{_id}; | ||
| 167 | |||
| 168 | # Needed for update and delete | ||
| 169 | $self->_rev(delete $body->{_rev}); | ||
| 170 | |||
| 171 | my $expires = delete $body->{expires}; | ||
| 172 | |||
| 173 | return $cb->($self, $expires, $body->{data}); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub delete { | ||
| 177 | my ($self, $sid, $cb) = @_; | ||
| 178 | |||
| 179 | $self->error(''); | ||
| 180 | |||
| 181 | my $url = $self->_build_url($sid, {rev => $self->_rev}); | ||
| 182 | |||
| 183 | my $tx = $self->client->delete($url); | ||
| 184 | |||
| 185 | if ($tx->error) { | ||
| 186 | $self->error($tx->error); | ||
| 187 | return $cb->($self); | ||
| 188 | } | ||
| 189 | |||
| 190 | unless ($tx->res->code == 200) { | ||
| 191 | $self->error('Wrong response'); | ||
| 192 | return $cb->($self); | ||
| 193 | } | ||
| 194 | |||
| 195 | my $body = $self->_decode_json($tx->res->body); | ||
| 196 | return $cb->($self) unless $body; | ||
| 197 | |||
| 198 | if ($body->{error}) { | ||
| 199 | $self->error($body->{error}); | ||
| 200 | return $cb->($self); | ||
| 201 | } | ||
| 202 | |||
| 203 | return $cb->($self); | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |