This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uicefox.py
230 lines (196 loc) · 5.37 KB
/
uicefox.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
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
import uasyncio as asyncio
import uasyncio.core as uasynccore
from uasyncio.stream import Stream
import usocket as socket
async def open_connection(host, port, ssl=False):
from uerrno import EINPROGRESS, ENOTCONN
ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] # TODO this is blocking!
s = socket.socket(ai[0], ai[1], ai[2])
s.setblocking(False)
try:
s.connect(ai[-1])
except OSError as er:
if er.errno != EINPROGRESS:
raise er
if ssl:
import ussl
while True:
try:
s = ussl.wrap_socket(s)
break
except OSError as er:
if er.errno != ENOTCONN:
raise er
await asyncio.sleep_ms(20)
ss = Stream(s)
yield uasynccore._io_queue.queue_write(s)
return ss, ss
class BaseResponse:
status = -1
headers = None
def __init__(self, reader):
self._reader = reader
class Response(BaseResponse):
content_length = None
async def read(self, size=-1):
# must limit or ECONNRESET
if size != -1:
size = min(self.content_length, size)
else:
size = self.content_length
self.content_length -= size
return await self._reader.read(size)
async def readline(self):
return await self._reader.readline()
async def readinto(self, buf):
await self._reader.readinto(buf)
async def close(self):
await self._reader.wait_closed()
# await self._reader.close()
def __repr__(self):
return "<%s %d %s>" % (self.__name__, self.status, self.headers)
class ChunkedResponse(BaseResponse):
chunk_size = 0
async def read(self, size=-1):
if self.chunk_size == 0:
l = await self._reader.readline()
l = l.split(b';', 1)[0]
self.chunk_size = int(l, 16)
if self.chunk_size == 0:
# EOF
sep = await self._reader.read(2)
assert sep == b'\r\n'
return b''
if size != -1:
data = await self._reader.read(min(size, self.chunk_size))
else:
data = await self._reader.read(self.chunk_size)
self.chunk_size -= len(data)
if self.chunk_size == 0:
sep = await self._reader.read(2)
assert sep == b'\r\n'
return data
async def readline(self):
raise NotImplementedError()
async def readinto(self, buf):
raise NotImplementedError()
class ChunkedWriter:
def __init__(self, writer):
self._writer = writer
async def write(self, buf, offset=0, size=-1):
if offset != 0 or size != -1:
buf = memoryview(buf)
if size != -1:
size = len(buf)
buf = buf[offset : offset + size]
chunk_size = len(buf)
assert chunk_size > 0
self._writer.write(b'%x' % chunk_size)
self._writer.write(b'\r\n')
self._writer.write(buf)
await self._writer.drain()
async def close(self):
self._writer.write(b'0\r\n\r\n')
await self._writer.drain()
self._writer = None
async def request_raw(method, url, data=None, json=None, headers=None, ua=None, chunked=False):
try:
proto, _, host, path = url.split("/", 3)
except ValueError:
proto, _, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
elif proto == "https:":
port = 443
else:
assert False
if ':' in host:
host, port = host.split(":", 1)
port = int(port)
# NOTE: in uasyncio, reader and writer are the same
rd, wrt = await open_connection(host, port, proto != "http:")
query = "%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: */*\r\nConnection: close\r\n\r\n" % (
method,path,host,
ua if ua else "Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0"
)
buf = memoryview(query)
wrt.write(buf)
await wrt.drain()
if headers:
for k in headers:
wrt.write(k)
wrt.write(b": ")
wrt.write(headers[k])
wrt.write(b"\r\n")
await s.drain()
if json:
assert data is None
import ujson
wrt.write(b"Content-Type: application/json\r\n")
data = ujson.dumps(json).encode('utf-8')
elif chunked:
assert data is None
wrt.write(b"Transfer-Encoding: chunked")
if data:
wrt.write(b"Content-Length: %d\r\n" % len(data))
wrt.write(b"\r\n")
await wrt.drain()
# TODO: handle chunked upload
if data:
wrt.write(data)
await wrt.drain()
if chunked:
return ChunkedWriter(writer), rd
return None, rd
async def request(method, url, *args, redir_limit=2, **kwargs):
redir_cnt = 0
redir_url = None
# process headers and redirs
while True:
_, reader = await request_raw(method,url,*args,**kwargs)
headers = []
sline = await reader.readline()
status = int(sline.split(None,2)[1])
chunked = False
while True:
line = await reader.readline()
if not line or line == b"\r\n": #EOF
break
headers.append(line)
if line.startswith(b"Transfer-Encoding:"):
if b"chunked" in line:
chunked = True
elif line.startswith(b"Location:"):
url = line.rstrip().split(None,1)[1].decode('utf-8')
elif line.startswith(b'Content-Length:'):
content_length = int(
line.rstrip().split(None,1)[1].decode('utf-8')
)
if 301 <= status <= 303:
redir_cnt += 1
if redir_cnt > redir_limit:
break # need to preserve last reader
await reader.wait_closed()
continue
break
if chunked:
resp = ChunkedResponse(reader)
else:
resp = Response(reader)
resp.status = status
resp.headers = headers
resp.content_length = content_length
return resp
def head(url, **kw):
return request("HEAD", url, **kw)
def get(url, **kw):
return request("GET", url, **kw)
def post(url, **kw):
return request("POST", url, **kw)
def put(url, **kw):
return request("PUT", url, **kw)
def patch(url, **kw):
return request("PATCH", url, **kw)
def delete(url, **kw):
return request("DELETE", url, **kw)