-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(curl): use const/let instead of var, strict equal
- Loading branch information
1 parent
aab6132
commit f1db108
Showing
1 changed file
with
56 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,72 @@ | ||
(function () { | ||
var global = window; | ||
if (!global.sense) | ||
global.sense = {}; | ||
const global = window; | ||
if (!global.sense) | ||
global.sense = {}; | ||
|
||
function detectCURL(text) { | ||
// returns true if text matches a curl request | ||
if (!text) return false; | ||
return text.match(/^\s*?curl\s+(-X[A-Z]+)?\s*['"]?.*?['"]?(\s*$|\s+?-d\s*?['"])/); | ||
function detectCURL(text) { | ||
// returns true if text matches a curl request | ||
if (!text) return false; | ||
return text.match(/^\s*?curl\s+(-X[A-Z]+)?\s*['"]?.*?['"]?(\s*$|\s+?-d\s*?['"])/); | ||
|
||
} | ||
} | ||
|
||
function parseCURL(text) { | ||
var matches = text.match(/^\s*?curl\s+(?:-s\s+)?(-X\s*[A-Z]+)?\s*/); | ||
var ret = { | ||
method: "", | ||
server: "", | ||
endpoint: "", | ||
data: "" | ||
}; | ||
if (matches[1]) { | ||
ret.method = matches[1].substring(2).trim(); // strip -X | ||
} | ||
text = text.substring(matches[0].length); // strip everything so far. | ||
if (text.length == 0) return ret; | ||
if (text[0] == '"') { | ||
matches = text.match(/^"([^"]*)"/); | ||
} | ||
else if (text[0] == "'") { | ||
matches = text.match(/^'([^']*)'/); | ||
} | ||
else { | ||
matches = text.match(/^(\S*)/); | ||
} | ||
function parseCURL(text) { | ||
let matches = text.match(/^\s*?curl\s+(?:-s\s+)?(-X\s*[A-Z]+)?\s*/); | ||
const ret = { | ||
method: "", | ||
server: "", | ||
endpoint: "", | ||
data: "" | ||
}; | ||
if (matches[1]) { | ||
ret.method = matches[1].substring(2).trim(); // strip -X | ||
} | ||
text = text.substring(matches[0].length); // strip everything so far. | ||
if (text.length === 0) return ret; | ||
if (text[0] === '"') { | ||
matches = text.match(/^"([^"]*)"/); | ||
} else if (text[0] === "'") { | ||
matches = text.match(/^'([^']*)'/); | ||
} else { | ||
matches = text.match(/^(\S*)/); | ||
} | ||
|
||
if (!matches) return ret; | ||
var url = matches[1]; | ||
if (!matches) return ret; | ||
let url = matches[1]; | ||
|
||
if (!url.match(/:\/\//)) url = "http://" + url; // inject http as curl does | ||
if (!url.match(/:\/\//)) url = "http://" + url; // inject http as curl does | ||
|
||
var urlAnchor = document.createElement("a"); | ||
urlAnchor.href = url; | ||
const urlAnchor = document.createElement("a"); | ||
urlAnchor.href = url; | ||
|
||
ret.server = (urlAnchor.protocol || "http") + "//" + urlAnchor.hostname; | ||
if (urlAnchor.port && urlAnchor.port != 0) ret.server += ":" + urlAnchor.port; | ||
ret.url = (urlAnchor.pathname || "") + (urlAnchor.search || ""); | ||
ret.server = (urlAnchor.protocol || "http") + "//" + urlAnchor.hostname; | ||
if (urlAnchor.port && urlAnchor.port !== "0") ret.server += ":" + urlAnchor.port; | ||
ret.url = (urlAnchor.pathname || "") + (urlAnchor.search || ""); | ||
|
||
text = text.substring(matches[0].length); | ||
text = text.substring(matches[0].length); | ||
|
||
// now search for -d | ||
matches = text.match(/.*-d\s*?'/); | ||
if (matches) { | ||
ret.data = text.substring(matches[0].length).replace(/'\s*$/, ''); | ||
} | ||
else { | ||
matches = text.match(/.*-d\s*?"/); | ||
if (matches) { | ||
ret.data = text.substring(matches[0].length).replace(/"\s*$/, ''); | ||
ret.data = ret.data.replace(/\\(.)/gi, "$1"); | ||
} | ||
} | ||
// now search for -d | ||
matches = text.match(/.*-d\s*?'/); | ||
if (matches) { | ||
ret.data = text.substring(matches[0].length).replace(/'\s*$/, ''); | ||
} else { | ||
matches = text.match(/.*-d\s*?"/); | ||
if (matches) { | ||
ret.data = text.substring(matches[0].length).replace(/"\s*$/, ''); | ||
ret.data = ret.data.replace(/\\(.)/gi, "$1"); | ||
} | ||
} | ||
|
||
if (ret.data) { | ||
ret.data = ret.data.trim(); | ||
} | ||
if (ret.data) { | ||
ret.data = ret.data.trim(); | ||
} | ||
|
||
return ret; | ||
} | ||
return ret; | ||
} | ||
|
||
|
||
global.sense.curl = {}; | ||
global.sense.curl.parseCURL = parseCURL; | ||
global.sense.curl.detectCURL = detectCURL; | ||
global.sense.curl = {}; | ||
global.sense.curl.parseCURL = parseCURL; | ||
global.sense.curl.detectCURL = detectCURL; | ||
|
||
})(); |