Rewrote the comments in the libraries under 'includes' in English (excluding those inside folders)

This commit is contained in:
2026-07-29 23:34:36 +08:00
parent 3633be1995
commit a2cc28a6ab
54 changed files with 7091 additions and 899 deletions

View File

@@ -7,21 +7,21 @@ import stdio
# ============================================================
# requests.py — HTTP 客户端库Python 原版风格)
# requests.py — HTTP client library (Python original style)
#
# 用法:
# requests._mbuddy = my_mbuddy
# resp = requests.get("http://example.com/path")
# if resp is not None:
# print(resp.status_code)
# print(resp.text)
# requests.release(resp)
# Usage:
# requests._mbuddy = my_mbuddy
# resp = requests.get("http://example.com/path")
# if resp is not None:
# print(resp.status_code)
# print(resp.text)
# requests.release(resp)
# ============================================================
# 模块级全局 MBuddy 指针(由用户在 main 中赋值)
# Module-level global MBuddy pointer (assigned by the user in main)
_mbuddy: memhub.MemBuddy | t.CPtr
# Socket 初始化标记
# Socket initialization flag
_socket_ready: bool = False
HTTP_PORT: t.CDefine = 80
@@ -29,26 +29,26 @@ HTTP_BUFSIZ: t.CDefine = 4096
# ============================================================
# Response — HTTP 响应对象
# Response — HTTP response object
# ============================================================
class Response:
"""HTTP 响应对象,贴近 Python requests.Response 用法。
"""HTTP response object, similar to how Python's requests.Response works.
从 _mbuddy 分配,使用完毕后调用 release(resp) 释放。
Allocated from _mbuddy, call release(resp) to free it after use.
"""
status_code: INT # HTTP 状态码(如 200404
text: str # 响应体文本(指向 _raw_buf 内部)
body_len: INT # 响应体长度
_raw_buf: str # 原始缓冲区指针
status_code: INT # HTTP status code (e.g., 200, 404)
text: str # Response body text (points into _raw_buf)
body_len: INT # Response body length
_raw_buf: str # Raw buffer pointer
# ============================================================
# 内部辅助函数
# Internal auxiliary functions
# ============================================================
def _EnsureSocketInit() -> INT:
"""确保 Socket 库已初始化(幂等)。"""
"""Ensure Socket library is initialized (idempotent)."""
global _socket_ready
if _socket_ready: return 0
result: INT = socket.SocketInit()
@@ -58,7 +58,7 @@ def _EnsureSocketInit() -> INT:
def _AllocResponse() -> Response | t.CPtr:
"""从 _mbuddy 分配 Response 结构体并就地初始化。"""
"""Allocate Response struct from _mbuddy and initialize it in-place."""
if _mbuddy is None: return None
resp: Response | t.CPtr = _mbuddy.alloc(Response.__sizeof__())
if resp is None: return None
@@ -70,17 +70,17 @@ def _AllocResponse() -> Response | t.CPtr:
def _ParseUrl(url: str, host_buf: str, host_size: INT, path_buf: str, path_size: INT, port: INT | t.CPtr) -> INT:
"""解析 URL提取 hostportpath"""
"""Parse URL and extract host, port, and path."""
if url is None: return -1
if host_buf is None or path_buf is None or port is None: return -1
p: str = url
# 跳过 "http://"
# Skip "http://"
if string.strncmp(p, "http://", 7) == 0:
p += 7
# 解析 host
# Parse host
host_start: str = p
host_len: INT = 0
while p[0] != '\0' and p[0] != ':' and p[0] != '/':
@@ -91,7 +91,7 @@ def _ParseUrl(url: str, host_buf: str, host_size: INT, path_buf: str, path_size:
string.strncpy(host_buf, host_start, host_len)
host_buf[host_len] = 0
# 解析 port
# Parse port
if p[0] == ':':
p += 1
port_val: INT = 0
@@ -102,7 +102,7 @@ def _ParseUrl(url: str, host_buf: str, host_size: INT, path_buf: str, path_size:
else:
c.DerefAs(port, HTTP_PORT)
# 解析 path
# Parse path
if p[0] == '/':
path_start: str = p
path_len: INT = 0
@@ -121,17 +121,17 @@ def _ParseUrl(url: str, host_buf: str, host_size: INT, path_buf: str, path_size:
def _ParseStatusLine(resp_text: str, out_code: INT | t.CPtr) -> INT:
"""解析 HTTP 状态行,提取状态码。返回状态行结束偏移。"""
"""Parse HTTP status line and extract status code."""
if resp_text is None or out_code is None: return -1
p: str = resp_text
# 跳过 "HTTP/x.x "
# Skip "HTTP/x.x "
while p[0] != '\0' and p[0] != ' ':
p += 1
if p[0] == ' ': p += 1
# 解析状态码
# Parse status code
code: INT = 0
while p[0] >= '0' and p[0] <= '9':
code = code * 10 + INT(p[0] - '0')
@@ -139,7 +139,7 @@ def _ParseStatusLine(resp_text: str, out_code: INT | t.CPtr) -> INT:
c.DerefAs(out_code, code)
# 找到 \n
# Find \n
while p[0] != '\0' and p[0] != '\n':
p += 1
if p[0] == '\n': p += 1
@@ -148,7 +148,7 @@ def _ParseStatusLine(resp_text: str, out_code: INT | t.CPtr) -> INT:
def _FindBodyStart(resp_text: str, resp_len: INT) -> INT:
"""找到 HTTP 响应体起始位置(\\r\\n\\r\\n 之后)。"""
"""Find the start of the HTTP response body (after rnrn)."""
if resp_text is None: return -1
end_of_headers: str = string.strstr(resp_text, "\r\n\r\n")
@@ -166,13 +166,12 @@ def _FindBodyStart(resp_text: str, resp_len: INT) -> INT:
# ============================================================
def get(url: str) -> Response | t.CPtr:
"""HTTP GET 请求。
"""HTTP GET request.
Args:
url: 完整 URL ( "http://example.com/path")
url: Full URL (e.g., "http://example.com/path")
Returns:
Response 对象指针(从 _mbuddy 分配),失败返回 None。
使用完毕后调用 release(resp) 释放。
Pointer to a Response object (allocated from _mbuddy), returns None on failure.
"""
if _mbuddy is None: return None
@@ -183,7 +182,7 @@ def get(url: str) -> Response | t.CPtr:
release(resp)
return None
# 解析 URL
# Parse URL
host_buf: t.CArray[t.CChar, 256]
path_buf: t.CArray[t.CChar, 1024]
port: INT = 0
@@ -192,7 +191,7 @@ def get(url: str) -> Response | t.CPtr:
release(resp)
return None
# 创建并连接 Socket
# Create Socket
sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
if sock.fd == socket.INVALID_SOCKET:
release(resp)
@@ -203,7 +202,7 @@ def get(url: str) -> Response | t.CPtr:
release(resp)
return None
# 构建 HTTP 请求
# Build HTTP request
req_buf: str = _mbuddy.alloc(HTTP_BUFSIZ)
if req_buf is None:
sock.close()
@@ -223,7 +222,7 @@ def get(url: str) -> Response | t.CPtr:
release(resp)
return None
# 接收响应
# Receive response body
resp_buf: str = _mbuddy.alloc(HTTP_BUFSIZ)
if resp_buf is None:
sock.close()
@@ -241,10 +240,10 @@ def get(url: str) -> Response | t.CPtr:
resp._raw_buf = resp_buf
# 解析状态码
# Parse status line
_ParseStatusLine(resp_buf, c.Addr(resp.status_code))
# 找到 body
# Find body start
body_offset: INT = _FindBodyStart(resp_buf, total)
if body_offset < 0 or body_offset >= total:
resp.text = None
@@ -257,14 +256,14 @@ def get(url: str) -> Response | t.CPtr:
def post(url: str, data: str = None, content_type: str = "application/x-www-form-urlencoded") -> Response | t.CPtr:
"""HTTP POST 请求。
"""HTTP POST request.
Args:
url: 完整 URL
data: 请求体数据
content_type: Content-Type 头(默认 "application/x-www-form-urlencoded"
url: Full URL
data: Request body data
content_type: Content-Type header (default is "application/x-www-form-urlencoded")
Returns:
Response 对象指针(从 _mbuddy 分配),失败返回 None。
Pointer to Response object (allocated from _mbuddy), returns None on failure.
"""
if _mbuddy is None: return None
@@ -275,7 +274,7 @@ def post(url: str, data: str = None, content_type: str = "application/x-www-form
release(resp)
return None
# 解析 URL
# Parse URL
host_buf: t.CArray[t.CChar, 256]
path_buf: t.CArray[t.CChar, 1024]
port: INT = 0
@@ -287,7 +286,7 @@ def post(url: str, data: str = None, content_type: str = "application/x-www-form
body_len: INT = INT(string.strlen(data)) if data is not None else 0
ct: str = content_type if content_type is not None else "application/x-www-form-urlencoded"
# 创建并连接 Socket
# Create Socket
sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
if sock.fd == socket.INVALID_SOCKET:
release(resp)
@@ -298,7 +297,7 @@ def post(url: str, data: str = None, content_type: str = "application/x-www-form
release(resp)
return None
# 构建 HTTP 请求
# Build HTTP request
req_buf: str = _mbuddy.alloc(HTTP_BUFSIZ)
if req_buf is None:
sock.close()
@@ -311,14 +310,14 @@ def post(url: str, data: str = None, content_type: str = "application/x-www-form
hdr_len: INT = INT(string.strlen(req_buf))
# 发送头部 + body
# Send headers + body
sent: INT = sock.send(req_buf, hdr_len)
if sent > 0 and body_len > 0 and data is not None:
sock.send(data, body_len)
_mbuddy.free(req_buf)
# 接收响应
# Receive response body
resp_buf: str = _mbuddy.alloc(HTTP_BUFSIZ)
if resp_buf is None:
sock.close()
@@ -349,13 +348,13 @@ def post(url: str, data: str = None, content_type: str = "application/x-www-form
def release(resp: Response | t.CPtr):
"""释放 Response 对象及其内部缓冲区。
"""Release Response object and its internal buffers.
Args:
resp: 要释放的 Response 对象
resp: The Response object to release
"""
if resp is None: return
if _mbuddy is None: return
if resp._raw_buf is not None:
_mbuddy.free(resp._raw_buf)
_mbuddy.free(t.CVoid(t.CUInt64T(resp), t.CPtr))
_mbuddy.free(t.CVoid(t.CUInt64T(resp), t.CPtr))