From 88b44d939aa95bb1af57ea7507904ff26bde2366 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 30 Nov 2023 00:09:23 +0000 Subject: [PATCH] Better domain --- Http/HttpRequest.fs | 20 +++++++++++++++++++- Http/Program.fs | 11 +++++++++-- Http/Socket.fs | 6 ++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Http/HttpRequest.fs b/Http/HttpRequest.fs index 573ff2e..da500c8 100644 --- a/Http/HttpRequest.fs +++ b/Http/HttpRequest.fs @@ -33,11 +33,15 @@ type FieldLine = // obvious candidate to improve speed by allocating less this.ToString () |> Encoding.ASCII.GetBytes |> request.AddRange +type ContentTransfer = + | ContentLength + | TransferEncoding of string + type HttpRequest = { Request : RequestLine Headers : FieldLine list - Body : byte[] + Body : (byte[] * ContentTransfer) option } member this.ToBytes () : byte[] = @@ -64,7 +68,21 @@ type HttpRequest = builder.Add (byte '\r') builder.Add (byte '\n') + match this.Body with + | None -> () + | Some (_, ContentTransfer.TransferEncoding encoding) -> + builder.AddRange (Encoding.ASCII.GetBytes "Transfer-Encoding: ") + builder.AddRange (Encoding.ASCII.GetBytes encoding) + | Some (body, ContentTransfer.ContentLength) -> + builder.AddRange (Encoding.ASCII.GetBytes "Content-Length: ") + builder.AddRange (Encoding.ASCII.GetBytes (body.Length.ToString ())) + builder.Add (byte '\r') builder.Add (byte '\n') + match this.Body with + | None -> () + | Some (_body, ContentTransfer.TransferEncoding _) -> failwith "not implemented" + | Some (body, ContentTransfer.ContentLength) -> builder.AddRange body + builder.ToArray () diff --git a/Http/Program.fs b/Http/Program.fs index d6d23f1..77e97a2 100644 --- a/Http/Program.fs +++ b/Http/Program.fs @@ -8,6 +8,9 @@ open System.Text module Program = [] let main argv = + let parentSw = Stopwatch.StartNew () + parentSw.Restart () + let sw = Stopwatch.StartNew () let ip = @@ -38,7 +41,7 @@ module Program = Value = "close" } ] - Body = Array.Empty<_> () + Body = None } |> fun r -> r.ToBytes () @@ -73,6 +76,10 @@ module Program = result.ToArray () - Console.WriteLine (System.Text.Encoding.ASCII.GetString response) + parentSw.Stop () + + Console.WriteLine (Encoding.ASCII.GetString response) + + Printfn.time "Total time:" parentSw.ElapsedMilliseconds 0 diff --git a/Http/Socket.fs b/Http/Socket.fs index f65053f..94c0688 100644 --- a/Http/Socket.fs +++ b/Http/Socket.fs @@ -82,15 +82,17 @@ module Sock = match isDone with | ConnectionState.Interrupted -> // It's OK to get "already connected", we were previously interrupted and we can't tell - // how far the connection had got before we retried + // how far the connection had got before we retried. + // I *have* seen this succeed but then a later write get ENOTCONN, and I don't know why. if err = Errno.EISCONN then + Console.WriteLine "Ignoring EISCONN after EINTR" isDone <- ConnectionState.Done else failwithf "failed to connect: %O" err | _ -> if err = Errno.EINTR then - printfn "Retrying due to EINTR" + Console.WriteLine "Retrying connect due to EINTR" isDone <- ConnectionState.Interrupted else failwithf "failed to connect: %O" err