|
| 1 | +-- | This module defines low-level bindings to the Node HTTP module. |
| 2 | + |
| 3 | +module Node.HTTP where |
| 4 | + |
| 5 | +import Prelude |
| 6 | + |
| 7 | +import Data.StrMap |
| 8 | +import Control.Monad.Eff |
| 9 | +import Node.Stream |
| 10 | +import Unsafe.Coerce (unsafeCoerce) |
| 11 | + |
| 12 | +-- | The type of a HTTP server object |
| 13 | +foreign import data Server :: * |
| 14 | + |
| 15 | +-- | A HTTP request object |
| 16 | +foreign import data Request :: * |
| 17 | + |
| 18 | +-- | A HTTP response object |
| 19 | +foreign import data Response :: * |
| 20 | + |
| 21 | +-- | The effect associated with using the HTTP module. |
| 22 | +foreign import data HTTP :: ! |
| 23 | + |
| 24 | +-- | Create a HTTP server, given a function to be executed when a request is received. |
| 25 | +foreign import createServer :: forall eff. (Request -> Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Server |
| 26 | + |
| 27 | +-- | Listen on the specified port. The specified callback will be run when setup is complete. |
| 28 | +foreign import listen :: forall eff. Server -> Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit |
| 29 | + |
| 30 | +-- | Get the request HTTP version |
| 31 | +httpVersion :: Request -> String |
| 32 | +httpVersion = _.httpVersion <<< unsafeCoerce |
| 33 | + |
| 34 | +-- | Get the request headers as a hash |
| 35 | +requestHeaders :: Request -> StrMap String |
| 36 | +requestHeaders = _.headers <<< unsafeCoerce |
| 37 | + |
| 38 | +-- | Get the request method (GET, POST, etc.) |
| 39 | +requestMethod :: Request -> String |
| 40 | +requestMethod = _.method <<< unsafeCoerce |
| 41 | + |
| 42 | +-- | Get the request URL |
| 43 | +requestURL :: Request -> String |
| 44 | +requestURL = _.url <<< unsafeCoerce |
| 45 | + |
| 46 | +-- | Coerce the request object into a readable stream. |
| 47 | +requestAsStream :: forall eff a. Request -> Readable () (http :: HTTP | eff) a |
| 48 | +requestAsStream = unsafeCoerce |
| 49 | + |
| 50 | +-- | Set a header with a single value. |
| 51 | +foreign import setHeader :: forall eff. Response -> String -> String -> Eff (http :: HTTP | eff) Unit |
| 52 | + |
| 53 | +-- | Set a header with multiple values. |
| 54 | +foreign import setHeaders :: forall eff. Response -> String -> Array String -> Eff (http :: HTTP | eff) Unit |
| 55 | + |
| 56 | +-- | Set the status code. |
| 57 | +foreign import setStatusCode :: forall eff. Response -> Int -> Eff (http :: HTTP | eff) Unit |
| 58 | + |
| 59 | +-- | Set the status message. |
| 60 | +foreign import setStatusMessage :: forall eff. Response -> String -> Eff (http :: HTTP | eff) Unit |
| 61 | + |
| 62 | +-- | Coerce the response object into a writable stream. |
| 63 | +responseAsStream :: forall eff a. Response -> Writable () (http :: HTTP | eff) a |
| 64 | +responseAsStream = unsafeCoerce |
0 commit comments