File: Browser.hs

package info (click to toggle)
haskell-http 20060707-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 220 kB
  • ctags: 7
  • sloc: haskell: 1,885; sh: 108; makefile: 82
file content (957 lines) | stat: -rw-r--r-- 35,831 bytes parent folder | download
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
-----------------------------------------------------------------------------
-- |
-- Module      :  Network.Browser
-- Copyright   :  (c) Warrick Gray 2002
-- License     :  BSD
-- 
-- Maintainer  :  bjorn@bringert.net
-- Stability   :  experimental
-- Portability :  non-portable (not tested)
--
-- An HTTP\/1.1 compatible wrapper for the HTTP module.
-----------------------------------------------------------------------------
 
{-

  Change Log:
   - altered 'closeTCP' to 'close', for consistency with altered HTTP
   - added debugging settings to browser.

  To Do: 
   - testing!!!
   - remove BrowserAction type? Possibly replace with IORef?
   - (more todo's in the HTTP mod)

-}

module Network.Browser (
    BrowserState,
    BrowserAction,      -- browser monad, effectively a state monad.
    Cookie,
    Form(..),
    Proxy(..),
    
    browse,             -- BrowserAction a -> IO a
    request,            -- Request -> BrowserAction Response
    
    setAllowRedirects,
    getAllowRedirects,
    
    setCookieFilter,
    defaultCookieFilter,
    userCookieFilter,
    
    getCookies,
    setCookies,
    addCookie,

    setErrHandler,
    setOutHandler,

    setProxy,

    setDebugLog,

    out,
    err,
    ioAction,           -- :: IO a -> BrowserAction a

    defaultGETRequest,
    formToRequest,
    uriDefaultTo,
    uriTrimHost
) where

import Network.HTTP

import Data.Char (toLower,isAlphaNum,isSpace)
import Data.List (isPrefixOf,isSuffixOf,elemIndex,elemIndices)
import Data.Maybe
import Control.Monad (foldM,filterM,liftM,when)
import Text.ParserCombinators.Parsec
import Network.URI

import qualified System.IO
import Data.Word (Word8)
import qualified Network.HTTP.MD5 as MD5
import qualified Network.HTTP.Base64 as Base64

type Octet = Word8

------------------------------------------------------------------
----------------------- Miscellaneous ----------------------------
------------------------------------------------------------------

word, quotedstring :: Parser String
quotedstring =
    do { char '"'
       ; str <- many (satisfy $ not . (=='"'))
       ; char '"'
       ; return str
       }

word = many1 (satisfy (\x -> isAlphaNum x || x=='_' || x=='.'))


-- misc string fns
trim :: String -> String
trim = let dropspace = dropWhile isSpace in
       reverse . dropspace . reverse . dropspace


-- | Splits a list into two parts, the delimiter occurs
-- at the head of the second list.  Nothing is returned
-- when no occurance of the delimiter is found.
split :: Eq a => a -> [a] -> Maybe ([a],[a])
split delim list = case delim `elemIndex` list of
    Nothing -> Nothing
    Just x  -> Just $ splitAt x list
    


-- | Removes delimiters.
splitMany :: Eq a => a -> [a] -> [[a]]
splitMany delim str = fn str ixs
    where
        ixs = elemIndices delim str
        fn _ [] = []
        fn ls (h:t) = let (a,b) = splitAt h ls in a : fn b t


-- | Returns a URI that is consistent with the first
-- argument uri when read in the context of a second.
-- If second argument is not sufficient context for
-- determining a full URI then anarchy reins.
uriDefaultTo :: URI -> URI -> URI
uriDefaultTo a b =
    case a `relativeTo` b of
        Nothing -> a
        Just x  -> x


uriTrimHost :: URI -> URI
uriTrimHost uri = uri { uriScheme="", uriAuthority=Nothing }


------------------------------------------------------------------
----------------------- Cookie Stuff -----------------------------
------------------------------------------------------------------

-- Some conventions: 
--     assume ckDomain is lowercase
--
data Cookie = MkCookie { ckDomain
                       , ckName
                       , ckValue :: String
                       , ckPath
                       , ckComment
                       , ckVersion :: Maybe String
                       }
    deriving(Show,Read)


instance Eq Cookie where
    a == b  =  ckDomain a == ckDomain b 
            && ckName a == ckName b 
            && ckPath a == ckPath b




defaultCookieFilter url cky = return True
userCookieFilter url cky =
    do putStrLn ("Set-Cookie received when requesting: " ++ show url)
       case ckComment cky of
          Nothing -> return ()
          Just x  -> putStrLn ("Cookie Comment:\n" ++ x)
       putStrLn ("Domain/Path: " ++ ckDomain cky ++ 
            case ckPath cky of
                Nothing -> ""
                Just x  -> "/" ++ x)
       putStrLn (ckName cky ++ '=' : ckValue cky)
       System.IO.hSetBuffering System.IO.stdout System.IO.NoBuffering
       System.IO.hSetBuffering System.IO.stdin System.IO.NoBuffering
       System.IO.hPutStr System.IO.stdout "Accept [y/n]? "
       x <- System.IO.hGetChar System.IO.stdin
       System.IO.hSetBuffering System.IO.stdin System.IO.LineBuffering
       System.IO.hSetBuffering System.IO.stdout System.IO.LineBuffering
       return (toLower x == 'y')
       


-- | Serialise a Cookie for inclusion in a request.
cookieToHeader :: Cookie -> Header
cookieToHeader ck = Header HdrCookie text
    where
        text = "$Version=" ++ fromMaybe "0" (ckVersion ck)
             ++ ';' : ckName ck ++ "=" ++ ckValue ck
             ++ (case ckPath ck of
                     Nothing -> ""
                     Just x  -> ";$Path=" ++ x)
             ++ ";$Domain=" ++ ckDomain ck



{- replace "error" call with [] in final version? -}
headerToCookies :: String -> Header -> [Cookie]
headerToCookies dom (Header HdrSetCookie val) = 
    case parse cookies "" val of
        Left e  -> error ("Cookie parse failure on: " ++ val ++ " " ++ show e)
        Right x  -> x
    where
        cookies :: Parser [Cookie]
        cookies = sepBy1 cookie (char ',')

        cookie :: Parser Cookie
        cookie =
            do { name <- word
               ; spaces
               ; char '='
               ; spaces
               ; val <- cvalue
               ; args <- cdetail
               ; return $ mkCookie name val args
               }

        cvalue :: Parser String
        
        spaces = many (satisfy isSpace)

        cvalue = quotedstring <|> many1 (satisfy $ not . (==';'))
       
        -- all keys in the result list MUST be in lower case
        cdetail :: Parser [(String,String)]
        cdetail = many $
            do { spaces
               ; char ';'
               ; spaces
               ; s1 <- word
               ; spaces
               ; s2 <- option "" (do { char '=' ; spaces ; v <- cvalue ; return v })
               ; return (map toLower s1,s2)
               }

        mkCookie :: String -> String -> [(String,String)] -> Cookie
        mkCookie nm val more = MkCookie { ckName=nm
                                        , ckValue=val
                                        , ckDomain=map toLower (fromMaybe dom (lookup "domain" more))
                                        , ckPath=lookup "path" more
                                        , ckVersion=lookup "version" more
                                        , ckComment=lookup "comment" more
                                        }

      

-- | Adds a cookie to the browser state, removing duplicates.
addCookie :: Cookie -> BrowserAction ()
addCookie c = alterBS (\b -> b { bsCookies=c : fn (bsCookies b) })
    where
        fn = filter (not . (==c))

setCookies cs = alterBS (\b -> b { bsCookies=cs })
getCookies = getBS bsCookies

-- ...get domain specific cookies...
-- ... this needs changing for consistency with rfc2109...
-- ... currently too broad.
getCookiesFor :: String -> String -> BrowserAction [Cookie]
getCookiesFor dom path =
    do cks <- getCookies
       return (filter cookiematch cks)
    where
        cookiematch :: Cookie -> Bool
        cookiematch ck = ckDomain ck `isSuffixOf` dom
                      && case ckPath ck of
                             Nothing -> True
                             Just p  -> p `isPrefixOf` path
      

setCookieFilter :: (URI -> Cookie -> IO Bool) -> BrowserAction ()
setCookieFilter f = alterBS (\b -> b { bsCookieFilter=f })

getCookieFilter :: BrowserAction (URI -> Cookie -> IO Bool)
getCookieFilter = getBS bsCookieFilter

------------------------------------------------------------------
----------------------- Authorisation Stuff ----------------------
------------------------------------------------------------------

{-

The browser handles 401 responses in the following manner:
  1) extract all WWW-Authenticate headers from a 401 response
  2) rewrite each as a Challenge object, using "headerToChallenge"
  3) pick a challenge to respond to, usually the strongest
     challenge understood by the client, using "pickChallenge"
  4) generate a username/password combination using the browsers
     "bsAuthorityGen" function (the default behaviour is to ask
     the user)
  5) build an Authority object based upon the challenge and user
     data, store this new Authority in the browser state
  6) convert the Authority to a request header and add this
     to a request using "withAuthority"
  7) send the amended request

