Browse Source
closes #11: first very crude session handling implementation, no persitence no memory cleanups, spread over to much files TODO: clean this
master
closes #11: first very crude session handling implementation, no persitence no memory cleanups, spread over to much files TODO: clean this
master
13 changed files with 475 additions and 19 deletions
-
2include/http/response.h
-
3include/http/worker.h
-
47include/session.h
-
5src/Makefile.am
-
63src/http/response/403.c
-
77src/http/response/login_form.c
-
34src/http/response/me.c
-
41src/http/worker.c
-
78src/http/worker/process.c
-
46src/session.c
-
36src/session/add.c
-
23src/session/delete.c
-
29src/session/get.c
@ -0,0 +1,47 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright (C) 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __SESSION_H__ |
|||
#define __SESSION_H__ |
|||
|
|||
#include <time.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
#define SESSION_LIVETIME 30 |
|||
|
|||
|
|||
CLASS(Session) { |
|||
unsigned long id; |
|||
time_t livetime; |
|||
|
|||
char * username; |
|||
}; |
|||
|
|||
Session sessionAdd(const Session *, Session); |
|||
Session sessionGet(const Session *, const unsigned long id); |
|||
void sessionDelete(const Session *, const unsigned long id); |
|||
|
|||
#endif // __SESSION_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright (C) 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
#include "http/response.h" |
|||
#include "http/message.h" |
|||
#include "http/header.h" |
|||
|
|||
|
|||
HttpResponse |
|||
httpResponse403() |
|||
{ |
|||
HttpResponse response; |
|||
HttpMessage message; |
|||
char buffer[200]; |
|||
size_t nbuf; |
|||
|
|||
response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); |
|||
message = (HttpMessage)response; |
|||
|
|||
message->type = HTTP_MESSAGE_BUFFERED; |
|||
message->nbody = 0; |
|||
message->body = NULL; |
|||
|
|||
nbuf = sprintf(buffer, "%d", message->nbody); |
|||
|
|||
httpHeaderAdd(&(message->header), |
|||
new(HttpHeader, |
|||
"Content-Length", |
|||
sizeof("Content-Length")-1, |
|||
buffer, |
|||
nbuf)); |
|||
|
|||
return response; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,77 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright (C) 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include <time.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
#include "http/response.h" |
|||
#include "http/message.h" |
|||
#include "http/header.h" |
|||
|
|||
#define RESP_DATA "<form action=\"/me/\" method=\"POST\">" \ |
|||
"<input name=\"username\" type=\"text\" />" \ |
|||
"<input type=\"submit\">" \ |
|||
"</form>" |
|||
|
|||
HttpResponse |
|||
httpResponseLoginForm() |
|||
{ |
|||
char buffer[200]; |
|||
HttpResponse response; |
|||
HttpMessage message; |
|||
size_t nbuf; |
|||
|
|||
response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
|||
message = (HttpMessage)response; |
|||
|
|||
httpHeaderAdd(&(message->header), |
|||
new(HttpHeader, |
|||
"Content-Type", |
|||
sizeof("Content-Type")-1, |
|||
"text/html", |
|||
sizeof("text/html")-1)); |
|||
|
|||
message->type = HTTP_MESSAGE_BUFFERED; |
|||
|
|||
message->nbody = sizeof(RESP_DATA)-1; |
|||
message->body = malloc(message->nbody); |
|||
memcpy(message->body, RESP_DATA, message->nbody); |
|||
|
|||
nbuf = sprintf(buffer, "%d", message->nbody); |
|||
|
|||
httpHeaderAdd(&(message->header), |
|||
new(HttpHeader, |
|||
"Content-Length", |
|||
sizeof("Content-Length")-1, |
|||
buffer, |
|||
nbuf)); |
|||
|
|||
return response; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,46 @@ |
|||
#include <time.h> |
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "session.h" |
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
#include "utils/hash.h" |
|||
#include "utils/memory.h" |
|||
|
|||
|
|||
static |
|||
int |
|||
sessionCtor(void * _this, va_list * params) |
|||
{ |
|||
Session this = _this; |
|||
char * uname = va_arg(* params, char *); |
|||
size_t nuname = va_arg(* params, size_t); |
|||
|
|||
this->livetime = time(NULL) + SESSION_LIVETIME; |
|||
this->id = sdbm((unsigned char *)uname, nuname) & this->livetime; |
|||
|
|||
this->username = malloc(nuname + 1); |
|||
this->username[nuname] = 0; |
|||
memcpy(this->username, uname, nuname); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
sessionDtor(void * _this) |
|||
{ |
|||
Session this = _this; |
|||
|
|||
FREE(this->username); |
|||
} |
|||
|
|||
INIT_IFACE(Class, sessionCtor, sessionDtor, NULL); |
|||
CREATE_CLASS(Session, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,36 @@ |
|||
#include <search.h> |
|||
|
|||
#include "session.h" |
|||
#include "interface/class.h" |
|||
|
|||
|
|||
static |
|||
inline |
|||
int |
|||
sessionAddComp(const void * _a, const void * _b) |
|||
{ |
|||
Session a = (Session)_a; |
|||
Session b = (Session)_b; |
|||
return (a->id < b->id)? -1 : (a->id > b->id)? 1 : 0; |
|||
} |
|||
|
|||
Session |
|||
sessionAdd(const Session * root, Session session) |
|||
{ |
|||
Session * found = tsearch(session, (void**)root, sessionAddComp); |
|||
|
|||
if (NULL == found) { |
|||
return NULL; |
|||
} |
|||
|
|||
if (*found != session) { |
|||
/** |
|||
* \todo this should not happen, so do some logging here. |
|||
*/ |
|||
delete(session); |
|||
} |
|||
|
|||
return *found; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,23 @@ |
|||
#include <search.h> |
|||
|
|||
#include "session.h" |
|||
#include "interface/class.h" |
|||
|
|||
|
|||
static |
|||
inline |
|||
int |
|||
sessionDeleteComp(const void * _a, const void * _b) |
|||
{ |
|||
unsigned long a = *(unsigned long *)_a; |
|||
Session b = (Session)_b; |
|||
return (a < b->id)? -1 : (a > b->id)? 1 : 0; |
|||
} |
|||
|
|||
void |
|||
sessionDelete(const Session * root, const unsigned long id) |
|||
{ |
|||
tdelete(&id, (void**)root, sessionDeleteComp); |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,29 @@ |
|||
#include <search.h> |
|||
#include <time.h> |
|||
|
|||
#include "session.h" |
|||
|
|||
|
|||
static |
|||
inline |
|||
int |
|||
sessionGetComp(const void * _a, const void * _b) |
|||
{ |
|||
unsigned long a = *(unsigned long *)_a; |
|||
Session b = (Session)_b; |
|||
return (a < b->id)? -1 : (a > b->id)? 1 : 0; |
|||
} |
|||
|
|||
Session |
|||
sessionGet(const Session * root, const unsigned long id) |
|||
{ |
|||
Session * found = tfind(&id, (void**)root, sessionGetComp); |
|||
|
|||
if (NULL == found) { |
|||
return NULL; |
|||
} |
|||
|
|||
return *found; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue