Skip to content

Commit

Permalink
HttpResponse Mappings and other minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Nov 8, 2015
1 parent 6aba6b2 commit 7f5e2df
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ nuget System.IO.FileSystem 4.0.1-beta-23506
nuget System.Reflection 4.1.0-beta-23506
nuget System.Reflection.TypeExtensions 4.1.0-beta-23506
nuget System.Runtime.Serialization.Json 4.0.1-beta-23506
nuget System.Security.Claims 4.0.1-beta-23506
nuget System.Security.Cryptography.Algorithms 4.0.0-beta-23506
nuget System.Security.Cryptography.Primitives 4.0.0-beta-23506
nuget System.Security.Cryptography.X509Certificates 4.0.0-beta-23506
nuget System.Threading.Thread 4.0.0-beta-23506
nuget System.Text.RegularExpressions 4.0.11-beta-23506

nuget runtime.win7-x64.Microsoft.FSharp.Compiler.netcore 1.0.0-alpha-0002
4 changes: 3 additions & 1 deletion src/Suave/Owin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ module OwinApp =
suaveLogger.Log LogLevel.Info (fun () ->
LogLine.mk "Suave.Owin" LogLevel.Info TraceHeader.empty None str
)
override x.Write (c : char) =
// Required because it is abstract. Does nothing interesting
()
}

type private Clock = uint64
Expand Down Expand Up @@ -575,7 +578,6 @@ module OwinApp =
type UnclosableMemoryStream() =
inherit IO.MemoryStream()
// I can't be bothered to do a full delegation... R# needed for that.
override x.Close () = ()
member x.Dispose() = ()
member x.RealDispose() = base.Dispose()

Expand Down
71 changes: 69 additions & 2 deletions src/Suave/Proxy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Suave.Proxy

open System
open System.Globalization
open System.IO
open System.Net
open System.Collections.Generic
Expand All @@ -19,6 +20,72 @@ open Suave.Http
open Suave.Http.Response
open Suave.Web.ParsingAndControl

let dateTimePatterns = [| "ddd, d MMM yyyy H:m:s 'GMT'"; // RFC 1123 (r, except it allows both 1 and 01 for date and time)
"ddd, d MMM yyyy H:m:s"; // RFC 1123, no zone - assume GMT
"d MMM yyyy H:m:s 'GMT'"; // RFC 1123, no day-of-week
"d MMM yyyy H:m:s"; // RFC 1123, no day-of-week, no zone
"ddd, d MMM yy H:m:s 'GMT'"; // RFC 1123, short year
"ddd, d MMM yy H:m:s"; // RFC 1123, short year, no zone
"d MMM yy H:m:s 'GMT'"; // RFC 1123, no day-of-week, short year
"d MMM yy H:m:s"; // RFC 1123, no day-of-week, short year, no zone
"dddd, d'-'MMM'-'yy H:m:s 'GMT'"; // RFC 850
"dddd, d'-'MMM'-'yy H:m:s"; // RFC 850 no zone
"ddd MMM d H:m:s yyyy"; // ANSI C's asctime() format
"ddd, d MMM yyyy H:m:s zzz"; // RFC 5322
"ddd, d MMM yyyy H:m:s"; // RFC 5322 no zone
"d MMM yyyy H:m:s zzz"; // RFC 5322 no day-of-week
"d MMM yyyy H:m:s" // RFC 5322 no day-of-week, no zone
|]

/// See: https://msdn.microsoft.com/en-us/library/system.net.webheadercollection(v=vs.110).aspx
let restrictedHeaders = [| "Accept";
"Connection";
"Content-Length";
"Content-Type";
"Date";
"Expect";
"Host";
"If-Modified-Since";
"Range";
"Referrer";
"Transfer-Encoding";
"User-Agent";
"Proxy-Connection"
|]

type private HttpWebRequest with
member this.GetRequestStream () =
let mutable value = null
let iar = this.BeginGetRequestStream(new AsyncCallback(fun r -> let req = r.AsyncState :?> HttpWebRequest
value <- req.EndGetRequestStream(r)
()), this)
if not iar.IsCompleted then do
iar.AsyncWaitHandle.WaitOne()
iar.AsyncWaitHandle.Dispose()
value

member this.Date
with get():DateTime =
let date = this.Headers.["Date"]
let result, value = DateTime.TryParseExact(date,
dateTimePatterns, DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowWhiteSpaces ||| DateTimeStyles.AssumeUniversal)
if result then value else Unchecked.defaultof<DateTime>
and set (value:DateTime) =
// Format according to RFC1123; 'r' uses invariant info (DateTimeFormatInfo.InvariantInfo)
let date = value.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture)
this.Headers.["Date"] <- date

member this.UserAgent
with get() = this.Headers.["User-Agent"]
and set value = this.Headers.["User-Agent"] <- value

type private System.Net.WebHeaderCollection with
static member IsRestricted (key:string):bool =
match restrictedHeaders |> Array.tryFind(fun elem -> String.Compare(key, elem, StringComparison.OrdinalIgnoreCase) = 0) with
| Some(_) -> true
| None -> false

/// Copies the headers from 'headers1' to 'headers2'
let private toHeaderList (headers : WebHeaderCollection) =
headers.AllKeys
Expand All @@ -43,7 +110,7 @@ let forward (ip : IPAddress) (port : uint16) (ctx : HttpContext) =
for e in h do
let key = fst e
if not (WebHeaderCollection.IsRestricted key) then
r.Add(key, snd e)
r.[key] <- (snd e)
r
let url = new UriBuilder("http", ip.ToString(), int port, p.url.AbsolutePath, p.rawQuery)
let q = WebRequest.Create(url.Uri) :?> HttpWebRequest
Expand All @@ -69,7 +136,7 @@ let forward (ip : IPAddress) (port : uint16) (ctx : HttpContext) =
header "transfer-encoding" |> Choice.iter (fun v -> q.TransferEncoding <- v)
header "user-agent" |> Choice.iter (fun v -> q.UserAgent <- v)

q.Headers.Add("X-Real-IP", ctx.clientIpTrustProxy.ToString())
q.Headers.["X-Real-IP"] <- (ctx.clientIpTrustProxy.ToString())

fun ctx -> socket {
if p.``method`` = HttpMethod.POST || p.``method`` = HttpMethod.PUT then
Expand Down
4 changes: 3 additions & 1 deletion src/Suave/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module CookieStateStore =
|> Map.tryFind StateStoreType
|> Option.map (mkStateStore ctx.runtime.cookieSerialiser ctx.userState)

#if SYSTEM_RUNTIME_CACHING
/// This module contains the implementation for the memory-cache backed session
/// state store, when the memory cache is global for the server.
module MemoryCacheStateStore =
Expand Down Expand Up @@ -152,4 +153,5 @@ module MemoryCacheStateStore =
let stateId = ctx |> HttpContext.stateId
Writers.setUserData StateStoreType (stateStore stateId))

let DefaultExpiry = TimeSpan.FromMinutes 30. |> MaxAge
let DefaultExpiry = TimeSpan.FromMinutes 30. |> MaxAge
#endif
5 changes: 4 additions & 1 deletion src/Suave/Suave.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<HintPath>..\..\packages\System.Net.Primitives\ref\netcore50\System.Net.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Requests">
<HintPath>..\..\packages\System.Net.Requests\ref\netcore50\System.Net.Requests.dll</HintPath>
<HintPath>..\..\packages\System.Net.Requests\ref\dotnet5.4\System.Net.Requests.dll</HintPath>
</Reference>
<Reference Include="System.Net.Sockets">
<HintPath>..\..\packages\System.Net.Sockets\ref\dotnet5.4\System.Net.Sockets.dll</HintPath>
Expand All @@ -206,6 +206,9 @@
<Reference Include="System.Runtime.Serialization.Json">
<HintPath>..\..\packages\System.Runtime.Serialization.Json\ref\netcore50\System.Runtime.Serialization.Json.dll</HintPath>
</Reference>
<Reference Include="System.Security.Claims">
<HintPath>..\..\packages\System.Security.Claims\ref\dotnet5.4\System.Security.Claims.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms">
<HintPath>..\..\packages\System.Security.Cryptography.Algorithms\ref\dotnet5.4\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
Expand Down
4 changes: 1 addition & 3 deletions src/Suave/Web.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module internal ParsingAndControl =
open System.Net.Sockets
open System.Threading
open System.Threading.Tasks
open System.Security.Permissions
open System.Security.Principal
open System.Collections.Generic

open Suave.Http
Expand Down Expand Up @@ -615,7 +613,7 @@ module internal ParsingAndControl =

let resolveDirectory homeDirectory =
match homeDirectory with
| None -> Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
| None -> Path.GetDirectoryName((Internals.getExecutingAssembly).Location)
| Some s -> s

////////////////////////////////////////////////////
Expand Down

0 comments on commit 7f5e2df

Please sign in to comment.