@@ -11,7 +11,6 @@ import Data.CaseInsensitive (foldCase, foldedCase, mk)
11
11
import Data.Either (isRight )
12
12
import Data.List.Split (wordsBy )
13
13
import Data.Maybe (fromMaybe , isJust )
14
- import Data.Semigroup (Semigroup (.. ))
15
14
import qualified Data.Text as T
16
15
import Data.Time (UTCTime , defaultTimeLocale , parseTimeM ,
17
16
rfc822DateFormat )
@@ -155,7 +154,7 @@ createContainsValidLocation
155
154
getsHaveLastModifiedHeader :: RequestPredicate
156
155
getsHaveLastModifiedHeader
157
156
= RequestPredicate $ \ req mgr ->
158
- if ( method req == methodGet)
157
+ if method req == methodGet
159
158
then do
160
159
resp <- httpLbs req mgr
161
160
unless (hasValidHeader " Last-Modified" isRFC822Date resp) $ do
@@ -189,15 +188,15 @@ notAllowedContainsAllowHeader
189
188
= RequestPredicate $ \ req mgr -> do
190
189
let reqs = [ req { method = renderStdMethod m } | m <- [minBound .. maxBound ]
191
190
, renderStdMethod m /= method req ]
192
- resp <- mapM (flip httpLbs mgr) reqs
191
+ resp <- mapM (` httpLbs` mgr) reqs
193
192
194
193
case filter pred' (zip reqs resp) of
195
194
(x: _) -> throw $ PredicateFailure " notAllowedContainsAllowHeader" (Just $ fst x) (snd x)
196
195
[] -> return resp
197
196
where
198
197
pred' (_, resp) = responseStatus resp == status405 && not (hasValidHeader " Allow" go resp)
199
198
where
200
- go x = all (\ y -> isRight $ parseMethod $ SBSC. pack y )
199
+ go x = all (isRight . parseMethod . SBSC. pack)
201
200
$ wordsBy (`elem` (" , " :: [Char ])) (SBSC. unpack x)
202
201
203
202
@@ -223,11 +222,9 @@ honoursAcceptHeader
223
222
let scode = responseStatus resp
224
223
sctype = lookup " Content-Type" $ responseHeaders resp
225
224
sacc = fromMaybe " */*" $ lookup " Accept" (requestHeaders req)
226
- if status100 < scode && scode < status300
227
- then if isJust $ sctype >>= \ x -> matchAccept [x] sacc
228
- then throw $ PredicateFailure " honoursAcceptHeader" (Just req) resp
229
- else return [resp]
230
- else return [resp]
225
+ (if (status100 < scode && scode < status300) && isJust (sctype >>= \ x -> matchAccept [x] sacc)
226
+ then throw $ PredicateFailure " honoursAcceptHeader" (Just req) resp
227
+ else return [resp])
231
228
232
229
233
230
-- | [__Best Practice__]
@@ -247,7 +244,7 @@ honoursAcceptHeader
247
244
getsHaveCacheControlHeader :: RequestPredicate
248
245
getsHaveCacheControlHeader
249
246
= RequestPredicate $ \ req mgr ->
250
- if ( method req == methodGet)
247
+ if method req == methodGet
251
248
then do
252
249
resp <- httpLbs req mgr
253
250
unless (hasValidHeader " Cache-Control" (const True ) resp) $ do
@@ -263,7 +260,7 @@ getsHaveCacheControlHeader
263
260
headsHaveCacheControlHeader :: RequestPredicate
264
261
headsHaveCacheControlHeader
265
262
= RequestPredicate $ \ req mgr ->
266
- if ( method req == methodHead)
263
+ if method req == methodHead
267
264
then do
268
265
resp <- httpLbs req mgr
269
266
unless (hasValidHeader " Cache-Control" (const True ) resp) $
@@ -334,10 +331,9 @@ linkHeadersAreValid
334
331
unauthorizedContainsWWWAuthenticate :: ResponsePredicate
335
332
unauthorizedContainsWWWAuthenticate
336
333
= ResponsePredicate $ \ resp ->
337
- if responseStatus resp == status401
338
- then unless (hasValidHeader " WWW-Authenticate" (const True ) resp) $
334
+ when ( responseStatus resp == status401) $
335
+ unless (hasValidHeader " WWW-Authenticate" (const True ) resp) $
339
336
throw $ PredicateFailure " unauthorizedContainsWWWAuthenticate" Nothing resp
340
- else return ()
341
337
342
338
343
339
-- | [__RFC Compliance__]
@@ -354,12 +350,10 @@ unauthorizedContainsWWWAuthenticate
354
350
htmlIncludesDoctype :: ResponsePredicate
355
351
htmlIncludesDoctype
356
352
= ResponsePredicate $ \ resp ->
357
- if hasValidHeader " Content-Type" (SBS. isPrefixOf . foldCase $ " text/html" ) resp
358
- then do
359
- let htmlContent = foldCase . LBS. take 20 $ responseBody resp
360
- unless (LBS. isPrefixOf (foldCase " <!doctype html>" ) htmlContent) $
361
- throw $ PredicateFailure " htmlIncludesDoctype" Nothing resp
362
- else return ()
353
+ when (hasValidHeader " Content-Type" (SBS. isPrefixOf . foldCase $ " text/html" ) resp) $ do
354
+ let htmlContent = foldCase . LBS. take 20 $ responseBody resp
355
+ unless (LBS. isPrefixOf (foldCase " <!doctype html>" ) htmlContent) $
356
+ throw $ PredicateFailure " htmlIncludesDoctype" Nothing resp
363
357
364
358
-- * Predicate logic
365
359
@@ -392,7 +386,7 @@ newtype RequestPredicate = RequestPredicate
392
386
393
387
-- TODO: This isn't actually a monoid
394
388
instance Monoid RequestPredicate where
395
- mempty = RequestPredicate (\ r m -> httpLbs r m >>= \ x -> return ( [x]) )
389
+ mempty = RequestPredicate (\ r m -> httpLbs r m >>= \ x -> return [x])
396
390
mappend = (<>)
397
391
398
392
-- TODO: This isn't actually a monoid
@@ -417,10 +411,10 @@ instance Monoid Predicates where
417
411
class JoinPreds a where
418
412
joinPreds :: a -> Predicates -> Predicates
419
413
420
- instance JoinPreds ( RequestPredicate ) where
414
+ instance JoinPreds RequestPredicate where
421
415
joinPreds p (Predicates x y) = Predicates (p <> x) y
422
416
423
- instance JoinPreds ( ResponsePredicate ) where
417
+ instance JoinPreds ResponsePredicate where
424
418
joinPreds p (Predicates x y) = Predicates x (p <> y)
425
419
426
420
-- | Adds a new predicate (either `ResponsePredicate` or `RequestPredicate`) to
@@ -444,9 +438,7 @@ finishPredicates p req mgr = go `catch` \(e :: PredicateFailure) -> return $ Jus
444
438
-- * helpers
445
439
446
440
hasValidHeader :: SBS. ByteString -> (SBS. ByteString -> Bool ) -> Response b -> Bool
447
- hasValidHeader hdr p r = case lookup (mk hdr) (responseHeaders r) of
448
- Nothing -> False
449
- Just v -> p v
441
+ hasValidHeader hdr p r = maybe False p (lookup (mk hdr) (responseHeaders r))
450
442
451
443
isRFC822Date :: SBS. ByteString -> Bool
452
444
isRFC822Date s
0 commit comments