mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 00:34:06 +02:00
session user
This commit is contained in:
parent
f98cce2747
commit
7d1eb9348c
@ -36,6 +36,7 @@ void route_post_users_register(HttpCtx* ctx);
|
|||||||
|
|
||||||
void route_post_sessions_login(HttpCtx* ctx);
|
void route_post_sessions_login(HttpCtx* ctx);
|
||||||
void route_post_sessions_logout(HttpCtx* ctx);
|
void route_post_sessions_logout(HttpCtx* ctx);
|
||||||
|
void route_get_sessions_user(HttpCtx* ctx);
|
||||||
|
|
||||||
const Session* header_session(HttpCtx* ctx);
|
const Session* header_session(HttpCtx* ctx);
|
||||||
const Session* middleware_session(HttpCtx* ctx);
|
const Session* middleware_session(HttpCtx* ctx);
|
||||||
|
@ -63,6 +63,28 @@ void route_post_sessions_logout(HttpCtx* ctx)
|
|||||||
RESPOND_JSON(ctx, 200, "{\"ok\":true}");
|
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)
|
const Session* header_session(HttpCtx* ctx)
|
||||||
{
|
{
|
||||||
Cx* cx = http_ctx_user_ctx(ctx);
|
Cx* cx = http_ctx_user_ctx(ctx);
|
||||||
|
@ -89,6 +89,8 @@ int http_server_listen(HttpServer* server)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("accepted\n");
|
||||||
|
|
||||||
Client req = { .file = res, client_addr };
|
Client req = { .file = res, client_addr };
|
||||||
pthread_mutex_lock(&ctx->mutex);
|
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);
|
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);
|
string_destroy(&res);
|
||||||
}
|
}
|
||||||
@ -281,7 +285,8 @@ static inline void worker_handle_request(Worker* worker, Client* client)
|
|||||||
MAX_HEADER_BUFFER_SIZE);
|
MAX_HEADER_BUFFER_SIZE);
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
}
|
||||||
// puts((char*)buffer);
|
puts("\nRequest:");
|
||||||
|
puts((char*)buffer);
|
||||||
|
|
||||||
Req req;
|
Req req;
|
||||||
size_t body_idx;
|
size_t body_idx;
|
||||||
@ -457,10 +462,24 @@ static inline void req_destroy(Req* req)
|
|||||||
header_vec_destroy(&req->headers);
|
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)
|
static inline bool req_has_header(const Req* req, const char* key)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < req->headers.size; ++i) {
|
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;
|
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)
|
static inline const char* req_get_header(const Req* req, const char* key)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < req->headers.size; ++i) {
|
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;
|
return req->headers.data[i].value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,7 @@ int main(void)
|
|||||||
http_server_post(server, "/api/sessions/login", route_post_sessions_login);
|
http_server_post(server, "/api/sessions/login", route_post_sessions_login);
|
||||||
http_server_post(
|
http_server_post(
|
||||||
server, "/api/sessions/logout", route_post_sessions_logout);
|
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_get(server, "/", route_get_index);
|
||||||
http_server_post(server, "/set_number", route_post_set_number);
|
http_server_post(server, "/set_number", route_post_set_number);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user