cycle never stop

This commit is contained in:
onceday 2022-11-23 23:58:14 +08:00
parent 5a333efcaa
commit 3d773b5709
8 changed files with 59 additions and 39 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@ bsp/pico/MDK/mdk/RTE
bsp/pico-dev/MDK/mdk/RTE
.vscode/
coredump/
#remove unnecessary file
test/out/

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=pikaMain.slice_a90"
"--gtest_filter=requests.std_request"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -1,2 +1,2 @@
cd build && rm ./test/pikascript_test -f && ninja -j0
cd build && rm ./test/pikascript_test -f && ninja -j3
cd .. && cp ./build/boot/demo06-pikamain/pikascript_demo06-pikamain package/pikascript/pika

View File

@ -10,10 +10,10 @@ class Response:
def request(self, method: str, **kwargs) -> int: ...
def request_init(self, method: str) -> int: ...
def request_del(self, ) -> None: ...
def proto_write(self, proto: str = None) -> int: ...
def urlencode_write(self, s1: str, s2: str = None, start: str = None, connect: str = None) -> int: ...
def request_del(self) -> None: ...
def proto_write(self, proto: str) -> int: ...
def urlencode_write(self, s1: str, s2: str, start: str , connect: str) -> int: ...
def header_write(self, header: str, value: str) -> int: ...
def __init__(): ...
def __del__(): ...
def __init__(self): ...
def __del__(self): ...

View File

