Cookies 以F#方式将设置Cookie字符串转换为记录?
我有以下Cookies 以F#方式将设置Cookie字符串转换为记录?,cookies,f#,Cookies,F#,我有以下Cookie记录 type Cookie = { NameValue : string * string; Domain : string option; Path : string option; Expires : string option; MaxAge : string; Secure : bool; // ? no associated value, anything better than bool HttpOnly
Cookie
记录
type Cookie = {
NameValue : string * string;
Domain : string option;
Path : string option;
Expires : string option;
MaxAge : string;
Secure : bool; // ? no associated value, anything better than bool
HttpOnly : bool; // ? no associated value, anything better than bool
}
我需要将set cookie
字符串转换为CookieAttricute列表
let GetCookie str =
let (|ParseRegex|_|) regex str =
let m = Regex(regex, RegexOptions.IgnoreCase).Match(str)
if m.Success
then Some (List.tail [ for x in m.Groups -> x.Value ])
else None
match str with
| ParseRegex @"(.+?)(?:=(.+?))?(?:;|$|,(?!\s))" [name; value] ->
.... // How to construct a Cookie here?
| _ -> None
例如,给定以下字符串
"ObSSOCookie=93KRPo;secure; httponly; path=/; domain=.xyz.com"
函数应该返回
{ NameValue = ("ObSSOCookie", "93KRPo");
Secure = true;
HttpOnly = true;
Path = Some("/");
Domain = Some(".xyz.com");
MaxAge = None;
Expires = None }
首先,我将使用
String.Split
,而不是将其与正则表达式过度复杂化。然后,我将为属性构建一个映射。大概是这样的:
let GetCookie (str: string) =
let allkeyvalues =
str.Split ';'
|> Array.map (fun str ->
match str.Split '=' with
| [| key |] -> (key, "")
| [| key; value |] -> (key, value)
| _ -> (* there's more than one '='; the format is incorrect. *))
let namevalue = allkeyvalues.[0]
let attributes =
Seq.skip 1 allkeyvalues
|> Seq.map (fun (key, value) ->
(key.ToLower(), value)) // attribute names are case-insensitive
|> Map.ofSeq
{
NameValue = namevalue
Secure = Map.containsKey "secure" attributes
Path = Map.tryFind "path" attributes
// etc...
}
Cookie是一个,而不是一个。