-log_in :: FilePath -> String -> String -> IO (Either String String)
-log_in cookie_jar username password =
- withCurlDo $ do
- -- Create a curl instance.
- curl <- initialize
-
- -- Perform the request, and get back a CurlResponse object.
- -- The cast is needed to specify how we would like our headers
- -- and body returned (Strings).
- resp <- do_curl_ curl login_url curl_opts :: IO CurlResponse
-
- -- Pull out the response code as a CurlCode.
- let code = respCurlCode resp
-
- return $
- case code of
- CurlOK -> Right (respBody resp)
- error_code -> Left $ "HTTP Error: " ++ (show error_code)
- -- If an error occurred, we want to dump as much information as
- -- possible. If this becomes a problem, we can use respGetInfo to
- -- query the response object for more information
+log_in :: String -> String -> IO (Either LBSResponse CookieJar)
+log_in username password = do
+ init_req <- parseUrl login_url
+ let req' = init_req { method = methodPost,
+ checkStatus = \_ _ -> Nothing,
+ redirectCount = 0 }
+ let req = urlEncodedBody post_data req'
+
+ resp <- withManager $ httpLbs req
+
+ -- The login page redirects. If we follow it, we lose our cookies.
+ if (responseStatus resp) == status302 then do
+ now <- getCurrentTime
+ let (cj,_) = updateCookieJar resp req now C.cj_empty
+ return $ Right cj
+ else do
+ return $ Left resp
+