Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
anonyxbiz committed Dec 23, 2024
1 parent 7c9483e commit 2847e27
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 175 deletions.
215 changes: 110 additions & 105 deletions Blazeio/Client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Blazeio.Client
from asyncio import Protocol, get_event_loop, sleep
from asyncio import Protocol, get_event_loop, new_event_loop, sleep
from time import perf_counter
from ssl import create_default_context, SSLError
from ujson import loads, dumps
from collections import deque, defaultdict
from ..Dependencies import Log

p = print

Expand All @@ -16,10 +15,37 @@ def __init__(app, message=None):
def __str__(app) -> str:
return app.message

loop = get_event_loop()

class Utl:
@classmethod
async def split_before(app, str_: (bytearray, bytes, str), to_find: (bytearray, bytes, str)):
await sleep(0)
if (idx := str_.find(to_find)) != -1:
return str_[:idx]
else:
return False

@classmethod
async def split_between(app, str_: (bytearray, bytes, str), to_find: (bytearray, bytes, str)):
await sleep(0)
if (idx := str_.find(to_find)) != -1:
return (str_[:idx], str_[idx + len(to_find):])
else:
return False

@classmethod
async def split_after(app, str_: (bytearray, bytes, str), to_find: (bytearray, bytes, str)):
await sleep(0)
if (idx := str_.find(to_find)) != -1:
return str_[idx + len(to_find):]
else:
return False

class BlazeioClientProtocol(Protocol):
def __init__(app, *args, **kwargs):
app.buffer = deque()
app.response_headers = defaultdict(str)
app.response_headers = False
app.__is_at_eof__ = False
app.__is_connection_lost__ = False
app.transport = None
Expand All @@ -28,9 +54,10 @@ def __init__(app, *args, **kwargs):

def connection_made(app, transport):
app.transport = transport
app.transport.pause_reading()

def data_received(app, data):
app.buffer.append(bytearray(data))
app.buffer.append(data)

def eof_received(app):
app.__is_at_eof__ = True
Expand All @@ -39,63 +66,64 @@ def connection_lost(app, exc=None):
app.__is_connection_lost__ = True

async def push(app, chunk):
if not isinstance(chunk, (bytes, bytearray)):
chunk = chunk.encode()

if not app.__is_connection_lost__:
app.transport.write(chunk)
else:
if app.__is_connection_lost__:
raise Err("Client has disconnected")

app.transport.write(chunk)

async def pull(app, timeout=30):
async def pull(app, timeout=5):
endl = b"\r\n0\r\n\r\n"
start_time = None

if app.response_headers:
app.expected_content_length = int(app.response_headers.get("Content-Length", "0"))

app.received_content_length = 0
else:
app.expected_content_length = False
app.received_content_length = 0
if (cl := app.response_headers.get("Content-Length")):
app.expected_cl = int(cl)
else:
app.expected_cl = app.response_headers.get("Transfer-Encoding")

app.received_cl = 0
else:
app.expected_cl = False
app.received_cl = 0

start = perf_counter()
while True:
await sleep(0)
if app.buffer:
app.transport.pause_reading()

buff = app.buffer.popleft()
yield buff

if endl in buff: break

if app.expected_content_length:
app.received_content_length += len(buff)
if app.received_content_length >= app.expected_content_length:
break
yield buff

if app.response_headers:
app.received_cl += len(buff)

elif endl in buff: break

start_time = perf_counter()
start = perf_counter()

else:
if start_time is not None:
if perf_counter() - float(start_time) >= timeout:
break

if app.expected_cl and app.expected_cl != "chunked":
if app.received_cl >= app.expected_cl: break
else:
if perf_counter() - start >= timeout: break

if app.__is_connection_lost__: break

if not app.transport.is_reading(): app.transport.resume_reading()


yield None

app.transport.close()
if not app.transport.is_reading(): app.transport.resume_reading()

#app.transport.close()

async def fetch_headers(app, sepr = b"\r\n\r\n", head_sepr = b"\r\n"):
tmp = bytearray()
app.response_headers
app.response_headers = defaultdict(str)

async for data in app.pull():
if data:
tmp.extend(data)
if (idx := tmp.rfind(sepr)) != -1:
app.buffer.appendleft(tmp[idx + len(sepr):])
rem = tmp[idx + len(sepr):]

app.buffer.appendleft(rem)

tmp = tmp[:idx]
break
Expand Down Expand Up @@ -124,22 +152,52 @@ async def fetch_headers(app, sepr = b"\r\n\r\n", head_sepr = b"\r\n"):
app.response_headers = dict(app.response_headers)
return app.response_headers

