[XQZone General] Cookies
Ron Hitchens
ron.hitchens at marklogic.com
Thu Mar 10 15:52:38 PST 2005
John,
Thank you very much for this submission. As Jason
mentioned, I've been thinking of creating a Commons
project in the Code Workshop to collect this sort of
"building block" bits of code.
I'll create the new project shortly and provide
details of where to put your code.
In the meantime, I noticed that your get-cookie-date-string()
function could probably be greatly simplified by using
the builtin xdmp:strftime() function. See:
http://developer.marklogic.com/pubs/2.2/apidocs/Extension.html#strftime
On Mar 10, 2005, at 3:18 AM, John Snelson wrote:
> I have recently delevoped an XQuery library to perform cookies
> operations, and was wondering whether it might be useful to anyone.
> It's not large, so I've attached it.
>
> Let me know if you're using it, and what could be improved with it...
>
> John Snelson
>
> --
> John Snelson, XML Consultant +44-1865-811184
> Parthenon Computing Ltd http://blog.parthcomp.com/dbxml
> (:~
> : Mark Logic CIS Cookie Library
> :
> : Copyright 2005 Parthenon Computing Ltd.
> :
> : Licensed under the Apache License, Version 2.0 (the "License");
> : you may not use this file except in compliance with the License.
> : You may obtain a copy of the License at
> :
> : http://www.apache.org/licenses/LICENSE-2.0
> :
> : Unless required by applicable law or agreed to in writing, software
> : distributed under the License is distributed on an "AS IS" BASIS,
> : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> : See the License for the specific language governing permissions and
> : limitations under the License.
> :
> : @author John Snelson (john at parthcomp.com)
> : @version 1.0
> :
> : @see http://wp.netscape.com/newsref/std/cookie_spec.html
> :
> :)
>
> module "http://parthcomp.com/cookies"
> declare namespace ck="http://parthcomp.com/cookies"
>
> (:~
> : Adds a cookie header to the response headers.
> :
> : @param $name the name of the cookie
> :
> : @param $value the value to store in the cookie
> :
> : @param $expires an optional date and time for the cookie to expire.
> : If this is in the past, the cookie is deleted from the client. If
> : an empty sequence is passed as this parameter, then the cookie will
> : expire when the user's session ends.
> :
> : @param $domain the domain name for which this cookie is valid.
> : If an empty sequence is passed as this parameter, then the domain
> name
> : or the current server will be used.
> :
> : @param $path the URLs in the domain for which the cookie is valid.
> This
> : should be a path starting with a "/". If an empty sequence is
> passed as
> : this parameter, then the cookie behaves as if the path was "/".
> :
> : @param $secure whether this cookie should only be sent over a secure
> : connection.
> :
> : @return ()
> :
> : @error Invalid domain parameter
> : @error Invalid path parameter
> :
> :)
> define function add-cookie($name as xs:string, $value as xs:string,
> $expires as xs:dateTime?,
> $domain as xs:string?, $path as xs:string?,
> $secure as xs:boolean) as empty()
> {
> if(fn:contains($domain, " ") or fn:contains($domain, ",") or
> fn:contains($domain, ";")) then (
> fn:error("Invalid domain parameter")
> ) else (),
>
> if(fn:contains($path, " ") or fn:contains($path, ",") or
> fn:contains($path, ";")) then (
> fn:error("Invalid path parameter")
> ) else (),
>
> let $cookie := fn:concat(xdmp:url-encode($name), "=",
> xdmp:url-encode($value))
> let $cookie := if(fn:exists($expires)) then fn:concat($cookie, ";
> expires=", get-cookie-date-string($expires)) else $cookie
> let $cookie := if(fn:exists($domain)) then fn:concat($cookie, ";
> domain=", $domain) else $cookie
> let $cookie := if(fn:exists($path)) then fn:concat($cookie, ";
> path=", $path) else $cookie
> let $cookie := if($secure) then fn:concat($cookie, "; secure") else
> $cookie
> return xdmp:add-response-header("Set-Cookie", $cookie)
> }
>
> (:~
> : Adds a cookie header to the response headers, that will delete
> : the specified client side cookie. It is important to specify the
> : correct domain and path for the cookie, otherwise it won't be
> : deleted.
> :
> : @param $name the name of the cookie
> :
> : @param $domain the domain name for which this cookie is valid.
> : If an empty sequence is passed as this parameter, then the domain
> name
> : or the current server will be used.
> :
> : @param $path the URLs in the domain for which the cookie is valid.
> This
> : should be a path starting with a "/". If an empty sequence is
> passed as
> : this parameter, then the cookie behaves as if the path was "/".
> :
> : @return ()
> :
> : @error Invalid domain parameter
> : @error Invalid path parameter
> :
> :)
> define function delete-cookie($name as xs:string, $domain as
> xs:string?, $path as xs:string?) as empty()
> {
> add-cookie($name, "", xs:dateTime("1979-11-27T06:23:37"), (), $path,
> fn:false())
> }
>
> (:~
> : Retrieves a named cookie from the request headers.
> :
> : @param $name the name of the cookie
> :
> : @return a sequence containing the values for the given cookie name.
> : If no cookies of that name were found, the empty sequence is
> returned.
> :
> :)
> define function get-cookie($name as xs:string) as xs:string*
> {
> let $urlname := xdmp:url-encode($name)
> let $header := xdmp:get-request-header("Cookie")
> let $cookies := fn:tokenize($header, "; ?")[fn:starts-with(.,
> $urlname)]
> for $c in $cookies
> return xdmp:url-decode(fn:substring-after($c, "="))
> }
>
> (:~
> : Retrieves the names of all the cookies available from the request
> : headers.
> :
> : @return a sequence containing the names of the available cookies.
> : If no cookies were found, the empty sequence is returned.
> :
> :)
> define function get-cookie-names() as xs:string*
> {
> fn:distinct-values(
> let $header := xdmp:get-request-header("Cookie")
> let $cookies := fn:tokenize($header, "; ?")
> for $c in $cookies
> return xdmp:url-decode(fn:substring-before($c, "="))
> )
> }
>
> (:~
> : Returns an RFC 822 compliant date string from the given dateTime,
> : that is suitable for use in a cookie header.
> :
> : @param $date the date and time to convert into a string
> :
> : @return the RFC 822 complient date string.
> :
> :)
> define function get-cookie-date-string($date as xs:dateTime) as
> xs:string
> {
> let $gmt := xdt:dayTimeDuration("PT0H")
> let $date := fn:adjust-dateTime-to-timezone($date, $gmt)
> let $day := two-digits(fn:get-day-from-dateTime($date))
> let $month := fn:get-month-from-dateTime($date)
> let $year := fn:string(fn:get-year-from-dateTime($date))
> let $hours := two-digits(fn:get-hours-from-dateTime($date))
> let $minutes := two-digits(fn:get-minutes-from-dateTime($date))
> let $seconds :=
> two-digits(xs:integer(fn:round(fn:get-seconds-from-dateTime($date))))
> return fn:concat(
> $day, "-",
> if($month eq 1) then "Jan"
> else if($month eq 2) then "Feb"
> else if($month eq 3) then "Mar"
> else if($month eq 4) then "Apr"
> else if($month eq 5) then "May"
> else if($month eq 6) then "Jun"
> else if($month eq 7) then "Jul"
> else if($month eq 8) then "Aug"
> else if($month eq 9) then "Sep"
> else if($month eq 10) then "Oct"
> else if($month eq 11) then "Nov"
> else if($month eq 12) then "Dec"
> else fn:error("Invalid month"), "-",
> $year, " ",
> $hours, ":",
> $minutes, ":",
> $seconds, " GMT"
> )
> }
>
> (:~
> : Internal function to return a string representation
> : of an integer that contains two digits. If the number
> : has less than two digits it is padded with zeros. If
> : it has more than two digits, it is truncated, and the
> : least significant digits are returned.
> :
> : @param $num the number to convert
> :
> : @return the two digit string
> :
> :)
> define function two-digits($num as xs:integer) as xs:string
> {
> let $result := fn:string($num)
> let $length := fn:string-length($result)
> let $result := if($length > 2) then fn:substring($result, $length -
> 1) else $result
> let $result := if($length = 1) then fn:concat("0", $result) else
> $result
> let $result := if($length = 0) then "00" else $result
> return $result
> }_______________________________________________
> General mailing list
> General at xqzone.marklogic.com
> http://xqzone.com/mailman/listinfo/general
>
---
Ron Hitchens {ron.hitchens at marklogic.com} 650-655-2351
This e-mail and any accompanying attachments are confidential. The
information is intended solely for the use of the individual to whom it
is addressed. Any review, disclosure, copying, distribution, or use of
this e-mail communication by others is strictly prohibited. If you are
not the intended recipient, please notify us immediately by returning
this message to the sender and delete all copies. Thank you for your
cooperation.
More information about the General
mailing list