feat(common): enhanced cookie parsing from curl arguments (#4977)

This commit is contained in:
Anwarul Islam 2025-04-10 13:26:05 +06:00 committed by GitHub
parent 111fdccfc8
commit 3c150ec90a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 18 deletions

View file

@ -14,7 +14,7 @@ import * as O from "fp-ts/Option"
import parser from "yargs-parser/browser"
import { getAuthObject } from "./sub_helpers/auth"
import { getHeaders, recordToHoppHeaders } from "./sub_helpers/headers"
// import { getCookies } from "./sub_helpers/cookies"
import { getCookies } from "./sub_helpers/cookies"
import {
objHasArrayProperty,
objHasProperty,
@ -66,7 +66,22 @@ export const parseCurlCommand = (curlCommand: string) => {
)
const method = getMethod(parsedArguments)
// const cookies = getCookies(parsedArguments)
const cookies = getCookies(parsedArguments)
// Add cookies to headers if they exist
if (Object.keys(cookies).length > 0) {
const cookieString = Object.entries(cookies)
.map(([key, value]) => `${key}=${value}`)
.join("; ")
hoppHeaders.push({
key: "Cookie",
value: cookieString,
active: true,
description: "",
})
}
const urlObject = getURLObject(parsedArguments)
const auth = getAuthObject(parsedArguments, headers, urlObject)

View file

@ -1,27 +1,64 @@
import parser from "yargs-parser"
import * as cookie from "cookie"
import { pipe } from "fp-ts/function"
import * as O from "fp-ts/Option"
import * as S from "fp-ts/string"
import { pipe, flow } from "fp-ts/function"
import { objHasProperty } from "~/helpers/functional/object"
import {
objHasArrayProperty,
objHasProperty,
} from "~/helpers/functional/object"
export function getCookies(parsedArguments: parser.Arguments) {
/**
* Parses cookies from curl arguments
* Handles both -b flag and --cookie parameter
*/
export const getCookies = (parsedArguments: any): Record<string, string> => {
// Handle -b or --cookie flags
return pipe(
parsedArguments,
O.fromPredicate(objHasProperty("cookie", "string")),
O.map((args) => args.cookie),
O.alt(() =>
O.fromPredicate(objHasArrayProperty("b", "string")),
O.map((args) => parseCookieStrings(args.b)),
O.altW(() =>
pipe(
parsedArguments,
O.fromPredicate(objHasProperty("b", "string")),
O.map((args) => args.b)
O.map((args) => parseCookieString(args.b))
)
),
O.map(flow(S.replace(/^cookie: /i, ""), cookie.parse)),
O.getOrElse(() => ({}))
O.altW(() =>
pipe(
parsedArguments,
O.fromPredicate(objHasArrayProperty("cookie", "string")),
O.map((args) => parseCookieStrings(args.cookie))
)
),
O.altW(() =>
pipe(
parsedArguments,
O.fromPredicate(objHasProperty("cookie", "string")),
O.map((args) => parseCookieString(args.cookie))
)
),
O.getOrElseW(() => ({}))
)
}
/**
* Parse multiple cookie strings and combine them
*/
const parseCookieStrings = (cookies: string[]): Record<string, string> => {
return cookies.reduce((acc, cookie) => {
return { ...acc, ...parseCookieString(cookie) }
}, {})
}
/**
* Parse a single cookie string into a record
*/
const parseCookieString = (cookieString: string): Record<string, string> => {
return cookieString
.split(";")
.map((pair) => pair.trim())
.filter(Boolean)
.reduce((acc, pair) => {
const [key, value] = pair.split("=", 2)
return { ...acc, [key]: value || "" }
}, {})
}