forked from hoppscotch/hoppscotch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curlparser.js
224 lines (209 loc) · 6.39 KB
/
curlparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as cookie from "cookie"
import * as URL from "url"
import * as querystring from "querystring"
import parser from "yargs-parser"
/**
* given this: [ 'msg1=value1', 'msg2=value2' ]
* output this: 'msg1=value1&msg2=value2'
* @param dataArguments
*/
const joinDataArguments = (dataArguments) => {
let data = ""
dataArguments.forEach((argument, i) => {
if (i === 0) {
data += argument
} else {
data += `&${argument}`
}
})
return data
}
const parseCurlCommand = (curlCommand) => {
let newlineFound = /\\/gi.test(curlCommand)
if (newlineFound) {
// remove '\' and newlines
curlCommand = curlCommand.replace(/\\/gi, "")
curlCommand = curlCommand.replace(/\n/g, "")
}
// yargs parses -XPOST as separate arguments. just prescreen for it.
curlCommand = curlCommand.replace(/ -XPOST/, " -X POST")
curlCommand = curlCommand.replace(/ -XGET/, " -X GET")
curlCommand = curlCommand.replace(/ -XPUT/, " -X PUT")
curlCommand = curlCommand.replace(/ -XPATCH/, " -X PATCH")
curlCommand = curlCommand.replace(/ -XDELETE/, " -X DELETE")
curlCommand = curlCommand.trim()
let parsedArguments = parser(curlCommand)
let cookieString
let cookies
let url = parsedArguments._[1]
if (!url) {
for (let argName in parsedArguments) {
if (typeof parsedArguments[argName] === "string") {
if (["http", "www."].includes(parsedArguments[argName])) {
url = parsedArguments[argName]
}
}
}
}
let headers
const parseHeaders = (headerFieldName) => {
if (parsedArguments[headerFieldName]) {
if (!headers) {
headers = {}
}
if (!Array.isArray(parsedArguments[headerFieldName])) {
parsedArguments[headerFieldName] = [parsedArguments[headerFieldName]]
}
parsedArguments[headerFieldName].forEach((header) => {
if (header.includes("Cookie")) {
// stupid javascript tricks: closure
cookieString = header
} else {
let colonIndex = header.indexOf(":")
let headerName = header.substring(0, colonIndex)
let headerValue = header.substring(colonIndex + 1).trim()
headers[headerName] = headerValue
}
})
}
}
parseHeaders("H")
parseHeaders("header")
if (parsedArguments.A) {
if (!headers) {
headers = []
}
headers["User-Agent"] = parsedArguments.A
} else if (parsedArguments["user-agent"]) {
if (!headers) {
headers = []
}
headers["User-Agent"] = parsedArguments["user-agent"]
}
if (parsedArguments.b) {
cookieString = parsedArguments.b
}
if (parsedArguments.cookie) {
cookieString = parsedArguments.cookie
}
let multipartUploads
if (parsedArguments.F) {
multipartUploads = {}
if (!Array.isArray(parsedArguments.F)) {
parsedArguments.F = [parsedArguments.F]
}
parsedArguments.F.forEach((multipartArgument) => {
// input looks like key=value. value could be json or a file path prepended with an @
const [key, value] = multipartArgument.split("=", 2)
multipartUploads[key] = value
})
}
if (cookieString) {
const cookieParseOptions = {
decode: (s) => s,
}
// separate out cookie headers into separate data structure
// note: cookie is case insensitive
cookies = cookie.parse(cookieString.replace(/^Cookie: /gi, ""), cookieParseOptions)
}
let method
if (parsedArguments.X === "POST") {
method = "post"
} else if (parsedArguments.X === "PUT" || parsedArguments["T"]) {
method = "put"
} else if (parsedArguments.X === "PATCH") {
method = "patch"
} else if (parsedArguments.X === "DELETE") {
method = "delete"
} else if (parsedArguments.X === "OPTIONS") {
method = "options"
} else if (
(parsedArguments["d"] ||
parsedArguments["data"] ||
parsedArguments["data-ascii"] ||
parsedArguments["data-binary"] ||
parsedArguments["F"] ||
parsedArguments["form"]) &&
!(parsedArguments["G"] || parsedArguments["get"])
) {
method = "post"
} else if (parsedArguments["I"] || parsedArguments["head"]) {
method = "head"
} else {
method = "get"
}
let compressed = !!parsedArguments.compressed
let urlObject = URL.parse(url) // eslint-disable-line
// if GET request with data, convert data to query string
// NB: the -G flag does not change the http verb. It just moves the data into the url.
if (parsedArguments["G"] || parsedArguments["get"]) {
urlObject.query = urlObject.query ? urlObject.query : ""
let option = "d" in parsedArguments ? "d" : "data" in parsedArguments ? "data" : null
if (option) {
let urlQueryString = ""
if (!url.includes("?")) {
url += "?"
} else {
urlQueryString += "&"
}
if (typeof parsedArguments[option] === "object") {
urlQueryString += parsedArguments[option].join("&")
} else {
urlQueryString += parsedArguments[option]
}
urlObject.query += urlQueryString
url += urlQueryString
delete parsedArguments[option]
}
}
let query = querystring.parse(urlObject.query, null, null, {
maxKeys: 10000,
})
urlObject.search = null // Clean out the search/query portion.
const request = {
url,
urlWithoutQuery: URL.format(urlObject),
}
if (compressed) {
request["compressed"] = true
}
if (Object.keys(query).length > 0) {
request.query = query
}
if (headers) {
request.headers = headers
}
request["method"] = method
if (cookies) {
request.cookies = cookies
request.cookieString = cookieString.replace("Cookie: ", "")
}
if (multipartUploads) {
request.multipartUploads = multipartUploads
}
if (parsedArguments.data) {
request.data = parsedArguments.data
} else if (parsedArguments["data-binary"]) {
request.data = parsedArguments["data-binary"]
request.isDataBinary = true
} else if (parsedArguments["d"]) {
request.data = parsedArguments["d"]
} else if (parsedArguments["data-ascii"]) {
request.data = parsedArguments["data-ascii"]
}
if (parsedArguments["u"]) {
request.auth = parsedArguments["u"]
}
if (parsedArguments["user"]) {
request.auth = parsedArguments["user"]
}
if (Array.isArray(request.data)) {
request.dataArray = request.data
request.data = joinDataArguments(request.data)
}
if (parsedArguments["k"] || parsedArguments["insecure"]) {
request.insecure = true
}
return request
}
export default parseCurlCommand