mirror of
https://github.com/sigmasternchen/CShore
synced 2025-03-15 08:08:56 +00:00
added basic session stuff management
This commit is contained in:
parent
6565658d07
commit
cbe3920b12
2 changed files with 125 additions and 0 deletions
102
src/sessions.c
Normal file
102
src/sessions.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <uuid.h>
|
||||
|
||||
#include <headers.h>
|
||||
|
||||
#include "sessions.h"
|
||||
|
||||
struct session {
|
||||
bool inUse;
|
||||
uuid_t id;
|
||||
time_t lastAccess;
|
||||
void* data;
|
||||
}* sessions = NULL;
|
||||
static size_t sessionno = 0;
|
||||
|
||||
#define SESSION_BLOCK_SIZE (128)
|
||||
|
||||
int resizeSessionList() {
|
||||
// TODO synchronization
|
||||
struct session* tmp = realloc(session, sizeof(struct session) * (sessionno + SESSION_BLOCK_SIZE));
|
||||
if (tmp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(tmp + sizeof(struct session) * sessionno, 0, sizeof(struct session) * SESSION_BLOCK_SIZE);
|
||||
sessions = tmp;
|
||||
sessionno += SESSION_BLOCK_SIZE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct session* newSession(size_t size) {
|
||||
for (size_t i = 0; i < sessionno; i++) {
|
||||
if (!sessions[i].inUse) {
|
||||
// TODO synchronization
|
||||
sessions[i].inUse = true;
|
||||
sessions[i].data = malloc(size);
|
||||
if (sessions[i].data == NULL) {
|
||||
sessions[i].inUse = false;
|
||||
return NULL;
|
||||
}
|
||||
return &(sessions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// no free session slot
|
||||
if (resizeSessionList() < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return newSession(size);
|
||||
}
|
||||
|
||||
struct session* findSession(uuid_t id) {
|
||||
for (size_t i = 0; i < sessionno; i++) {
|
||||
if (!sessions[i].inUse) {
|
||||
continue;
|
||||
}
|
||||
if (uuid_compare(sessions[i].id, id) == 0) {
|
||||
return &(sessions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* _session_start(ctx_t* ctx, const char* cookie, size_t size) {
|
||||
char* cookieValue = getCookie(ctx, cookie);
|
||||
|
||||
bool isValid = false;
|
||||
uuid_t id;
|
||||
struct session* session = NULL;
|
||||
|
||||
if (cookieValue != NULL) {
|
||||
if (uuid_parse(cookieValue, id) == 0) {
|
||||
isValid = true;
|
||||
}
|
||||
|
||||
free(cookieValue);
|
||||
} else {
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
if (isValid) {
|
||||
session = findSession(id);
|
||||
}
|
||||
|
||||
if (session == NULL) {
|
||||
session = newSession(size);
|
||||
if (session == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uuid_generate_time(session->id);
|
||||
}
|
||||
|
||||
session->lastAccess = time(NULL);
|
||||
|
||||
return session->data;
|
||||
}
|
23
src/sessions.h
Normal file
23
src/sessions.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef SESSIONS_H_
|
||||
#define SESSIONS_H_
|
||||
|
||||
#ifndef SESSION_PTR_TYPE
|
||||
#warning "session ptr type not defined"
|
||||
#define SESSION_PTR_TYPE int
|
||||
#endif
|
||||
|
||||
#ifndef SESSION_LENGTH
|
||||
#define SESSION_LENGTH (24*60*60*1000)
|
||||
#endif
|
||||
|
||||
#ifndef SESSION_COOKIE_NAME
|
||||
#define SESSION_COOKIE_NAME "cshore_session"
|
||||
#endif
|
||||
|
||||
#include "request.h"
|
||||
|
||||
void* _session_start(ctx_t*, const char*, size_t);
|
||||
|
||||
#define session_start(c) (SESSION_PTR_TYPE*) _session_start(c, SESSION_COOKIE_NAME, sizeof(SESSION_PTR_TYPE))
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue