on master help sign in
Switch to http-types
commitaddcaed2e45be76b9d74267dd94242e883ec9a23 authorMichael Snoyman <michael@snoyman.com> dateThu 10 Mar 2011, 07:31 (16 years ago) parent2bb48bb
2 files changed+14−177
Network/Wai.hs+11−175 wai.cabal+3−2
Network/Wai.hs+11−175
@@ -26,40 +26,8 @@ would kill the performance of a CGI application.
2626
2727-}
2828module Network.Wai
29− ( -- * Data types
30−
31− -- ** Request method
32− Method
33− -- ** HTTP protocol versions
34− , HttpVersion
35− , http09
36− , http10
37− , http11
38− -- ** Case-insensitive byte strings
39− , CIByteString (..)
40− , mkCIByteString
41− -- ** Request header names
42− , RequestHeader
43− , RequestHeaders
44− -- ** Response header names
45− , ResponseHeader
46− , ResponseHeaders
47− -- ** Response status code
48− , Status (..)
49− , status200, statusOK
50− , status201, statusCreated
51− , status206, statusPartialContent
52− , status301, statusMovedPermanently
53− , status302, statusFound
54− , status303, statusSeeOther
55− , status400, statusBadRequest
56− , status401, statusUnauthorized
57− , status403, statusForbidden
58− , status404, statusNotFound
59− , status405, statusNotAllowed
60− , status500, statusServerError
61− -- * WAI interface
62− , Request (..)
29+ ( -- * WAI interface
30+ Request (..)
6331 , Response (..)
6432 , ResponseEnumerator
6533 , responseEnumerator
@@ -70,152 +38,20 @@ module Network.Wai
7038 ) where
7139
7240import qualified Data.ByteString as B
73−import qualified Data.ByteString.Char8 as B8
7441import qualified Data.ByteString.Lazy as L
75−import Data.Char (toLower)
76−import Data.String (IsString (..))
7742import Data.Typeable (Typeable)
7843import Data.Enumerator (Iteratee, ($$), joinI, run_)
7944import qualified Data.Enumerator as E
80−import Data.Enumerator.IO (enumFile)
45+import Data.Enumerator.Binary (enumFile)
8146import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString)
82−import Data.Data (Data)
8347import Network.Socket (SockAddr)
84−
85−-- | HTTP request method. Since the HTTP protocol allows arbitrary request
86−-- methods, we leave this open as a 'B.ByteString'. Please note the request
87−-- methods are case-sensitive.
88−type Method = B.ByteString
89−
90−-- | Version of HTTP protocol used in current request. The value given here
91−-- should be everything following the \"HTTP/\" line in a request. In other
92−-- words, HTTP\/1.1 -> \"1.1\", HTTP\/1.0 -> \"1.0\".
93−type HttpVersion = B.ByteString
94−
95−-- | HTTP/0.9
96−http09 :: HttpVersion
97−http09 = B8.pack "0.9"
98−
99−-- | HTTP/1.0
100−http10 :: HttpVersion
101−http10 = B8.pack "1.0"
102−
103−-- | HTTP/1.1
104−http11 :: HttpVersion
105−http11 = B8.pack "1.1"
106−
107−-- | A case insensitive bytestring, where the 'Eq' and 'Ord' instances do
108−-- comparisons based on the lower-cased version of this string. For efficiency,
109−-- this datatype contains both the original and lower-case version of the
110−-- string; this means there is no need to lower-case the bytestring for every
111−-- comparison.
112−--
113−-- Please note that this datatype has an 'IsString' instance, which can allow
114−-- for very concise code when using the OverloadedStrings language extension.
115−data CIByteString = CIByteString
116− { ciOriginal :: !B.ByteString
117− , ciLowerCase :: !B.ByteString
118− }
119− deriving (Data, Typeable)
120−
121−-- | Convert a regular bytestring to a case-insensitive bytestring.
122−mkCIByteString :: B.ByteString -> CIByteString
123−mkCIByteString bs = CIByteString bs $ B8.map toLower bs
124−
125−instance Show CIByteString where
126− show = show . ciOriginal
127−instance Read CIByteString where
128− readsPrec i = map (\(x, y) -> (mkCIByteString x, y)) . readsPrec i
129−instance Eq CIByteString where
130− x == y = ciLowerCase x == ciLowerCase y
131−instance Ord CIByteString where
132− x <= y = ciLowerCase x <= ciLowerCase y
133−instance IsString CIByteString where
134− fromString = mkCIByteString . fromString
135−
136−-- | Headers sent from the client to the server. Note that this is a
137−-- case-insensitive string, as the HTTP spec specifies.
138−type RequestHeader = CIByteString
139−type RequestHeaders = [(RequestHeader, B.ByteString)]
140−
141−-- | Headers sent from the server to the client. Note that this is a
142−-- case-insensitive string, as the HTTP spec specifies.
143−type ResponseHeader = CIByteString
144−type ResponseHeaders = [(ResponseHeader, B.ByteString)]
145−
146−-- | HTTP status code; a combination of the integral code and a status message.
147−-- Equality is determined solely on the basis of the integral code.
148−data Status = Status { statusCode :: Int, statusMessage :: B.ByteString }
149− deriving Show
150−
151−instance Eq Status where
152− x == y = statusCode x == statusCode y
153−
154−-- | OK
155−status200, statusOK :: Status
156−status200 = Status 200 $ B8.pack "OK"
157−statusOK = status200
158−
159−-- | Created
160−status201, statusCreated :: Status
161−status201 = Status 201 $ B8.pack "Created"
162−statusCreated = status201
163−
164−-- | Partial Content
165−status206, statusPartialContent :: Status
166−status206 = Status 206 $ B8.pack "Partial Content"
167−statusPartialContent = status206
168−
169−-- | Moved Permanently
170−status301, statusMovedPermanently :: Status
171−status301 = Status 301 $ B8.pack "Moved Permanently"
172−statusMovedPermanently = status301
173−
174−-- | Found
175−status302, statusFound :: Status
176−status302 = Status 302 $ B8.pack "Found"
177−statusFound = status302
178−
179−-- | See Other
180−status303, statusSeeOther :: Status
181−status303 = Status 303 $ B8.pack "See Other"
182−statusSeeOther = status303
183−
184−-- | Bad Request
185−status400, statusBadRequest :: Status
186−status400 = Status 400 $ B8.pack "Bad Request"
187−statusBadRequest = status400
188−
189−-- | Unauthorized
190−status401, statusUnauthorized :: Status
191−status401 = Status 401 $ B8.pack "Unauthorized"
192−statusUnauthorized = status401
193−
194−-- | Forbidden
195−status403, statusForbidden :: Status
196−status403 = Status 403 $ B8.pack "Forbidden"
197−statusForbidden = status403
198−
199−-- | Not Found
200−status404, statusNotFound :: Status
201−status404 = Status 404 $ B8.pack "Not Found"
202−statusNotFound = status404
203−
204−-- | Method Not Allowed
205−status405, statusNotAllowed :: Status
206−status405 = Status 405 $ B8.pack "Method Not Allowed"
207−statusNotAllowed = status405
208−
209−-- | Internal Server Error
210−status500, statusServerError :: Status
211−status500 = Status 500 $ B8.pack "Internal Server Error"
212−statusServerError = status500
48+import qualified Network.HTTP.Types as H
21349
21450-- | Information on the request sent by the client. This abstracts away the
21551-- details of the underlying implementation.
21652data Request = Request
217− { requestMethod :: Method
218− , httpVersion :: HttpVersion
53+ { requestMethod :: H.Method
54+ , httpVersion :: H.HttpVersion
21955 -- | Extra path information sent by the client. The meaning varies slightly
22056 -- depending on backend; in a standalone server setting, this is most likely
22157 -- all information after the domain name. In a CGI application, this would be
@@ -225,7 +61,7 @@ data Request = Request
22561 , queryString :: B.ByteString
22662 , serverName :: B.ByteString
22763 , serverPort :: Int
228− , requestHeaders :: [(RequestHeader, B.ByteString)]
64+ , requestHeaders :: H.RequestHeaders
22965 -- ^ Was this request made over an SSL connection?
23066 , isSecure :: Bool
23167 -- ^ Log the given line in some method; how this is accomplished is
@@ -237,13 +73,13 @@ data Request = Request
23773 deriving Typeable
23874
23975data Response
240− = ResponseFile Status ResponseHeaders FilePath
241− | ResponseBuilder Status ResponseHeaders Builder
76+ = ResponseFile H.Status H.ResponseHeaders FilePath
77+ | ResponseBuilder H.Status H.ResponseHeaders Builder
24278 | ResponseEnumerator (forall a. ResponseEnumerator a)
24379 deriving Typeable
24480
24581type ResponseEnumerator a =
246− (Status -> ResponseHeaders -> Iteratee Builder IO a) -> IO a
82+ (H.Status -> H.ResponseHeaders -> Iteratee Builder IO a) -> IO a
24783
24884responseEnumerator :: Response -> ResponseEnumerator a
24985responseEnumerator (ResponseEnumerator e) f = e f
@@ -253,7 +89,7 @@ responseEnumerator (ResponseBuilder s h b) f = run_ $ do
25389 E.yield () $ E.Chunks [b]
25490 f s h
25591
256−responseLBS :: Status -> ResponseHeaders -> L.ByteString -> Response
92+responseLBS :: H.Status -> H.ResponseHeaders -> L.ByteString -> Response
25793responseLBS s h = ResponseBuilder s h . fromLazyByteString
25894
25995type Application = Request -> Iteratee B.ByteString IO Response
wai.cabal+3−2
@@ -1,5 +1,5 @@
11Name: wai
2−Version: 0.3.1
2+Version: 0.4.0
33Synopsis: Web Application Interface.
44Description: Provides a common protocol for communication between web aplications and web servers.
55License: BSD3
@@ -20,7 +20,8 @@ Library
2020 Build-Depends: base >= 3 && < 5
2121 , bytestring >= 0.9 && < 0.10
2222 , blaze-builder >= 0.2 && < 0.3
23− , enumerator >= 0.4 && < 0.5
23+ , enumerator >= 0.4.7 && < 0.5
2424 , network >= 2.2 && < 2.4
25+ , http-types >= 0.5 && < 0.6
2526 Exposed-modules: Network.Wai
2627 ghc-options: -Wall