Skip to content

Commit bad12d3

Browse files
author
Shashi Ranjan
committed
C module
1 parent 2db6089 commit bad12d3

19 files changed

Lines changed: 164 additions & 1 deletion

src/targets/c/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

src/targets/c/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'c',
5+
title: 'C',
6+
extname: '.c',
7+
default: 'libcurl'
8+
}

src/targets/c/libcurl.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
var CodeBuilder = require('../../helpers/code-builder')
4+
5+
module.exports = function (source, options) {
6+
var code = new CodeBuilder()
7+
8+
code.push('CURL *hnd = curl_easy_init();')
9+
code.push('curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "%s");', source.method.toUpperCase())
10+
code.push('curl_easy_setopt(hnd, CURLOPT_URL, "%s");', source.fullUrl)
11+
12+
// Add headers, including the cookies
13+
var headers = Object.keys(source.headersObj)
14+
15+
// construct headers
16+
if (headers.length) {
17+
code.push('struct curl_slist *headers = NULL;')
18+
headers.map(function (key) {
19+
code.push('headers = curl_slist_append(headers, "%s: %s");', key, source.headersObj[key])
20+
})
21+
code.push('curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);')
22+
}
23+
24+
// construct cookies
25+
if (source.allHeaders.cookie) {
26+
code.push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', source.allHeaders.cookie)
27+
}
28+
29+
if (source.postData.text) {
30+
code.push('curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, %s);', JSON.stringify(source.postData.text))
31+
}
32+
33+
code.push('CURLcode ret = curl_easy_perform(hnd);')
34+
35+
return code.join()
36+
}
37+
38+
module.exports.info = {
39+
key: 'libcurl',
40+
title: 'Libcurl',
41+
link: 'http://curl.haxx.se/libcurl/',
42+
description: 'Simple REST and HTTP API Client for C'
43+
}

test/fixtures/available-targets.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
}
202202
]
203203
},
204-
{
204+
{
205205
"key": "csharp",
206206
"title": "C#",
207207
"extname": ".cs",
@@ -214,5 +214,19 @@
214214
"description": "Simple REST and HTTP API Client for .NET"
215215
}
216216
]
217+
},
218+
{
219+
"key": "c",
220+
"title": "C",
221+
"extname": ".c",
222+
"default": "libcurl",
223+
"clients": [
224+
{
225+
"key": "libcurl",
226+
"title": "Libcurl",
227+
"link": "http://curl.haxx.se/libcurl/",
228+
"description": "Simple REST and HTTP API Client for C"
229+
}
230+
]
217231
}
218232
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
4+
struct curl_slist *headers = NULL;
5+
headers = curl_slist_append(headers, "content-type: application/x-www-form-urlencoded");
6+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
7+
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "foo=bar&hello=world");
8+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
4+
struct curl_slist *headers = NULL;
5+
headers = curl_slist_append(headers, "content-type: application/json");
6+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
7+
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}]}");
8+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
4+
curl_easy_setopt(hnd, CURLOPT_COOKIE, "foo=bar; bar=baz");
5+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "PROPFIND");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
4+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value");
4+
struct curl_slist *headers = NULL;
5+
headers = curl_slist_append(headers, "content-type: application/x-www-form-urlencoded");
6+
headers = curl_slist_append(headers, "accept: application/json");
7+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
8+
curl_easy_setopt(hnd, CURLOPT_COOKIE, "foo=bar; bar=baz");
9+
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "foo=bar");
10+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CURL *hnd = curl_easy_init();
2+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET");
3+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
4+
struct curl_slist *headers = NULL;
5+
headers = curl_slist_append(headers, "x-foo: Bar");
6+
headers = curl_slist_append(headers, "accept: application/json");
7+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
8+
CURLcode ret = curl_easy_perform(hnd);

0 commit comments

Comments
 (0)