chore: introduce curl parser tests and minor changes (#2145)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
kyteinsky
2022-03-14 18:09:32 +00:00
committed by GitHub
parent ab23fc4e0f
commit dcdd0379d4
6 changed files with 862 additions and 46 deletions

View File

@@ -14,6 +14,8 @@ import { safeParseJSON } from "~/helpers/functional/json"
export function detectContentType(
rawData: string
): HoppRESTReqBody["contentType"] {
if (!rawData) return "text/plain"
let contentType: HoppRESTReqBody["contentType"]
if (O.isSome(safeParseJSON(rawData))) {
@@ -25,15 +27,21 @@ export function detectContentType(
// everything is HTML
contentType = "text/html"
}
} else if (/([^&=]+)=([^&=]+)/.test(rawData)) {
contentType = "application/x-www-form-urlencoded"
} else {
contentType = pipe(
rawData.match(/^-{2,}.+\\r\\n/),
rawData.match(/^-{2,}[A-Za-z0-9]+\\r\\n/),
O.fromNullable,
O.filter((boundaryMatch) => boundaryMatch && boundaryMatch.length > 1),
O.filter((boundaryMatch) => boundaryMatch.length > 0),
O.match(
() => "text/plain",
() =>
pipe(
rawData,
O.fromPredicate((rd) => /([^&=]+)=([^&=]*)/.test(rd)),
O.match(
() => "text/plain",
() => "application/x-www-form-urlencoded"
)
),
() => "multipart/form-data"
)
)
@@ -162,7 +170,7 @@ export function parseBody(
O.fromNullable,
O.map(decodeURIComponent),
O.chain((rd) =>
pipe(rd.match(/(([^&=]+)=?([^&=]+))/g), O.fromNullable)
pipe(rd.match(/(([^&=]+)=?([^&=]*))/g), O.fromNullable)
),
O.map((pairs) => pairs.map((p) => p.replace("=", ": ")).join("\n"))
)
@@ -190,7 +198,7 @@ export function parseBody(
O.match(
() =>
pipe(
rawData.match(/^-{2,}.+\\r\\n/),
rawData.match(/-{2,}[A-Za-z0-9]+\\r\\n/g),
O.fromNullable,
O.filter((boundaryMatch) => boundaryMatch.length > 1),
O.map((matches) => matches[0])
@@ -220,14 +228,14 @@ export function parseBody(
RA.filter((p) => p !== "" && p.includes("name")),
RA.map((p) =>
pipe(
p.replaceAll(/[\r\n]+/g, "\r\n"),
p.replaceAll(/\\r\\n+/g, "\\r\\n"),
S.split("\\r\\n"),
RA.filter((q) => q !== "")
)
),
RA.filterMap((p) =>
pipe(
p[0].match(/name=(.+)$/),
p[0].match(/ name="(\w+)"/),
O.fromNullable,
O.filter((nameMatch) => nameMatch.length > 0),
O.map((nameMatch) => {