@ -20,7 +20,7 @@
#define likely(x) __builtin_expect(!!(x), 1)
#endif
int _requests_Response_request(PikaObj *self, char* method, PikaDict* kwargs)
int _requests_Response_request(PikaObj *self, char *method, PikaDict *kwargs)
{
const char *this_url; /* 真实组装之后的url */
const char *this_header; /* 填充之后响应头信息 */
@ -39,7 +39,6 @@ int _requests_Response_request(PikaObj *self, char* method, PikaDict* kwargs)
}
timeout = 0; /* 默认超时时间不限 */
data_len = 0; /* 默认无数据传输 */
this_data = NULL; /* 默认无数据 */
if (kwargs != NULL)
@ -107,7 +106,14 @@ int _requests_Response_request(PikaObj *self, char* method, PikaDict* kwargs)
}
else if (strEqu(method, "GET"))
{
data_len = strlen(this_data);
if (this_data == NULL)
{
data_len = 0;
}
else
{
data_len = strlen(this_data);
}
/* FIXME: 默认二进制数据 */
if (strstr(session->header->buffer, "Content-Length") == RT_NULL)
{
@ -152,7 +158,7 @@ int _requests_Response_request(PikaObj *self, char* method, PikaDict* kwargs)
return 1;
}
int _requests_Response_header_write(PikaObj *self, char* header, char* value)
int _requests_Response_header_write(PikaObj *self, char *header, char *value)
{
struct webclient_session *session;
@ -172,7 +178,7 @@ int _requests_Response_header_write(PikaObj *self, char* header, char* value)
return 1;
}
int _requests_Response_proto_write(PikaObj *self, PikaObj* proto)
int _requests_Response_proto_write(PikaObj *self, char *proto)
{
struct webclient_session *session;
@ -184,7 +190,7 @@ int _requests_Response_proto_write(PikaObj *self, PikaObj* proto)
}
/* 写入请求初始内容 */
if (proto != NULL)
if (proto != NULL && *proto != '\0')
{
if (webclient_header_fields_add(session, " %s\r\n", proto) < 0)
{
@ -209,7 +215,7 @@ char to_hex(char code)
return hex[code & 15];
}
int _requests_Response_urlencode_write(PikaObj *self, char* s1, PikaObj* s2, PikaObj* start, PikaObj* connect)
int _requests_Response_urlencode_write(PikaObj *self, char* s1, char* s2, char* start, char* connect)
{
struct webclient_session *session;
char *url_address, *p, *s;
@ -228,9 +234,10 @@ int _requests_Response_urlencode_write(PikaObj *self, char* s1, PikaObj* s2, Pik
if (start != NULL)
{
/* 写入前置符号 */
while (*start)
s = (char *)start;
while (*s)
{
*p++ = *start++;
*p++ = *s++;
}
}
@ -256,12 +263,13 @@ int _requests_Response_urlencode_write(PikaObj *self, char* s1, PikaObj* s2, Pik
if (connect != NULL)
{
/* 写入连接符号 */
while (*connect)
s = (char *)connect;
while (*s)
{
*p++ = *connect++;
*p++ = *s++;
}
}
s = s2;
s = (char *)s2;
if (s != NULL)
{
while (*s)
@ -297,7 +305,7 @@ int _requests_Response_urlencode_write(PikaObj *self, char* s1, PikaObj* s2, Pik
return 1;
}
int _requests_Response_request_init(PikaObj *self, char* method)
int _requests_Response_request_init(PikaObj *self, char *method)
{
/* 创建会话对象header长度固定 */
struct webclient_session *session;
@ -334,13 +342,13 @@ int _requests_Response_request_init(PikaObj *self, char* method)
return 1;
}
PikaObj* _requests_Response_request_del(PikaObj *self)
PikaObj *_requests_Response_request_del(PikaObj *self)
{
struct webclient_session *session;
session = obj_getInt(self, "session_address");
if (session == -999999999)
session = (struct webclient_session *)obj_getInt(self, "session_address");
if (session == (void *)-999999999)
{
session = 0;
session = NULL;
}
if (session)
{

View File

@ -2,16 +2,16 @@ import _requests
class Response(_requests.Response):
def __init__():
_requests.__init__()
def __init__(self):
super().__init__()
def __del__():
_requests.__del__()
def __del__(self):
super().__del__()
def _append_params_to_url(rqst: Response, url: str, params: dict) -> int:
if params is None:
ret = rqst.urlencode_write(url)
ret = rqst.urlencode_write(url, '\0', '\0', '\0')
return 1
if '?' in url:
first_connect = '&'
@ -19,7 +19,7 @@ def _append_params_to_url(rqst: Response, url: str, params: dict) -> int:
else:
first_connect = '?'
# 初始化连接url
ret = rqst.urlencode_write(url)
ret = rqst.urlencode_write(url, '\0', '\0', '\0')
if ret != 1:
return ret
count = 0
@ -30,6 +30,7 @@ def _append_params_to_url(rqst: Response, url: str, params: dict) -> int:
ret = rqst.urlencode_write(str(k), str(v), first_connect, connect)
if ret != 1:
return ret
count+=1
else:
ret = rqst.urlencode_write(str(k), str(v), start, connect)
if ret != 1:
@ -63,7 +64,7 @@ def request(method: str, url: str, params=None, headers=None, **kwargs) -> Respo
del rqst
return None
# 写入默认HTTP版本号
ret = rqst.proto_write()
ret = rqst.proto_write('\0')
if ret != 1:
del rqst
return None

View File

@ -1,10 +1,6 @@
import requests
requests._append_params_to_url('http://www.rt-thread.com', {
'a': 1,
'b': 2
})
requests._append_params_to_url('http://www.rt-thread.com', {
'a': ' ',
'b': '%',
})
a = requests.request("GET", 'http://www.rt-thread.com')
print(a.headers)
print(a.content)

View File

@ -229,4 +229,18 @@ TEST(requests, append_params_to_url) {
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(requests, std_request) {
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
extern unsigned char pikaModules_py_a[];
obj_linkLibrary(pikaMain, pikaModules_py_a);
pikaVM_runSingleFile(pikaMain, "test/python/requests/requests_encode.py");
/* assert */
EXPECT_STREQ(log_buff[1], "'http://www.rt-thread.com?b=2&a=1'\r\n");
EXPECT_STREQ(log_buff[0], "'http://www.rt-thread.com?b=%25&a=+'\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif