Skip to content

Commit

Permalink
Handle cookies and headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpi committed Jul 9, 2011
1 parent f9b0f7d commit 0959db3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
30 changes: 26 additions & 4 deletions src/PicoMvc/Aspnet.fs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ open Strangelights.Log4f


module AspNet = module AspNet =
let processRequest (urlName: string) (routingTables: RoutingTable) (encoding: Encoding) (requestContext: RequestContext) (httpContext:HttpContext) actions = let processRequest (urlName: string) (routingTables: RoutingTable) (encoding: Encoding) (requestContext: RequestContext) (httpContext:HttpContext) actions =
let dict = new Dictionary<string, string>()
for x in httpContext.Request.Params.AllKeys do dict.Add(x, httpContext.Request.Params.[x])
let fullUrl = string requestContext.RouteData.Values.[urlName] let fullUrl = string requestContext.RouteData.Values.[urlName]
let urlPart, urlExtension = let urlPart, urlExtension =
if String.IsNullOrEmpty fullUrl || fullUrl = "/" then if String.IsNullOrEmpty fullUrl || fullUrl = "/" then
Expand All @@ -22,9 +20,33 @@ module AspNet =
elif fullUrl.EndsWith("/") then elif fullUrl.EndsWith("/") then
fullUrl.[ .. fullUrl.Length - 2], "" fullUrl.[ .. fullUrl.Length - 2], ""
else fullUrl, "" else fullUrl, ""
let request = new PicoRequest(urlPart, urlExtension, httpContext.Request.HttpMethod, dict, httpContext.Request.InputStream, new StreamReader(httpContext.Request.InputStream, httpContext.Request.ContentEncoding)) let parameters = httpContext.Request.Params.AllKeys |> Seq.fold (fun acc x -> Map.add x httpContext.Request.Params.[x] acc) Map.empty
let headers = httpContext.Request.Headers.AllKeys |> Seq.fold (fun acc x -> Map.add x httpContext.Request.Headers.[x] acc) Map.empty
let makeCookie (cookie: HttpCookie) =
{ Domain = cookie.Domain
Expires = cookie.Expires
HttpOnly = cookie.HttpOnly
Name = cookie.Name
Path = cookie.Path
Secure = cookie.Secure
Value = if String.IsNullOrEmpty(cookie.Value) then None else Some cookie.Value
Values = cookie.Values.AllKeys |> Seq.fold (fun acc x -> Map.add x cookie.Values.[x] acc) Map.empty }
let cookies = httpContext.Request.Cookies.AllKeys |> Seq.fold (fun acc x -> Map.add x (makeCookie httpContext.Request.Cookies.[x]) acc) Map.empty
let request = new PicoRequest(urlPart, urlExtension, httpContext.Request.HttpMethod, parameters, headers, cookies, httpContext.Request.InputStream, new StreamReader(httpContext.Request.InputStream, httpContext.Request.ContentEncoding))
use outstream = new StreamWriter(httpContext.Response.OutputStream, encoding) use outstream = new StreamWriter(httpContext.Response.OutputStream, encoding)
let response = new PicoResponse(httpContext.Response.OutputStream, outstream, fun x -> httpContext.Response.StatusCode <- x) let writeCookie cookie =
let httpCookie = new HttpCookie(cookie.Name,
Domain = cookie.Domain,
Expires = cookie.Expires,
HttpOnly = cookie.HttpOnly,
Path = cookie.Path,
Secure = cookie.Secure)
match cookie.Value with
| Some x -> httpCookie.Value <- x | None -> ()
for x in cookie.Values do httpCookie.Values.Add(x.Key, x.Value)
httpContext.Response.Cookies.Add(httpCookie)
let setStatus x = httpContext.Response.StatusCode <- x
let response = new PicoResponse(httpContext.Response.OutputStream, outstream, setStatus, writeCookie)
httpContext.Response.ContentEncoding <- encoding httpContext.Response.ContentEncoding <- encoding
// TODO hack, would be better to give developer the control of this // TODO hack, would be better to give developer the control of this
httpContext.Response.ContentType <- sprintf "%s; charset=%s" httpContext.Response.ContentType encoding.WebName httpContext.Response.ContentType <- sprintf "%s; charset=%s" httpContext.Response.ContentType encoding.WebName
Expand Down
27 changes: 25 additions & 2 deletions src/PicoMvc/Core.fs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,18 +26,41 @@ type ErrorMessage =
{ Code: int { Code: int
Error: string } Error: string }


type PicoRequest(urlPart: string, urlExtension: string: string, verb: string, parameters: Dictionary<string, string>, rawStream: Stream, requestStream: StreamReader) = type Cookie =
{ Domain: string
Expires: DateTime
HttpOnly: bool
Name: string
Path: string
Secure: bool
Value: option<string>
Values: Map<string,string> }

type PicoRequest(urlPart: string,
urlExtension: string: string,
verb: string,
parameters: Map<string, string>,
headers: Map<string, string>,
cookies: Map<string, Cookie>,
rawStream: Stream,
requestStream: StreamReader) =
member x.UrlPart = urlPart member x.UrlPart = urlPart
member x.UrlExtension = urlExtension member x.UrlExtension = urlExtension
member x.Verb = verb member x.Verb = verb
member x.Parameters = parameters member x.Parameters = parameters
member x.Headers = headers
member x.Cookies = cookies
member x.RawStream = rawStream member x.RawStream = rawStream
member x.RequestStream = requestStream member x.RequestStream = requestStream


type PicoResponse(rawStream: Stream, responceStream: StreamWriter, setStatusCode: int -> unit) = type PicoResponse(rawStream: Stream,
responceStream: StreamWriter,
setStatusCode: int -> unit,
writeCookie: Cookie -> unit) =
member x.RawStream = rawStream member x.RawStream = rawStream
member x.ResponceStream = responceStream member x.ResponceStream = responceStream
member x.SetStatusCode code = setStatusCode code member x.SetStatusCode code = setStatusCode code
member x.WriteCookie cookie = writeCookie cookie


type PicoContext(request: PicoRequest, response: PicoResponse) = type PicoContext(request: PicoRequest, response: PicoResponse) =
member x.Request = request member x.Request = request
Expand Down

0 comments on commit 0959db3

Please sign in to comment.