Skip to content

Commit

Permalink
Content-Type now filled in by framework rather than former FFI stub.
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Diehl authored and Larry Diehl committed May 25, 2010
1 parent e434ddc commit 866c2c4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
23 changes: 14 additions & 9 deletions Lemmachine/FFI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ data Request = Request {
queryString :: QueryString,
port :: Port
}

data Response = Response {
status :: String,
respBody :: String
}

toRequest :: Hack.Env -> Request
toRequest e = Request {
Expand All @@ -54,14 +49,24 @@ toRequest e = Request {
}
where toHeader h = RequestHeader (fst h) (snd h)

data ResponseHeader = ResponseHeader String String
type ResponseHeaders = [ResponseHeader]

data Response = Response {
status :: String,
respHeaders :: ResponseHeaders,
respBody :: String
}

run :: (Request -> Response) -> IO ()
run f = Handler.run $ return . \env ->
let response = f $ toRequest env in
Hack.Response
{ Hack.status = read $ status response
, Hack.headers = [ ("Content-Type", "text/html") ]
, Hack.body = pack $ respBody response
Hack.Response {
Hack.status = read $ status response,
Hack.headers = map fromHeader $ respHeaders response,
Hack.body = pack $ respBody response
}
where fromHeader (ResponseHeader k v) = (k , v)



17 changes: 10 additions & 7 deletions Lemmachine/Resolve.agda
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open import Lemmachine.Resource
module Lemmachine.Resolve (c : Resource) where
open import Lemmachine.Request
open import Lemmachine.Response.Status
open import Lemmachine.Response
open import Lemmachine.Utils
open import Data.Bool
open import Data.Nat
Expand Down Expand Up @@ -165,10 +165,10 @@ D4+D5 r with fetchHeader "Accept-Language" (Request.headers r)
C3+C4 : Request Status
C3+C4 r with fetchHeader "Accept" (Request.headers r)
... | nothing = D4+D5 r
... | just contentType with any (_==_ contentType)
(Resource.contentTypesProvided c r)
... | true = D4+D5 r
... | false = NotAcceptable
... | just contentType with fetchContentType contentType
(Resource.contentTypesProvided c r)
... | just _ = D4+D5 r
... | nothing = NotAcceptable

B3 : Request Status
B3 r with eqMethod (Request.method r) OPTIONS
Expand Down Expand Up @@ -233,7 +233,10 @@ resolveStatus r = B13 r
resolve : Application
resolve r = record {
status = s
; headers = []
; body = Resource.body c s
; headers = hs $ fetchAccept (Request.headers r) (Resource.contentTypesProvided c r)
; body = Resource.body c s
} where
s = resolveStatus r
hs : Maybe String List ResponseHeader
hs (just s) = [ "Content-Type" , s ]
hs nothing = []
12 changes: 10 additions & 2 deletions Lemmachine/Response.agda
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ open import Data.List hiding (_++_)
open import Data.Nat
open import Data.String

ResponseHeader = String × String
data ResponseHeader : Set where
_,_ : String String ResponseHeader

{-# COMPILED_DATA ResponseHeader
Lemmachine.FFI.ResponseHeader
Lemmachine.FFI.ResponseHeader
#-}

ResponseHeaders = List ResponseHeader

record Response : Set where
field
status : Status
headers : List ResponseHeader
headers : ResponseHeaders
body : String

showStatus : Status String
Expand Down
3 changes: 2 additions & 1 deletion Lemmachine/Runner.agda
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private
}

data Data-Response : Set where
response : String String Data-Response
response : String ResponseHeaders String Data-Response

{-# COMPILED_DATA Data-Response
Lemmachine.FFI.Response
Expand All @@ -75,6 +75,7 @@ private
fromResponse : Response Data-Response
fromResponse resp = response
(showStatus $ Response.status resp)
(Response.headers resp)
(Response.body resp)

Data-resolve : Application Data-Request Data-Response
Expand Down
20 changes: 14 additions & 6 deletions Lemmachine/Utils.agda
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ fetch x xs with any (_≟_ x ∘ proj₁) xs
... | yes p = just (proj₂ (proj₁ (find p)))
... | no _ = nothing

fetchContentType : String List (String × String) Maybe String
fetchContentType _ [] = nothing
fetchContentType "*" (x ∷ _) = just (proj₂ x)
fetchContentType "*/*" (x ∷ _) = just (proj₂ x)
fetchContentType c (x ∷ xs) = fetch c (x ∷ xs)

private
fromHeaders : RequestHeaders List (String × String)
fromHeaders xs = Data.List.map f xs where
Expand All @@ -36,6 +30,20 @@ fetchHeader x xs with any (_≟_ x ∘ headerKey) xs
... | yes p = just (headerValue (proj₁ (find p)))
... | no _ = nothing

fetchContentType : String List String Maybe String
fetchContentType _ [] = nothing
fetchContentType "*" (y ∷ _) = just y
fetchContentType "*/*" (y ∷ _) = just y
fetchContentType x (y ∷ ys) with x == y
... | true = just y
... | false = fetchContentType x ys

fetchAccept : RequestHeaders List String Maybe String
fetchAccept hs cs with fetchHeader "Accept" hs
fetchAccept hs _ | nothing = nothing
fetchAccept _ [] | just _ = nothing
fetchAccept _ xss | just y = fetchContentType y xss

postulate
isDate : String Bool
isModified : String String Bool
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Lemmachine currently uses the Haskell [Hack](http://github.com/nfjinjing/hack) a

## Status ##

This project is still very much an early-stage work in progress, without guarantees for API stability. It currently only renders response statuses (not headers or body). Lemmas and proofs exist for the status resolution, and now there is a real runner hardcoded against the default resource!
The project is still in development and rapidly changing. Lemmas and proofs exist for status resolution, and you can now run resources! The focus will now comprise of a gradual direct translation of RFC 2616 sections into dependent type theory.

## Setup ##

Expand Down Expand Up @@ -46,8 +46,6 @@ Then add this to `Agda2 Include Dirs`:

## Running ##

Lemmachine currently supports resolving HTTP status codes (resolving headers & body will come later).

Run the following to see this in action for [Lemmachine.Default](http://github.com/larrytheliquid/Lemmachine/blob/master/Lemmachine/Default.agda):
agda -c -i . -i ./vendor/stdlib/src Lemmachine/Default.agda
./Default
Expand Down

0 comments on commit 866c2c4

Please sign in to comment.