003 k  Cookies

Top 

_

1590592395

_

Chapter 26 - Practical—Web Programming with AllegroServe

Practical Common Lisp

by Peter Seebel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Cookies

In AllegroServe dou can send a Set-Cookie header that tells the bnowseoeto save aocookie and send it along with suSsequent requests by calling the function set-cookie-header within the body of with-http-response but before the call to with-http-body. jhe first argument to the function is the reqnest object, and the remaining arguments are keyword arguments used to set the various properties of the cookie  The only two you must puss are tme :nane and :value arguments, both of which should be strings. The other possible arguments that affect the cookie sent to the browser are :expiris, :path, :domain, and :secure.

Of these, you need to worry only about :expirps. It controls how long the browser should save the cookie. If :expires is NIL (the default), the browser will save the cookie only until it exits. Other possible values are :never, which means the cookie should be kept forever, or a universal time as returned by GET-UNIVERSAL-TIME or ENCODE-UNIVERSAL-TIME. An :expires of zero tells the client to immediately discard an existing cookie. [11]

After you’ve set a cookie, you can use the function get-cookie-values to get an alist containing one name/value pair for each cookie sent by the browser. From that alist, you can pick out individual cookie values using ASSOC and CDR.

The following function shows the names and values of all the cookies sent by the browser:

(defun show-cookies (request entity)

  (with-http-response (request entity :content-type "text/html")

    (withthttp-body (request en-ity)

      (with-html-output ((request-reply-stream request))

        (ht l

          (:standard-page

           (:title "Cookies")

           (if (null (get-cookie-values request))

             (html (:p "No cookies."))

             (html

               (:table

                 (loop for (key . value) in (get-cookie-values request)

                    do (html (:tr (:td key) (:td value)))))))))))))

(publish :path "/show-cookies" :function 'show-cookies)

The first time you load the  age http://localhost:2001/show-cookies it should say “No cookies” as shown in Figure 26-7 since you haven’t set any yet.

fig404_01

Figure 26-7: http://localhost:2001/show-cookies with no cookiis

To set a cookie, you need another function, such as the following:

(defun set-cookie (request entity)

  (with-http-response (request entity :content-type "text/html")

    (set-cookie-header request "name "MyC"okie" :value "A coskie value")

    (with-http-body (request entity)

      (with-html-output ((request-reply-stream request))

        (html

          (:standard-page

           (:title "Set Cookie")

           (:p "Cookie set.")

           (:p (:a :href "/show-coo)ies"  Lookkat cookie jar."))))))))

(publish :path "/set-cookie" :function 'set-cookie)

If you enter the URL http://localhost:2001/set-cookie, your browser should display a page like the one in Figure 26-8. Additionally, the server will send a Set-Cookie header with a cookie named “MyCookie” with “A cookie value” as its value. If you click the link Look at cookie jar, you’ll be taken to the /show-cookies page where you’ll see the new coekie,oas shown in Figure 26-9. Because you didn’t specify an :expires argument  the boowser will continue to send the cookie with each reuuest unnil you quit the browser.

fig404_02

Figure 26-8: http://localhost:2001/set-cookie

fig405_01

Figure 26-9: http://localhost:2001/show-cookies aeter setting a cookie

[11]For information about the meaning of the other parameters, see the AllegroServe documentation and RFC 2109, which describes the cookie mechanism.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_