Created
October 16, 2011 08:23
-
-
Save shinout/1290655 to your computer and use it in GitHub Desktop.
managing cookies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(url, ret) { | |
if (typeof url != "string") { | |
throw new Error("type of url must be string"); | |
} | |
var op = require('url').parse(url); | |
if (!op.hostname) { | |
if (op.pathname.charAt(0) != '/') { | |
var splits = op.pathname.split("/"); | |
op.hostname = splits.shift(); | |
op.pathname = splits.join("/"); | |
} | |
} | |
ret || (ret = {}); | |
ret.method = ret.method || 'GET'; | |
ret.host = op.hostname; | |
ret.port = op.port || (op.protocol == 'https:') ? 443 : 80; | |
ret.path = ( op.pathname | |
? ((op.pathname.slice(0,1) == "/") ? "" : "/") + op.pathname + (op.search || "") | |
: "/"); | |
ret.protocol = op.protocol ? op.protocol.slice(0, -1) : 'http'; | |
if (ret.method != 'GET') { | |
ret.body = require('querystring').stringify(ret.data); | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment