xyncro / freya

Freya Web Stack - Meta-Package
https://freya.io
Other
330 stars 30 forks source link

Adding e-tag to my machine causes runtime error #176

Closed Vidarls closed 8 years ago

Vidarls commented 8 years ago

This works:

freyaMachine {
            including api
            including (acceptedMethods [ GET; OPTIONS ])
            handleOk (myHandler)
            exists (myExistsDecision)
            // Naive dummy / testing stuff for looking at caching headers
            expires ((System.DateTime.UtcNow.AddDays 1.0) |> Freya.init )
            lastModified (System.DateTime.UtcNow |> Freya.init)
        } |> FreyaMachine.toPipeline

This fails:

freyaMachine {
            including api
            including (acceptedMethods [ GET; OPTIONS ])
            handleOk (myHandler)
            exists (myExistsDecision)
            // Naive dummy / testing stuff for looking at caching headers
            expires ((System.DateTime.UtcNow.AddDays 1.0) |> Freya.init )
            lastModified (System.DateTime.UtcNow |> Freya.init)
            etag (Weak(System.Guid.Empty.ToString()) |> Freya.init)
        } |> FreyaMachine.toPipeline

I have no idea whether I I'm doing it wrong or if there is a bug

The exception is an InvalidCastException:

Unable to cast object of type 'nameOfMyMachineFunction@61-15' to type 'Microsoft.FSharp.Core.FSharpFunc2[Freya.Core.Types+FreyaState,Microsoft.FSharp.Control.FSharpAsync1[System.Tuple`2[System.DateTime,Freya.Core.Types+FreyaState]]]'.

kolektiv commented 8 years ago

That looks like a bug to me. I suspect it might be actually in the lastModified implementation, which I believe is fixed in the (released very soon) 3.0. There are a couple of suspected bugs lurking in there which are hopefully no longer present.

At this point I think it's probably better to wait a week or so for 3.0, but if it's a significant blocker for you now, I'll see what I can do?

Vidarls commented 8 years ago

I can live for a week without last-modified :)

Vidarls commented 8 years ago

Sadly, it seems removing the last-modified did not help, adding just the etag, with no last-modified caused the same error.

kolektiv commented 8 years ago

Interesting, definitely a bug. I'll take a look and make sure it's not still a bug in 3.0...

kolektiv commented 8 years ago

I've pushed a new prerelease (b294) of Freya 3 which, I hope, addresses this. Let me know if that helps - more work may be needed, but hopefully that's an improvement!

Vidarls commented 8 years ago

Thanx, I'll give it a go :-)

Vidarls commented 8 years ago

So far so good, no more runtime error, and when 200 + new version served. I have not tested the on-non-match query yet, will get back to you once I have

Vidarls commented 8 years ago

Still getting 304 no matter as long as an etag is provided

kolektiv commented 8 years ago

Well, better at least in terms of exceptions... Have you got a minimal reproduction case? What are you setting the etag to, and what methods/headers are you trying?

Vidarls commented 8 years ago

Using:

let handleWithPrivateCache maxAge eTag handler = 
        let cachingHandler handler = 
            freya {
                do! (Freya.Optic.set Response.Headers.cacheControl_ 
                    (Some(CacheControl(
                            [CacheDirective.Private; 
                             CacheDirective.MaxAge(System.TimeSpan.FromSeconds(maxAge))]))))

                do! (Freya.Optic.set Response.Headers.vary_ (Some("Accept Authorization")))
                return! handler
            }
        freyaMachine {
            expires (DateTime.UtcNow.AddSeconds(maxAge))
            //TODO, etag proper when bug fixed
            etag (Freya.map(Strong) eTag)
            handleOk (cachingHandler handler)
        }

just passing System.Guid.NewGuid().ToString() as the etag to force a new one each call.

sample request:

GET /my/1/path/2 HTTP/1.1
Host: localhost:44305
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Content-Type: text/plain; charset=utf-8
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2,sv;q=0.2
Cookie: [redacted]
If-None-Match: "a6b35e63-e7fa-40f2-a7c7-886165210296"

Response:

HTTP/1.1 304 Not Modified
Expires: Fri, 26 Feb 2016 23:13:11 GMT
ETag: "a6b35e63-e7fa-40f2-a7c7-886165210296"
Server: Microsoft-IIS/10.0
X-SourceFiles: =?UTF-8?B?QzpcU2VjcmV0U3R1ZmZcc3JjXFZldHNlcnZlLldlYi5BcGlcY2xpbmljc1wxMVxzcGVjaWVzXDEw?=
X-Powered-By: ASP.NET
Date: Fri, 26 Feb 2016 23:13:03 GMT
kolektiv commented 8 years ago

I'll take a look (although probably tomorrow now!)

Does If-Match seem to work as you'd expect (is the problem solely If-None-Match?)

Vidarls commented 8 years ago

Have not tested, (I actually need to read up on that to see how it is supposed to work..).

But a bit late now

kolektiv commented 8 years ago

No problem - a bit late for me too! I'll try and pick up tomorrow, and get this sorted :)

Vidarls commented 8 years ago

Solved, thanx @kolektiv :smile: