7d54d940cc7c471f577e742ff57185a53d946b48
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / node_modules / cookie-jar / index.js
1 /*!
2  * Tobi - Cookie
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 `Cookie` with the given cookie `str` and `req`.
15  *
16  * @param {String} str
17  * @param {IncomingRequest} req
18  * @api private
19  */
20
21 var Cookie = exports = module.exports = function Cookie(str, req) {
22   this.str = str;
23
24   // Map the key/val pairs
25   str.split(/ *; */).reduce(function(obj, pair){
26    var p = pair.indexOf('=');
27    var key = p > 0 ? pair.substring(0, p).trim() : pair.trim();
28    var lowerCasedKey = key.toLowerCase();
29    var value = p > 0 ? pair.substring(p + 1).trim() : true;
30
31    if (!obj.name) {
32     // First key is the name
33     obj.name = key;
34     obj.value = value;
35    }
36    else if (lowerCasedKey === 'httponly') {
37     obj.httpOnly = value;
38    }
39    else {
40     obj[lowerCasedKey] = value;
41    }
42    return obj;
43   }, this);
44
45   // Expires
46   this.expires = this.expires
47     ? new Date(this.expires)
48     : Infinity;
49
50   // Default or trim path
51   this.path = this.path
52     ? this.path.trim(): req 
53     ? url.parse(req.url).pathname: '/';
54 };
55
56 /**
57  * Return the original cookie string.
58  *
59  * @return {String}
60  * @api public
61  */
62
63 Cookie.prototype.toString = function(){
64   return this.str;
65 };
66
67 module.exports.Jar = require('./jar')