\href{https://npmjs.org/package/cookie}{\texttt{ }} \href{https://npmjs.org/package/cookie}{\texttt{ }} \href{https://nodejs.org/en/download}{\texttt{ }} \href{https://travis-ci.org/jshttp/cookie}{\texttt{ }} \href{https://coveralls.io/r/jshttp/cookie?branch=master}{\texttt{ }}

Basic HTTP cookie parser and serializer for HTTP servers.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6563}{}\doxysection{Installation}\label{md_sfcode_node_modules_cookie_README_autotoc_md6563}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install cookie }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6564}{}\doxysection{API}\label{md_sfcode_node_modules_cookie_README_autotoc_md6564}

\begin{DoxyCode}{0}
\DoxyCodeLine{var cookie = require('cookie'); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6565}{}\doxysubsection{cookie.\+parse(str, options)}\label{md_sfcode_node_modules_cookie_README_autotoc_md6565}
Parse an HTTP {\ttfamily Cookie} header string and returning an object of all cookie name-\/value pairs. The {\ttfamily str} argument is the string representing a {\ttfamily Cookie} header value and {\ttfamily options} is an optional object containing additional parsing options.


\begin{DoxyCode}{0}
\DoxyCodeLine{var cookies = cookie.parse('foo=bar; equation=E\%3Dmc\%5E2');}
\DoxyCodeLine{// \{ foo: 'bar', equation: 'E=mc\string^2' \} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6566}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_cookie_README_autotoc_md6566}
{\ttfamily cookie.\+parse} accepts these properties in the options object.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6567}{}\doxyparagraph{decode}\label{md_sfcode_node_modules_cookie_README_autotoc_md6567}
Specifies a function that will be used to decode a cookie\textquotesingle{}s value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously-\/encoded cookie value into a Java\+Script string or other object.

The default function is the global {\ttfamily decode\+URIComponent}, which will decode any URL-\/encoded sequences into their byte representations.

{\bfseries{note}} if an error is thrown from this function, the original, non-\/decoded cookie value will be returned as the cookie\textquotesingle{}s value.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6568}{}\doxysubsection{cookie.\+serialize(name, value, options)}\label{md_sfcode_node_modules_cookie_README_autotoc_md6568}
Serialize a cookie name-\/value pair into a {\ttfamily Set-\/\+Cookie} header string. The {\ttfamily name} argument is the name for the cookie, the {\ttfamily value} argument is the value to set the cookie to, and the {\ttfamily options} argument is an optional object containing additional serialization options.


\begin{DoxyCode}{0}
\DoxyCodeLine{var setCookie = cookie.serialize('foo', 'bar');}
\DoxyCodeLine{// foo=bar }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6569}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_cookie_README_autotoc_md6569}
{\ttfamily cookie.\+serialize} accepts these properties in the options object.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6570}{}\doxyparagraph{domain}\label{md_sfcode_node_modules_cookie_README_autotoc_md6570}
Specifies the value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.3}{\texttt{ {\ttfamily Domain} {\ttfamily Set-\/\+Cookie} attribute}}. By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6571}{}\doxyparagraph{encode}\label{md_sfcode_node_modules_cookie_README_autotoc_md6571}
Specifies a function that will be used to encode a cookie\textquotesingle{}s value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie\textquotesingle{}s value.

The default function is the global {\ttfamily encode\+URIComponent}, which will encode a Java\+Script string into UTF-\/8 byte sequences and then URL-\/encode any that fall outside of the cookie range.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6572}{}\doxyparagraph{expires}\label{md_sfcode_node_modules_cookie_README_autotoc_md6572}
Specifies the {\ttfamily Date} object to be the value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.1}{\texttt{ {\ttfamily Expires} {\ttfamily Set-\/\+Cookie} attribute}}. By default, no expiration is set, and most clients will consider this a \char`\"{}non-\/persistent cookie\char`\"{} and will delete it on a condition like exiting a web browser application.

{\bfseries{note}} the \href{https://tools.ietf.org/html/rfc6265\#section-5.3}{\texttt{ cookie storage model specification}} states that if both {\ttfamily expires} and {\ttfamily max\+Age} are set, then {\ttfamily max\+Age} takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6573}{}\doxyparagraph{http\+Only}\label{md_sfcode_node_modules_cookie_README_autotoc_md6573}
Specifies the {\ttfamily boolean} value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.6}{\texttt{ {\ttfamily Http\+Only} {\ttfamily Set-\/\+Cookie} attribute}}. When truthy, the {\ttfamily Http\+Only} attribute is set, otherwise it is not. By default, the {\ttfamily Http\+Only} attribute is not set.

