From d7bc7be5ff407896b2ff35f1ace21652d94cf8f7 Mon Sep 17 00:00:00 2001 From: ashic Date: Sat, 4 Apr 2015 13:10:47 +0100 Subject: [PATCH] cleanup, defense against empty sequences, etc. Thanks vasily! --- src/app/Fake.Deploy.Lib/HttpHeaderHelper.fs | 33 +++++++++---------- src/app/Fake.Deploy/DeploymentAgent.fs | 20 +++++++---- .../Fake.Deploy.Web/Modules/Api.Package.fs | 14 ++++---- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/app/Fake.Deploy.Lib/HttpHeaderHelper.fs b/src/app/Fake.Deploy.Lib/HttpHeaderHelper.fs index 6fb10280934..71eaecd0931 100644 --- a/src/app/Fake.Deploy.Lib/HttpHeaderHelper.fs +++ b/src/app/Fake.Deploy.Lib/HttpHeaderHelper.fs @@ -9,26 +9,23 @@ let toHeaderValue (values:string []) : string = x.Replace("\"", "%22") |> sprintf "\"%s\"" ) - |> fun strs -> System.String.Join(",", strs - ) + |> String.concat "," let private regex = Regex("(\"[^\"]*\")(?:,(\"[^\"]*\"))*", RegexOptions.Compiled) -let fromHeaderValue (value:string) : string [] = - let matches = regex.Matches(value) - //back compat: existing agents not expecting quoted params will continue to function. - if matches.Count = 0 then [|value|] - else - matches |> Seq.cast - |> Seq.collect (fun (m:Match) -> m.Groups |> Seq.cast) - |> Seq.skip 1 - |> Seq.collect (fun (g:Group) -> - g.Captures |> Seq.cast |> Seq.map (fun (c:Capture) -> c.Value) - |> Seq.map (fun (x:string) -> - x.Substring(1, x.Length - 2) - |> fun y -> y.Replace("%22", "\"") - ) - ) - |> Array.ofSeq + +let fromHeaderValue (value: string) = + match regex.Matches value |> Seq.cast |> Seq.toList with + | [] -> + //back compat: existing agents not expecting quoted params will continue to function. + [|value|] + | matches -> + match [ for m in matches do + for x in m.Groups -> x ] with + | _ :: gs -> + [| for g in gs do + for c in g.Captures do + yield c.Value.[1..c.Value.Length - 2].Replace("%22", "\"") |] + | _ -> [|value|] \ No newline at end of file diff --git a/src/app/Fake.Deploy/DeploymentAgent.fs b/src/app/Fake.Deploy/DeploymentAgent.fs index 549afac0baf..7e2141107de 100644 --- a/src/app/Fake.Deploy/DeploymentAgent.fs +++ b/src/app/Fake.Deploy/DeploymentAgent.fs @@ -17,12 +17,20 @@ let getBodyFromNancyRequest (ctx : Nancy.Request) = ctx.Body.CopyTo ms ms.ToArray() -let getScriptArgumentsFromNancyRequest (ctx : Nancy.Request) = - ctx.Headers - |> Seq.choose (fun pair -> if pair.Key = FakeDeployAgentHelper.scriptArgumentsHeaderName then Some <| pair.Value else None) - |> Seq.head - |> Seq.head - |> fromHeaderValue +let getScriptArgumentsFromNancyRequest (ctx : Nancy.Request) : string [] = + let args = + ctx.Headers + |> Seq.choose (fun pair -> + if pair.Key = FakeDeployAgentHelper.scriptArgumentsHeaderName then + Some pair.Value + else None + ) + |> List.ofSeq + + match args with + | [] -> [||] + | _ -> args |> Seq.collect id |> Seq.map fromHeaderValue |> Seq.collect id |> Array.ofSeq + let runDeployment workDir (ctx : Nancy.Request) = let packageBytes = getBodyFromNancyRequest ctx diff --git a/src/deploy.web/Fake.Deploy.Web/Modules/Api.Package.fs b/src/deploy.web/Fake.Deploy.Web/Modules/Api.Package.fs index 319c96b9b91..fecf332fe86 100644 --- a/src/deploy.web/Fake.Deploy.Web/Modules/Api.Package.fs +++ b/src/deploy.web/Fake.Deploy.Web/Modules/Api.Package.fs @@ -48,11 +48,13 @@ type ApiPackage (dataProvider : IDataProvider) as http = let agent = dataProvider.GetAgents [agentId] |> Seq.head let url = agent.Address.AbsoluteUri + "fake/" let env = - [agent.EnvironmentId] - |>dataProvider.GetEnvironments - |> Seq.head - |> fun x -> x.Name - |> sprintf "env=%s" + match [agent.EnvironmentId] + |>dataProvider.GetEnvironments with + | [||] -> None + | envs -> envs |> Seq.head |> fun x -> x.Name |> sprintf "env=%s" |> Some + + let args = Seq.choose id [env] |> Array.ofSeq + Directory.CreateDirectory(packageTemp) |> ignore let files = http.Request.Files @@ -64,7 +66,7 @@ type ApiPackage (dataProvider : IDataProvider) as http = let code, message = files |> Seq.map(fun file -> - match postDeploymentPackage url file [|env|] with + match postDeploymentPackage url file args with | Failure(err) -> file, Some err, HttpStatusCode.InternalServerError, Some(err) | Success a ->