Everywhere: Make session->status() callable

This commit is contained in:
Alec Murphy 2025-02-19 21:07:09 -05:00
parent ca8e7ae7f6
commit 6813c13ab3
23 changed files with 100 additions and 74 deletions

View file

@ -284,7 +284,7 @@ U0 @slon_admin_server_get(SlonHttpSession* session)
{
if (!db->@("setup")) {
if (StrICmp("/", @slon_http_request_path(session))) {
@slon_http_set_status_code(session, 302);
session->status(302);
@slon_http_set_header(session, "Location", "/");
} else {
@slon_http_send_html_file(session, "M:/Slon/Static/html/admin/setup_instance.html");
@ -312,7 +312,7 @@ U0 @slon_admin_server_get(SlonHttpSession* session)
return;
}
@slon_http_set_status_code(session, 404);
session->status(404);
}
U0 @slon_admin_setup_instance(SlonHttpSession* session)
@ -349,7 +349,7 @@ U0 @slon_admin_server_post(SlonHttpSession* session)
return;
}
@slon_http_set_status_code(session, 404);
session->status(404);
}
U0 @slon_admin_http_handle_get_request(SlonHttpSession* session)
@ -375,7 +375,7 @@ U0 @slon_admin_http_handle_request(SlonHttpSession* session)
@slon_admin_http_handle_post_request(session);
break;
default:
@slon_http_set_status_code(session, 405);
session->status(405);
}
}
@ -394,7 +394,7 @@ U0 @slon_admin_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400);
session->status(400);
goto slon_admin_http_task_send_response;
}

View file

@ -113,7 +113,7 @@ U0 @slon_local_server_directory_listing(SlonHttpSession* session, U8* path)
U0 @slon_local_server_not_found(SlonHttpSession* session)
{
@slon_http_set_status_code(session, 404);
session->status(404);
@slon_http_set_content_type(session, "text/html");
@slon_http_send(session, "<h2>404 Not Found</h2>", 22);
}
@ -162,7 +162,7 @@ U0 @slon_local_server_get(SlonHttpSession* session)
return;
} else {
if (IsDir(scratch_buffer)) {
@slon_http_set_status_code(session, 301);
session->status(301);
StrPrint(scratch_buffer, "%s/", path);
@slon_http_set_header(session, "Location", scratch_buffer);
} else {
@ -172,7 +172,7 @@ U0 @slon_local_server_get(SlonHttpSession* session)
}
// shouldn't get here :^)
@slon_http_set_status_code(session, 400);
session->status(400);
}
U0 @slon_local_http_handle_get_request(SlonHttpSession* session)
@ -187,7 +187,7 @@ U0 @slon_local_http_handle_request(SlonHttpSession* session)
@slon_local_http_handle_get_request(session);
break;
default:
@slon_http_set_status_code(session, 405);
session->status(405);
}
}
@ -206,7 +206,7 @@ U0 @slon_local_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400);
session->status(400);
goto slon_local_http_task_send_response;
}

View file

@ -43,6 +43,7 @@ U0 @slon_http_free_session(SlonHttpSession* session)
@slon_http_free_response(session, session->response);
@slon_http_free_request(session, session->request);
I64 bytes_used = session->bytes_used - MSize2(session);
Free(session->status);
Free(session);
if (bytes_used) {
AdamLog("*** Session leaked %d bytes of memory ***\n", bytes_used);
@ -72,6 +73,17 @@ SlonHttpSession* @slon_http_init_session(TcpSocket* s)
session->s = s;
session->request = @slon_http_init_request(session);
session->response = @slon_http_init_response(session);
// Create a copy of function and patch status
U64 a;
I64 code_size = MSize(&@slon_session_status_wrapper_function);
session->status = CAlloc(code_size, adam_task->code_heap);
MemCpy(session->status, &@slon_session_status_wrapper_function, code_size);
a = session->status;
a += 0x10;
MemSetI64(a, session, 1);
return session;
}
@ -413,7 +425,7 @@ U0 @slon_http_handle_get_request(SlonHttpSession* session)
/* clang-format on */
@slon_http_set_status_code(session, 404);
session->status(404);
}
U0 @slon_http_handle_patch_request(SlonHttpSession* session)
@ -436,7 +448,7 @@ U0 @slon_http_handle_patch_request(SlonHttpSession* session)
/* clang-format on */
@slon_http_set_status_code(session, 404);
session->status(404);
}
U0 @slon_http_handle_post_request(SlonHttpSession* session)
@ -467,7 +479,7 @@ U0 @slon_http_handle_post_request(SlonHttpSession* session)
/* clang-format on */
@slon_http_set_status_code(session, 404);
session->status(404);
}
U0 @slon_http_handle_request(SlonHttpSession* session)
@ -483,7 +495,7 @@ U0 @slon_http_handle_request(SlonHttpSession* session)
@slon_http_handle_get_request(session);
break;
case SLON_HTTP_VERB_OPTIONS:
@slon_http_set_status_code(session, 200);
session->status(200);
break;
case SLON_HTTP_VERB_PATCH:
@slon_http_handle_patch_request(session);
@ -492,7 +504,7 @@ U0 @slon_http_handle_request(SlonHttpSession* session)
@slon_http_handle_post_request(session);
break;
default:
@slon_http_set_status_code(session, 405);
session->status(405);
}
}
@ -511,7 +523,7 @@ U0 @slon_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400);
session->status(400);
goto slon_http_task_send_response;
}