Skip to content

Commit

Permalink
Merge pull request #726 from postmanlabs/fix/libcurl-free-up-headers-…
Browse files Browse the repository at this point in the history
…list

[FIX] libcurl: Free up headers list to prevent memory leak
  • Loading branch information
VShingala committed Jan 18, 2024
2 parents 2411e41 + 504f0e3 commit c347472
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
3 changes: 3 additions & 0 deletions codegens/libcurl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ self = module.exports = {
if (body.mode === 'formdata' && options.useMimeType) {
snippet += indentString + 'curl_mime_free(mime);\n';
}
if (headersData) {
snippet += indentString + 'curl_slist_free_all(headers);\n';
}
snippet += '}\n';
snippet += 'curl_easy_cleanup(curl);\n';
(options.includeBoilerplate) &&
Expand Down
32 changes: 32 additions & 0 deletions codegens/libcurl/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,38 @@ describe('libcurl convert function', function () {
expect(snippet).to.include('curl_mime_name(part, "invalid src");');
});
});

it('should free up headers list after request is sent', function () {
var request = new sdk.Request({
'method': 'GET',
'header': [
{
'key': 'Accept',
'value': 'application/json'
},
{
'key': 'Content-Type',
'value': 'application/json'
}
],
'url': {
'raw': 'https://google.com',
'protocol': 'https',
'host': [
'google',
'com'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('curl_slist_free_all(headers)');
});
});

});

describe('getOptions function', function () {
Expand Down
39 changes: 10 additions & 29 deletions test/codegen/newman/fixtures/basicCollection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "f52ee07d-6345-4220-89af-e6696b3c0122",
"_postman_id": "b303fb8b-9b21-4429-b00f-33b6a7efa186",
"name": "Basic Collection",
"description": "This collection contains requests that will be used to test validity of plugin created to convert postman request into code snippet of particular language.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
Expand All @@ -12,7 +12,6 @@
{
"listen": "test",
"script": {
"id": "34edbfa7-7d32-42d6-8397-af2378c3aaa4",
"exec": [
""
],
Expand Down Expand Up @@ -53,7 +52,6 @@
},
{
"name": "Request Headers",
"event": [],
"request": {
"method": "GET",
"header": [
Expand Down Expand Up @@ -95,7 +93,6 @@
{
"listen": "test",
"script": {
"id": "e150d55b-0273-430a-9e1d-11969b433734",
"exec": [
""
],
Expand Down Expand Up @@ -141,7 +138,6 @@
{
"listen": "test",
"script": {
"id": "1bfe1fc3-c244-4a42-83c5-1a0d94d56ffd",
"exec": [
""
],
Expand Down Expand Up @@ -188,7 +184,6 @@
{
"listen": "test",
"script": {
"id": "a3ddecd1-e89d-426d-995c-0d6a678caa91",
"exec": [
"var responseJSON;",
"",
Expand Down Expand Up @@ -216,8 +211,7 @@
"header": [
{
"key": "Content-Type",
"value": "text/plain",
"disabled": false
"value": "text/plain"
}
],
"body": {
Expand Down Expand Up @@ -245,7 +239,6 @@
{
"listen": "test",
"script": {
"id": "e926912d-1c99-4c54-9b53-91c8f63acef0",
"exec": [
""
],
Expand Down Expand Up @@ -312,7 +305,6 @@
{
"listen": "test",
"script": {
"id": "d211bdad-60b3-4cd6-869f-853377bf03ef",
"exec": [
""
],
Expand Down Expand Up @@ -353,7 +345,6 @@
{
"listen": "test",
"script": {
"id": "532fef57-48fd-4ffe-ac7e-f5a7e32facc2",
"exec": [
""
],
Expand Down Expand Up @@ -394,7 +385,6 @@
{
"listen": "test",
"script": {
"id": "8bbbbc5b-2983-4979-8347-3ced95a69f7e",
"exec": [
""
],
Expand Down Expand Up @@ -435,7 +425,6 @@
{
"listen": "test",
"script": {
"id": "48da0505-470f-4cf3-bb77-30665415af60",
"exec": [
""
],
Expand Down Expand Up @@ -618,7 +607,6 @@
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"var responseJSON;",
"",
Expand All @@ -634,28 +622,23 @@
"",
"tests['response has PUT data'] = _.has(responseJSON, 'data');",
"tests['response matches the data sent in request'] = (responseJSON.data && responseJSON.data.length === 256);"
]
],
"type": "text/javascript"
}
}
],
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"value": "text/plain"
}
],
"body": {},
"header": [],
"url": {
"raw": "https://mockbin.org/request",
"raw": "https://postman-echo.com/delete",
"protocol": "https",
"host": [
"mockbin",
"org"
"postman-echo",
"com"
],
"path": [
"request"
"delete"
]
},
"description": "The HTTP `DELETE` method is used to delete resources on a server. The exact\nuse of `DELETE` requests depends on the server implementation. In general, \n`DELETE` requests support both, Query String parameters as well as a Request \nBody.\n\nThis endpoint accepts an HTTP `DELETE` request and provides debug information\nsuch as the HTTP headers, Query String arguments, and the Request Body."
Expand All @@ -667,7 +650,6 @@
{
"listen": "prerequest",
"script": {
"id": "e80b6162-6c90-4150-bfa1-7f42f11c8f64",
"type": "text/javascript",
"exec": [
""
Expand All @@ -677,12 +659,11 @@
{
"listen": "test",
"script": {
"id": "538efa04-97ce-456c-a5a1-772c466591d5",
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
}
19 changes: 9 additions & 10 deletions test/codegen/newman/fixtures/redirectCollection.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
{
"info": {
"_postman_id": "3ef1c00f-c58f-4604-8419-7a4931958235",
"_postman_id": "17d62db7-ca12-4298-8782-1d6f018c7be2",
"name": "Redirect test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Follow Redirects",
"protocolProfileBehavior": {
"followRedirects": true
},
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://mockbin.org/redirect/302/1/?to=https://postman-echo.com/get",
"raw": "https://httpbin.org/redirect-to?url=https://postman-echo.com/get",
"protocol": "https",
"host": [
"mockbin",
"httpbin",
"org"
],
"path": [
"redirect",
"302",
"1",
""
"redirect-to"
],
"query": [
{
"key": "to",
"key": "url",
"value": "https://postman-echo.com/get"
}
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
]
}

0 comments on commit c347472

Please sign in to comment.