kopia lustrzana https://github.com/cirospaciari/socketify.py
optimize send a little bit
rodzic
f04a5fd642
commit
a84dba6eab
|
@ -4,10 +4,12 @@ import multiprocessing
|
|||
import asyncio
|
||||
def run_app():
|
||||
app = App(request_response_factory_max_items=200_000)
|
||||
async def home(res, req):
|
||||
res.end("Hello, World!")
|
||||
router = app.router()
|
||||
|
||||
@router.get("/")
|
||||
def home(res, req):
|
||||
res.send(b"Hello, World!")
|
||||
|
||||
app.get("/", home)
|
||||
app.listen(
|
||||
8000,
|
||||
lambda config: print(
|
||||
|
|
|
@ -61,7 +61,7 @@ class AppResponse:
|
|||
def get_proxied_remote_address_bytes(self):
|
||||
def get_proxied_remote_address(self):
|
||||
def cork_send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def send(self, message: any = b"", content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def end(self, message, end_connection=False):
|
||||
def pause(self):
|
||||
def resume(self):
|
||||
|
|
|
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||
|
||||
[project]
|
||||
name = "socketify"
|
||||
version = "0.0.12"
|
||||
version = "0.0.13"
|
||||
authors = [
|
||||
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
||||
]
|
||||
|
|
2
setup.py
2
setup.py
|
@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|||
|
||||
setuptools.setup(
|
||||
name="socketify",
|
||||
version="0.0.12",
|
||||
version="0.0.13",
|
||||
platforms=["any"],
|
||||
author="Ciro Spaciari",
|
||||
author_email="ciro.spaciari@gmail.com",
|
||||
|
|
|
@ -385,6 +385,12 @@ void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size
|
|||
|
||||
|
||||
void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool close_connection);
|
||||
|
||||
void socketify_res_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
|
||||
void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
"""
|
||||
)
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
extern "C"
|
||||
{
|
||||
|
||||
|
||||
static std::map<int, const char *> status_codes{
|
||||
{100, "100 Continue"},
|
||||
{101, "101 Switching Protocols"},
|
||||
|
@ -71,11 +70,12 @@ static std::map<int, const char*> status_codes{
|
|||
{507, "507 Insufficient Storage"},
|
||||
{508, "508 Loop Detected"},
|
||||
{510, "510 Not Extended"},
|
||||
{ 511, "511 Network Authentication Required"}
|
||||
};
|
||||
{511, "511 Network Authentication Required"}};
|
||||
|
||||
bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code) {
|
||||
if (code == 200) {
|
||||
bool socketify_res_write_int_status(int ssl, uws_res_t *res, int code)
|
||||
{
|
||||
if (code == 200)
|
||||
{
|
||||
uws_res_write_status(ssl, res, "200 OK", 6);
|
||||
return true; // default
|
||||
}
|
||||
|
@ -90,33 +90,171 @@ bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void socketify_res_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection)
|
||||
{
|
||||
socketify_res_write_int_status(ssl, res, code);
|
||||
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
void socketify_res_write_headers(int ssl, uws_res_t* res, socketify_header* headers) {
|
||||
if (content_data != NULL)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
|
||||
uwsRes->cork([=](){
|
||||
socketify_res_write_int_status(ssl, res, code);
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
if (content_data != NULL)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
|
||||
uwsRes->cork([=](){
|
||||
|
||||
socketify_res_write_int_status(ssl, res, code);
|
||||
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
if (content_data != NULL)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection)
|
||||
{
|
||||
|
||||
if (ssl)
|
||||
{
|
||||
uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
|
||||
uwsRes->cork([=](){
|
||||
|
||||
uws_res_write_status(ssl, res, status_code, status_code_size);
|
||||
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
if (content_data != NULL)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
|
||||
uwsRes->cork([=](){
|
||||
|
||||
uws_res_write_status(ssl, res, status_code, status_code_size);
|
||||
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
if (content_data != NULL)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection)
|
||||
{
|
||||
uws_res_write_status(ssl, res, status_code, status_code_size);
|
||||
|
||||
if (content_type && content_type_size)
|
||||
{
|
||||
uws_res_write_header(ssl, res, "Content-Type", 12, content_type, content_type_size);
|
||||
}
|
||||
|
||||
if (content_data && content_data_size)
|
||||
{
|
||||
uws_res_end(ssl, res, content_data, content_data_size, close_connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
uws_res_end_without_body(ssl, res, close_connection);
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_res_write_headers(int ssl, uws_res_t *res, socketify_header *headers)
|
||||
{
|
||||
while (headers != NULL)
|
||||
{
|
||||
uws_res_write_header(ssl, res, headers->name, headers->name_size, headers->value, headers->value_size);
|
||||
headers = (socketify_header *)headers->next;
|
||||
}
|
||||
}
|
||||
|
||||
bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t* res, int code, socketify_header* headers) {
|
||||
if(socketify_res_write_int_status(ssl, res, code)){
|
||||
bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t *res, int code, socketify_header *headers)
|
||||
{
|
||||
if (socketify_res_write_int_status(ssl, res, code))
|
||||
{
|
||||
socketify_res_write_headers(ssl, res, headers);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void socketify_destroy_headers(socketify_header* headers){
|
||||
void socketify_destroy_headers(socketify_header *headers)
|
||||
{
|
||||
|
||||
socketify_header *current = headers;
|
||||
while(current != NULL){
|
||||
while (current != NULL)
|
||||
{
|
||||
socketify_header *next = (socketify_header *)current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res){
|
||||
socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res)
|
||||
{
|
||||
|
||||
socketify_asgi_data result;
|
||||
|
||||
|
@ -158,26 +296,30 @@ socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *r
|
|||
current->name = name.data();
|
||||
current->name_size = name.length();
|
||||
|
||||
if(name.compare("content-length") == 0 || name.compare("transfer-encoding") == 0){
|
||||
if (name.compare("content-length") == 0 || name.compare("transfer-encoding") == 0)
|
||||
{
|
||||
result.has_content = true;
|
||||
}
|
||||
|
||||
current->value = header.second.data();
|
||||
current->value_size = header.second.length();
|
||||
current->next = NULL;
|
||||
if(last == NULL){
|
||||
if (last == NULL)
|
||||
{
|
||||
result.header_list = current;
|
||||
last = current;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
last->next = current;
|
||||
last = current;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res){
|
||||
socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res)
|
||||
{
|
||||
|
||||
socketify_asgi_ws_data result;
|
||||
|
||||
|
@ -225,14 +367,19 @@ socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_re
|
|||
const char *value_data = value.data();
|
||||
size_t value_size = value.length();
|
||||
|
||||
if (name.compare("sec-websocket-key") == 0){
|
||||
if (name.compare("sec-websocket-key") == 0)
|
||||
{
|
||||
key = value_data;
|
||||
key_size = value_size;
|
||||
}else if (name.compare("sec-websocket-protocol") == 0){
|
||||
}
|
||||
else if (name.compare("sec-websocket-protocol") == 0)
|
||||
{
|
||||
protocol = value_data;
|
||||
protocol_size = value_size;
|
||||
continue; // exclude protocol
|
||||
}else if (name.compare("sec-websocket-extensions") == 0){
|
||||
}
|
||||
else if (name.compare("sec-websocket-extensions") == 0)
|
||||
{
|
||||
extensions = value_data;
|
||||
extensions_size = value_size;
|
||||
}
|
||||
|
@ -243,12 +390,14 @@ socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_re
|
|||
current->value = value_data;
|
||||
current->value_size = value_size;
|
||||
|
||||
|
||||
current->next = NULL;
|
||||
if(last == NULL){
|
||||
if (last == NULL)
|
||||
{
|
||||
result.header_list = current;
|
||||
last = current;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
last->next = current;
|
||||
last = current;
|
||||
}
|
||||
|
@ -260,94 +409,89 @@ socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_re
|
|||
result.key_size = key_size;
|
||||
result.extensions_size = extensions_size;
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
void socketify_asgi_http_handler(uws_res_t *response, uws_req_t *request, void *user_data){
|
||||
void socketify_asgi_http_handler(uws_res_t *response, uws_req_t *request, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_app_info *info = ((socksocketify_asgi_app_info *)user_data);
|
||||
socketify_asgi_data data = socketify_asgi_request(info->ssl, request, response);
|
||||
bool *aborted = (bool *)malloc(sizeof(aborted));
|
||||
*aborted = false;
|
||||
uws_res_on_aborted(info->ssl, response, [](uws_res_t *res, void *opcional_data){
|
||||
uws_res_on_aborted(
|
||||
info->ssl, response, [](uws_res_t *res, void *opcional_data)
|
||||
{
|
||||
bool* aborted = (bool*)opcional_data;
|
||||
*aborted = true;
|
||||
}, aborted);
|
||||
*aborted = true; },
|
||||
aborted);
|
||||
info->handler(info->ssl, response, data, info->user_data, aborted);
|
||||
socketify_destroy_headers(data.header_list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void socketify_res_cork_write(int ssl, uws_res_t *res, const char* data, size_t length){
|
||||
void socketify_res_cork_write(int ssl, uws_res_t *res, const char *data, size_t length)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
|
||||
uwsRes->cork([=](){
|
||||
uwsRes->write(std::string_view(data, length));
|
||||
});
|
||||
uwsRes->cork([=]()
|
||||
{ uwsRes->write(std::string_view(data, length)); });
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
|
||||
uwsRes->cork([=](){
|
||||
uwsRes->write(std::string_view(data, length));
|
||||
});
|
||||
uwsRes->cork([=]()
|
||||
{ uwsRes->write(std::string_view(data, length)); });
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_res_cork_end(int ssl, uws_res_t *res, const char* data, size_t length, bool close_connection){
|
||||
void socketify_res_cork_end(int ssl, uws_res_t *res, const char *data, size_t length, bool close_connection)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
|
||||
uwsRes->cork([=](){
|
||||
uwsRes->end(std::string_view(data, length), close_connection);
|
||||
});
|
||||
uwsRes->cork([=]()
|
||||
{ uwsRes->end(std::string_view(data, length), close_connection); });
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
|
||||
uwsRes->cork([=](){
|
||||
uwsRes->end(std::string_view(data, length), close_connection);
|
||||
});
|
||||
uwsRes->cork([=]()
|
||||
{ uwsRes->end(std::string_view(data, length), close_connection); });
|
||||
}
|
||||
}
|
||||
void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode){
|
||||
void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
uWS::WebSocket<true, true, void *> *uws = (uWS::WebSocket<true, true, void *> *)ws;
|
||||
uws->cork([&](){
|
||||
uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char) opcode);
|
||||
});
|
||||
uws->cork([&]()
|
||||
{ uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char)opcode); });
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::WebSocket<false, true, void *> *uws = (uWS::WebSocket<false, true, void *> *)ws;
|
||||
uws->cork([&](){
|
||||
uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char) opcode);
|
||||
});
|
||||
uws->cork([&]()
|
||||
{ uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char)opcode); });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool fin){
|
||||
void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode, bool compress, bool fin)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
uWS::WebSocket<true, true, void *> *uws = (uWS::WebSocket<true, true, void *> *)ws;
|
||||
uws->cork([&](){
|
||||
uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char) opcode, compress, fin);
|
||||
});
|
||||
uws->cork([&]()
|
||||
{ uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char)opcode, compress, fin); });
|
||||
}
|
||||
else
|
||||
{
|
||||
uWS::WebSocket<false, true, void *> *uws = (uWS::WebSocket<false, true, void *> *)ws;
|
||||
uws->cork([&](){
|
||||
uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char) opcode, compress, fin);
|
||||
});
|
||||
uws->cork([&]()
|
||||
{ uws->send(std::string_view(data, length), (uWS::OpCode)(unsigned char)opcode, compress, fin); });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data){
|
||||
socksocketify_asgi_ws_app_info *socketify_add_asgi_ws_handler(int ssl, uws_app_t *app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_ws_app_info *info = (socksocketify_asgi_ws_app_info *)malloc(sizeof(socksocketify_asgi_ws_app_info));
|
||||
info->ssl = ssl;
|
||||
info->app = app;
|
||||
|
@ -359,30 +503,36 @@ socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t
|
|||
uws_socket_behavior_t ws_behavior;
|
||||
memcpy(&ws_behavior, &behavior, sizeof(behavior));
|
||||
|
||||
ws_behavior.upgrade = [](uws_res_t *response, uws_req_t *request, uws_socket_context_t *context, void* user_data){
|
||||
ws_behavior.upgrade = [](uws_res_t *response, uws_req_t *request, uws_socket_context_t *context, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_ws_app_info *info = ((socksocketify_asgi_ws_app_info *)user_data);
|
||||
socketify_asgi_ws_data data = socketify_asgi_ws_request(info->ssl, request, response);
|
||||
bool *aborted = (bool *)malloc(sizeof(aborted));
|
||||
*aborted = false;
|
||||
uws_res_on_aborted(info->ssl, response, [](uws_res_t *res, void *opcional_data){
|
||||
uws_res_on_aborted(
|
||||
info->ssl, response, [](uws_res_t *res, void *opcional_data)
|
||||
{
|
||||
bool* aborted = (bool*)opcional_data;
|
||||
*aborted = true;
|
||||
}, aborted);
|
||||
*aborted = true; },
|
||||
aborted);
|
||||
info->handler(info->ssl, response, data, context, info->user_data, aborted);
|
||||
socketify_destroy_headers(data.header_list);
|
||||
};
|
||||
ws_behavior.open = [](uws_websocket_t *ws, void* user_data){
|
||||
ws_behavior.open = [](uws_websocket_t *ws, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_ws_app_info *info = ((socksocketify_asgi_ws_app_info *)user_data);
|
||||
auto socket_data = uws_ws_get_user_data(info->ssl, ws);
|
||||
info->behavior.open(ws, socket_data);
|
||||
};
|
||||
ws_behavior.message = [](uws_websocket_t* ws, const char* message, size_t length, uws_opcode_t opcode, void* user_data){
|
||||
ws_behavior.message = [](uws_websocket_t *ws, const char *message, size_t length, uws_opcode_t opcode, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_ws_app_info *info = ((socksocketify_asgi_ws_app_info *)user_data);
|
||||
auto socket_data = uws_ws_get_user_data(info->ssl, ws);
|
||||
info->behavior.message(ws, message, length, opcode, socket_data);
|
||||
};
|
||||
|
||||
ws_behavior.close = [](uws_websocket_t* ws, int code, const char* message, size_t length, void* user_data){
|
||||
ws_behavior.close = [](uws_websocket_t *ws, int code, const char *message, size_t length, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_ws_app_info *info = ((socksocketify_asgi_ws_app_info *)user_data);
|
||||
auto socket_data = uws_ws_get_user_data(info->ssl, ws);
|
||||
info->behavior.close(ws, code, message, length, socket_data);
|
||||
|
@ -392,7 +542,8 @@ socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t
|
|||
return info;
|
||||
}
|
||||
|
||||
socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t* app, socketify_asgi_method_handler handler, void* user_data){
|
||||
socksocketify_asgi_app_info *socketify_add_asgi_http_handler(int ssl, uws_app_t *app, socketify_asgi_method_handler handler, void *user_data)
|
||||
{
|
||||
socksocketify_asgi_app_info *info = (socksocketify_asgi_app_info *)malloc(sizeof(socksocketify_asgi_app_info));
|
||||
info->ssl = ssl;
|
||||
info->app = app;
|
||||
|
@ -404,41 +555,48 @@ socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t*
|
|||
return info;
|
||||
}
|
||||
|
||||
void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info* app){
|
||||
void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info *app)
|
||||
{
|
||||
free(app);
|
||||
}
|
||||
void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app){
|
||||
void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info *app)
|
||||
{
|
||||
free(app);
|
||||
}
|
||||
|
||||
void socketify_generic_prepare_callback(uv_prepare_t *prepare){
|
||||
void socketify_generic_prepare_callback(uv_prepare_t *prepare)
|
||||
{
|
||||
socketify_loop *loop = (socketify_loop *)uv_handle_get_data((uv_handle_t *)prepare);
|
||||
loop->on_prepare_handler(loop->on_prepare_data);
|
||||
}
|
||||
|
||||
void socketify_generic_timer_callback(uv_timer_t *timer){
|
||||
void socketify_generic_timer_callback(uv_timer_t *timer)
|
||||
{
|
||||
socketify_timer *loop_data = (socketify_timer *)uv_handle_get_data((uv_handle_t *)timer);
|
||||
loop_data->handler(loop_data->user_data);
|
||||
}
|
||||
|
||||
void socketify_generic_check_callback(uv_check_t *timer){
|
||||
void socketify_generic_check_callback(uv_check_t *timer)
|
||||
{
|
||||
socketify_timer *loop_data = (socketify_timer *)uv_handle_get_data((uv_handle_t *)timer);
|
||||
loop_data->handler(loop_data->user_data);
|
||||
}
|
||||
|
||||
|
||||
void* socketify_get_native_loop(socketify_loop* loop){
|
||||
void *socketify_get_native_loop(socketify_loop *loop)
|
||||
{
|
||||
return loop->uv_loop;
|
||||
}
|
||||
|
||||
socketify_loop * socketify_create_loop(){
|
||||
socketify_loop *socketify_create_loop()
|
||||
{
|
||||
socketify_loop *loop = (socketify_loop *)malloc(sizeof(uv_prepare_t));
|
||||
loop->uv_loop = NULL;
|
||||
loop->on_prepare_handler = NULL;
|
||||
loop->uv_prepare_ptr = NULL;
|
||||
|
||||
uv_loop_t *uv_loop = (uv_loop_t *)malloc(sizeof(uv_loop_t));
|
||||
if(uv_loop_init(uv_loop)){
|
||||
if (uv_loop_init(uv_loop))
|
||||
{
|
||||
free(uv_loop);
|
||||
return loop;
|
||||
}
|
||||
|
@ -446,15 +604,20 @@ socketify_loop * socketify_create_loop(){
|
|||
return loop;
|
||||
}
|
||||
|
||||
bool socketify_constructor_failed(socketify_loop* loop){
|
||||
bool socketify_constructor_failed(socketify_loop *loop)
|
||||
{
|
||||
return loop->uv_loop == NULL;
|
||||
}
|
||||
|
||||
bool socketify_on_prepare(socketify_loop* loop, socketify_prepare_handler handler, void* user_data){
|
||||
if (loop->uv_prepare_ptr != NULL) return false;
|
||||
if(handler == NULL) return false;
|
||||
bool socketify_on_prepare(socketify_loop *loop, socketify_prepare_handler handler, void *user_data)
|
||||
{
|
||||
if (loop->uv_prepare_ptr != NULL)
|
||||
return false;
|
||||
if (handler == NULL)
|
||||
return false;
|
||||
uv_prepare_t *prepare = (uv_prepare_t *)malloc(sizeof(uv_prepare_t));
|
||||
if(uv_prepare_init((uv_loop_t*)loop->uv_loop, prepare)){
|
||||
if (uv_prepare_init((uv_loop_t *)loop->uv_loop, prepare))
|
||||
{
|
||||
free(prepare);
|
||||
return false;
|
||||
}
|
||||
|
@ -468,8 +631,10 @@ bool socketify_on_prepare(socketify_loop* loop, socketify_prepare_handler handle
|
|||
return true;
|
||||
}
|
||||
|
||||
bool socketify_prepare_unbind(socketify_loop* loop){
|
||||
if(loop->uv_prepare_ptr == NULL) return false;
|
||||
bool socketify_prepare_unbind(socketify_loop *loop)
|
||||
{
|
||||
if (loop->uv_prepare_ptr == NULL)
|
||||
return false;
|
||||
uv_prepare_stop((uv_prepare_t *)loop->uv_prepare_ptr);
|
||||
|
||||
free(loop->uv_prepare_ptr);
|
||||
|
@ -477,32 +642,39 @@ bool socketify_prepare_unbind(socketify_loop* loop){
|
|||
return true;
|
||||
}
|
||||
|
||||
int socketify_loop_run(socketify_loop* loop, socketify_run_mode mode){
|
||||
int socketify_loop_run(socketify_loop *loop, socketify_run_mode mode)
|
||||
{
|
||||
return uv_run((uv_loop_t *)loop->uv_loop, (uv_run_mode)mode);
|
||||
}
|
||||
|
||||
void socketify_loop_stop(socketify_loop* loop){
|
||||
if(uv_loop_alive((uv_loop_t*)loop->uv_loop)){
|
||||
void socketify_loop_stop(socketify_loop *loop)
|
||||
{
|
||||
if (uv_loop_alive((uv_loop_t *)loop->uv_loop))
|
||||
{
|
||||
uv_stop((uv_loop_t *)loop->uv_loop);
|
||||
}
|
||||
}
|
||||
|
||||
void socketify_destroy_loop(socketify_loop* loop){
|
||||
void socketify_destroy_loop(socketify_loop *loop)
|
||||
{
|
||||
socketify_loop_stop(loop);
|
||||
|
||||
uv_loop_close((uv_loop_t *)loop->uv_loop);
|
||||
free(loop->uv_loop);
|
||||
if(loop->uv_prepare_ptr){
|
||||
if (loop->uv_prepare_ptr)
|
||||
{
|
||||
free(loop->uv_prepare_ptr);
|
||||
}
|
||||
free(loop);
|
||||
}
|
||||
|
||||
socketify_timer* socketify_create_timer(socketify_loop* loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void* user_data){
|
||||
socketify_timer *socketify_create_timer(socketify_loop *loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void *user_data)
|
||||
{
|
||||
|
||||
uv_timer_t *uv_timer = (uv_timer_t *)malloc(sizeof(uv_timer_t));
|
||||
|
||||
if(uv_timer_init((uv_loop_t*)loop->uv_loop, uv_timer)){
|
||||
if (uv_timer_init((uv_loop_t *)loop->uv_loop, uv_timer))
|
||||
{
|
||||
free(uv_timer);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -518,24 +690,25 @@ socketify_timer* socketify_create_timer(socketify_loop* loop, uint64_t timeout,
|
|||
return timer;
|
||||
}
|
||||
|
||||
void socketify_timer_set_repeat(socketify_timer* timer, uint64_t repeat){
|
||||
void socketify_timer_set_repeat(socketify_timer *timer, uint64_t repeat)
|
||||
{
|
||||
uv_timer_set_repeat((uv_timer_t *)timer->uv_timer_ptr, repeat);
|
||||
}
|
||||
|
||||
|
||||
// stops and destroy timer info
|
||||
void socketify_timer_destroy(socketify_timer* timer){
|
||||
void socketify_timer_destroy(socketify_timer *timer)
|
||||
{
|
||||
uv_timer_stop((uv_timer_t *)timer->uv_timer_ptr);
|
||||
free(timer->uv_timer_ptr);
|
||||
free(timer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
socketify_timer* socketify_create_check(socketify_loop* loop, socketify_timer_handler handler, void* user_data){
|
||||
socketify_timer *socketify_create_check(socketify_loop *loop, socketify_timer_handler handler, void *user_data)
|
||||
{
|
||||
|
||||
uv_check_t *uv_timer = (uv_check_t *)malloc(sizeof(uv_check_t));
|
||||
if(uv_check_init((uv_loop_t*)loop->uv_loop, uv_timer)){
|
||||
if (uv_check_init((uv_loop_t *)loop->uv_loop, uv_timer))
|
||||
{
|
||||
free(uv_timer);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -552,7 +725,8 @@ socketify_timer* socketify_create_check(socketify_loop* loop, socketify_timer_ha
|
|||
}
|
||||
|
||||
// stops and destroy timer info
|
||||
void socketify_check_destroy(socketify_timer* timer){
|
||||
void socketify_check_destroy(socketify_timer *timer)
|
||||
{
|
||||
uv_check_stop((uv_check_t *)timer->uv_timer_ptr);
|
||||
free(timer->uv_timer_ptr);
|
||||
free(timer);
|
||||
|
|
|
@ -138,6 +138,10 @@ DLL_EXPORT socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl
|
|||
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
|
||||
DLL_EXPORT void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode);
|
||||
DLL_EXPORT void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool fin);
|
||||
DLL_EXPORT void socketify_res_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -1733,6 +1733,7 @@ class AppResponse:
|
|||
headers=None,
|
||||
end_connection: bool = False,
|
||||
):
|
||||
# TODO: use socketify_res_cork_send_int_code and socketify_res_cork_send after optimize headers
|
||||
self.cork(
|
||||
lambda res: res.send(message, content_type, status, headers, end_connection)
|
||||
)
|
||||
|
@ -1740,42 +1741,107 @@ class AppResponse:
|
|||
|
||||
def send(
|
||||
self,
|
||||
message: any,
|
||||
message: any = b"",
|
||||
content_type: Union[str, bytes] = b"text/plain",
|
||||
status: Union[str, bytes, int] = b"200 OK",
|
||||
headers = None,
|
||||
end_connection: bool = False,
|
||||
):
|
||||
if self.aborted:
|
||||
return self
|
||||
|
||||
self.write_status(status)
|
||||
|
||||
# TODO: optimize headers
|
||||
if headers is not None:
|
||||
for name, value in headers:
|
||||
self.write_header(name, value)
|
||||
try:
|
||||
|
||||
# TODO: optimize Set-Cookie
|
||||
if self._write_jar is not None:
|
||||
self.write_header("Set-Cookie", self._write_jar.output(header=""))
|
||||
self._write_jar = None
|
||||
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
elif isinstance(message, bytes):
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
data = message
|
||||
elif message is None:
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
self.end_without_body(end_connection)
|
||||
if isinstance(status, int):
|
||||
lib.socketify_res_send_int_code(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
elif isinstance(status, str):
|
||||
status = status.encode("utf-8")
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
else:
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
return self
|
||||
else:
|
||||
data = self.app._json_serializer.dumps(message).encode("utf-8")
|
||||
# ignores content_type should always be json here
|
||||
self.write_header(b"Content-Type", b"application/json")
|
||||
content_type = b"application/json"
|
||||
|
||||
lib.uws_res_end(
|
||||
self.app.SSL, self.res, data, len(data), 1 if end_connection else 0
|
||||
if isinstance(status, int):
|
||||
lib.socketify_res_send_int_code(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
data,
|
||||
len(data),
|
||||
status,
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
elif isinstance(status, str):
|
||||
status = status.encode("utf-8")
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
else:
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
data,
|
||||
len(data),
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
|
||||
finally:
|
||||
return self
|
||||
|
||||
|
@ -2506,7 +2572,6 @@ class App:
|
|||
self._native_options.append(cert_file_name)
|
||||
socket_options.cert_file_name = cert_file_name
|
||||
|
||||
|
||||
passphrase = (
|
||||
ffi.NULL
|
||||
if options.passphrase is None
|
||||
|
|
Ładowanie…
Reference in New Issue