loop = get_event_loop()

class Session:
def __init__(app, **kwargs):
app.protocols = deque()
app.ssl_context = create_default_context()

async def fetch(app,
async def fetch(app, *args, **kwargs): return await app.prepare(*args, **kwargs)

async def url_to_host(
app,
url: str,
sepr: str = "://",
sepr2: str = ":",
sepr3: str = "/"
):
host = url

if await Utl.split_before(host, "https"):
port: int = 443
else:
port: int = 80

if (_ := await Utl.split_after(host, sepr)):
host = _
else:
raise Err("%s is not a valid host" % host)

if (_ := await Utl.split_between(host, sepr3)):
host = _[0]
path = sepr3 + _[1]

if (__ := await Utl.split_between(host, sepr2)):
port = int(__[1])
host = __[0]
else:
path = "/"

return host, port, path

async def prepare(app,
url: str,
method: str = "GET",
headers = None,
connect_only=False,
params=None,
body=None
connect_only = False,
):
host, port, path = await app.url_to_host(url)

if not headers:
_headers_ = {}
else:
Expand All @@ -150,71 +208,18 @@ async def fetch(app,
host=host, port=port, ssl=app.ssl_context if port == 443 else None
)

await protocol.push(f"{method} {path} HTTP/1.1\r\n")
await protocol.push(bytearray("%s %s HTTP/1.1\r\n" % (method, path), "utf-8"))

if not "Host" in _headers_:
_headers_["Host"] = host

for key, val in _headers_.items():
await protocol.push(f"{key}: {val}\r\n".encode())
await protocol.push(bytearray("%s: %s\r\n" % (key, val), "utf-8"))

await protocol.push("\r\n".encode())
await protocol.push(b"\r\n")

if connect_only: return protocol

# if method in ["GET", "HEAD", "OPTIONS"]:
await protocol.fetch_headers()

#if method in ("GET", "HEAD", "OPTIONS",):
if not connect_only:
await protocol.fetch_headers()
return protocol

async def url_to_host(app, url: str):
sepr = "://"
sepr2 = ":"
sepr3 = "/"
host = url
port = None

if "https" in host:
port = 443
else:
port = 80

if sepr in host:
host = host.split(sepr)[-1]

if sepr2 in host:
_ = host.split(sepr2)
host, port_ = _[0], _[1]

if sepr3 in port_:
port_ = port_.split(sepr3)

if 1:
try:
port = int(port_[0])
except Exception as e:
pass



if sepr3 in host:
host = host.split(sepr3)[0]

if sepr3 in url and len((_ := url.split(sepr3))) >= 3:
path = sepr3 + sepr3.join(_[3:])
else:
path = sepr3

return host, port, path

async def close(app):
return
try:
while app.protocols:
prot = app.protocols.popleft()
prot.transport.close()

except SSLError as e:
return
except Exception as e:
p("Exception: " + str(e))

26 changes: 22 additions & 4 deletions Blazeio/Client/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from Client import Session
from Blazeio.Client import Session
from Blazeio.Dependencies import Log

from time import perf_counter
from asyncio import run, sleep, gather, create_task
from asyncio import run, sleep, gather, create_task, new_event_loop, get_event_loop

class Test:
async def test(app, url="http://example.com"):
Expand Down Expand Up @@ -31,8 +33,24 @@ async def main(app):

p(f"Duration: {perf_counter() - start:.4f} seconds")



async def test():
client = Session()
i = await client.prepare("https://www.google.com/search")

await Log.debug(i.response_headers)

chunks = bytearray()
async for chunk in i.pull():
if chunk: chunks.extend(chunk)

await Log.debug(chunks)


if __name__ == "__main__":
#run(Test().test())
run(Test().main())
#run(Test().main())
loop = get_event_loop()

loop.run_until_complete(test())

Binary file modified Blazeio/Client/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified Blazeio/Client/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified Blazeio/Client/__pycache__/__main__.cpython-312.pyc
Binary file not shown.
2 changes: 0 additions & 2 deletions Blazeio/Dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@

class Err(Exception):
def __init__(app, message=None):
super().__init__(message)
app.message = str(message)

def __str__(app) -> str:
return app.message

class ServerGotInTrouble(Exception):
def __init__(app, message=None):
super().__init__(message)
app.message = str(message)

def __str__(app) -> str:
Expand Down
Loading

0 comments on commit 2847e27

Please sign in to comment.