forked from tornadoweb/tornado
/
httpclient.py
168 lines (136 loc) · 5.13 KB
/
httpclient.py
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
# coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
import types
from cyclone import escape
from cyclone.web import HTTPError
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from twisted.web.iweb import IBodyProducer
from zope.interface import implements
agent = Agent(reactor)
class StringProducer(object):
implements(IBodyProducer)
def __init__(self, body):
self.body = body
self.length = len(body)
def startProducing(self, consumer):
consumer.write(self.body)
return defer.succeed(None)
def pauseProducing(self):
pass
def stopProducing(self):
pass
class Receiver(Protocol):
def __init__(self, finished):
self.finished = finished
self.data = []
def dataReceived(self, bytes):
self.data.append(bytes)
def connectionLost(self, reason):
self.finished.callback("".join(self.data))
class HTTPClient(object):
def __init__(self, url, *args, **kwargs):
self._args = args
self._kwargs = kwargs
self.url = url
self.followRedirect = self._kwargs.get("followRedirect", 0)
self.maxRedirects = self._kwargs.get("maxRedirects", 3)
self.headers = self._kwargs.get("headers", {})
self.body = self._kwargs.get("postdata")
self.method = self._kwargs.get("method", self.body and "POST" or "GET")
agent._connectTimeout = self._kwargs.get("timeout", None)
if self.method.upper() == "POST" and \
"Content-Type" not in self.headers:
self.headers["Content-Type"] = \
["application/x-www-form-urlencoded"]
self.response = None
if self.body:
self.body_producer = StringProducer(self.body)
else:
self.body_producer = None
@defer.inlineCallbacks
def fetch(self):
request_headers = Headers(self.headers)
response = yield agent.request(
self.method,
self.url,
request_headers,
self.body_producer)
mr = self.maxRedirects
while mr >= 1:
if response.code in (301, 302, 303) and self.followRedirect:
mr -= 1
headers = dict(response.headers.getAllRawHeaders())
location = headers.get("Location")
if location:
if isinstance(location, types.ListType):
location = location[0]
#print("redirecting to:", location)
response = yield agent.request(
"GET", # self.method,
location,
request_headers,
self.body_producer)
else:
break
else:
break
response.error = None
response.headers = dict(response.headers.getAllRawHeaders())
# HTTP 204 and 304 responses have no body
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
if response.code in (204, 304):
response.body = ''
else:
d = defer.Deferred()
response.deliverBody(Receiver(d))
response.body = yield d
response.request = self
defer.returnValue(response)
def fetch(url, *args, **kwargs):
return HTTPClient(escape.utf8(url), *args, **kwargs).fetch()
class JsonRPC:
def __init__(self, url):
self.__rpcId = 0
self.__rpcUrl = url
def __getattr__(self, attr):
return functools.partial(self.__rpcRequest, attr)
def __rpcRequest(self, method, *args):
q = escape.json_encode({"method": method, "params": args,
"id": self.__rpcId})
self.__rpcId += 1
r = defer.Deferred()
d = fetch(self.__rpcUrl, method="POST", postdata=q)
def _success(response, deferred):
if response.code == 200:
data = escape.json_decode(response.body)
error = data.get("error")
if error:
deferred.errback(Exception(error))
else:
deferred.callback(data.get("result"))
else:
deferred.errback(HTTPError(response.code, response.phrase))
def _failure(failure, deferred):
deferred.errback(failure)
d.addCallback(_success, r)
d.addErrback(_failure, r)
return r