Commit 865ac097 authored by Sergey Lyubka's avatar Sergey Lyubka

started to work on sessions

parent b5d7c34b
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
#include "mongoose.h" #include "mongoose.h"
#define MAX_USER_LEN 20
#define MAX_MESSAGE_LEN 100
#define MAX_MESSAGES 5
#define MAX_SESSIONS 2
static const char *login_url = "/login.html"; static const char *login_url = "/login.html";
static const char *authorize_url = "/authorize"; static const char *authorize_url = "/authorize";
static const char *web_root = "./html"; static const char *web_root = "./html";
...@@ -37,13 +42,23 @@ static const char *ajax_reply_start = ...@@ -37,13 +42,23 @@ static const char *ajax_reply_start =
// the message is then originated from the server itself. // the message is then originated from the server itself.
struct message { struct message {
long id; long id;
char user[20]; char user[MAX_USER_LEN];
char text[200]; char text[MAX_MESSAGE_LEN];
time_t utc_timestamp; time_t utc_timestamp;
}; };
static struct message messages[5]; // Ringbuffer where messages are kept // Describes web session.
struct session {
char session_id[33];
char authenticated_user[MAX_USER_LEN];
time_t expiration_timestamp_utc;
};
static struct message messages[MAX_MESSAGES]; // Ringbuffer for messages
static struct session sessions[MAX_SESSIONS]; // Current sessions
static long last_message_id; static long last_message_id;
// Protects messages, sessions, last_message_id
static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
// Get a get of messages with IDs greater than last_id and transform them // Get a get of messages with IDs greater than last_id and transform them
...@@ -208,9 +223,9 @@ static int must_authorize(const struct mg_request_info *request_info) { ...@@ -208,9 +223,9 @@ static int must_authorize(const struct mg_request_info *request_info) {
strcmp(request_info->uri, authorize_url) != 0); strcmp(request_info->uri, authorize_url) != 0);
} }
static int process_request(struct mg_connection *conn, static enum mg_error_t process_request(struct mg_connection *conn,
const struct mg_request_info *request_info) { const struct mg_request_info *request_info) {
int processed = 1; enum mg_error_t processed = MG_SUCCESS;
if (must_authorize(request_info) && if (must_authorize(request_info) &&
!is_authorized(conn, request_info)) { !is_authorized(conn, request_info)) {
...@@ -225,7 +240,7 @@ static int process_request(struct mg_connection *conn, ...@@ -225,7 +240,7 @@ static int process_request(struct mg_connection *conn,
} else { } else {
// No suitable handler found, mark as not processed. Mongoose will // No suitable handler found, mark as not processed. Mongoose will
// try to serve the request. // try to serve the request.
processed = 0; processed = MG_ERROR;
} }
return processed; return processed;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment