Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed Rfc 2616 (5.1 Request-Line, 5.1.2 Request-URI).

  • Loading branch information...
commit 9501780e553f227e22f8127379de81f3b5f09247 1 parent ae3e381
@aycanirican authored
View
2  Network/Parser/Rfc2616.hs
@@ -68,8 +68,10 @@ requestUri :: Parser RequestUri
requestUri = try (Asterisk <$ word8 42)
<|> AbsoluteUri <$> R3986.absoluteUri
<|> (AbsolutePath . W.pack) <$> R3986.pathAbsolute
+ <|> RelativeRef <$> R3986.relativeRef
<|> Authority <$> R3986.authority
+-- parse requestLine (C.pack "GET /my.cgi?foo=bar&john=doe HTTP/1.1\n")
requestLine :: Parser (Method, RequestUri, HttpVersion)
requestLine = ret <$> method <* sp
<*> requestUri <* sp
View
62 Network/Types.hs
@@ -22,16 +22,6 @@ import Control.Exception as Ex
import Data.Typeable
import Data.ByteString.Char8 as C
--- | HTTP error.
-data HttpError
- = InvalidRequestError { httpErrorMessage :: String }
- deriving (Eq, Typeable)
-
-instance Exception HttpError
-
-instance Show HttpError where
- show (InvalidRequestError msg) = "Invalid HTTP request: " ++ msg
-
-- | HTTP Version holds major and minor numbers.
data HttpVersion =
HttpVersion { httpMajor :: Int
@@ -47,7 +37,11 @@ http11 :: HttpVersion
http11 = HttpVersion 1 1
-- data HttpMessage = Request | Response
+
+-- | HTTP Headers
data Header = GeneralHeader | RequestHeader | EntityHeader
+
+-- | HTTP Methods
data Method = GET
| HEAD
| POST
@@ -57,9 +51,9 @@ data Method = GET
| OPTIONS
| CONNECT
| EXTENSIONMETHOD ByteString
- deriving (Show,Read,Ord,Eq)
+ deriving (Eq, Show)
-data Request =
+data Request =
Request {
rqMethod :: Method -- ^ Request Method
, rqUri :: RequestUri -- ^ Request URI
@@ -68,12 +62,12 @@ data Request =
, rqBody :: ByteString -- ^ Request Body
} deriving (Eq, Show)
-
-data RequestUri = Asterisk
- | AbsoluteUri URI
- | AbsolutePath ByteString
- | Authority (Maybe URIAuth)
- deriving (Eq, Show)
+data RequestUri = Asterisk -- ^ like in OPTIONS * HTTP/1.1
+ | AbsoluteUri URI -- ^ commonly used in proxy servers
+ | AbsolutePath ByteString -- ^ like /asd.cgi
+ | RelativeRef URI -- ^ with a query part like /asd.cgi?foo=bar
+ | Authority (Maybe URIAuth) -- ^ Just the authority part
+ deriving (Eq, Show)
data Response =
Response {
@@ -84,18 +78,19 @@ data Response =
} deriving (Eq, Show)
data URI = URI
- { uriScheme :: String
- , uriAuthority :: Maybe URIAuth
- , uriPath :: String
- , uriQuery :: String
- , uriFragment :: String
- } deriving (Eq, Typeable, Show)
+ { uriScheme :: String -- ^ Ex: http or https
+ , uriAuthority :: Maybe URIAuth -- ^ authority = [ userinfo "@" ] host [ ":" port ]
+ , uriPath :: String -- ^ Path is the part between the
+ -- authority and the query
+ , uriQuery :: String -- ^ Query begins with '?'
+ , uriFragment :: String -- ^ Fragment begins with '#'
+ } deriving (Eq, Show)
data URIAuth = URIAuth
- { uriUserInfo :: String
- , uriRegName :: String
- , uriPort :: String
- } deriving (Eq, Typeable, Show)
+ { uriUserInfo :: String -- ^ username:password
+ , uriRegName :: String -- ^ registered name, ex: www.core.gen.tr
+ , uriPort :: String -- ^ Port as a string
+ } deriving (Eq, Show)
nullURI :: URI
nullURI = URI
@@ -105,3 +100,14 @@ nullURI = URI
, uriQuery = ""
, uriFragment = ""
}
+
+-- | HTTP error.
+data HttpError
+ = InvalidRequestError { httpErrorMessage :: String }
+ deriving (Eq, Typeable)
+
+instance Exception HttpError
+
+instance Show HttpError where
+ show (InvalidRequestError msg) = "Invalid HTTP request: " ++ msg
+
View
4 test/Test/Parser/Rfc2616.hs
@@ -47,8 +47,8 @@ test_requestline2 = ae "requestLine"
(aP requestLine "GET /index.html HTTP/1.1\n")
test_requestline3 = ae "requestLine"
- (Just (GET,AbsolutePath "/asd.cgi?foo=bar",http11))
- (aP requestLine "GET /asd.cgi?foo=bar HTTP/1.1\n")
+ (Just (GET, RelativeRef (URI {uriScheme = "", uriAuthority = Nothing, uriPath = "/my.cgi", uriQuery = "?foo=bar&john=doe", uriFragment = ""}),http11))
+ (aP requestLine "GET /my.cgi?foo=bar&john=doe HTTP/1.1\n")
test_request = ae "request"
(Just $ Request {rqMethod = GET, rqUri = AbsolutePath "/favicon.ico", rqVersion = http11, rqHeaders = [("Host","0.0.0.0=5000"),("User-Agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0"),("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),("Accept-Language","en-us,en;q=0.5"),("Accept-Encoding","gzip,deflate"),("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"),("Keep-Alive","300"),("Connection","keep-alive")], rqBody = ""})
Please sign in to comment.
Something went wrong with that request. Please try again.