Note that by default requests are annotated with authority headers
before the first sending, based upon previously generated Authority
objects (which contain domain information).  Once a specific authority
is added to a rejected request this predictive annotation is suppressed.

407 responses are handled in a similar manner, except
   a) Authorities are not collected, only a single proxy authority
      is kept by the browser
   b) If the proxy used by the browser (type Proxy) is NoProxy, then
      a 407 response will generate output on the "err" stream and
      the response will be returned.


Notes:
 - digest authentication so far ignores qop, so fails to authenticate 
   properly with qop=auth-int challenges
 - calculates a1 more than necessary
 - doesn't reverse authenticate
 - doesn't properly receive AuthenticationInfo headers, so fails
   to use next-nonce etc

-}


data Algorithm = AlgMD5 | AlgMD5sess
    deriving(Eq)

instance Show Algorithm where
    show AlgMD5 = "md5"
    show AlgMD5sess = "md5-sess"


data Qop = QopAuth | QopAuthInt
    deriving(Eq,Show)


data Challenge = ChalBasic  { chRealm   :: String }
               | ChalDigest { chRealm   :: String
                            , chDomain  :: [URI]
                            , chNonce   :: String
                            , chOpaque  :: Maybe String
                            , chStale   :: Bool
                            , chAlgorithm ::Maybe Algorithm
                            , chQop     :: [Qop]
                            }

