summaryrefslogtreecommitdiff
path: root/src/Web/VKHS/Client.hs
blob: 6603eb4e80f90a73b1b84e254cb9f469e23b32a9 (plain)
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
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Web.VKHS.Client where

import Data.List
import Data.Maybe
import Data.Time
import Data.Either
import Data.Monoid((<>))
import Control.Applicative
import Control.Arrow ((&&&),(***))
import Control.Monad
import Control.Monad.State
import Control.Monad.Cont
import Data.Default.Class
import qualified Data.CaseInsensitive as CI
import Data.Map (Map)
import qualified Data.Map as Map
import Data.List.Split
import Data.Text(Text)
import qualified Data.Text as Text
import Control.Concurrent (threadDelay)
import System.IO as IO
import System.IO.Unsafe as IO
import System.Clock as Clock

import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS

import Network.HTTP.Client ()
import Network.HTTP.Client.Internal (setUri)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import qualified Network.HTTP.Types as Client
import qualified Network.HTTP.Client as Client
import qualified Network.HTTP.Client.Internal as Client
import qualified Network.URI as Client
import qualified Network.Shpider.Forms as Shpider

import Pipes.Prelude as PP (foldM)
import qualified Pipes as Pipes (Producer(..), for, runEffect, (>->))
import qualified Pipes.HTTP as Pipes hiding (Request, Response)

import qualified Text.Parsec as Parsec

import Debug.Trace

import Web.VKHS.Types

data Error = ErrorParseURL { euri :: String, emsg :: String }
           | ErrorSetURL { eurl :: URL, emsg :: String }
  deriving(Show, Eq)

{-
 __  __                       _
|  \/  | ___  _ __   __ _  __| |
| |\/| |/ _ \| '_ \ / _` |/ _` |
| |  | | (_) | | | | (_| | (_| |
|_|  |_|\___/|_| |_|\__,_|\__,_|
-}

data ClientState = ClientState {
    cl_man :: Client.Manager
  , cl_last_execute :: TimeSpec
  , cl_minimum_interval_ns :: Integer
  , cl_verbose :: Bool
  }

defaultState :: GenericOptions -> IO ClientState
defaultState GenericOptions{..} = do
  cl_man <- Client.newManager
              (Client.managerSetProxy (Client.proxyEnvironment Nothing)
                (case o_use_https of
                  True -> tlsManagerSettings
                  False -> Client.defaultManagerSettings))
  cl_last_execute <- pure (TimeSpec 0 0)
  cl_minimum_interval_ns <- pure (round (10^9  / o_max_request_rate_per_sec))
  cl_verbose <- pure o_verbose
  return ClientState{..}

class ToClientState s where
  toClientState :: s -> ClientState
  modifyClientState :: (ClientState -> ClientState) -> (s -> s)

class (MonadIO m, MonadState s m, ToClientState s) => MonadClient m s | m -> s

-- newtype ClientT m a = ClientT { unClient :: StateT ClientState m a }
--   deriving(Functor, Applicative, Monad, MonadState ClientState, MonadIO)

-- runClient :: (MonadIO m) => ClientT m a -> m a
-- runClient l = do
--   cl_man <- liftIO $ newManager defaultManagerSettings
--   evalStateT (unClient l) ClientState{..}

-- liftClient :: (Monad m) => m a -> ClientT m a
-- liftClient = ClientT . lift

{-
 _   _ ____  _
| | | |  _ \| |
| | | | |_) | |
| |_| |  _ <| |___
 \___/|_| \_\_____|
-}

newtype URL_Protocol = URL_Protocol { urlproto :: String }
  deriving(Show)
newtype URL_Query = URL_Query { urlq :: String }
  deriving(Show)
newtype URL_Host = URL_Host { urlh :: String }
  deriving(Show)
newtype URL_Port = URL_Port { urlp :: String }
  deriving(Show)
newtype URL_Path = URL_Path { urlpath :: String }
  deriving(Show)
newtype URL = URL { uri :: Client.URI }
  deriving(Show, Eq)

-- * FIXME Pack Text to ByteStrings, not to String
buildQuery :: [(String,String)] -> URL_Query
buildQuery qis = URL_Query ("?" ++ intercalate "&" (map (\(a,b) -> (esc a) ++ "=" ++ (esc b)) qis)) where
  esc x = Client.escapeURIString Client.isAllowedInURI x

urlCreate :: URL_Protocol -> URL_Host -> Maybe URL_Port -> URL_Path -> URL_Query -> Either Error URL
urlCreate URL_Protocol{..} URL_Host{..} port  URL_Path{..} URL_Query{..} =
  pure $ URL $ Client.URI (urlproto ++ ":") (Just (Client.URIAuth "" urlh (maybe "" ((":"++).urlp) port))) urlpath urlq []

urlFromString :: String -> Either Error URL
urlFromString s =
  case Client.parseURI s of
    Nothing -> Left (ErrorParseURL s "Client.parseURI failed")
    Just u -> Right (URL u)

-- | * FIXME Convert to BS
splitFragments :: String -> String -> String -> [(String,String)]
splitFragments sep eqs =
    filter (\(a, b) -> not (null a))
  . map (f . splitOn eqs)
  . concat
  . map (splitOn sep)
  . lines
  where f []     = ("", "")
        f [x]    = (trim x, "")
        f (x:xs) = (trim x, trim $ intercalate eqs xs)

        trim = rev (dropWhile (`elem` (" \t\n\r" :: String)))
          where rev f = reverse . f . reverse . f

-- | * FIXME Convert to BS
urlFragments :: URL -> [(String,String)]
urlFragments URL{..} = splitFragments "&" "=" $  unsharp $ Client.uriFragment uri where
  unsharp ('#':x) = x
  unsharp y = y

{-
  ____            _    _
 / ___|___   ___ | | _(_) ___
| |   / _ \ / _ \| |/ / |/ _ \
| |__| (_) | (_) |   <| |  __/
 \____\___/ \___/|_|\_\_|\___|
-}

data Cookies = Cookies { jar :: Client.CookieJar }
  deriving(Show,Eq)

cookiesCreate :: () -> Cookies
cookiesCreate () = Cookies (Client.createCookieJar [])

{-
 _   _ _____ _____ ____
| | | |_   _|_   _|  _ \
| |_| | | |   | | | |_) |
|  _  | | |   | | |  __/
|_| |_| |_|   |_| |_|
-}

