{-# LANGUAGE TypeSynonymInstances #-}

-- | Hack spec! Stolen from Rack with some simplification.
module Hack where

import Data.Default
import System.IO

version :: [Int]
version = [2009, 4, 52]

data RequestMethod =
     OPTIONS
  |  GET
  |  HEAD
  |  POST
  |  PUT
  |  DELETE
  |  TRACE
  |  CONNECT
  deriving (Show, Read, Eq)

data Hack_UrlScheme = HTTP | HTTPS deriving (Show, Eq)

-- | customizable error stream
type HackErrors = String -> IO ()

instance Show HackErrors where
  show _ = "HackErrors"

data Env = Env 
  {  request_method :: RequestMethod            -- ^ HTTP request method  
  ,  script_name :: String                      -- ^ The initial portion of the request URL's \"path\" that corresponds to the application object, so that the application knows its virtual \"location\". This may be an empty string, if the application corresponds to the \"root\" of the server.   
  ,  path_info :: String                        -- ^ The remainder of the request URL's \"path\", designating the virtual \"location\" of the request's target within the application. This will always starts with \"/\"
  ,  query_string :: String                     -- ^ The portion of the request URL that follows the ?, if any. May be empty
  ,  server_name :: String                      -- ^ When combined with SCRIPT_NAME and PATH_INFO, these variables can be used to complete the URL. Note, however, that HTTP_HOST, if present, should be used 
  ,  server_port :: Int                         -- ^ preference to SERVER_NAME for reconstructing the request URL. SERVER_NAME and SERVER_PORT can never be empty strings, and so are always required.    
  ,  http :: [(String, String)]                 -- ^ Variables corresponding to the client-supplied HTTP request headers
  ,  hack_version :: [Int]                      -- ^ The Array [0,1], representing this version of Hack
  ,  hack_url_scheme :: Hack_UrlScheme          -- ^ HTTP or HTTPS, depending on the request URL
  ,  hack_input :: String                       -- ^ body of the request
  ,  hack_errors :: HackErrors                  -- ^ error stream
  ,  hack_multithread :: Bool                   -- ^ true if the application object may be simultaneously invoked by another thread in the same process, false otherwise. 
  ,  hack_multiprocess :: Bool                  -- ^ true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
  ,  hack_run_once :: Bool                      -- ^ true if the server expects (but does not guarantee!) that the application will only be invoked this one time during the life of its containing process. Normally, this will only be true for a server based on CGI (or something similar). 
  ,  custom :: [(String, String)]               -- ^ any non HTTP standard header
  }
  deriving (Show)

data Response = Response 
  {  status :: Int                              -- ^ must be greater than or equal to 100. 
  ,  headers :: [(String, String)]              -- ^ The header must not contain a Status key, contain keys with : or newlines in their name, contain keys names that end in - or _, but only contain keys that consist of letters, digits, _ or - and start with a letter. The values of the header must be Strings, consisting of lines (for multiple header values) seperated by \"\\n\". The lines must not contain characters below 037. 
  ,  body :: String                             -- ^ body of the response
  }
  deriving (Show)

instance Default RequestMethod where
  def = GET

instance Default Hack_UrlScheme where
  def = HTTP

instance Default Response where
  def = Response def def def

instance Default Env where
  def = Env def def def def def def def version def def stderr_stream False False False def
    where stderr_stream = hPutStr stderr

type Application = Env -> IO Response

type Middleware = Application -> Application