Skip to content

Commit

Permalink
Sessions exercise added
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsolomon committed Oct 30, 2011
1 parent 6a9ca09 commit 2617e98
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 1 deletion.
29 changes: 29 additions & 0 deletions exercises/part1/ex11/step1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/perl
use Dancer;
use Data::Dump qw/pp/;

set logger => 'console';
set log => 'debug';
set show_errors => 1;


set engines => {
template_toolkit =>
{
start_tag => '[%',
stop_tag => '%]'
}
};
set template => 'template_toolkit';

get '/' => sub {
my $ra_shopping_list = params->{item};
return template 'write-it-on-my-forehead' => {
shopping_list => $ra_shopping_list ,
req_headers => pp(request->headers),
};
};



Dancer->dance;
21 changes: 21 additions & 0 deletions exercises/part1/ex11/views/write-it-on-my-forehead.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>Shopping List on Forehead</h1>
<form action="/" method="get">

Remember to buy: <input type="text" name="item" />
<input type="submit" value="Submit" />
<br/>
<br/>

<b> Don't forget </b><br/>
[% FOREACH item in shopping_list %]
<input type="text" name="item" value="[% item %]" readonly/> <br/>
[% END %]
</form>




<b> Headers: </b> <pre> [% req_headers %] </pre>



62 changes: 61 additions & 1 deletion slides/part1.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ Finally,
$ cp part1/ex1/step1.pl part1/ex1/step2-forward.pl
```

and change the output to be a bit prettier using HTML tags:
* change the output to be a bit prettier using HTML tags:

```<h1>Hello World!</h1>```

* experiment with the ```debug``` function
for displaying information in the terminal.


&raquo; Exercise 2: A non-index route
==============================
Expand Down Expand Up @@ -613,3 +616,60 @@ template 'hello-multiple-adj' => { adjective_list => params->{adjective} };
```

What you'll have learnt from this is that multiple inputs of the same name are presented to the controller in an array ref of that name.


Exercise 11: Magic Cookies and Sessions
----------------------------------------

In this exercise we'll be implementing a page for constructing a shopping
list. As a starting point, we've put the naive solution to this problem
in your ```training.dancer.lpw.2011/exercises/part1/ex11``` directory as
```step1.pl```

Read the code and understand how the GET parameters are appended to the URL
each time you add a new item to your shopping list.

While this is very easy, there are a number of problems:
* Size limit - the maximum length of a url is 2048 characters in Internet Explorer
* Privacy - Every internet server through which your request is managed can see what's in your shopping basket.
* Theft - You can add things to your basket just by modifying the url.

The solution to these problems is based the concepts of 'cookie' and 'session'.

* On a user's first HTTP request to the website, a random string
(which we call a
<b>cookie</b>) is generated by the web server and is included in the HTTP
response header. On subsequent calls to this website,
your browser will send this cookie in the HTTP request header.

* On receiving the user's cookie, the server uses it to retrieve any
data it has stored in relation to this user. The continuity of visits
to a website by a user, brought about by this data is referred
to as a <b>session</b>.


<b>Task:</b> Copy ```step1.pl``` to ```step2.pl```, and
```views/write-it-on-my-forehead.tt``` to ```views/remember.tt```
and change it to use cookies and sessions,
so that each GET request only has the next item for the shopping list
and previous items are stored in the session.

<b>Hints:</b>
* Near the top of ```step2.pl``` call ```set session => 'Simple'```
* The command
```
session shopping_list => $data
```
stores ```$data``` in Dancer's internal hash of the form
``` $sessions{$cookie_id} = { shopping_list => $data } ```
and there's no need to retrieve the cookie yourself.
* The command
```session('shopping_list')```
returns any value saved against ```shopping_list``` in this session.
* Because we're using Dancer::Session::Simple you can store
any scalar against the ```shopping_list``` session key - even
a HashRef...




29 changes: 29 additions & 0 deletions solutions/part1/ex11/step1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/perl
use Dancer;
use Data::Dump qw/pp/;

set logger => 'console';
set log => 'debug';
set show_errors => 1;


set engines => {
template_toolkit =>
{
start_tag => '[%',
stop_tag => '%]'
}
};
set template => 'template_toolkit';

get '/' => sub {
my $ra_shopping_list = params->{item};
return template 'write-it-on-my-forehead' => {
shopping_list => $ra_shopping_list ,
req_headers => pp(request->headers),
};
};



Dancer->dance;
40 changes: 40 additions & 0 deletions solutions/part1/ex11/step2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/perl
use Dancer;
use Data::Dump qw/pp/;

set logger => 'console';
set log => 'debug';
set show_errors => 1;
set session => 'Simple';


set engines => {
template_toolkit =>
{
start_tag => '[%',
stop_tag => '%]'
}
};
set template => 'template_toolkit';

get '/' => sub {
debug ('Storing: '.pp(params->{item}));
my $one_item = params->{item};
my @items;
if (defined( session('shopping_list') )) {
@items = @{session('shopping_list') };
}
push @items, $one_item if $one_item;
session shopping_list => \@items;


return template 'remember' => {
list => \@items ,
req_headers => pp(request->headers),
};

};



Dancer->dance;
28 changes: 28 additions & 0 deletions solutions/part1/ex11/views/remember.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Shopping List in Memory</h1>
<form action="/" method="get">

Remember to buy: <input type="text" name="item" />
<input type="submit" value="Submit" />

</form>

<br/>
<br/>

<b> Don't forget </b><br/>
<ul>
[% FOREACH item in list %]
<li>[% item %]</li>
[% END %]
</ul>


<br/>
<br/>
<br/>
<br/>

<b> Headers: </b> <pre> [% req_headers %] </pre>



21 changes: 21 additions & 0 deletions solutions/part1/ex11/views/write-it-on-my-forehead.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>Shopping List on Forehead</h1>
<form action="/" method="get">

Remember to buy: <input type="text" name="item" />
<input type="submit" value="Submit" />
<br/>
<br/>

<b> Don't forget </b><br/>
[% FOREACH item in shopping_list %]
<input type="text" name="item" value="[% item %]" readonly/> <br/>
[% END %]
</form>




<b> Headers: </b> <pre> [% req_headers %] </pre>



0 comments on commit 2617e98

Please sign in to comment.