Skip to content

Commit 201e4b0

Browse files
committed
php + wget + curl
1 parent eb8e777 commit 201e4b0

File tree

8 files changed

+139
-58
lines changed

8 files changed

+139
-58
lines changed
File renamed without changes.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "httpsnippet",
32
"version": "1.0.0",
3+
"name": "httpsnippet",
44
"description": "HTTP Request snippet generator for *most* languages",
55
"main": "./src/index.js",
6-
"bin": "./bin/httpsnippet.js",
6+
"bin": "./bin/httpsnippet",
77
"scripts": {
88
"test": "mocha -R spec"
99
},

src/index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
'use strict';
22

3-
var targets = {
4-
curl: require('./targets/curl.js'),
5-
php: require('./targets/php.js')
6-
};
3+
var mapper = require('./mapper');
4+
var url = require('url');
5+
6+
var curl = require('./targets/curl');
7+
var php = require('./targets/php');
8+
var wget = require('./targets/wget');
79

810
module.exports = function (req, lang, opts) {
9-
var target = targets[lang];
11+
var targets = {
12+
curl: curl,
13+
php: php,
14+
wget: wget
15+
};
16+
17+
// construct query string object
18+
req.queryObj = {};
19+
req.headersObj = {};
20+
21+
// construct query objects
22+
if (req.queryString && req.queryString.length) {
23+
req.queryString.map(mapper(req.queryObj));
24+
}
1025

11-
return target.call(target, req, opts);
12-
}
26+
// construct headers objects
27+
if (req.headers && req.headers.length) {
28+
req.headers.map(mapper(req.headersObj));
29+
}
30+
31+
// deconstruct and reset the uri
32+
req.uriObj = url.parse(req.url);
33+
34+
// reset the query string
35+
req.uriObj.query = req.queryObj;
36+
req.url = url.format(req.uriObj);
37+
38+
return targets[lang].call(lang, req, opts);
39+
};

src/mapper.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports = function (target) {
4+
return function (pair) {
5+
if (this[pair.name] === undefined) {
6+
return this[pair.name] = pair.value;
7+
}
8+
9+
// convert to array
10+
var arr = new Array(this[pair.name], pair.value);
11+
12+
this[pair.name] = arr;
13+
}.bind(target);
14+
};

src/targets/curl.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ var util = require('util');
55
module.exports = function (req, opts) {
66
var code = [];
77

8-
code.push('curl -X ' + req.method);
8+
code.push(util.format('curl --request %s', req.method));
9+
10+
code.push(util.format('--url "%s"', req.url));
911

1012
if (req.httpVersion === 'HTTP/1.0') {
11-
code.push('-0');
13+
code.push('--http1.0');
1214
}
1315

16+
// construct cookies argument
1417
if (req.cookies && req.cookies.length) {
15-
var cookies = req.cookies.map(function(cookie) {
18+
var cookies = req.cookies.map(function (cookie) {
1619
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
1720
});
1821

19-
code.push(util.format('-b "%s"', cookies.join('&')));
22+
code.push(util.format('--cookie "%s"', cookies.join('; ')));
2023
}
2124

2225
if (req.headers && req.headers.length) {
23-
req.headers.map(function(header) {
24-
code.push(util.format('-H "%s: %s"', header.name, header.value));
26+
req.headers.map(function (header) {
27+
code.push(util.format('--header "%s: %s"', header.name, header.value));
2528
});
2629
}
2730

2831
if (req.postData) {
29-
code.push('-d ' + JSON.stringify(req.postData.text));
32+
code.push('--data ' + JSON.stringify(req.postData.text));
3033
}
3134

32-
code.push(req.url);
33-
34-
return code.join(' \\\n');
35-
}
35+
return code.join(' \\\n ');
36+
};

src/targets/php.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,64 @@ module.exports = function (req, opts) {
88
code.push('$curl = curl_init();');
99

1010
var options = [{
11+
escape: true,
12+
name: 'CURLOPT_port',
13+
value: req.uriObj.port
14+
}, {
1115
escape: true,
1216
name: 'CURLOPT_URL',
1317
value: req.url
14-
},
15-
{
18+
}, {
1619
escape: false,
1720
name: 'CURLOPT_RETURNTRANSFER',
1821
value: 'true'
19-
},
20-
{
22+
}, {
2123
escape: false,
2224
name: 'CURLOPT_HTTP_VERSION',
2325
value: req.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
24-
},{
26+
}, {
2527
escape: true,
2628
name: 'CURLOPT_CUSTOMREQUEST',
2729
value: req.method
28-
},{
30+
}, {
2931
escape: true,
3032
name: 'CURLOPT_POSTFIELDS',
3133
value: req.postData ? req.postData.text : undefined
3234
}];
3335

36+
code.push('curl_setopt_array($curl, array(');
37+
38+
var curlopts = [];
39+
3440
options.map(function (option) {
3541
if (option.value) {
36-
code.push(util.format('curl_setopt($curl, %s, %s);', option.name, option.escape ? JSON.stringify(option.value) : option.value));
42+
curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value));
3743
}
3844
});
3945

40-
41-
/**
46+
// construct cookies
4247
if (req.cookies && req.cookies.length) {
43-
var cookies = req.cookies.map(function(cookie) {
48+
var cookies = req.cookies.map(function (cookie) {
4449
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
4550
});
4651

47-
code.push(util.format('-b "%s"', cookies.join('&')));
52+
curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')));
4853
}
4954

55+
// construct cookies
5056
if (req.headers && req.headers.length) {
51-
req.headers.map(function(header) {
52-
code.push(util.format('-H "%s: %s"', header.name, header.value));
57+
var headers = req.headers.map(function (header) {
58+
return util.format('"%s: %s"', header.name, header.value);
5359
});
54-
}
5560

56-
if (req.postData) {
57-
code.push(util.format('-d "%s"', req.postData.text));
61+
curlopts.push(util.format('CURLOPT_HTTPHEADER => array(\n\t\t%s\n\t),', headers.join(',\n\t\t')));
5862
}
59-
*/
63+
64+
code.push('\t' + curlopts.join('\n\t'));
65+
66+
code.push('));');
67+
code.push('$response = curl_exec($curl);');
68+
code.push('curl_close($curl);');
6069

6170
return code.join('\n');
62-
}
71+
};

