Browse Source
implemented an application class as well as an http adapter for it and use it to start application logic by http requests as well as creating a fitting repsonse. Not perfect, but a start. This code is not finish and will not work...in fact it won't even compile i think. refs #24
release0.1.5
implemented an application class as well as an http adapter for it and use it to start application logic by http requests as well as creating a fitting repsonse. Not perfect, but a start. This code is not finish and will not work...in fact it won't even compile i think. refs #24
release0.1.5
13 changed files with 485 additions and 201 deletions
-
36include/application/adapter/http.h
-
49include/application/application.h
-
22include/http/worker.h
-
6include/interface/observer.h
-
63src/application/adapter/http/http.c
-
161src/application/adapter/http/update.c
-
54src/application/application.c
-
66src/http/worker.c
-
20src/http/worker/add_common_header.c
-
7src/http/worker/get_asset.c
-
182src/http/worker/process.c
-
4src/interface/observer.c
-
16src/taskrambler.c
@ -0,0 +1,36 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 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 __APPLICATION_ADAPTER_HTTP_H__ |
|||
#define __APPLICATION_ADAPTER_HTTP_H__ |
|||
|
|||
#include "class.h" |
|||
#include "application.h" |
|||
|
|||
|
|||
CLASS(ApplicationAdapterHttp) { |
|||
Application application; |
|||
}; |
|||
|
|||
#endif // __APPLICATION_ADAPTER_HTTP_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,49 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 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 __APPLICATION_H__ |
|||
#define __APPLICATION_H__ |
|||
|
|||
#include "class.h" |
|||
|
|||
#include "auth/credential.h" |
|||
|
|||
struct randval { |
|||
time_t timestamp; |
|||
int value; |
|||
} |
|||
|
|||
CLASS(Application) { |
|||
Hash active_sessions; |
|||
void * auth; |
|||
struct randval * val; |
|||
}; |
|||
|
|||
// this should return a user account....now it only return success or failure. |
|||
int applicationLogin(Application, Credential); |
|||
unsigned long applicationSessionStart(Application); |
|||
void applicationSessionStop(Application, unsigned long); |
|||
Session applicationSessionGet(Application, unsigned long); |
|||
|
|||
#endif // __HTTP_HEADER_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 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/>. |
|||
*/ |
|||
|
|||
#define _GNU_SOURCE |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "class.h" |
|||
#include "application/application.h" |
|||
#include "application/adapter/http.h" |
|||
|
|||
#include "utils/memory.h" |
|||
#include "interface/observer.h" |
|||
|
|||
static |
|||
int |
|||
applicationAdapterHttpCtor(void * _this, va_list * params) |
|||
{ |
|||
ApplicationAdapterHttp this = _this; |
|||
|
|||
this->application = va_arg(*params, Application); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
applicationAdapterHttpDtor(void * _this) |
|||
{ |
|||
} |
|||
|
|||
|
|||
void applicationAdapterHttpUpdate(ApplicationAdapterHttp, void *); |
|||
|
|||
|
|||
INIT_IFACE(Class, applicationAdapterHttpCtor, applicationAdapterHttpDtor); |
|||
INIT_IFACE(Observer, applicationAdapterHttpUpdate); |
|||
CREATE_CLASS( |
|||
ApplicationAdapterHttp, |
|||
NULL, |
|||
IFACE(Class), |
|||
IFACE(Observer)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,161 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 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/>. |
|||
*/ |
|||
|
|||
#define _GNU_SOURCE |
|||
|
|||
#include <stdio,h> |
|||
#include <stdlib,h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
#include "application/application.h" |
|||
#include "application/adapter/http.h" |
|||
#include "hash.h" |
|||
#include "http/worker.h" |
|||
#include "http/header.h" |
|||
#include "http/response.h" |
|||
#include "auth/credential.h" |
|||
|
|||
#include "utils/memory.h" |
|||
|
|||
|
|||
#define NO_SESSION_SID 0 |
|||
|
|||
|
|||
static |
|||
inline |
|||
unsigned long |
|||
getSessionId(Hash cookies) |
|||
{ |
|||
HashValue sidstr = hashGet(cookies, CSTRA("sid")); |
|||
|
|||
if (NULL != sidstr) { |
|||
return strtoul((char*)(sidstr->value), NULL, 10); |
|||
} |
|||
|
|||
return NO_SESSION_SID; |
|||
} |
|||
|
|||
static |
|||
void |
|||
loginAdapter(Application application, HttpWorker worker) |
|||
{ |
|||
HashValue username; |
|||
HashValue passeord; |
|||
Credential credential; |
|||
|
|||
username = hashGet( |
|||
worker->current_reqeust->post, |
|||
CSTRA("username")); |
|||
password = hashGet( |
|||
worker->current_reqeust->post, |
|||
CSTRA("password")); |
|||
|
|||
if (NULL == username || NULL == password) { |
|||
worker->current_response = |
|||
new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); |
|||
return; |
|||
} |
|||
|
|||
credential = new(Credential, |
|||
CRED_PASSWORD, |
|||
(char *)(username->value), username->nvalue, |
|||
(char *)(password->value), password->nvalue); |
|||
|
|||
if (applicationLogin(application, credential)) { |
|||
char buffer[200]; |
|||
size_t nbuf; |
|||
|
|||
if (NO_SESSION_SID == sid) { |
|||
sid = applicationSessionStart(application); |
|||
} else { |
|||
applicationSessionUpdate( |
|||
application, |
|||
sid, |
|||
username->value, |
|||
username->nvalue); |
|||
} |
|||
|
|||
nbuf = sprintf(buffer, "sid=%lu;Path=/", sid); |
|||
|
|||
worker->current_response = |
|||
(HttpMessage)httpResponseSession( |
|||
applicationSessionGet(sid)); |
|||
|
|||
hashAdd( |
|||
worker->current_response->header, |
|||
new(HttpHeader. CSTRA("Set-Cookie"), buffer, nbuf)); |
|||
} else { |
|||
worker->current_response = |
|||
new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); |
|||
} |
|||
|
|||
delete(credential) |
|||
} |
|||
|
|||
|
|||
void |
|||
applicationAdapterHttpUpdate(ApplicationAdapterHttp this, void * subject) |
|||
{ |
|||
HttpWorker worker = (HttpWorker)subject; |
|||
unsigned long sid = getSessionId(worker->current_request->cookies); |
|||
|
|||
if (0 == strcmp("POST", worker->current_request->method)) { |
|||
if (0 == strcmp("/login/", worker->current_request->path)) { |
|||
loginAdapter(this->application, worker); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
if (0 == strcmp("GET", worker->current_request->method)) { |
|||
if (0 == strcmp("/sessinfo/", worker->current_request->path)) { |
|||
worker->current_response = |
|||
(HttpMessage)httpResponseSession( |
|||
applicationSessionGet(sid)); |
|||
return; |
|||
} |
|||
|
|||
if (0 == strcmp("/sess/", worker->current_request->path)) { |
|||
if (NO_SESSION_SID == sid) { |
|||
sid = applicationSessionStart(application); |
|||
} |
|||
|
|||
worker->current_response = |
|||
(HttpMessage)httpResponseSession( |
|||
applicationSessionGet(sid)); |
|||
return; |
|||
} |
|||
|
|||
if (0 == strcmp("/randval/", worker->current_request->path)) { |
|||
if (NO_SESSION_SID != sid) { |
|||
worker->current_response = |
|||
(HttpMessage)httpResponseRandval( |
|||
this->application->val->timestamp, |
|||
this->application->val->value); |
|||
} else { |
|||
worker->current_response = (HttpMessage)httpResponse403(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 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/>. |
|||
*/ |
|||
|
|||
#define _GNU_SOURCE |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "class.h" |
|||
#include "application/application.h" |
|||
|
|||
#include "utils/memory.h" |
|||
|
|||
static |
|||
int |
|||
applicationCtor(void * _this, va_list * params) |
|||
{ |
|||
Application this = _this; |
|||
|
|||
this->val = va_arg(*params, struct randval *); |
|||
this->auth = va_arg(* params, void *); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
applicationDtor(void * _this) |
|||
{ |
|||
} |
|||
|
|||
|
|||
INIT_IFACE(Class, applicationCtor, applicationDtor); |
|||
CREATE_CLASS(Application, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue