Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 94 additions & 93 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const words = require('shellwords')
const url = require('url')
const words = require('shellwords');
const url = require('url');

// TODO -F, --form
// TODO --data-binary
Expand All @@ -11,111 +11,111 @@ const url = require('url')
* Attempt to parse the given curl string.
*/

module.exports = exports.default = function(s) {
if (0 != s.indexOf('curl ')) return
const args = rewrite(words.split(s))
const out = { method: 'GET', header: {} }
var state = ''

args.forEach(function(arg){
switch (true) {
case isURL(arg):
out.url = arg
break;

case arg == '-A' || arg == '--user-agent':
state = 'user-agent'
break;

case arg == '-H' || arg == '--header':
state = 'header'
break;

case arg == '-d' || arg == '--data' || arg == '--data-ascii':
state = 'data'
break;

case arg == '-u' || arg == '--user':
state = 'user'
break;

case arg == '-I' || arg == '--head':
out.method = 'HEAD'
break;

case arg == '-X' || arg == '--request':
state = 'method'
break;

case arg == '-b' || arg =='--cookie':
state = 'cookie'
break;

case arg == '--compressed':
out.header['Accept-Encoding'] = out.header['Accept-Encoding'] || 'deflate, gzip'
break;

case !!arg:
switch (state) {
case 'header':
var field = parseField(arg)
out.header[field[0]] = field[1]
state = ''
break;
case 'user-agent':
out.header['User-Agent'] = arg
state = ''
break;
case 'data':
out.header['Content-Type'] = out.header['Content-Type'] || 'application/x-www-form-urlencoded'
out.body = out.body
? out.body + '&' + arg
: arg
state = ''
break;
case 'user':
out.header['Authorization'] = 'Basic ' + btoa(arg)
state = ''
break;
case 'method':
out.method = arg
state = ''
break;
case 'cookie':
out.header['Set-Cookie'] = arg
state = ''
break;
module.exports = exports.default = function (s) {
if (0 != s.indexOf('curl ')) return;
const args = rewrite(words.split(s));
const out = { method: 'GET', header: {} };
var state = '';

args.forEach(function (arg) {
switch (true) {
case isURL(arg):
out.url = arg;
break;

case arg == '-A' || arg == '--user-agent':
state = 'user-agent';
break;

case arg == '-H' || arg == '--header':
state = 'header';
break;

case arg == '-d' || arg == '--data' || arg == '--data-ascii':
state = 'data';
break;

case arg == '-u' || arg == '--user':
state = 'user';
break;

case arg == '-I' || arg == '--head':
out.method = 'HEAD';
break;

case arg == '-X' || arg == '--request':
state = 'method';
break;

case arg == '-b' || arg == '--cookie':
state = 'cookie';
break;

case arg == '--compressed':
out.header['Accept-Encoding'] = out.header['Accept-Encoding'] || 'deflate, gzip';
break;

case !!arg:
switch (state) {
case 'header':
var field = parseField(arg);
out.header[field[0]] = field[1];
state = '';
break;
case 'user-agent':
out.header['User-Agent'] = arg;
state = '';
break;
case 'data':
out.header['Content-Type'] = out.header['Content-Type'] || 'application/x-www-form-urlencoded';
out.body = out.body
? out.body + '&' + arg
: arg;
state = '';
break;
case 'user':
out.header['Authorization'] = 'Basic ' + btoa(arg);
state = '';
break;
case 'method':
out.method = arg;
state = '';
break;
case 'cookie':
out.header['Set-Cookie'] = arg;
state = '';
break;
}
break;
}
break;
}
})
});

return out
}
return out;
};

/**
* Rewrite args for special cases such as -XPUT.
*/

function rewrite(args) {
return args.reduce(function(args, a){
if (0 == a.indexOf('-X')) {
args.push('-X')
args.push(a.slice(2))
} else {
args.push(a)
}

return args
}, [])
return args.reduce(function (args, a) {
if (0 == a.indexOf('-X')) {
args.push('-X');
args.push(a.slice(2));
} else {
args.push(a);
}

return args;
}, []);
}

/**
* Parse header field.
*/

function parseField(s) {
return s.split(/: */)
return s.split(/: */);
}

/**
Expand All @@ -124,5 +124,6 @@ function parseField(s) {

function isURL(s) {
// TODO: others at some point
return /^https?:\/\//.test(s)
var URLRegxp = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/;
return URLRegxp.test(s);
}
Loading