1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
--
-- HTTP types for use with io-streams and pipes
--
-- Copyright © 2012-2014 Operational Dynamics Consulting, Pty Ltd
--
-- The code in this file, and the program it is a part of, is
-- made available to you by its authors as open source software:
-- you can redistribute it and/or modify it under the terms of
-- the BSD licence.
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide, prune #-}
--
{- | If you're not http-streams or pipes-http and you're importing this,
you're Doing It Wrong.
-}
module Network.Http.Internal (
Hostname,
Port,
ContentType,
FieldName,
Request (..),
EntityBody (..),
ExpectMode (..),
Boundary,
unBoundary,
emptyBoundary,
randomBoundary,
packBoundary,
Response (..),
StatusCode,
TransferEncoding (..),
ContentEncoding (..),
getStatusCode,
getStatusMessage,
getHeader,
Method (..),
Headers,
emptyHeaders,
updateHeader,
removeHeader,
buildHeaders,
lookupHeader,
retrieveHeaders,
HttpType (getHeaders),
HttpParseException (..),
composeMultipartBytes,
composeMultipartEnding,
-- for testing
composeRequestBytes,
composeResponseBytes,
) where
import Prelude hiding (lookup)
import Blaze.ByteString.Builder (Builder)
import qualified Blaze.ByteString.Builder as Builder (
copyByteString,
fromByteString,
toByteString,
)
import qualified Blaze.ByteString.Builder.Char8 as Builder (
fromChar,
fromShow,
fromString,
)
import Control.Exception (Exception)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S
import Data.CaseInsensitive (CI, mk, original)
import Data.Char (chr)
import Data.HashMap.Strict (
HashMap,
delete,
empty,
foldrWithKey,
insert,
insertWith,
lookup,
toList,
)
import Data.Int (Int64)
import Data.List (foldl')
import Data.Typeable (Typeable)
import Data.Word (Word16)
import System.Random (newStdGen, randomRs)
type Hostname = ByteString
type Port = Word16
type ContentType = ByteString
type FieldName = ByteString
-- | HTTP Methods, as per RFC 2616
data Method
= GET
| HEAD
| POST
| PUT
| DELETE
| TRACE
| OPTIONS
| CONNECT
| PATCH
| Method ByteString
deriving (Show, Read, Ord)
instance Eq Method where
GET == GET = True
HEAD == HEAD = True
POST == POST = True
PUT == PUT = True
DELETE == DELETE = True
TRACE == TRACE = True
OPTIONS == OPTIONS = True
CONNECT == CONNECT = True
PATCH == PATCH = True
GET == Method "GET" = True
HEAD == Method "HEAD" = True
POST == Method "POST" = True
PUT == Method "PUT" = True
DELETE == Method "DELETE" = True
TRACE == Method "TRACE" = True
OPTIONS == Method "OPTIONS" = True
CONNECT == Method "CONNECT" = True
PATCH == Method "PATCH" = True
Method a == Method b = a == b
m@(Method _) == other = other == m
_ == _ = False
--
{- |
A description of the request that will be sent to the server. Note
unlike other HTTP libraries, the request body is /not/ a part of this
object; that will be streamed out by you when actually sending the
request with 'sendRequest'.
'Request' has a useful @Show@ instance that will output the request
line and headers (as it will be sent over the wire but with the @\\r@
characters stripped) which can be handy for debugging.
Note that the actual @Host:@ header is not set until the request is sent,
so you will not see it in the Show instance (unless you call 'setHostname'
to override the value inherited from the @Connection@).
-}
data Request = Request
{ qMethod :: !Method
, qHost :: !(Maybe ByteString)
, qPath :: !ByteString
, qBody :: !EntityBody
, qExpect :: !ExpectMode
, qHeaders :: !Headers
, qBoundary :: !Boundary
}
deriving (Eq)
instance Show Request where
show q =
{-# SCC "Request.show" #-}
S.unpack $ S.filter (/= '\r') $ Builder.toByteString $ composeRequestBytes q "<to be determined>"
data EntityBody = Empty | Chunking | Static Int64 deriving (Show, Eq, Ord)
data ExpectMode = Normal | Continue deriving (Show, Eq, Ord)
newtype Boundary = Boundary ByteString deriving (Show, Eq)
unBoundary :: Boundary -> ByteString
unBoundary (Boundary b') = b'
emptyBoundary :: Boundary
emptyBoundary = Boundary S.empty
represent :: Int -> Char
represent x
| x < 10 = chr (48 + x)
| x < 36 = chr (65 + x - 10)
| x < 62 = chr (97 + x - 36)
| otherwise = '@'
{- |
Generate a random string to be used as an inter-part boundary in RFC 7578
multipart form data. You pass this value to
'Network.Http.Client.setContentMultipart' and subsequently to
'Network.Http.Client.multipartFormBody'.
-}
randomBoundary :: IO Boundary
randomBoundary = do
gen <- newStdGen
let result = S.pack . fmap represent . take 20 . randomRs (0, 61) $ gen
pure (Boundary result)
{- |
If you want to fix the multipart boundary to a known value (for testing
purposes) you can use this. The ideal such string, in case you are wondering,
is @\"bEacHV0113YB\@ll\"@.
This isn't safe for use in production; you need to use an unpredictable value
as the boundary separtor so prefer 'randomBoundary'.
-}
packBoundary :: String -> Boundary
packBoundary = Boundary . S.pack
{-
The bit that builds up the actual string to be transmitted. This
is on the critical path for every request, so we'll want to revisit
this to improve performance.
- Rewrite rule for Method?
- How can serializing the Headers be made efficient?
This code includes the RFC compliant CR-LF sequences as line
terminators, which is why the Show instance above has to bother
with removing them.
-}
composeRequestBytes :: Request -> ByteString -> Builder
composeRequestBytes q h' =
mconcat
[ requestline
, hostLine
, headerFields
, crlf
]
where
requestline =
mconcat
[ method
, sp
, uri
, sp
, version
, crlf
]
method = case qMethod q of
GET -> Builder.fromString "GET"
HEAD -> Builder.fromString "HEAD"
POST -> Builder.fromString "POST"
PUT -> Builder.fromString "PUT"
DELETE -> Builder.fromString "DELETE"
TRACE -> Builder.fromString "TRACE"
OPTIONS -> Builder.fromString "OPTIONS"
CONNECT -> Builder.fromString "CONNECT"
PATCH -> Builder.fromString "PATCH"
(Method x) -> Builder.fromByteString x
uri = case qPath q of
"" -> Builder.fromChar '/'
path -> Builder.copyByteString path
version = Builder.fromString "HTTP/1.1"
hostLine =
mconcat
[ Builder.fromString "Host: "
, hostname
, crlf
]
hostname = case qHost q of
Just x' -> Builder.copyByteString x'
Nothing -> Builder.copyByteString h'
headerFields = joinHeaders $ unWrap $ qHeaders q
crlf = Builder.fromString "\r\n"
sp = Builder.fromChar ' '
dashdash = Builder.fromString "--"
composeMultipartBytes :: Boundary -> FieldName -> Maybe FilePath -> Maybe ContentType -> Builder
composeMultipartBytes boundary name possibleFilename possibleContentType =
mconcat
[ boundaryLine
, dispositionLine
, mimetypeLine
, crlf -- second CR LF
]
where
boundaryLine =
crlf
<> dashdash
<> Builder.copyByteString (unBoundary boundary)
<> crlf
dispositionLine =
"Content-Disposition: form-data; name=\""
<> Builder.copyByteString name
<> "\""
<> case possibleFilename of
Just filename ->
"; filename=\""
<> Builder.fromString filename
<> "\""
Nothing -> mempty
<> crlf
mimetypeLine =
case possibleContentType of
Just mimetype ->
"Content-Type: " <> Builder.copyByteString mimetype
<> crlf
Nothing -> mempty
composeMultipartEnding :: Boundary -> Builder
composeMultipartEnding boundary =
crlf
<> dashdash
<> Builder.copyByteString (unBoundary boundary)
<> dashdash
<> crlf
type StatusCode = Int
{- |
A description of the response received from the server. Note
unlike other HTTP libraries, the response body is /not/ a part
of this object; that will be streamed in by you when calling
'receiveResponse'.
Like 'Request', 'Response' has a @Show@ instance that will output
the status line and response headers as they were received from the
server.
-}
data Response = Response
{ pStatusCode :: !StatusCode
, pStatusMsg :: !ByteString
, pTransferEncoding :: !TransferEncoding
, pContentEncoding :: !ContentEncoding
, pContentLength :: !(Maybe Int64)
, pHeaders :: !Headers
}
instance Show Response where
show p =
{-# SCC "Response.show" #-}
S.unpack $ S.filter (/= '\r') $ Builder.toByteString $ composeResponseBytes p
data TransferEncoding = None | Chunked
data ContentEncoding = Identity | Gzip | Deflate
deriving (Show)
-- | Get the HTTP response status code.
getStatusCode :: Response -> StatusCode
getStatusCode = pStatusCode
{-# INLINE getStatusCode #-}
{- |
Get the HTTP response status message. Keep in mind that this is
/not/ normative; whereas 'getStatusCode' values are authoritative.
-}
getStatusMessage :: Response -> ByteString
getStatusMessage = pStatusMsg
{-# INLINE getStatusMessage #-}
{- |
Lookup a header in the response. HTTP header field names are
case-insensitive, so you can specify the name to lookup however you
like. If the header is not present @Nothing@ will be returned.
> let n = case getHeader p "Content-Length" of
> Just x' -> read x' :: Int
> Nothing -> 0
which of course is essentially what goes on inside the client library when
it receives a response from the server and has to figure out how many bytes
to read.
There is a fair bit of complexity in some of the other HTTP response
fields, so there are a number of specialized functions for reading
those values where we've found them useful.
-}
getHeader :: Response -> ByteString -> Maybe ByteString
getHeader p k =
lookupHeader h k
where
h = pHeaders p
{- |
Accessors common to both the outbound and return sides of an HTTP connection.
Most people do not need this; for most cases you just need to get a header or
two from the response, for which you can use 'getHeader'. On the other hand,
if you do need to poke around in the raw headers,
@
import Network.Http.Types
@
will give you functions like 'lookupHeader' and 'updateHeader' to to work
with.
-}
class HttpType τ where
-- | Get the Headers from a Request or Response.y
getHeaders :: τ -> Headers
instance HttpType Request where
getHeaders q = qHeaders q
instance HttpType Response where
getHeaders p = pHeaders p
composeResponseBytes :: Response -> Builder
composeResponseBytes p =
mconcat
[ statusline
, headerFields
, crlf
]
where
statusline =
mconcat
[ version
, sp
, code
, sp
, message
, crlf
]
code = Builder.fromShow $ pStatusCode p
message = Builder.copyByteString $ pStatusMsg p
version = Builder.fromString "HTTP/1.1"
headerFields = joinHeaders $ unWrap $ pHeaders p
{- |
The map of headers in a 'Request' or 'Response'. Note that HTTP
header field names are case insensitive, so if you call 'setHeader'
on a field that's already defined but with a different capitalization
you will replace the existing value.
-}
{-
This is a fair bit of trouble just to avoid using a typedef here.
Probably worth it, though; every other HTTP client library out there
exposes the gory details of the underlying map implementation, and
to use it you need to figure out all kinds of crazy imports. Indeed,
this code used here in the Show instance for debugging has been
copied & pasted around various projects of mine since I started
writing Haskell. It's quite tedious, and very arcane! So, wrap it
up.
-}
newtype Headers = Wrap
{ unWrap :: HashMap (CI ByteString) ByteString
}
deriving (Eq)
instance Show Headers where
show x = S.unpack $ S.filter (/= '\r') $ Builder.toByteString $ joinHeaders $ unWrap x
joinHeaders :: HashMap (CI ByteString) ByteString -> Builder
joinHeaders m = foldrWithKey combine mempty m
combine :: CI ByteString -> ByteString -> Builder -> Builder
combine k v acc =
mconcat [acc, key, Builder.fromString ": ", value, crlf]
where
key = Builder.copyByteString $ original k
value = Builder.fromByteString v
{-# INLINE combine #-}
emptyHeaders :: Headers
emptyHeaders =
Wrap empty
{- |
Set a header field to the specified value. This will overwrite
any existing value for the field. Remember that HTTP fields names
are case insensitive!
-}
updateHeader :: Headers -> ByteString -> ByteString -> Headers
updateHeader x k v =
Wrap result
where
!result = insert (mk k) v m
!m = unWrap x
{- |
Remove a header from the map. If a field with that name is not present,
then this will have no effect.
-}
removeHeader :: Headers -> ByteString -> Headers
removeHeader x k =
Wrap result
where
!result = delete (mk k) m
!m = unWrap x
-- | Given a list of field-name,field-value pairs, construct a Headers map.
{-
This is only going to be used by RequestBuilder and ResponseParser,
obviously. And yes, as usual, we go to a lot of trouble to splice out the
function doing the work, in the name of type sanity.
-}
buildHeaders :: [(ByteString, ByteString)] -> Headers
buildHeaders hs =
Wrap result
where
result = foldl' addHeader empty hs
{-
insertWith is used here for the case where a header is repeated
(for example, Set-Cookie) and the values need to be intercalated
with ',' as per RFC 2616 §4.2.
-}
addHeader ::
HashMap (CI ByteString) ByteString ->
(ByteString, ByteString) ->
HashMap (CI ByteString) ByteString
addHeader m (k, v) =
insertWith f (mk k) v m
where
f new old = S.concat [old, ",", new]
lookupHeader :: Headers -> ByteString -> Maybe ByteString
lookupHeader x k =
lookup (mk k) m
where
!m = unWrap x
-- | Get the headers as a field-name,field-value association list.
retrieveHeaders :: Headers -> [(ByteString, ByteString)]
retrieveHeaders x =
map down $ toList m
where
!m = unWrap x
down :: (CI ByteString, ByteString) -> (ByteString, ByteString)
down (k, v) =
(original k, v)
data HttpParseException = HttpParseException String
deriving (Typeable, Show)
instance Exception HttpParseException
|