diff --git a/backend/src/controllers.h b/backend/src/controllers.h index a7fdc84..6376a08 100644 --- a/backend/src/controllers.h +++ b/backend/src/controllers.h @@ -36,6 +36,7 @@ void route_post_users_register(HttpCtx* ctx); void route_post_sessions_login(HttpCtx* ctx); void route_post_sessions_logout(HttpCtx* ctx); +void route_get_sessions_user(HttpCtx* ctx); const Session* header_session(HttpCtx* ctx); const Session* middleware_session(HttpCtx* ctx); diff --git a/backend/src/controllers/sessions.c b/backend/src/controllers/sessions.c index c96542a..230c7c7 100644 --- a/backend/src/controllers/sessions.c +++ b/backend/src/controllers/sessions.c @@ -63,6 +63,28 @@ void route_post_sessions_logout(HttpCtx* ctx) RESPOND_JSON(ctx, 200, "{\"ok\":true}"); } +void route_get_sessions_user(HttpCtx* ctx) +{ + printf("1\n"); + Cx* cx = http_ctx_user_ctx(ctx); + const Session* session = middleware_session(ctx); + if (!session) + return; + + User user; + DbRes db_res = db_user_from_id(cx->db, &user, session->user_id); + if (db_res != DbRes_Ok) { + RESPOND_BAD_REQUEST(ctx, "user not found"); + return; + } + + char* user_json = user_to_json_string(&user); + user_destroy(&user); + + RESPOND_JSON(ctx, 200, "{\"ok\":true,\"user\":%s}", user_json); + free(user_json); +} + const Session* header_session(HttpCtx* ctx) { Cx* cx = http_ctx_user_ctx(ctx); diff --git a/backend/src/http_server.c b/backend/src/http_server.c index 1fb6fdc..0d254ea 100644 --- a/backend/src/http_server.c +++ b/backend/src/http_server.c @@ -89,6 +89,8 @@ int http_server_listen(HttpServer* server) return -1; } + printf("accepted\n"); + Client req = { .file = res, client_addr }; pthread_mutex_lock(&ctx->mutex); @@ -181,7 +183,9 @@ void http_ctx_respond(HttpCtx* ctx, int status, const char* body) string_push_str(&res, body); - send(ctx->client->file, res.data, res.size, 0); + write(ctx->client->file, res.data, res.size); + puts("\nResponse:"); + puts(res.data); string_destroy(&res); } @@ -281,7 +285,8 @@ static inline void worker_handle_request(Worker* worker, Client* client) MAX_HEADER_BUFFER_SIZE); goto l0_return; } - // puts((char*)buffer); + puts("\nRequest:"); + puts((char*)buffer); Req req; size_t body_idx; @@ -457,10 +462,24 @@ static inline void req_destroy(Req* req) header_vec_destroy(&req->headers); } +static inline int strcmp_lower(const char* a, const char* b) +{ + size_t i = 0; + for (; a[i] != '\0' && b[i] != '\0'; ++i) { + if (tolower(a[i]) != tolower(b[i])) { + return 1; + } + } + if (a[i] != b[i]) { + return 1; + } + return 0; +} + static inline bool req_has_header(const Req* req, const char* key) { for (size_t i = 0; i < req->headers.size; ++i) { - if (strcmp(key, req->headers.data[i].key) == 0) { + if (strcmp_lower(key, req->headers.data[i].key) == 0) { return true; } } @@ -470,7 +489,7 @@ static inline bool req_has_header(const Req* req, const char* key) static inline const char* req_get_header(const Req* req, const char* key) { for (size_t i = 0; i < req->headers.size; ++i) { - if (strcmp(key, req->headers.data[i].key) == 0) { + if (strcmp_lower(key, req->headers.data[i].key) == 0) { return req->headers.data[i].value; } } diff --git a/backend/src/main.c b/backend/src/main.c index 44660d7..2990f2d 100644 --- a/backend/src/main.c +++ b/backend/src/main.c @@ -47,6 +47,7 @@ int main(void) http_server_post(server, "/api/sessions/login", route_post_sessions_login); http_server_post( server, "/api/sessions/logout", route_post_sessions_logout); + http_server_get(server, "/api/sessions/user", route_get_sessions_user); http_server_get(server, "/", route_get_index); http_server_post(server, "/set_number", route_post_set_number);