src/targets/wget.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function (req, opts) {
6+
var code = [];
7+
8+
code.push('wget --quiet');
9+
10+
code.push(util.format('--method %s', req.method));
11+
12+
// construct cookies argument
13+
if (req.cookies && req.cookies.length) {
14+
var cookies = req.cookies.map(function (cookie) {
15+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
16+
});
17+
18+
code.push(util.format('--header "Cookie: %s"', cookies.join('; ')));
19+
}
20+
21+
if (req.headers && req.headers.length) {
22+
req.headers.map(function (header) {
23+
code.push(util.format('--header "%s: %s"', header.name, header.value));
24+
});
25+
}
26+
27+
if (req.postData) {
28+
code.push('--body-data ' + JSON.stringify(req.postData.text));
29+
}
30+
31+
code.push('--output-document');
32+
33+
code.push(util.format('- "%s"', req.url));
34+
35+
return code.join(' \\\n ');
36+
};

test/fixture.json

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
{
22
"method": "POST",
3-
"url": "http://api.domain.com/path/",
3+
"url": "http://httpconsole.com/debug",
44
"httpVersion": "HTTP/1.1",
55
"queryString": [
6-
{
7-
"name": "foo",
8-
"value": "bar"
9-
},
10-
{
11-
"name": "baz",
12-
"value": "abc"
13-
}
6+
{ "name": "foo", "value": "bar" },
7+
{ "name": "foo", "value": "baz" },
8+
{ "name": "baz", "value": "abc" }
149
],
1510
"headers": [
16-
{
17-
"name": "Accept",
18-
"value": "text/plain"
19-
},
20-
{
21-
"name": "Cookie",
22-
"value": "ijafhIAGWF3Awf93f"
23-
}
11+
{ "name": "Accept", "value": "text/plain" },
12+
{ "name": "Content-Type", "value": "application/json" },
13+
{ "name": "X-Pretty-Print", "value": "2" }
14+
],
15+
"cookies": [
16+
{ "name": "foo", "value": "bar", "path": "/", "domain": "www.httpconsole.com", "expires": "2015-02-11T04:28:14.821Z", "httpOnly": false, "secure": false },
17+
{ "name": "bar", "value": "baz", "path": "/", "domain": "www.httpconsole.com", "expires": "2015-02-11T04:28:14.821Z", "httpOnly": false, "secure": false }
2418
],
2519
"headersSize": 44,
2620
"bodySize": 14,
2721
"postData": {
28-
"size": 14,
29-
"mimeType": "application/json",
30-
"text": "{\"foo\": \"bar\"}"
22+
"size": 14,
23+
"mimeType": "application/json",
24+
"text": "{\"foo\": \"bar\"}"
3125
}
3226
}

0 commit comments

Comments
 (0)