{-# LANGUAGE CPP, OverloadedStrings #-}
------------------------------------------------------------
-- Copyright : Erik de Castro Lopo <erikd@mega-nerd.com>
-- License : BSD3
------------------------------------------------------------

module Network.HTTP.Proxy.Request
    ( Port
    , Request (..)

    , proxyRequest
    , waiRequest
    , waiRequestHost
    )
    where

import Data.ByteString.Char8 (ByteString)
import Data.Maybe (fromMaybe)
import Network.HTTP.Types (Method)

import qualified Network.HTTP.Types as HT
import qualified Network.Wai as Wai

type Port = Int


-- |
data Request = Request
    {
    -- | Request method such as GET.
      Request -> Method
requestMethod :: Method
    -- | HTTP version such as 1.1.
    , Request -> HttpVersion
httpVersion :: HT.HttpVersion
    -- | A list of header (a pair of key and value) in an HTTP request.
    , Request -> RequestHeaders
requestHeaders :: HT.RequestHeaders
    -- | The part of the URL before the query part.
    , Request -> Method
requestPath :: ByteString
    -- | Parsed query string information
    , Request -> Method
queryString :: ByteString
    } deriving (Int -> Request -> ShowS
[Request] -> ShowS
Request -> String
(Int -> Request -> ShowS)
-> (Request -> String) -> ([Request] -> ShowS) -> Show Request
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Request] -> ShowS
$cshowList :: [Request] -> ShowS
show :: Request -> String
$cshow :: Request -> String
showsPrec :: Int -> Request -> ShowS
$cshowsPrec :: Int -> Request -> ShowS
Show, Request -> Request -> Bool
(Request -> Request -> Bool)
-> (Request -> Request -> Bool) -> Eq Request
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Request -> Request -> Bool
$c/= :: Request -> Request -> Bool
== :: Request -> Request -> Bool
$c== :: Request -> Request -> Bool
Eq)


proxyRequest :: Wai.Request -> Request
proxyRequest :: Request -> Request
proxyRequest Request
wreq = Method
-> HttpVersion -> RequestHeaders -> Method -> Method -> Request
Request
                        (Request -> Method
Wai.requestMethod Request
wreq)
                        (Request -> HttpVersion
Wai.httpVersion Request
wreq)
                        (Request -> RequestHeaders
Wai.requestHeaders Request
wreq)
                        (Request -> Method
Wai.rawPathInfo Request
wreq)
                        (Request -> Method
Wai.rawQueryString Request
wreq)

waiRequest :: Wai.Request -> Request -> Wai.Request
waiRequest :: Request -> Request -> Request
waiRequest Request
original Request
req = Request
original
    { requestMethod :: Method
Wai.requestMethod  = Request -> Method
requestMethod Request
req
    , httpVersion :: HttpVersion
Wai.httpVersion    = Request -> HttpVersion
httpVersion Request
req
    , requestHeaders :: RequestHeaders
Wai.requestHeaders = Request -> RequestHeaders
requestHeaders Request
req
    , rawPathInfo :: Method
Wai.rawPathInfo    = Request -> Method
requestPath Request
req
    , rawQueryString :: Method
Wai.rawQueryString = Request -> Method
queryString Request
req
    }


waiRequestHost :: Wai.Request -> ByteString
waiRequestHost :: Request -> Method
waiRequestHost Request
req = Method -> Maybe Method -> Method
forall a. a -> Maybe a -> a
fromMaybe Method
"???" (Maybe Method -> Method) -> Maybe Method -> Method
forall a b. (a -> b) -> a -> b
$ Request -> Maybe Method
Wai.requestHeaderHost Request
req