2018-09-27 20:22:35 +03:00
|
|
|
import asyncio
|
2018-07-08 18:45:49 +03:00
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
from .connection import Connection, PacketCodec
|
2018-07-08 18:45:49 +03:00
|
|
|
|
|
|
|
|
2018-10-04 18:11:31 +03:00
|
|
|
SSL_PORT = 443
|
2018-09-27 20:22:35 +03:00
|
|
|
|
2018-10-04 18:11:31 +03:00
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
class HttpPacketCodec(PacketCodec):
|
|
|
|
tag = None
|
|
|
|
obfuscate_tag = None
|
2018-09-27 20:22:35 +03:00
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
def encode_packet(self, data):
|
|
|
|
return ('POST /api HTTP/1.1\r\n'
|
|
|
|
'Host: {}:{}\r\n'
|
|
|
|
'Content-Type: application/x-www-form-urlencoded\r\n'
|
|
|
|
'Connection: keep-alive\r\n'
|
|
|
|
'Keep-Alive: timeout=100000, max=10000000\r\n'
|
|
|
|
'Content-Length: {}\r\n\r\n'
|
2019-05-03 14:59:17 +03:00
|
|
|
.format(self._conn._ip, self._conn._port, len(data))
|
2019-03-12 03:12:55 +03:00
|
|
|
.encode('ascii') + data)
|
|
|
|
|
|
|
|
async def read_packet(self, reader):
|
2018-09-27 20:22:35 +03:00
|
|
|
while True:
|
2019-03-12 03:12:55 +03:00
|
|
|
line = await reader.readline()
|
2018-09-27 20:22:35 +03:00
|
|
|
if not line or line[-1] != b'\n':
|
|
|
|
raise asyncio.IncompleteReadError(line, None)
|
|
|
|
|
|
|
|
if line.lower().startswith(b'content-length: '):
|
2019-03-12 03:12:55 +03:00
|
|
|
await reader.readexactly(2)
|
2018-09-27 20:22:35 +03:00
|
|
|
length = int(line[16:-2])
|
2019-03-12 03:12:55 +03:00
|
|
|
return await reader.readexactly(length)
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionHttp(Connection):
|
|
|
|
packet_codec = HttpPacketCodec
|
|
|
|
|
|
|
|
async def connect(self, timeout=None, ssl=None):
|
|
|
|
await super().connect(timeout=timeout, ssl=self._port == SSL_PORT)
|