parse -F to a list of lists (#348)

* parse -F to a list of lists

* detect repeated form keys
This commit is contained in:
Boris Verkhovskiy
2022-03-18 19:01:27 -07:00
committed by GitHub
parent b12081bc7e
commit 46c54b318f
8 changed files with 41 additions and 26 deletions

View File

@@ -103,8 +103,7 @@ function getFormDataString (request) {
let fileArgs = []
let dataArgs = []
for (const multipartKey in request.multipartUploads) {
const multipartValue = request.multipartUploads[multipartKey]
for (const [multipartKey, multipartValue] of request.multipartUploads) {
if (multipartValue.startsWith('@')) {
const fileName = multipartValue.slice(1)
fileArgs.push(` {:file, ~s|${fileName}|}`)

View File

@@ -58,8 +58,7 @@ function getFilesString (request) {
data.files = {}
data.data = {}
for (const multipartKey in request.multipartUploads) {
const multipartValue = request.multipartUploads[multipartKey]
for (const [multipartKey, multipartValue] of request.multipartUploads) {
if (multipartValue.startsWith('@')) {
const fileName = multipartValue.slice(1)
data.files[repr(multipartKey)] = repr(fileName)

View File

@@ -93,7 +93,7 @@ const prepareMultipartUploads = (request) => {
let response = null
if (request.multipartUploads) {
const params = []
for (const [key, value] of Object.entries(request.multipartUploads)) {
for (const [key, value] of request.multipartUploads) {
const pair = []
pair.push(repr(key))
const fileProvider = prepareDataProvider(value, null, '', 1)

View File

@@ -50,8 +50,7 @@ export const _toPhp = request => {
let requestDataCode = ''
if (request.multipartUploads) {
requestDataCode = '[\n'
for (const multipartKey in request.multipartUploads) {
const multipartValue = request.multipartUploads[multipartKey]
for (const [multipartKey, multipartValue] of request.multipartUploads) {
if (multipartValue.charAt(0) === '@') {
requestDataCode += " '" + quote(multipartKey) + "' => new CURLFile('" + quote(multipartValue.substring(1)) + "'),\n"
} else {

View File

@@ -117,18 +117,33 @@ function getDataString (request) {
}
function getFilesString (request) {
// http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
let filesString = 'files = {\n'
for (const multipartKey in request.multipartUploads) {
const multipartValue = request.multipartUploads[multipartKey]
if (multipartValue.startsWith('@')) {
const fileName = multipartValue.slice(1)
filesString += ' ' + repr(multipartKey) + ': (' + repr(fileName) + ', open(' + repr(fileName) + ", 'rb')),\n"
const multipartUploads = request.multipartUploads.map(m => {
let multipartValue
if (m[1].startsWith('@')) {
const fileName = m[1].slice(1)
multipartValue = '(' + repr(fileName) + ', open(' + repr(fileName) + ", 'rb'))"
} else {
filesString += ' ' + repr(multipartKey) + ': (None, ' + repr(multipartValue) + '),\n'
multipartValue = '(None, ' + repr(m[1]) + ')'
}
return [m[0], multipartValue]
})
const multipartUploadsAsDict = Object.fromEntries(multipartUploads)
let filesString = 'files = '
if (Object.keys(multipartUploadsAsDict).length === multipartUploads.length) {
filesString += '{\n'
for (const [multipartKey, multipartValue] of multipartUploads) {
filesString += ' ' + repr(multipartKey) + ': ' + multipartValue + ',\n'
}
filesString += '}\n'
} else {
filesString += '[\n'
for (const [multipartKey, multipartValue] of multipartUploads) {
filesString += ' (' + repr(multipartKey) + ', ' + multipartValue + '),\n'
}
filesString += ']\n'
}
filesString += '}\n'
return filesString
}

View File

@@ -95,8 +95,8 @@ function getMultipleDataString (request, parsedQueryString) {
function getFilesString (request) {
// http://docs.rstats-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
let filesString = 'files = list(\n'
filesString += Object.keys(request.multipartUploads).map((multipartKey) => {
const multipartValue = request.multipartUploads[multipartKey]
filesString += request.multipartUploads.map((m) => {
const [multipartKey, multipartValue] = m
let fileParam
if (multipartValue.startsWith('@')) {
const fileName = multipartValue.slice(1)

View File

@@ -38,8 +38,8 @@ export const _toRust = request => {
if (request.multipartUploads) {
lines.push(indent('let form = multipart::Form::new()'))
const parts = Object.keys(request.multipartUploads).map(partType => {
const partValue = request.multipartUploads[partType]
const parts = request.multipartUploads.map(m => {
const [partType, partValue] = m
switch (partType) {
case 'image':
case 'file': {

15
util.js
View File

@@ -974,13 +974,13 @@ const buildRequest = parsedArguments => {
let multipartUploads
if (parsedArguments.form) {
multipartUploads = {}
multipartUploads = []
parsedArguments.form.forEach(multipartArgument => {
// input looks like key=value. value could be json or a file path prepended with an @
// TODO: what if multipartArgument is empty string?
// TODO: if string has more than one '=', this throws away data
const [key, value] = multipartArgument.split('=', 2)
multipartUploads[key] = value
// -F is the most complicated option, we just assume it looks
// like key=value and some generators handle value being @filepath
// TODO: https://curl.se/docs/manpage.html#-F
const [key, value] = multipartArgument.split(/=(.*)/s, 2)
multipartUploads.push([key, value || ''])
})
}
@@ -1175,6 +1175,9 @@ const hasHeader = (request, header) => {
}
const deleteHeader = (request, header) => {
if (!request.headers) {
return
}
const lookup = header.toLowerCase()
for (let i = request.headers.length - 1; i >= 0; i--) {
if (request.headers[i][0].toLowerCase() === lookup) {