Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 90 lines (66 sloc) 1.879 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

<erl>
-export([pserv/0]).

ensure_pcookie_server() ->
    case whereis(pcookie_server) of
        undefined ->
            proc_lib:start(?MODULE, pserv,[]);
        _ ->
            ok
    end.

pserv() ->
    catch begin
              register(pcookie_server, self()),
              T = ets:new(pcookies, [set, public,
                                     named_table]),
              ets:insert(T, {counter, 0})
          end,
    proc_lib:init_ack(ok),
    pserv_loop().

pserv_loop() ->
    receive
        X ->
            pserv_loop()
    end.

setcookie(A) ->
    Num = ets:update_counter(pcookies, counter, 1),
    Data = {(A#arg.headers)#headers.user_agent,
            calendar:local_time(),
            inet:peername(A#arg.clisock)},
    ets:insert(pcookies, {{cookie, Num}, Data}),
    yaws_api:setcookie("pfoobar",integer_to_list(Num), "/").



out(A) ->
    ensure_pcookie_server(),
    H = A#arg.headers,
    C = H#headers.cookie,
    case C of
        [Cookie|_] ->
            case string:tokens(Cookie, [$=]) of
                ["pfoobar", NumStr] ->
                    Num = list_to_integer(NumStr),
                    %% cookie allready set
                    case ets:lookup(pcookies, {c, Num}) of
                        [{cookie, Data}] ->
                            ok; %% cookie already set
                        [] ->
                            setcookie(A)
                    end;
                _ ->
                    setcookie(A)
            end;
        _ ->
            setcookie(A)
    end.
</erl>


<erl>


out(A) -> {ssi, "TAB.inc", [],[]}.
</erl>

<div id="entry">





<h2> Set persistent cookie </h2>

<p>This is the page that set a persistent cookie in
the browser.
<a href="readpcookie.yaws">readpcookie.yaws</a> will
read the cookie and report persistent information.



</div>


<erl>
out(A) -> {ssi, "END2",[],[]}.
</erl>
Something went wrong with that request. Please try again.