-- | Convert WWW-Authenticate header into a Challenge object
headerToChallenge :: URI -> Header -> Maybe Challenge
headerToChallenge baseURI (Header _ str) =
    case parse challenge "" str of
        Left e -> Nothing
        Right (name,props) -> case name of
            "basic"  -> mkBasic props
            "digest" -> mkDigest props
            _        -> Nothing
    where
        challenge :: Parser (String,[(String,String)])
        challenge =
            do { nme <- word
               ; pps <- cprops
               ; return (map toLower nme,pps)
               }

        cprops = sepBy1 cprop comma

        comma = do { spaces ; char ',' ; spaces }

        cprop =
            do { nm <- word
               ; char '='
               ; val <- quotedstring
               ; return (map toLower nm,val)
               }

        quotedstring =
            do { char '"'
               ; str <- many (satisfy (not.(=='"')))
               ; char '"'
               ; return str
               }        
        
        mkBasic, mkDigest :: [(String,String)] -> Maybe Challenge

        mkBasic params = fmap ChalBasic (lookup "realm" params)

        mkDigest params =
            -- with Maybe monad
            do { r <- lookup "realm" params
               ; n <- lookup "nonce" params
               ; return $ 
                    ChalDigest { chRealm  = r
                               , chDomain = (annotateURIs 
                                            $ map parseURI
                                            $ words 
                                            $ fromMaybe [] 
                                            $ lookup "domain" params)
                               , chNonce  = n
                               , chOpaque = lookup "opaque" params
                               , chStale  = "true" == (map toLower
                                           $ fromMaybe "" (lookup "stale" params))
                               , chAlgorithm= readAlgorithm (fromMaybe "MD5" $ lookup "algorithm" params)
                               , chQop    = readQop (fromMaybe "" $ lookup "qop" params)
                               }
               }

        annotateURIs :: [Maybe URI] -> [URI]
        annotateURIs = (map (\u -> fromMaybe u (u `relativeTo` baseURI))) . catMaybes

        -- Change These:
        readQop :: String -> [Qop]
        readQop = catMaybes . (map strToQop) . (splitMany ',')

        strToQop str = case map toLower (trim str) of
            "auth"     -> Just QopAuth
            "auth-int" -> Just QopAuthInt
            _          -> Nothing

        readAlgorithm str = case map toLower (trim str) of
            "md5"      -> Just AlgMD5
            "md5-sess" -> Just AlgMD5sess
            _          -> Nothing


