Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Handle cookies and headers.

  • Loading branch information...
commit 0959db39f30caf144194801196c221b0d71b9b89 1 parent f9b0f7d
Robert Pickering authored July 09, 2011
30  src/PicoMvc/Aspnet.fs
@@ -9,8 +9,6 @@ open Strangelights.Log4f
9 9
 
10 10
 module AspNet =
11 11
     let processRequest (urlName: string) (routingTables: RoutingTable) (encoding: Encoding) (requestContext: RequestContext) (httpContext:HttpContext) actions =
12  
-        let dict = new Dictionary<string, string>()
13  
-        for x in httpContext.Request.Params.AllKeys do dict.Add(x, httpContext.Request.Params.[x])
14 12
         let fullUrl = string requestContext.RouteData.Values.[urlName]
15 13
         let urlPart, urlExtension  = 
16 14
             if String.IsNullOrEmpty fullUrl || fullUrl = "/" then
@@ -22,9 +20,33 @@ module AspNet =
22 20
                 elif fullUrl.EndsWith("/") then
23 21
                     fullUrl.[ .. fullUrl.Length - 2], ""
24 22
                 else fullUrl, ""
25  
-        let request = new PicoRequest(urlPart, urlExtension, httpContext.Request.HttpMethod, dict, httpContext.Request.InputStream, new StreamReader(httpContext.Request.InputStream, httpContext.Request.ContentEncoding))
  23
+        let parameters = httpContext.Request.Params.AllKeys |> Seq.fold (fun acc x -> Map.add x httpContext.Request.Params.[x] acc) Map.empty
  24
+        let headers = httpContext.Request.Headers.AllKeys |> Seq.fold (fun acc x -> Map.add x httpContext.Request.Headers.[x] acc) Map.empty
  25
+        let makeCookie (cookie: HttpCookie) =
  26
+            { Domain = cookie.Domain
  27
+              Expires = cookie.Expires
  28
+              HttpOnly = cookie.HttpOnly
  29
+              Name = cookie.Name
  30
+              Path = cookie.Path
  31
+              Secure = cookie.Secure
  32
+              Value = if String.IsNullOrEmpty(cookie.Value) then None else Some cookie.Value
  33
+              Values = cookie.Values.AllKeys |> Seq.fold (fun acc x -> Map.add x cookie.Values.[x] acc) Map.empty  }
  34
+        let cookies = httpContext.Request.Cookies.AllKeys |> Seq.fold (fun acc x -> Map.add x (makeCookie httpContext.Request.Cookies.[x]) acc) Map.empty
  35
+        let request = new PicoRequest(urlPart, urlExtension, httpContext.Request.HttpMethod, parameters, headers, cookies, httpContext.Request.InputStream, new StreamReader(httpContext.Request.InputStream, httpContext.Request.ContentEncoding))
26 36
         use outstream = new StreamWriter(httpContext.Response.OutputStream, encoding)
27  
-        let response = new PicoResponse(httpContext.Response.OutputStream, outstream, fun x -> httpContext.Response.StatusCode <- x)
  37
+        let writeCookie cookie =
  38
+            let httpCookie = new HttpCookie(cookie.Name, 
  39
+                                            Domain = cookie.Domain, 
  40
+                                            Expires = cookie.Expires, 
  41
+                                            HttpOnly = cookie.HttpOnly, 
  42
+                                            Path = cookie.Path,
  43
+                                            Secure = cookie.Secure)
  44
+            match cookie.Value with
  45
+            | Some x -> httpCookie.Value <- x | None -> ()
  46
+            for x in cookie.Values do httpCookie.Values.Add(x.Key, x.Value)
  47
+            httpContext.Response.Cookies.Add(httpCookie)
  48
+        let setStatus x = httpContext.Response.StatusCode <- x
  49
+        let response = new PicoResponse(httpContext.Response.OutputStream, outstream, setStatus, writeCookie)
28 50
         httpContext.Response.ContentEncoding <- encoding
29 51
         // TODO hack, would be better to give developer the control of this
30 52
         httpContext.Response.ContentType <- sprintf "%s; charset=%s" httpContext.Response.ContentType encoding.WebName 
27  src/PicoMvc/Core.fs
@@ -26,18 +26,41 @@ type ErrorMessage =
26 26
     { Code: int
27 27
       Error: string }
28 28
 
29  
-type PicoRequest(urlPart: string, urlExtension: string: string, verb: string, parameters: Dictionary<string, string>, rawStream: Stream, requestStream: StreamReader) =
  29
+type Cookie =
  30
+    { Domain: string
  31
+      Expires: DateTime
  32
+      HttpOnly: bool
  33
+      Name: string
  34
+      Path: string
  35
+      Secure: bool
  36
+      Value: option<string>
  37
+      Values: Map<string,string> }
  38
+
  39
+type PicoRequest(urlPart: string, 
  40
+                 urlExtension: string: string, 
  41
+                 verb: string, 
  42
+                 parameters: Map<string, string>, 
  43
+                 headers: Map<string, string>, 
  44
+                 cookies: Map<string, Cookie>, 
  45
+                 rawStream: Stream, 
  46
+                 requestStream: StreamReader) =
30 47
     member x.UrlPart = urlPart
31 48
     member x.UrlExtension = urlExtension
32 49
     member x.Verb = verb
33 50
     member x.Parameters = parameters
  51
+    member x.Headers = headers
  52
+    member x.Cookies = cookies
34 53
     member x.RawStream = rawStream
35 54
     member x.RequestStream = requestStream
36 55
 
37  
-type PicoResponse(rawStream: Stream, responceStream: StreamWriter, setStatusCode: int -> unit) =
  56
+type PicoResponse(rawStream: Stream, 
  57
+                  responceStream: StreamWriter, 
  58
+                  setStatusCode: int -> unit, 
  59
+                  writeCookie: Cookie -> unit) =
38 60
     member x.RawStream = rawStream
39 61
     member x.ResponceStream = responceStream
40 62
     member x.SetStatusCode code = setStatusCode code
  63
+    member x.WriteCookie cookie = writeCookie cookie
41 64
 
42 65
 type PicoContext(request: PicoRequest, response: PicoResponse) =
43 66
     member x.Request = request

0 notes on commit 0959db3

Please sign in to comment.
Something went wrong with that request. Please try again.