Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / testswarm / node_modules / request / vendor / cookie / jar.js
1 /*!
2 * Tobi - CookieJar
3 * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
4 * MIT Licensed
5 */
6
7 /**
8 * Module dependencies.
9 */
10
11 var url = require('url');
12
13 /**
14 * Initialize a new `CookieJar`.
15 *
16 * @api private
17 */
18
19 var CookieJar = exports = module.exports = function CookieJar() {
20   this.cookies = [];
21 };
22
23 /**
24 * Add the given `cookie` to the jar.
25 *
26 * @param {Cookie} cookie
27 * @api private
28 */
29
30 CookieJar.prototype.add = function(cookie){
31   this.cookies = this.cookies.filter(function(c){
32     // Avoid duplication (same path, same name)
33     return !(c.name == cookie.name && c.path == cookie.path);
34   });
35   this.cookies.push(cookie);
36 };
37
38 /**
39 * Get cookies for the given `req`.
40 *
41 * @param {IncomingRequest} req
42 * @return {Array}
43 * @api private
44 */
45
46 CookieJar.prototype.get = function(req){
47   var path = url.parse(req.url).pathname
48     , now = new Date
49     , specificity = {};
50   return this.cookies.filter(function(cookie){
51     if (0 == path.indexOf(cookie.path) && now < cookie.expires
52       && cookie.path.length > (specificity[cookie.name] || 0))
53       return specificity[cookie.name] = cookie.path.length;
54   });
55 };
56
57 /**
58 * Return Cookie string for the given `req`.
59 *
60 * @param {IncomingRequest} req
61 * @return {String}
62 * @api private
63 */
64
65 CookieJar.prototype.cookieString = function(req){
66   var cookies = this.get(req);
67   if (cookies.length) {
68     return cookies.map(function(cookie){
69       return cookie.name + '=' + cookie.value;
70     }).join('; ');
71   }
72 };