data Authority = AuthBasic { auRealm    :: String
                           , auUsername :: String
                           , auPassword :: String
                           , auSite     :: URI
                           }
               | AuthDigest { auRealm     :: String
                            , auUsername  :: String
                            , auPassword  :: String
                            , auNonce     :: String
                            , auAlgorithm :: Maybe Algorithm
                            , auDomain    :: [URI]
                            , auOpaque    :: Maybe String
                            , auQop       :: [Qop]
                            }


-- | Return authorities for a given domain and path.
-- Assumes "dom" is lower case
getAuthFor :: String -> String -> BrowserAction [Authority]
getAuthFor dom pth =
    do { list <- getAuthorities
       ; return (filter match list)
       }
    where
        match :: Authority -> Bool
        match (AuthBasic _ _ _ s) = matchURI s
        match (AuthDigest _ _ _ _ _ ds _ _) = or (map matchURI ds)            

        matchURI :: URI -> Bool
        matchURI s = (authority s == dom) && (path s `isPrefixOf` pth)
    

-- | Interacting with browser state:
getAuthorities :: BrowserAction [Authority]
getAuthorities = getBS bsAuthorities

setAuthorities :: [Authority] -> BrowserAction ()
setAuthorities as = alterBS (\b -> b { bsAuthorities=as })

addAuthority :: Authority -> BrowserAction ()
addAuthority a = alterBS (\b -> b { bsAuthorities=a:bsAuthorities b })

getAuthorityGen :: BrowserAction (URI -> String -> IO (Maybe (String,String)))
getAuthorityGen = getBS bsAuthorityGen

setAuthorityGen :: (URI -> String -> IO (Maybe (String,String))) -> BrowserAction ()
setAuthorityGen f = alterBS (\b -> b { bsAuthorityGen=f })

setAllowBasicAuth :: Bool -> BrowserAction ()
setAllowBasicAuth ba = alterBS (\b -> b { bsAllowBasicAuth=ba })




-- TO BE CHANGED!!!
pickChallenge :: [Challenge] -> Maybe Challenge
pickChallenge = listToMaybe



-- | Retrieve a likely looking authority for a Request.
anticipateChallenge :: Request -> BrowserAction (Maybe Authority)
anticipateChallenge rq =
    let uri = rqURI rq in
    do { authlist <- getAuthFor (authority uri) (path uri)
       ; return (listToMaybe authlist)
       }


-- | Asking the user to respond to a challenge
challengeToAuthority :: URI -> Challenge -> BrowserAction (Maybe Authority)
challengeToAuthority uri ch =
    -- prompt user for authority
    if answerable ch then
        do { prompt <- getAuthorityGen
           ; userdetails <- ioAction $ prompt uri (chRealm ch)
           ; case userdetails of
               Nothing    -> return Nothing
               Just (u,p) -> return (Just $ buildAuth ch u p)
           }
    else return Nothing
    where
        answerable :: Challenge -> Bool
        answerable (ChalBasic _) = True
        answerable ch            = (chAlgorithm ch) == Just AlgMD5

        buildAuth :: Challenge -> String -> String -> Authority
        buildAuth (ChalBasic r) u p = 
            AuthBasic { auSite=uri
                      , auRealm=r
                      , auUsername=u
                      , auPassword=p
                      }

         -- note to self: this is a pretty stupid operation
         -- to perform isn't it? ChalX and AuthX are so very
         -- similar.
        buildAuth (ChalDigest r d n o s a q) u p =
            AuthDigest { auRealm=r
                       , auUsername=u
                       , auPassword=p
                       , auDomain=d
                       , auNonce=n
                       , auOpaque=o
                       , auAlgorithm=a
                       , auQop=q
                       }


