Cfml support (#376)

* Add CFML Support

* convert to TS and update Readme Page

* Delete package-lock.json

unchanged, does not need to be a part of the PR

* Changed file extension of test fixtures to match utility

* update tests to show .cfm extension

* updated generator with quote function, updated a few tests

* Update README.md

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* updated generator with cookie, and binary file, and charset changes. Updated tests to allot for generator changes

* fix formatting

* add back lock file

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* revert lock file

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* update timeout syntax

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* update default proxy port

* update default proxy port2 with tests

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

* update setting auth credentials

* add prettier formatting

* Update src/generators/cfml.ts

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>

Co-authored-by: Boris Verkhovskiy <boris.verk@gmail.com>
This commit is contained in:
Scott Steinbeck
2022-04-09 17:31:11 -07:00
committed by GitHub
parent 31ca0dfb08
commit 4e46ceaf26
27 changed files with 334 additions and 1 deletions

View File

@@ -13,7 +13,7 @@ data = {
response = requests.post('http://example.com', data=data)
```
You can choose the output language by passing `--language <language>`. The options are `python` (the default), `javascript`/`node`/`node-request`, `php`, `go`, `java`, `r`, `rust`, `elixir`, `dart`, `matlab` and a few more.
You can choose the output language by passing `--language <language>`. The options are `python` (the default), `javascript`/`node`/`node-request`, `php`, `go`, `java`, `r`, `rust`, `elixir`, `dart`, `matlab`, `cfml` and a few more.
[![NPM version][npm-image]][npm-url]
@@ -154,6 +154,7 @@ If you get stuck, please reach out via email. I am always willing to hop on a Go
- cf512 (bugfixes and feature requests)
- DainisGorbunovs (MATLAB support)
- TennyZhuang (data-raw support)
- scottsteinbeck (CFML support)
## License

View File

@@ -13,6 +13,7 @@ import type { LongOpts, ShortOpts, Request } from "./util.js";
import { _toAnsible } from "./generators/ansible.js";
import { _toDart } from "./generators/dart.js";
import { _toCFML } from "./generators/cfml.js";
import { _toElixir } from "./generators/elixir.js";
import { _toGo } from "./generators/go.js";
import { _toJava } from "./generators/java.js";
@@ -40,6 +41,7 @@ const defaultLanguage = "python";
// NOTE: make sure to update this when adding language support
const translate: { [key: string]: (request: Request) => string } = {
ansible: _toAnsible,
cfml: _toCFML,
browser: _toJavaScript, // for backwards compatibility, undocumented
dart: _toDart,
elixir: _toElixir,
@@ -62,6 +64,7 @@ const USAGE = `Usage: curlconverter [--language <language>] [-] [curl_options...
language: the language to convert the curl command to. The choices are
ansible
cfml
dart
elixir
go

118
src/generators/cfml.ts Normal file
View File

@@ -0,0 +1,118 @@
import * as util from "../util.js";
import type { Request } from "../util.js";
import jsesc from "jsesc";
const quote = (str: string): string => {
return jsesc(str, { quotes: "single" }).replace(/"/g, '""');
};
export const _toCFML = (request: Request): string => {
let cfmlCode = "";
cfmlCode += "httpService = new http();\n";
cfmlCode += 'httpService.setUrl("' + quote(request.url as string) + '");\n';
cfmlCode += 'httpService.setMethod("' + quote(request.method) + '");\n';
if (request.cookies) {
for (const [headerName, headerValue] of request.cookies) {
cfmlCode +=
'httpService.addParam(type="cookie", name="' +
quote(headerName) +
'", value="' +
quote(headerValue) +
'");\n';
}
util.deleteHeader(request, "Cookie");
}
if (request.headers && request.headers.length) {
for (const [headerName, headerValue] of request.headers) {
cfmlCode +=
'httpService.addParam(type="header", name="' +
quote(headerName) +
'", value="' +
quote(headerValue as string) +
'");\n';
}
}
if (request.timeout) {
cfmlCode +=
"httpService.setTimeout(" + (parseInt(request.timeout) || 0) + ");\n";
}
if (request.auth) {
const [authUser, authPassword] = request.auth;
cfmlCode += 'httpService.setUsername("' + quote(authUser) + '");\n';
cfmlCode +=
'httpService.setPassword("' + quote(authPassword || "") + '");\n';
}
if (request.proxy) {
let proxy = request.proxy;
let proxyPort = "1080";
const proxyPart = (request.proxy as string).match(/:([0-9]+)/);
if (proxyPart) {
proxy = request.proxy.slice(0, proxyPart.index);
proxyPort = proxyPart[1];
}
cfmlCode += 'httpService.setProxyServer("' + quote(proxy) + '");\n';
cfmlCode += "httpService.setProxyPort(" + quote(proxyPort) + ");\n";
if (request.proxyAuth) {
const [proxyUser, proxyPassword] = request.proxyAuth.split(/:(.*)/s, 2);
cfmlCode += 'httpService.setProxyUser("' + quote(proxyUser) + '");\n';
cfmlCode +=
'httpService.setProxyPassword("' + quote(proxyPassword || "") + '");\n';
}
}
if (request.data || request.multipartUploads) {
if (request.multipartUploads) {
for (const [multipartKey, multipartValue] of request.multipartUploads) {
if (multipartValue.charAt(0) === "@") {
cfmlCode +=
'httpService.addParam(type="file", name="' +
quote(multipartKey) +
'", file="#expandPath("' +
quote(multipartValue.substring(1)) +
'")#");\n';
} else {
cfmlCode +=
'httpService.addParam(type="formfield", name="' +
quote(multipartKey) +
'", value="' +
quote(multipartValue) +
'");\n';
}
}
} else if (
!request.isDataRaw &&
(request.data as string).charAt(0) === "@"
) {
cfmlCode +=
'httpService.addParam(type="body", value="#' +
(request.isDataBinary ? "fileReadBinary" : "fileRead") +
'(expandPath("' +
quote((request.data as string).substring(1)) +
'"))#");\n';
} else {
cfmlCode +=
'httpService.addParam(type="body", value="' +
quote(request.data as string) +
'");\n';
}
}
cfmlCode += "\nresult = httpService.send().getPrefix();\n";
cfmlCode += "writeDump(result);\n";
return cfmlCode;
};
export const toCFML = (curlCommand: string | string[]): string => {
const request = util.parseCurlCommand(curlCommand);
return _toCFML(request);
};

View File

@@ -1,4 +1,5 @@
export { toAnsible } from "./generators/ansible.js";
export { toCFML } from "./generators/cfml.js";
export { toJavaScript } from "./generators/javascript/javascript.js";
// backwards compatibility alias
export { toJavaScript as toBrowser } from "./generators/javascript/javascript.js";

6
test/fixtures/cfml/delete.cfm vendored Normal file
View File

@@ -0,0 +1,6 @@
httpService = new http();
httpService.setUrl("http://www.url.com/page");
httpService.setMethod("DELETE");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("example.com");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-Type", value="application/json");
httpService.addParam(type="body", value="{}");
result = httpService.send().getPrefix();
writeDump(result);

8
test/fixtures/cfml/get_basic_auth.cfm vendored Normal file
View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("https://api.test.com/");
httpService.setMethod("GET");
httpService.setUsername("some_username");
httpService.setPassword("some_password");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("https://api.test.com/");
httpService.setMethod("GET");
httpService.setUsername("");
httpService.setPassword("some_password");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,10 @@
httpService = new http();
httpService.setUrl("http://api.ipify.org/?format=json&");
httpService.setMethod("GET");
httpService.addParam(type="header", name="Host", value="api.ipify.org");
httpService.addParam(type="header", name="Accept", value="*/*");
httpService.addParam(type="header", name="User-Agent", value="GiftTalk/2.7.2 (iPhone; iOS 9.0.2; Scale/3.00)");
httpService.addParam(type="header", name="Accept-Language", value="en-CN;q=1, zh-Hans-CN;q=0.9");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("http://localhost:28139/");
httpService.setMethod("GET");
httpService.setUsername("some_username");
httpService.setPassword("some_password");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,6 @@
httpService = new http();
httpService.setUrl("http://example.net");
httpService.setMethod("GET");
result = httpService.send().getPrefix();
writeDump(result);

7
test/fixtures/cfml/get_max_time.cfm vendored Normal file
View File

@@ -0,0 +1,7 @@
httpService = new http();
httpService.setUrl("http://example.com");
httpService.setMethod("GET");
httpService.setTimeout(20);
result = httpService.send().getPrefix();
writeDump(result);

8
test/fixtures/cfml/get_proxy.cfm vendored Normal file
View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("http://localhost:9000");
httpService.setMethod("GET");
httpService.setProxyServer("http://localhost");
httpService.setProxyPort(8080);
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,10 @@
httpService = new http();
httpService.setUrl("http://localhost:9000");
httpService.setMethod("GET");
httpService.setProxyServer("http://localhost");
httpService.setProxyPort(8080);
httpService.setProxyUser("anonymous");
httpService.setProxyPassword("anonymous");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,18 @@
httpService = new http();
httpService.setUrl("http://en.wikipedia.org/");
httpService.setMethod("GET");
httpService.addParam(type="cookie", name="GeoIP", value="US:Albuquerque:35.1241:-106.7675:v4");
httpService.addParam(type="cookie", name="uls-previous-languages", value="%5B%22en%22%5D");
httpService.addParam(type="cookie", name="mediaWiki.user.sessionId", value="VaHaeVW3m0ymvx9kacwshZIDkv8zgF9y");
httpService.addParam(type="cookie", name="centralnotice_buckets_by_campaign", value="%7B%22C14_enUS_dsk_lw_FR%22%3A%7B%22val%22%3A%220%22%2C%22start%22%3A1412172000%2C%22end%22%3A1422576000%7D%2C%22C14_en5C_dec_dsk_FR%22%3A%7B%22val%22%3A3%2C%22start%22%3A1417514400%2C%22end%22%3A1425290400%7D%2C%22C14_en5C_bkup_dsk_FR%22%3A%7B%22val%22%3A1%2C%22start%22%3A1417428000%2C%22end%22%3A1425290400%7D%7D");
httpService.addParam(type="cookie", name="centralnotice_bannercount_fr12", value="22");
httpService.addParam(type="cookie", name="centralnotice_bannercount_fr12-wait", value="14");
httpService.addParam(type="header", name="Accept-Encoding", value="gzip, deflate, sdch");
httpService.addParam(type="header", name="Accept-Language", value="en-US,en;q=0.8");
httpService.addParam(type="header", name="User-Agent", value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
httpService.addParam(type="header", name="Accept", value="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpService.addParam(type="header", name="Referer", value="http://www.wikipedia.org/");
httpService.addParam(type="header", name="Connection", value="keep-alive");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,7 @@
httpService = new http();
httpService.setUrl("http://httpbin.org/patch");
httpService.setMethod("PATCH");
httpService.addParam(type="file", name="file1", file="#expandPath("./fixtures/curl_commands/delete.sh")#");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,9 @@
httpService = new http();
httpService.setUrl("http://httpbin.org/patch");
httpService.setMethod("PATCH");
httpService.addParam(type="file", name="file1", file="#expandPath("./fixtures/curl_commands/delete.sh")#");
httpService.addParam(type="formfield", name="form1", value="form+data+1");
httpService.addParam(type="formfield", name="form2", value="form_data_2");
result = httpService.send().getPrefix();
writeDump(result);

8
test/fixtures/cfml/j_post_form_f.cfm vendored Normal file
View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("http://httpbin.org/post");
httpService.setMethod("POST");
httpService.addParam(type="formfield", name="d1", value="data1");
httpService.addParam(type="formfield", name="d2", value="data");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,9 @@
httpService = new http();
httpService.setUrl("http://lodstories.isi.edu:3030/american-art/query");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-type", value="application/sparql-query");
httpService.addParam(type="header", name="Accept", value="application/sparql-results+json");
httpService.addParam(type="body", value="#fileReadBinary(expandPath("./sample.sparql"))#");
result = httpService.send().getPrefix();
writeDump(result);

8
test/fixtures/cfml/post_form.cfm vendored Normal file
View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("http://domain.tld/post-to-me.php");
httpService.setMethod("POST");
httpService.addParam(type="formfield", name="username", value="davidwalsh");
httpService.addParam(type="formfield", name="password", value="something");
result = httpService.send().getPrefix();
writeDump(result);

7
test/fixtures/cfml/post_image.cfm vendored Normal file
View File

@@ -0,0 +1,7 @@
httpService = new http();
httpService.setUrl("http://example.com/targetservice");
httpService.setMethod("POST");
httpService.addParam(type="file", name="image", file="#expandPath("image.jpg")#");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("google.com");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
httpService.addParam(type="body", value="field=don%27t%20you%20like%20quotes");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,17 @@
httpService = new http();
httpService.setUrl("http://www.w3schools.com/ajax/demo_post.asp");
httpService.setMethod("POST");
httpService.addParam(type="cookie", name="_gat", value="1");
httpService.addParam(type="cookie", name="ASPSESSIONIDACCRDTDC", value="MCMDKFMBLLLHGKCGNMKNGPKI");
httpService.addParam(type="cookie", name="_ga", value="GA1.2.1424920226.1419478126");
httpService.addParam(type="header", name="Origin", value="http://www.w3schools.com");
httpService.addParam(type="header", name="Accept-Encoding", value="gzip, deflate");
httpService.addParam(type="header", name="Accept-Language", value="en-US,en;q=0.8");
httpService.addParam(type="header", name="User-Agent", value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
httpService.addParam(type="header", name="Accept", value="*/*");
httpService.addParam(type="header", name="Referer", value="http://www.w3schools.com/ajax/tryit_view.asp?x=0.07944501144811511");
httpService.addParam(type="header", name="Connection", value="keep-alive");
httpService.addParam(type="header", name="Content-Length", value="0");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,8 @@
httpService = new http();
httpService.setUrl("http://example.com/post");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
httpService.addParam(type="body", value="msg1=wow&msg2=such&msg3=@rawmsg");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,16 @@
httpService = new http();
httpService.setUrl("http://fiddle.jshell.net/echo/html/");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Origin", value="http://fiddle.jshell.net");
httpService.addParam(type="header", name="Accept-Encoding", value="gzip, deflate");
httpService.addParam(type="header", name="Accept-Language", value="en-US,en;q=0.8");
httpService.addParam(type="header", name="User-Agent", value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded; charset=UTF-8");
httpService.addParam(type="header", name="Accept", value="*/*");
httpService.addParam(type="header", name="Referer", value="http://fiddle.jshell.net/_display/");
httpService.addParam(type="header", name="X-Requested-With", value="XMLHttpRequest");
httpService.addParam(type="header", name="Connection", value="keep-alive");
httpService.addParam(type="body", value="msg1=wow&msg2=such");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -0,0 +1,10 @@
httpService = new http();
httpService.setUrl("http://localhost:5984/test/_security");
httpService.setMethod("PUT");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
httpService.setUsername("admin");
httpService.setPassword("123");
httpService.addParam(type="body", value="{""admins"":{""names"":[], ""roles"":[]}, ""readers"":{""names"":[""joe""],""roles"":[]}}");
result = httpService.send().getPrefix();
writeDump(result);

View File

@@ -19,6 +19,7 @@ const toParser = (curl: string | string[]): string => {
// TODO: 'parser' ?
type Converter =
| "ansible"
| "cfml"
| "dart"
| "elixir"
| "go"
@@ -40,6 +41,11 @@ const converters = {
extension: ".yml",
converter: curlconverter.toAnsible,
},
cfml: {
name: "ColdFusion",
extension: ".cfm",
converter: curlconverter.toCFML,
},
dart: {
name: "Dart",
extension: ".dart",