{\bfseries{note}} be careful when setting this to {\ttfamily true}, as compliant clients will not allow client-\/side Java\+Script to see the cookie in {\ttfamily document.\+cookie}.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6574}{}\doxyparagraph{max\+Age}\label{md_sfcode_node_modules_cookie_README_autotoc_md6574}
Specifies the {\ttfamily number} (in seconds) to be the value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.2}{\texttt{ {\ttfamily Max-\/\+Age} {\ttfamily Set-\/\+Cookie} attribute}}. The given number will be converted to an integer by rounding down. By default, no maximum age is set.

{\bfseries{note}} the \href{https://tools.ietf.org/html/rfc6265\#section-5.3}{\texttt{ cookie storage model specification}} states that if both {\ttfamily expires} and {\ttfamily max\+Age} are set, then {\ttfamily max\+Age} takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6575}{}\doxyparagraph{path}\label{md_sfcode_node_modules_cookie_README_autotoc_md6575}
Specifies the value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.4}{\texttt{ {\ttfamily Path} {\ttfamily Set-\/\+Cookie} attribute}}. By default, the path is considered the \href{https://tools.ietf.org/html/rfc6265\#section-5.1.4}{\texttt{ \char`\"{}default path\char`\"{}}}.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6576}{}\doxyparagraph{same\+Site}\label{md_sfcode_node_modules_cookie_README_autotoc_md6576}
Specifies the {\ttfamily boolean} or {\ttfamily string} to be the value for the \href{https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03\#section-4.1.2.7}{\texttt{ {\ttfamily Same\+Site} {\ttfamily Set-\/\+Cookie} attribute}}.


\begin{DoxyItemize}
\item {\ttfamily true} will set the {\ttfamily Same\+Site} attribute to {\ttfamily Strict} for strict same site enforcement.
\item {\ttfamily false} will not set the {\ttfamily Same\+Site} attribute.
\item `\textquotesingle{}lax'{\ttfamily will set the}Same\+Site{\ttfamily attribute to}Lax{\ttfamily for lax same site enforcement. -\/}\textquotesingle{}none\textquotesingle{}{\ttfamily will set the}Same\+Site{\ttfamily attribute to}None{\ttfamily for an explicit cross-\/site cookie. -\/}\textquotesingle{}strict\textquotesingle{}{\ttfamily will set the}Same\+Site{\ttfamily attribute to}Strict\`{} for strict same site enforcement.
\end{DoxyItemize}

More information about the different enforcement levels can be found in \href{https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03\#section-4.1.2.7}{\texttt{ the specification}}.

{\bfseries{note}} This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6577}{}\doxyparagraph{secure}\label{md_sfcode_node_modules_cookie_README_autotoc_md6577}
Specifies the {\ttfamily boolean} value for the \href{https://tools.ietf.org/html/rfc6265\#section-5.2.5}{\texttt{ {\ttfamily Secure} {\ttfamily Set-\/\+Cookie} attribute}}. When truthy, the {\ttfamily Secure} attribute is set, otherwise it is not. By default, the {\ttfamily Secure} attribute is not set.

{\bfseries{note}} be careful when setting this to {\ttfamily true}, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection.\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6578}{}\doxysection{Example}\label{md_sfcode_node_modules_cookie_README_autotoc_md6578}
The following example uses this module in conjunction with the Node.\+js core HTTP server to prompt a user for their name and display it back on future visits.


\begin{DoxyCode}{0}
\DoxyCodeLine{var cookie = require('cookie');}
\DoxyCodeLine{var escapeHtml = require('escape-\/html');}
\DoxyCodeLine{var http = require('http');}
\DoxyCodeLine{var url = require('url');}
\DoxyCodeLine{}
\DoxyCodeLine{function onRequest(req, res) \{}
\DoxyCodeLine{  // Parse the query string}
\DoxyCodeLine{  var query = url.parse(req.url, true, true).query;}
\DoxyCodeLine{}
\DoxyCodeLine{  if (query \&\& query.name) \{}
\DoxyCodeLine{    // Set a new cookie with the name}
\DoxyCodeLine{    res.setHeader('Set-\/Cookie', cookie.serialize('name', String(query.name), \{}
\DoxyCodeLine{      httpOnly: true,}
\DoxyCodeLine{      maxAge: 60 * 60 * 24 * 7 // 1 week}
\DoxyCodeLine{    \}));}
\DoxyCodeLine{}
\DoxyCodeLine{    // Redirect back after setting cookie}
\DoxyCodeLine{    res.statusCode = 302;}
\DoxyCodeLine{    res.setHeader('Location', req.headers.referer || '/');}
\DoxyCodeLine{    res.end();}
\DoxyCodeLine{    return;}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // Parse the cookies on the request}
\DoxyCodeLine{  var cookies = cookie.parse(req.headers.cookie || '');}
\DoxyCodeLine{}
\DoxyCodeLine{  // Get the visitor name set in the cookie}
\DoxyCodeLine{  var name = cookies.name;}
\DoxyCodeLine{}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/html; charset=UTF-\/8');}
\DoxyCodeLine{}
\DoxyCodeLine{  if (name) \{}
\DoxyCodeLine{    res.write('<p>Welcome back, <b>' + escapeHtml(name) + '</b>!</p>');}
\DoxyCodeLine{  \} else \{}
\DoxyCodeLine{    res.write('<p>Hello, new visitor!</p>');}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  res.write('<form method="{}GET"{}>');}
\DoxyCodeLine{  res.write('<input placeholder="{}enter your name"{} name="{}name"{}> <input type="{}submit"{} value="{}Set Name"{}>');}
\DoxyCodeLine{  res.end('</form>');}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(onRequest).listen(3000); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6579}{}\doxysection{Testing}\label{md_sfcode_node_modules_cookie_README_autotoc_md6579}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm test }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6580}{}\doxysection{Benchmark}\label{md_sfcode_node_modules_cookie_README_autotoc_md6580}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm run bench}
\DoxyCodeLine{}
\DoxyCodeLine{> cookie@0.3.1 bench cookie}
\DoxyCodeLine{> node benchmark/index.js}
\DoxyCodeLine{}
\DoxyCodeLine{  http\_parser@2.8.0}
\DoxyCodeLine{  node@6.14.2}
\DoxyCodeLine{  v8@5.1.281.111}
\DoxyCodeLine{  uv@1.16.1}
\DoxyCodeLine{  zlib@1.2.11}
\DoxyCodeLine{  ares@1.10.1-\/DEV}
\DoxyCodeLine{  icu@58.2}
\DoxyCodeLine{  modules@48}
\DoxyCodeLine{  napi@3}
\DoxyCodeLine{  openssl@1.0.2o}
\DoxyCodeLine{}
\DoxyCodeLine{> node benchmark/parse.js}
\DoxyCodeLine{}
\DoxyCodeLine{  cookie.parse}
\DoxyCodeLine{}
\DoxyCodeLine{  6 tests completed.}
\DoxyCodeLine{}
\DoxyCodeLine{  simple      x 1,200,691 ops/sec ±1.12\% (189 runs sampled)}
\DoxyCodeLine{  decode      x 1,012,994 ops/sec ±0.97\% (186 runs sampled)}
\DoxyCodeLine{  unquote     x 1,074,174 ops/sec ±2.43\% (186 runs sampled)}
\DoxyCodeLine{  duplicates  x   438,424 ops/sec ±2.17\% (184 runs sampled)}
\DoxyCodeLine{  10 cookies  x   147,154 ops/sec ±1.01\% (186 runs sampled)}
\DoxyCodeLine{  100 cookies x    14,274 ops/sec ±1.07\% (187 runs sampled) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6581}{}\doxysection{References}\label{md_sfcode_node_modules_cookie_README_autotoc_md6581}

\begin{DoxyItemize}
\item \href{https://tools.ietf.org/html/rfc6265}{\texttt{ RFC 6265\+: HTTP State Management Mechanism}}
\item \href{https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03\#section-4.1.2.7}{\texttt{ Same-\/site Cookies}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_cookie_README_autotoc_md6582}{}\doxysection{License}\label{md_sfcode_node_modules_cookie_README_autotoc_md6582}
\mbox{[}MIT\mbox{]}(LICENSE) 