-- | Generating a credentials value from an Authority, in
-- the context of a specific request.  If a client nonce
-- was to be used then this function might need to
-- be of type ... -> BrowserAction String
withAuthority :: Authority -> Request -> String
withAuthority a rq = case a of
        AuthBasic _ _ user pass ->
	    "basic " ++ base64encode (auUsername a ++ ':' : auPassword a)
        AuthDigest _ _ _ _ _ _ _ _ ->
            "digest username=\"" ++ auUsername a 
              ++ "\",realm=\"" ++ auRealm a
              ++ "\",nonce=\"" ++ auNonce a
              ++ "\",uri=\"" ++ digesturi
              ++ ",response=\"" ++ rspdigest 
              ++ "\""
              -- plus optional stuff:
              ++ ( if isJust (auAlgorithm a) then "" else ",algorithm=\"" ++ show (fromJust $ auAlgorithm a) ++ "\"" )
              ++ ( if isJust (auOpaque a) then "" else ",opaque=\"" ++ (fromJust $ auOpaque a) ++ "\"" )
              ++ ( if null (auQop a) then "" else ",qop=auth" )
    where
        rspdigest = "\"" 
                 ++ map toLower (kd (md5 a1) (noncevalue ++ ":" ++ md5 a2))
                 ++ "\""

        -- FIXME: these probably only work right for latin-1 strings
	stringToOctets :: String -> [Octet]
	stringToOctets = map (fromIntegral . fromEnum)
	octetsToString :: [Octet] -> String
	octetsToString = map (toEnum . fromIntegral)
	base64encode :: String -> String
	base64encode = Base64.encode . stringToOctets
	md5 :: String -> String
	md5 = octetsToString . MD5.hash . stringToOctets

        kd :: String -> String -> String
        kd a b = md5 (a ++ ":" ++ b)

        a1, a2 :: String
        a1 = auUsername a ++ ":" ++ auRealm a ++ ":" ++ auPassword a
        
        {-
        If the "qop" directive's value is "auth" or is unspecified, then A2
        is:
           A2  = Method ":" digest-uri-value
        If the "qop" value is "auth-int", then A2 is:
           A2  = Method ":" digest-uri-value ":" H(entity-body)
        -}
        a2 = show (rqMethod rq) ++ ":" ++ digesturi

        digesturi = show (rqURI rq)
        noncevalue = auNonce a


------------------------------------------------------------------
------------------ Proxy Stuff -----------------------------------
------------------------------------------------------------------

data Proxy = NoProxy
           | Proxy String (Maybe Authority)


------------------------------------------------------------------
------------------ Browser State Actions -------------------------
------------------------------------------------------------------


data BrowserState = BS { bsErr, bsOut     :: String -> IO ()
                       , bsCookies        :: [Cookie]
                       , bsCookieFilter   :: URI -> Cookie -> IO Bool
                       , bsAuthorityGen   :: URI -> String -> IO (Maybe (String,String))
                       , bsAuthorities    :: [Authority]
                       , bsAllowRedirects :: Bool
                       , bsAllowBasicAuth :: Bool
                       , bsConnectionPool :: [Connection]
                       , bsProxy          :: Proxy
                       , bsDebug          :: Maybe String
                       }

instance Show BrowserState where
    show bs =  "BrowserState { " 
            ++ show (bsCookies bs)  ++ "\n"
           {- ++ show (bsAuthorities bs) ++ "\n"-}
            ++ "AllowRedirects: " ++ show (bsAllowRedirects bs)
            ++ "} "


-- Simple DIY stateful behaviour, with IO
data BrowserAction a = BA { lift :: (BrowserState -> IO (BrowserState,a)) }

instance Monad BrowserAction where
    a >>= f  =  BA (\b -> do { (nb,v) <- lift a b ; lift (f v) nb})
    return x =  BA (\b -> return (b,x))

