Skip to content

Commit 5a86147

Browse files
committed
new: php pecl_http v1
new: php pecl_http v2 add: source.cookiesObj
1 parent 1455c35 commit 5a86147

27 files changed

+765
-4
lines changed

src/targets/node/request.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ module.exports = function (source, options) {
5252
source.postData.params.forEach(function (param) {
5353
var attachement = {}
5454

55-
if (param.value) {
56-
attachement.value = param.value
57-
} else if (param.fileName) {
55+
if (param.fileName && !param.value) {
5856
includeFS = true
57+
5958
attachement.value = 'fs.createReadStream("' + param.fileName + '")'
59+
} else if (param.value) {
60+
attachement.value = param.value
6061
}
6162

6263
if (param.fileName) {
@@ -73,7 +74,7 @@ module.exports = function (source, options) {
7374
break
7475

7576
default:
76-
if (source.postData.text !== '') {
77+
if (source.postData.text) {
7778
reqOpts.body = source.postData.text
7879
}
7980
}

src/targets/php/helpers.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
var convert = function (obj, indent, last_indent) {
4+
var i, result
5+
6+
if (!last_indent) {
7+
last_indent = ''
8+
}
9+
10+
switch (Object.prototype.toString.call(obj)) {
11+
case '[object Null]':
12+
result = 'null'
13+
break
14+
15+
case '[object Undefined]':
16+
result = 'null'
17+
break
18+
19+
case '[object String]':
20+
result = "'" + obj.replace(/\\/g, '\\\\').replace(/\'/g, "\'") + "'"
21+
break
22+
23+
case '[object Number]':
24+
result = obj.toString()
25+
break
26+
27+
case '[object Array]':
28+
result = []
29+
30+
obj.map(function (item) {
31+
result.push(convert(item, indent + indent, indent))
32+
})
33+
34+
result = 'array(\n' + indent + result.join(',\n' + indent) + '\n' + last_indent + ')'
35+
break
36+
37+
case '[object Object]':
38+
result = []
39+
for (i in obj) {
40+
if (obj.hasOwnProperty(i)) {
41+
result.push(convert(i, indent) + ' => ' + convert(obj[i], indent + indent, indent))
42+
}
43+
}
44+
result = 'array(\n' + indent + result.join(',\n' + indent) + '\n' + last_indent + ')'
45+
break
46+
47+
default:
48+
result = 'null'
49+
}
50+
51+
return result
52+
}
53+
54+
module.exports = {
55+
convert: convert,
56+
methods: [
57+
'ACL',
58+
'BASELINE_CONTROL',
59+
'CHECKIN',
60+
'CHECKOUT',
61+
'CONNECT',
62+
'COPY',
63+
'DELETE',
64+
'GET',
65+
'HEAD',
66+
'LABEL',
67+
'LOCK',
68+
'MERGE',
69+
'MKACTIVITY',
70+
'MKCOL',
71+
'MKWORKSPACE',
72+
'MOVE',
73+
'OPTIONS',
74+
'POST',
75+
'PROPFIND',
76+
'PROPPATCH',
77+
'PUT',
78+
'REPORT',
79+
'TRACE',
80+
'UNCHECKOUT',
81+
'UNLOCK',
82+
'UPDATE',
83+
'VERSION_CONTROL'
84+
]
85+
}

src/targets/php/http1.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for PHP using curl-ext.
4+
*
5+
* @author
6+
* @AhmadNassri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var helpers = require('./helpers')
15+
16+
module.exports = function (source, options) {
17+
var opts = util._extend({
18+
indent: ' ',
19+
noTags: false,
20+
closingTag: false
21+
}, options)
22+
23+
var code = []
24+
25+
if (!opts.noTags) {
26+
code.push('<?php')
27+
code.push(null)
28+
}
29+
30+
if (!~helpers.methods.indexOf(source.method.toUpperCase())) {
31+
code.push(util.format("HttpRequest::methodRegister('%s');", source.method))
32+
}
33+
34+
code.push('$request = new HttpRequest();')
35+
code.push(util.format('$request->setUrl(%s);', helpers.convert(source.url)))
36+
37+
if (~helpers.methods.indexOf(source.method.toUpperCase())) {
38+
code.push(util.format('$request->setMethod(HTTP_METH_%s);', source.method.toUpperCase()))
39+
} else {
40+
code.push(util.format('$request->setMethod(HttpRequest::HTTP_METH_%s);', source.method.toUpperCase()))
41+
}
42+
code.push(null)
43+
44+
if (Object.keys(source.queryObj).length) {
45+
code.push(util.format('$request->setQueryData(%s);', helpers.convert(source.queryObj, opts.indent)))
46+
code.push(null)
47+
}
48+
49+
if (Object.keys(source.headersObj).length) {
50+
code.push(util.format('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)))
51+
code.push(null)
52+
}
53+
54+
if (Object.keys(source.cookiesObj).length) {
55+
code.push(util.format('$request->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)))
56+
code.push(null)
57+
}
58+
59+
switch (source.postData.mimeType) {
60+
case 'application/x-www-form-urlencoded':
61+
code.push(util.format('$request->setContentType(%s);', helpers.convert(source.postData.mimeType)))
62+
63+
code.push(util.format('$request->setPostFields(%s);', helpers.convert(source.postData.paramsObj, opts.indent)))
64+
code.push(null)
65+
break
66+
67+
default:
68+
if (source.postData.text) {
69+
code.push(util.format('$request->setBody(%s);', helpers.convert(source.postData.text)))
70+
code.push(null)
71+
}
72+
}
73+
74+
code.push('try {')
75+
code.push(opts.indent + '$response = $request->send();')
76+
code.push(null)
77+
code.push(opts.indent + 'echo $response->getBody();')
78+
code.push('} catch (HttpException $ex) {')
79+
code.push(opts.indent + 'echo $ex;')
80+
code.push('}')
81+
82+
if (opts.closingTag) {
83+
code.push(null)
84+
code.push('?>')
85+
}
86+
87+
return code.join('\n')
88+
}
89+
90+
module.exports.info = {
91+
key: 'http1',
92+
title: 'HTTP v1',
93+
link: 'http://php.net/manual/en/book.http.php',
94+
description: 'PHP with pecl/http v1'
95+
}

src/targets/php/http2.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for PHP using curl-ext.
4+
*
5+
* @author
6+
* @AhmadNassri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var helpers = require('./helpers')
15+
16+
module.exports = function (source, options) {
17+
var opts = util._extend({
18+
indent: ' ',
19+
noTags: false,
20+
closingTag: false
21+
}, options)
22+
23+
var code = []
24+
var hasBody = false
25+
26+
if (!opts.noTags) {
27+
code.push('<?php')
28+
code.push(null)
29+
}
30+
31+
if (!~helpers.methods.indexOf(source.method.toUpperCase())) {
32+
code.push(util.format("HttpRequest::methodRegister('%s');", source.method))
33+
}
34+
35+
code.push('$client = new http\\Client;')
36+
code.push('$request = new http\\Client\\Request;')
37+
code.push(null)
38+
39+
switch (source.postData.mimeType) {
40+
case 'application/x-www-form-urlencoded':
41+
code.push('$body = new http\\Message\\Body;')
42+
code.push(util.format('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent)))
43+
code.push(null)
44+
hasBody = true
45+
break
46+
47+
case 'multipart/form-data':
48+
var files = []
49+
var fields = {}
50+
51+
source.postData.params.forEach(function (param) {
52+
if (param.fileName) {
53+
files.push({
54+
name: param.name,
55+
type: param.contentType,
56+
file: param.fileName,
57+
data: param.value
58+
})
59+
} else if (param.value) {
60+
fields[param.name] = param.value
61+
}
62+
})
63+
64+
code.push('$body = new http\\Message\\Body;')
65+
66+
code.push(util.format('$body->addForm(%s, %s);',
67+
Object.keys(fields).length ? helpers.convert(fields, opts.indent) : 'NULL',
68+
files.length ? helpers.convert(files, opts.indent) : 'NULL'
69+
))
70+
71+
// remove the contentType header
72+
if (~source.headersObj['content-type'].indexOf('boundary')) {
73+
delete source.headersObj['content-type']
74+
}
75+
76+
code.push(null)
77+
78+
hasBody = true
79+
break
80+
81+
default:
82+
if (source.postData.text) {
83+
code.push('$body = new http\\Message\\Body;')
84+
code.push(util.format('$body->append(%s);', helpers.convert(source.postData.text)))
85+
code.push(null)
86+
hasBody = true
87+
}
88+
}
89+
90+
code.push(util.format('$request->setRequestUrl(%s);', helpers.convert(source.url)))
91+
code.push(util.format('$request->setRequestMethod(%s);', helpers.convert(source.method)))
92+
93+
if (hasBody) {
94+
code.push(util.format('$request->setBody($body);'))
95+
code.push(null)
96+
}
97+
98+
if (Object.keys(source.queryObj).length) {
99+
code.push(util.format('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent)))
100+
code.push(null)
101+
}
102+
103+
if (Object.keys(source.headersObj).length) {
104+
code.push(util.format('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)))
105+
code.push(null)
106+
}
107+
108+
if (Object.keys(source.cookiesObj).length) {
109+
code.push(null)
110+
code.push(util.format('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)))
111+
code.push(null)
112+
}
113+
114+
code.push('$client->enqueue($request)->send();')
115+
code.push('$response = $client->getResponse();')
116+
code.push(null)
117+
code.push('echo $response->getBody();')
118+
119+
if (opts.closingTag) {
120+
code.push(null)
121+
code.push('?>')
122+
}
123+
124+
return code.join('\n')
125+
}
126+
127+
module.exports.info = {
128+
key: 'http2',
129+
title: 'HTTP v2',
130+
link: 'http://devel-m6w6.rhcloud.com/mdref/http',
131+
description: 'PHP with pecl/http v2'
132+
}

test/fixtures/available-targets.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
"title": "cURL",
7777
"link": "http://php.net/manual/en/book.curl.php",
7878
"description": "PHP with libcurl"
79+
},
80+
{
81+
"key": "http1",
82+
"title": "HTTP v1",
83+
"link": "http://php.net/manual/en/book.http.php",
84+
"description": "PHP with pecl/http v1"
85+
},
86+
{
87+
"key": "http2",
88+
"title": "HTTP v2",
89+
"link": "http://devel-m6w6.rhcloud.com/mdref/http",
90+
"description": "PHP with pecl/http v2"
7991
}
8092
]
8193
},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
$request = new HttpRequest();
4+
$request->setUrl('http://mockbin.com/har');
5+
$request->setMethod(HTTP_METH_POST);
6+
7+
$request->setHeaders(array(
8+
'content-type' => 'application/x-www-form-urlencoded'
9+
));
10+
11+
$request->setContentType('application/x-www-form-urlencoded');
12+
$request->setPostFields(array(
13+
'foo' => 'bar',
14+
'hello' => 'world'
15+
));
16+
17+
try {
18+
$response = $request->send();
19+
20+
echo $response->getBody();
21+
} catch (HttpException $ex) {
22+
echo $ex;
23+
}

0 commit comments

Comments
 (0)