data Request = Request {
    req :: Client.Request
  , req_jar :: Client.CookieJar
  }

requestCreateGet :: (MonadClient m s) => URL -> Cookies -> m (Either Error Request)
requestCreateGet URL{..} Cookies{..} = do
  case setUri Client.defaultRequest uri of
    Left exc -> do
      return $ Left $ ErrorSetURL (URL uri) (show exc)

    Right r -> do
      now <- liftIO getCurrentTime
      (r',_) <- pure $ Client.insertCookiesIntoRequest r jar now
      return $ Right $ Request {
          req = r'{
              Client.redirectCount = 0
            },
          req_jar = jar
      }

requestCreatePost :: (MonadClient m s) => FilledForm -> Cookies -> m (Either Error Request)
requestCreatePost (FilledForm tit Shpider.Form{..}) c = do
  case Client.parseURI (Client.escapeURIString Client.isAllowedInURI action) of
    Nothing -> return (Left (ErrorParseURL action "parseURI failed"))
    Just action_uri -> do
      r <- requestCreateGet (URL action_uri) c
      case r of
        Left err -> do
          return $ Left err
        Right Request{..} -> do
          return $ Right $ Request (Client.urlEncodedBody (map (BS.pack *** BS.pack) $ Map.toList inputs) req) req_jar

requestUploadPhoto :: (MonadClient m s) => Text -> ByteString -> m (Either Error Request)
requestUploadPhoto text_url bs = do
  case Client.parseURI (Text.unpack text_url) of
    Nothing -> return (Left (ErrorParseURL (Text.unpack text_url) "parseURI failed"))
    Just uri -> do
      r <- requestCreateGet (URL uri) (cookiesCreate ())
      case r of
        Left err -> do
          return $ Left err
        Right Request{..} -> do
          return $ Right $ Request ((Client.urlEncodedBody [("photo", bs)]) req) req_jar

data Response = Response {
    resp :: Client.Response (Pipes.Producer ByteString IO ())
  , resp_body :: ByteString
  }

responseBodyS :: Response -> String
responseBodyS Response{..} = BS.unpack $ resp_body

responseBody :: Response -> ByteString
responseBody Response{..} = resp_body

dumpResponseBody :: (MonadClient m s) => FilePath -> Response -> m ()
dumpResponseBody f Response{..} = liftIO $ BS.writeFile f resp_body

responseCookies :: Response -> Cookies
responseCookies Response{..} = Cookies (Client.responseCookieJar resp)

responseHeaders :: Response -> [(String,String)]
responseHeaders Response{..} =
  map (\(o,h) -> (BS.unpack (CI.original o), BS.unpack h)) $ Client.responseHeaders resp

responseCode :: Response -> Int
responseCode Response{..} = Client.statusCode $ Client.responseStatus resp

responseCodeMessage :: Response -> String
responseCodeMessage Response{..} = BS.unpack $ Client.statusMessage $ Client.responseStatus resp

responseRedirect :: Response -> Maybe URL
responseRedirect r =
  case lookup "Location" (responseHeaders r) of
    Just loc -> URL <$> Client.parseURI loc
    Nothing -> Nothing

responseOK :: Response -> Bool
responseOK r = c == 200 where
  c = responseCode r

requestExecute :: (MonadClient m s) => Request -> m (Response, Cookies)
requestExecute Request{..} = do
  jar <- pure req_jar
  ClientState{..} <- toClientState <$> get
  clk <- liftIO $ do
    clk <- Clock.getTime Clock.Realtime
    let interval_ns = toNanoSecs (clk `diffTimeSpec` cl_last_execute)
    when (interval_ns < cl_minimum_interval_ns) $ do
      when cl_verbose $ do
        hPutStrLn stderr $ "Delaying execution to match the request threshold limit of " <> show cl_minimum_interval_ns <> " ns"
      threadDelay (fromInteger $ (cl_minimum_interval_ns - interval_ns) `div` 1000); -- convert ns to us
    return clk

  modify (modifyClientState (\s -> s{cl_last_execute = clk}))

  liftIO $ do
    Pipes.withHTTP req cl_man $ \resp -> do
      resp_body <- PP.foldM (\a b -> return $ BS.append a b) (return BS.empty) return (Client.responseBody resp)
      now <- getCurrentTime
      let (jar', resp') = Client.updateCookieJar resp req now jar
      return (Response resp resp_body, Cookies jar')

downloadFileWith :: (MonadClient m s) => URL -> (ByteString -> IO ()) -> m ()
downloadFileWith url h = do
  (ClientState{..}) <- toClientState <$> get
  (Right Request{..}) <- requestCreateGet url (cookiesCreate ())
  liftIO $ Pipes.withHTTP req cl_man $ \resp -> do
      PP.foldM (\() a -> h a) (return ()) (const (return ())) (Client.responseBody resp)