instance Functor BrowserAction where
    fmap f   = liftM f


-- | Apply a browser action to a state.
browse :: BrowserAction a -> IO a
browse act = do x <- lift act defaultBrowserState
                return (snd x)
    where
        defaultBrowserState :: BrowserState
        defaultBrowserState = 
            BS { bsErr              = putStrLn
               , bsOut              = putStrLn
               , bsCookies          = []
               , bsCookieFilter     = defaultCookieFilter
               , bsAuthorityGen     = (error "bsAuthGen wanted")
               , bsAuthorities      = []
               , bsAllowRedirects   = True
               , bsAllowBasicAuth   = False
               , bsConnectionPool   = []
               , bsProxy            = NoProxy
               , bsDebug            = Nothing 
               }

-- | Alter browser state
alterBS :: (BrowserState -> BrowserState) -> BrowserAction ()
alterBS f = BA (\b -> return (f b,()))

getBS :: (BrowserState -> a) -> BrowserAction a
getBS f = BA (\b -> return (b,f b))

-- | Do an io action
ioAction :: IO a -> BrowserAction a
ioAction a = BA (\b -> a >>= \v -> return (b,v))


-- Stream handlers
setErrHandler, setOutHandler :: (String -> IO ()) -> BrowserAction ()
setErrHandler h = alterBS (\b -> b { bsErr=h })
setOutHandler h = alterBS (\b -> b { bsOut=h })

out, err :: String -> BrowserAction ()
out s = do { f <- getBS bsOut ; ioAction $ f s }
err s = do { f <- getBS bsErr ; ioAction $ f s }

-- Redirects
setAllowRedirects :: Bool -> BrowserAction ()
setAllowRedirects bl = alterBS (\b -> b {bsAllowRedirects=bl})

getAllowRedirects :: BrowserAction Bool
getAllowRedirects = getBS bsAllowRedirects


-- Proxy
setProxy :: Proxy -> BrowserAction ()
setProxy p = alterBS (\b -> b {bsProxy = p})

getProxy :: BrowserAction Proxy
getProxy = getBS bsProxy


-- Debug
setDebugLog :: Maybe String -> BrowserAction ()
setDebugLog v = alterBS (\b -> b {bsDebug=v})


-- Page control
type RequestState = ( Int    -- number of 401 responses so far
                    , Int    -- number of redirects so far
                    , Int    -- number of retrys so far
                    , Bool   -- whether to pre-empt 401 response
                    )



-- Surely the most important bit:
request = request' initialState
    where
        initialState = (0,0,0,True)


request' :: RequestState -> Request -> BrowserAction (URI,Response)
request' (denycount,redirectcount,retrycount,preempt) rq =
    do -- add cookies to request
       let uri = rqURI rq
       cookies <- getCookiesFor (authority uri) (path uri)
       
       when (not $ null cookies) 
            (out $ "Adding cookies to request.  Cookie names: " 
                 ++ foldl spaceappend "" (map ckName cookies))
       
       -- add credentials to request
       rq' <- if not preempt then return rq else
              do { auth <- anticipateChallenge rq
                 ; case auth of
                     Just x  -> return (insertHeader HdrAuthorization (withAuthority x rq) rq)
                     Nothing -> return rq
                 }
               
       let rq'' = insertHeaders (map cookieToHeader cookies) rq'

       p <- getProxy

       out ("Sending:\n" ++ show rq'') 
       e_rsp <- case p of
            NoProxy -> dorequest (uriAuth $ rqURI rq'') rq''
            Proxy str ath ->
                let rq''' = case ath of 
                                Nothing -> rq''
                                Just x  -> insertHeader HdrProxyAuthorization (withAuthority x rq'') rq''
                in dorequest (URIAuth "" str "") rq'''

       case e_rsp of
           Left v -> if (retrycount < 4) && (v == ErrorReset || v == ErrorClosed)
               then request' (denycount,redirectcount,retrycount+1,preempt) rq
               else error ("Exception raised in request: " ++ show v)
           Right rsp -> do 
               out ("Received:\n" ++ show rsp)

               -- add new cookies to browser state
               let cookieheaders = retrieveHeaders HdrSetCookie rsp
               let newcookies = concat (map (headerToCookies $ authority uri) cookieheaders)

               when (not $ null newcookies)
                    (out $ foldl (\x y -> x ++ "\n  " ++ show y) "Cookies received:" newcookies)
               
               filterfn <- getCookieFilter
               newcookies' <- ioAction (filterM (filterfn uri) newcookies)
               foldM (\_ -> addCookie) () newcookies'

               when (not $ null newcookies)
                    (out $ "Accepting cookies with names: " ++ foldl spaceappend "" (map ckName newcookies'))
       
               case rspCode rsp of
                   (4,0,1) ->  -- Credentials not sent or refused.
                       out "401 - credentials not sent or refused" >>
                       if denycount > 2 then return (uri,rsp) else
                       do { let hdrs = retrieveHeaders HdrWWWAuthenticate rsp
                          ; case pickChallenge (catMaybes $ map (headerToChallenge uri) hdrs) of
                                Just x  ->
                                    do { au <- challengeToAuthority uri x
                                       ; case au of
                                            Just au' ->
                                                out "Retrying request with new credentials" >>
                                                request' (denycount+1,redirectcount,retrycount,False)
                                                         (insertHeader HdrAuthorization (withAuthority au' rq) rq)
                                            Nothing  -> return (uri,rsp)   {- do nothing -}
                                       }
                                          
                                Nothing -> return (uri,rsp)   {- do nothing -}
                          }
                   

                   (4,0,7) ->  -- Proxy Authentication required
                       out "407 - proxy authentication required" >>
                       if denycount > 2 then return (uri,rsp) else
                       do { let hdrs = retrieveHeaders HdrProxyAuthenticate rsp
                          ; case pickChallenge (catMaybes $ map (headerToChallenge uri) hdrs) of
                                Just x  ->
                                    do { au <- challengeToAuthority uri x
                                       ; case au of
                                            Just au' ->
                                                do { pxy <- getBS bsProxy
                                                   ; case pxy of
                                                        NoProxy ->
                                                            do { err "Proxy authentication required without proxy!"
                                                               ; return (uri,rsp)
                                                               }
                                                        Proxy x y ->
                                                            do { out "Retrying with proxy authentication"
                                                               ; setProxy (Proxy x $ Just au')
                                                               ; request' (denycount+1,redirectcount,retrycount,False) rq
                                                               }
                                                   }                                                      
                                            Nothing  -> return (uri,rsp)   {- do nothing -}
                                       }
                                          
                                Nothing -> return (uri,rsp)   {- do nothing -}
                          }


                   (3,0,3) ->  -- Redirect using GET request method.
                       do { out "303 - redirect using GET"
                          ; rd <- getAllowRedirects
                          ; if not rd || redirectcount > 4 then return (uri,rsp) else
                            case retrieveHeaders HdrLocation rsp of
                                (Header _ u:_) -> case parseURIReference u of
                                    Just newuri ->
                                        let newuri' = case newuri `relativeTo` uri of
                                                        Nothing -> newuri
                                                        Just x  -> x
                                        in do { out ("Redirecting to " ++ show newuri' ++ " ...") 
                                              ; let rq = rq { rqMethod=GET, rqURI=newuri', rqBody="" }
                                              ; request' (0,redirectcount+1,retrycount,True)
                                                         (replaceHeader HdrContentLength "0" rq) 
                                              }
                                    Nothing ->
                                        do { err ("Parse of Location header in a redirect response failed: " ++ u)
                                           ; return (uri,rsp)
                                           }
                                [] -> do { err "No Location header in redirect response"
                                         ; return (uri,rsp)
                                         }
                          }
                        
                   (3,0,5) ->
                        case retrieveHeaders HdrLocation rsp of
                            (Header _ u:_) -> case parseURIReference u of
                                Just newuri ->
                                    do { out ("Retrying with proxy " ++ show newuri ++ "...")
                                       ; setProxy (Proxy (authority newuri) Nothing)
                                       ; request' (0,0,retrycount+1,True) rq
                                       }
                                Nothing ->
                                    do { err ("Parse of Location header in a proxy redirect response failed: " ++ u)
                                       ; return (uri,rsp)
                                       }
                            [] -> do { err "No Location header in proxy redirect response."
                                     ; return (uri,rsp)
                                     }
                   

                   (3,_,_) ->  redirect uri rsp
                   _       -> return (uri,rsp)

    where      
        spaceappend :: String -> String -> String
        spaceappend x y = x ++ ' ' : y

        redirect uri rsp = do
            rd <- getAllowRedirects
            if not rd || redirectcount > 4 then return (uri,rsp) else do
            case retrieveHeaders HdrLocation rsp of
              (Header _ u:_) -> case parseURIReference u of
                                  Just newuri -> do
                                      let newuri' = case newuri `relativeTo` uri of
                                                      Nothing -> newuri
                                                      Just x  -> x
                                      out ("Redirecting to " ++ show newuri' ++ " ...") 
                                      request' (0,redirectcount+1,retrycount,True) (rq { rqURI=newuri' }) 
                                  Nothing -> do
                                      err ("Parse of Location header in a redirect response failed: " ++ u)
                                      return (uri,rsp)
              [] -> do err "No Location header in redirect response."
                       return (uri,rsp)


        dorequest :: URIAuth -> Request -> BrowserAction (Either ConnError Response)
        dorequest hst rqst = 
            do { pool <- getBS bsConnectionPool
               ; conn <- ioAction $ filterM (\c -> c `isConnectedTo` uriAuthToString hst) pool
               ; rsp <- case conn of
                    [] -> do { out ("Creating new connection to " ++ uriAuthToString hst)
                             ; let port = case uriPort hst of
                                            (':':s) -> read s
                                            _       -> 80
                             ; c <- ioAction $ openTCPPort (uriRegName hst) port
                             ; let pool' = if length pool > 5
                                           then init pool
                                           else pool
                             ; when (length pool > 5)
                                    (ioAction $ close (last pool))
                             ; alterBS (\b -> b { bsConnectionPool=c:pool' })
                             ; dorequest2 hst c rqst
                             }
                    (c:_) ->
                        do { out ("Recovering connection to " ++ uriAuthToString hst)
                           ; dorequest2 hst c rqst
                           }
               ; 
               ; return rsp
               }

        dorequest2 hst c r =
            do { dbg <- getBS bsDebug
               ; ioAction $ case dbg of
                 Nothing -> sendHTTP c r
                 Just f  ->
                    debugStream (f++'-': uriAuthToString hst) c 
                    >>= \c' -> sendHTTP c' r  
               }
 


uriAuth x = case uriAuthority x of
              Just ua -> ua
              _       -> error ("No uri authority for: "++show x)

uriAuthToString :: URIAuth -> String
uriAuthToString URIAuth { uriUserInfo = uinfo
                        , uriRegName  = regname
                        , uriPort     = port
                        } =
                    ((if null uinfo then id else (uinfo++))
                     . (regname++)
                     . (port++)) ""



------------------------------------------------------------------
------------------ Request Building ------------------------------
------------------------------------------------------------------


libUA = "haskell-libwww/0.1"

defaultGETRequest uri = 
    Request { rqURI=uri
            , rqBody=""
            , rqHeaders=[ Header HdrContentLength "0"
                        , Header HdrUserAgent libUA
                        ]
            , rqMethod=GET
            }



-- This form junk is completely untested...

type FormVar = (String,String)

data Form = Form RequestMethod URI [FormVar]


formToRequest :: Form -> Request
formToRequest (Form m u vs) =
    let enc = urlEncodeVars vs
    in case m of
        GET -> Request { rqMethod=GET
                       , rqHeaders=[ Header HdrContentLength "0" ]
                       , rqBody=""
                       , rqURI=u { uriQuery= '?' : enc }  -- What about old query?
                       }
        POST -> Request { rqMethod=POST
                        , rqHeaders=[ Header HdrContentLength (show $ length enc) ]
                        , rqBody=enc
                        , rqURI=u
                        }