From 61f8dc58e1507ee37c81818098b98a294cd9b62f Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Wed, 21 Mar 2012 09:05:23 +0100 Subject: [PATCH] add latest stuff from parent project yabrog. Essentially some request variable handling and authentication interface with ldap implementation. Additionally add docBlocks to all C source and header files and make a real copyright sign from the (C). --- .gitignore | 3 +- TODO | 8 ++ assets/html/main.html | 50 ++++++++ assets/{ => image}/waldschrat.jpg | Bin assets/js/init.js | 37 ++++++ assets/{ => js}/jquery-1.7.1.js | 0 assets/{ => js}/jquery-1.7.1.min.js | 0 assets/js/serverval.js | 78 +++++++++++++ assets/js/session.js | 78 +++++++++++++ assets/style/common.css | 58 ++++++++++ include/auth.h | 45 ++++++++ include/auth/ldap.h | 41 +++++++ include/cbuf.h | 2 +- include/class.h | 2 +- include/commons.h | 38 +++++++ include/credential.h | 54 +++++++++ include/hash.h | 42 +++++++ include/hash_value.h | 42 +++++++ include/http/cookie.h | 51 +++++++++ include/http/header.h | 4 +- include/http/message.h | 7 +- include/http/message/queue.h | 2 +- include/http/parser.h | 5 +- include/http/request.h | 8 +- include/http/response.h | 4 +- include/http/worker.h | 4 +- include/http/writer.h | 2 +- include/interface.h | 2 +- include/interface/auth.h | 49 ++++++++ include/interface/class.h | 2 +- include/interface/hashable.h | 45 ++++++++ include/interface/http_intro.h | 2 +- include/interface/logger.h | 2 +- include/interface/observer.h | 2 +- include/interface/stream_reader.h | 2 +- include/interface/stream_writer.h | 2 +- include/interface/subject.h | 2 +- include/logger.h | 2 +- include/server.h | 2 +- include/session.h | 2 +- include/socket.h | 2 +- include/stream.h | 22 ++++ include/utils/hash.h | 2 +- include/utils/http.h | 22 ++++ include/utils/memory.h | 2 +- include/utils/signalHandling.h | 2 +- src/Makefile.am | 23 ++-- src/auth/ldap.c | 110 ++++++++++++++++++ src/cbuf.c | 2 +- src/cbuf/addr_index.c | 2 +- src/cbuf/empty.c | 2 +- src/cbuf/get_data.c | 2 +- src/cbuf/get_free.c | 2 +- src/cbuf/get_line.c | 2 +- src/cbuf/get_read.c | 2 +- src/cbuf/get_write.c | 2 +- src/cbuf/inc_read.c | 2 +- src/cbuf/inc_write.c | 2 +- src/cbuf/is_empty.c | 2 +- src/cbuf/is_locked.c | 2 +- src/cbuf/lock.c | 2 +- src/cbuf/memchr.c | 2 +- src/cbuf/read.c | 2 +- src/cbuf/release.c | 2 +- src/cbuf/set_data.c | 2 +- src/cbuf/skip_non_alpha.c | 2 +- src/cbuf/write.c | 2 +- src/credential.c | 86 ++++++++++++++ src/hash.c | 64 +++++++++++ src/{http/header => hash}/add.c | 41 +++---- src/hash/delete.c | 47 ++++++++ src/hash/each.c | 47 ++++++++ src/{http/header => hash}/get.c | 26 ++--- src/hash_value.c | 109 ++++++++++++++++++ src/http/cookie.c | 104 +++++++++++++++++ src/http/header.c | 33 +++++- src/http/header/to_string.c | 2 +- src/http/message.c | 25 ++-- src/http/message/get_version.c | 2 +- src/http/message/has_keep_alive.c | 5 +- src/http/message/has_valid_version.c | 2 +- src/http/message/header_size_get.c | 11 +- src/http/message/header_to_string.c | 13 ++- src/http/message/queue.c | 2 +- src/http/parser.c | 2 +- src/http/parser/body.c | 4 +- src/http/parser/header.c | 44 +++++++- src/http/parser/new_message.c | 22 ++++ src/http/parser/parse.c | 29 ++++- src/http/parser/post_vars.c | 72 ++++++++++++ src/http/parser/request_vars.c | 71 ++++++++++++ src/http/request.c | 11 +- src/http/request/has_valid_method.c | 2 +- src/http/response.c | 2 +- src/http/response/304.c | 9 +- src/http/response/403.c | 2 +- src/http/response/404.c | 5 +- src/http/response/asset.c | 10 +- src/http/response/login_form.c | 5 +- src/http/response/me.c | 151 ------------------------- src/http/response/randval.c | 5 +- src/http/response/session.c | 71 ++++++++++++ src/http/worker.c | 5 +- src/http/worker/add_common_header.c | 36 ++++-- src/http/worker/get_asset.c | 27 ++++- src/http/worker/process.c | 163 ++++++++++++++++++++------- src/http/worker/write.c | 2 +- src/http/writer.c | 2 +- src/http/writer/write.c | 6 +- src/interface.c | 2 +- src/interface/auth.c | 42 +++++++ src/interface/class.c | 2 +- src/interface/hashable.c | 51 +++++++++ src/interface/http_intro.c | 2 +- src/interface/logger.c | 2 +- src/interface/observer.c | 2 +- src/interface/stream_reader.c | 2 +- src/interface/stream_writer.c | 2 +- src/interface/subject.c | 2 +- src/logger.c | 2 +- src/logger/stderr.c | 2 +- src/logger/syslog.c | 2 +- src/server.c | 22 ++-- src/server/close_conn.c | 7 +- src/server/handle_accept.c | 8 +- src/server/poll.c | 3 +- src/server/read.c | 2 +- src/server/run.c | 2 +- src/server/write.c | 2 +- src/session.c | 24 +++- src/session/add.c | 22 ++++ src/session/delete.c | 22 ++++ src/session/get.c | 22 ++++ src/socket.c | 2 +- src/socket/accept.c | 2 +- src/socket/connect.c | 2 +- src/socket/listen.c | 2 +- src/stream.c | 22 ++++ src/stream/read.c | 22 ++++ src/stream/write.c | 22 ++++ src/taskrambler.c | 13 ++- src/utils/daemonize.c | 2 +- src/utils/hash.c | 23 ++++ src/utils/http.c | 22 ++++ src/utils/memory.c | 22 ++++ src/utils/signalHandling.c | 2 +- 146 files changed, 2372 insertions(+), 418 deletions(-) create mode 100644 assets/html/main.html rename assets/{ => image}/waldschrat.jpg (100%) create mode 100644 assets/js/init.js rename assets/{ => js}/jquery-1.7.1.js (100%) rename assets/{ => js}/jquery-1.7.1.min.js (100%) create mode 100644 assets/js/serverval.js create mode 100644 assets/js/session.js create mode 100644 assets/style/common.css create mode 100644 include/auth.h create mode 100644 include/auth/ldap.h create mode 100644 include/credential.h create mode 100644 include/hash.h create mode 100644 include/hash_value.h create mode 100644 include/http/cookie.h create mode 100644 include/interface/auth.h create mode 100644 include/interface/hashable.h create mode 100644 src/auth/ldap.c create mode 100644 src/credential.c create mode 100644 src/hash.c rename src/{http/header => hash}/add.c (50%) create mode 100644 src/hash/delete.c create mode 100644 src/hash/each.c rename src/{http/header => hash}/get.c (56%) create mode 100644 src/hash_value.c create mode 100644 src/http/cookie.c create mode 100644 src/http/parser/post_vars.c create mode 100644 src/http/parser/request_vars.c delete mode 100644 src/http/response/me.c create mode 100644 src/http/response/session.c create mode 100644 src/interface/auth.c create mode 100644 src/interface/hashable.c diff --git a/.gitignore b/.gitignore index 9c79b2b..6b8107e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,9 @@ .deps/ Makefile Makefile.in +/config* m4/ /docs/ -/config.* -/configure /INSTALL *.m4 /autom4te.cache/ diff --git a/TODO b/TODO index e69de29..6bfe265 100644 --- a/TODO +++ b/TODO @@ -0,0 +1,8 @@ +VERY BIG TODO: +- right now i will use long polling ajax calls when feedback from to the client + is needed. In the long term this should be changed to websockets (ws). But + right now ws specification is not final anyway. :) + +- handle errors after all system call...especially open, close, etc. + +- IPV6 support diff --git a/assets/html/main.html b/assets/html/main.html new file mode 100644 index 0000000..a61e444 --- /dev/null +++ b/assets/html/main.html @@ -0,0 +1,50 @@ + + + + + My own little Web-App + + + + + + + + +
+ Session:
+ +
+
+
+ +
+ +
+ +
+
+
+ + Value created at:
+
+ Next value in:
+
+ Value: +
+
+

Testpage

+ Welcome!!!
+ +
+
+
+ + + + diff --git a/assets/waldschrat.jpg b/assets/image/waldschrat.jpg similarity index 100% rename from assets/waldschrat.jpg rename to assets/image/waldschrat.jpg diff --git a/assets/js/init.js b/assets/js/init.js new file mode 100644 index 0000000..b36c539 --- /dev/null +++ b/assets/js/init.js @@ -0,0 +1,37 @@ +var sess = null; + +$(document).ready(function() { + var sval = new ServerVal("#randval"); + + sess = new Session("#sessinfo"); + + $(window).focus(function() { + $.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess)); + }); + + $("ul#menu li:eq(0)").click(function() { + sval.start(); + }); + + $("ul#menu li:eq(1)").click(function() { + $.getJSON("/sess/", $.proxy(sess.loadJSON, sess)); + }); + + $("ul#menu li:eq(2)").click(function() { + $("#login").removeClass("hide"); + }); + + $("#randval").click(function() { + sval.stop(); + }); + + $("#login form").submit(function(event) { + event.preventDefault(); + $.post("/login/", + $("#login form").serialize(), + $.proxy(sess.loadJSON, sess)); + $("#login").addClass("hide"); + }); +}); + +// vim: set ts=4 sw=4: diff --git a/assets/jquery-1.7.1.js b/assets/js/jquery-1.7.1.js similarity index 100% rename from assets/jquery-1.7.1.js rename to assets/js/jquery-1.7.1.js diff --git a/assets/jquery-1.7.1.min.js b/assets/js/jquery-1.7.1.min.js similarity index 100% rename from assets/jquery-1.7.1.min.js rename to assets/js/jquery-1.7.1.min.js diff --git a/assets/js/serverval.js b/assets/js/serverval.js new file mode 100644 index 0000000..bdb848b --- /dev/null +++ b/assets/js/serverval.js @@ -0,0 +1,78 @@ +function ServerVal(eId) +{ + this.eId = eId; + this.eCtime = eId + " span:eq(1)"; + this.eVnext = eId + " span:eq(2)"; + this.eValue = eId + " span:eq(3)"; + + this.interval = null; + this.ctime = null; + this.vnext = 0; + this.value = null; +} + +ServerVal.prototype.loadJSON = function(data) +{ + this.ctime = new Date(data.ctime * 1000); + this.vnext = data.vnext; + this.value = data.value; + + $.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess)); + this.show(); +} + +ServerVal.prototype.show = function() +{ + $(this.eCtime).empty().append(this.ctime.toString()); + $(this.eVnext).empty().append(this.vnext); + $(this.eValue).empty().append(this.value); + + if ($(this.eId).hasClass("hide")) { + $(this.eId).removeClass("hide"); + } +} + +ServerVal.prototype.start = function() +{ + if (null === this.interval) { + this.interval = setInterval($.proxy(this.process, this), 1000); + } +} + +ServerVal.prototype.process = function() +{ + if (0 >= this.vnext) { + $.getJSON("/randval/", $.proxy(this.loadJSON, this)) + .error($.proxy(function(xhr) { + this.stop(); + $("#msg").append("AJAX error (" + xhr.status + "): "); + switch(xhr.status) { + case 403: + $("#msg").append( + "Please log in to access this function.
"); + break; + + default: + $("#msg").append( + "Unhandled - " + xhr.responseText + "
"); + break; + } + }, this)); + } + + else { + this.vnext--; + $(this.eVnext).empty().append(this.vnext); + } +} + +ServerVal.prototype.stop = function() +{ + $(this.eId).addClass("hide"); + + clearInterval(this.interval); + this.interval = null; + this.vnext = 0; +} + +// vim: set ts=4 sw=4: diff --git a/assets/js/session.js b/assets/js/session.js new file mode 100644 index 0000000..0c4350b --- /dev/null +++ b/assets/js/session.js @@ -0,0 +1,78 @@ +function Session(sId) +{ + this.eSid = $(sId + " span"); + this.canvas = $(sId + " canvas").get(0); + this.context = this.canvas.getContext("2d"); + + this.id = "none" + this.timeout = 0; + this.timeleft = 0; + this.username = ""; + this.interval = null; + + this.draw(); +} + +Session.prototype.loadJSON = function(data) +{ + this.stop(); + + this.id = ("0" == data.id)? "none" : data.id; + this.timeout = data.timeout * 10; + this.timeleft = data.timeleft * 10; + this.username = data.username; + + this.eSid.empty().append(this.id); + $("#main span:eq(0)").empty().append(" " + this.username); + + this.draw(); + if (0 < this.timeleft) + this.start(); +} + +Session.prototype.draw = function() +{ + this.context.fillStyle = "rgb(255, 0, 0)"; + this.context.fillRect(0, 0, this.canvas.width, this.canvas.height); + + this.context.fillStyle = "rgb(0, 255, 0)"; + this.context.fillRect(0, 0, + this.canvas.width / this.timeout * this.timeleft, + this.canvas.height); +} + +Session.prototype.start = function() +{ + if (null === this.interval) { + this.interval = setInterval($.proxy(this.process, this), 100); + } +} + +Session.prototype.process = function() +{ + if (0 >= this.timeleft) { + this.stop(); + } + + else { + this.timeleft--; + this.draw(); + } +} + +Session.prototype.stop = function() +{ + clearInterval(this.interval); + this.interval = null; + this.id = "none"; + this.timeout = 0; + this.timeleft = 0; + this.username = ""; + + this.eSid.empty().append(this.id); + $("#main span:eq(0)").empty().append(" " + this.username); + + this.draw(); +} + +// vim: set ts=4 sw=4: diff --git a/assets/style/common.css b/assets/style/common.css new file mode 100644 index 0000000..bd3bab4 --- /dev/null +++ b/assets/style/common.css @@ -0,0 +1,58 @@ +div#randval { + left: 200px; + top: 100px; + padding: 10px; + position: fixed; + background-color: white; + border: 1px solid black; + border-radius: 10px; +} + +div#login { + padding: 5px; + position: fixed; + background-color: white; + border: 1px solid black; + border-radius: 10px; +} + +div.hide { + top: -500px !important; +} + +.small { + font-size: small; +} + +.x-small { + font-size: x-small; +} + +ul#menu { + list-style: none inside; + margin: 0px; + padding: 1px 0px 0px; + border-bottom: 1px solid #7b0b2b; + display: inline-block; + width: 100%; +} + +ul#menu li { + padding: 2px; + border-top-left-radius: 10px; + border-top-right-radius: 10px; + border-top: 1px solid #7b0b2b; + border-left: 1px solid #7b0b2b; + border-right: 1px solid #7b0b2b; + text-align: center; + cursor: pointer; + + float: left; + margin-right: 1px; +} + +div#sessinfo canvas { + border: 1px solid black; +} + +/* vim: set st=4 sw=4: */ diff --git a/include/auth.h b/include/auth.h new file mode 100644 index 0000000..a83eb51 --- /dev/null +++ b/include/auth.h @@ -0,0 +1,45 @@ +/** + * \file + * Authenticatio module factory + * + * A factory to get a specific authentication module. + * An authentication module is a class that implement the Auth interface. + * + * \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 . + */ + +#ifndef __AUTH_H__ +#define __AUTH_H__ + +#include "class.h" +#include "auth/ldap.h" + +typedef enum e_AuthModule { + AUTH_LDAP = 0 +} AuthModule; + +CLASS(Auth) { +}; + +void * authCreateById(Auth, int); +AuthLdap authCreateLdap(Auth); + +#endif // __AUTH_H__ + +// vim: set ts=4 sw=4: diff --git a/include/auth/ldap.h b/include/auth/ldap.h new file mode 100644 index 0000000..4c6e98c --- /dev/null +++ b/include/auth/ldap.h @@ -0,0 +1,41 @@ +/** + * \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 . + */ + +#ifndef __AUTH_LDAP_H__ +#define __AUTH_LDAP_H__ + +#include +#include + +#include "class.h" + +CLASS(AuthLdap) { + LDAP * ldap; + char * url; + char * base_dn; + int version; + size_t nbase_dn; +}; + +#endif // __AUTH_LDAP_H__ + +// vim: set ts=4 sw=4: diff --git a/include/cbuf.h b/include/cbuf.h index 053b836..dd2de94 100644 --- a/include/cbuf.h +++ b/include/cbuf.h @@ -11,7 +11,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/class.h b/include/class.h index efecb79..6f34fe9 100644 --- a/include/class.h +++ b/include/class.h @@ -8,7 +8,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/commons.h b/include/commons.h index 876df6a..7f354f7 100644 --- a/include/commons.h +++ b/include/commons.h @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #ifndef __COMMONS_H__ #define __COMMONS_H__ @@ -5,6 +27,22 @@ #define TRUE 1 #define FALSE 0 +#ifndef MAX +# define MAX(a,b) ((a)>(b)? (a) : (b)) +#endif + +#ifndef MIN +# define MIN(a,b) ((a)<(b)? (a) : (b)) +#endif + +#define SWAP_FUN(a, b) ((a)^=(b),(b)^=(a),(a)^=(b)) + +#define SWAP(type, a, b) do { \ + type tmp = (a); \ + (a) = (b); \ + (b) = tmp; \ +} while(0); + #endif // __COMMONS_H__ // vim: set ts=4 sw=4: diff --git a/include/credential.h b/include/credential.h new file mode 100644 index 0000000..945f780 --- /dev/null +++ b/include/credential.h @@ -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 . + */ + +#ifndef __CREDENTIAL_H__ +#define __CREDENTIAL_H__ + +#include + +#include "class.h" + +#define CRED_PWD(c) (((c)->cred).pwd) + +typedef enum e_CredentialType { + CRED_PASSWORD = 0 +} CredentialType; + + +CLASS(Credential) { + CredentialType type; + + union { + + struct { + char * user; + size_t nuser; + char * pass; + size_t npass; + } pwd; + + } cred; +}; + +#endif // __CREDENTIAL_H__ + +// vim: set ts=4 sw=4: diff --git a/include/hash.h b/include/hash.h new file mode 100644 index 0000000..4c6eb92 --- /dev/null +++ b/include/hash.h @@ -0,0 +1,42 @@ +/** + * \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 . + */ + +#ifndef __HASH_H__ +#define __HASH_H__ + +#include + +#include "class.h" + + +CLASS(Hash) { + void * root; +}; + +void * hashAdd(Hash, void *); +void * hashDelete(Hash, const char *, size_t); +void * hashGet(Hash, const char *, size_t); +void hashEach(Hash, void (*)(const void*)); + +#endif // __HASH_H__ + +// vim: set ts=4 sw=4: diff --git a/include/hash_value.h b/include/hash_value.h new file mode 100644 index 0000000..17a4f6c --- /dev/null +++ b/include/hash_value.h @@ -0,0 +1,42 @@ +/** + * \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 . + */ + +#ifndef __HASH_VALUE_H__ +#define __HASH_VALUE_H__ + +#include + +#include "class.h" + +CLASS(HashValue) { + unsigned long hash; + + char * key; + void * value; + + size_t nkey; + size_t nvalue; +}; + +#endif // __HASH_VALUE_H__ + +// vim: set ts=4 sw=4: diff --git a/include/http/cookie.h b/include/http/cookie.h new file mode 100644 index 0000000..358be49 --- /dev/null +++ b/include/http/cookie.h @@ -0,0 +1,51 @@ +/** + * \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 . + */ + +#ifndef __HTTP_COOKIE_H__ +#define __HTTP_COOKIE_H__ + +#include +#include + +#include "class.h" + +CLASS(HttpCookie) { + unsigned long hash; + + char * key; + char * value; + char * domain; + char * path; + + time_t expires; + time_t max_age; + + size_t nkey; + size_t nvalue; +}; + +char * httpCookieToString(HttpCookie); +HttpCookie httpStringToCookie(const char *); + +#endif // __HTTP_COOKIE_H__ + +// vim: set ts=4 sw=4: diff --git a/include/http/header.h b/include/http/header.h index eac9dd8..865ffd7 100644 --- a/include/http/header.h +++ b/include/http/header.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -41,8 +41,6 @@ CLASS(HttpHeader) { size_t size; //!< full size of this header }; -HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader); -HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t); size_t httpHeaderToString(HttpHeader, char *); #endif // __HTTP_HEADER_H__ diff --git a/include/http/message.h b/include/http/message.h index 8a51e16..b85ec93 100644 --- a/include/http/message.h +++ b/include/http/message.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -25,7 +25,7 @@ #define __HTTP_MESSAGE__ #include "class.h" -#include "http/header.h" +#include "hash.h" #include "stream.h" typedef enum e_HttpMessageType { @@ -37,7 +37,8 @@ typedef enum e_HttpMessageType { CLASS(HttpMessage) { char * version; - HttpHeader header; + Hash header; + Hash cookies; HttpMessageType type; Stream handle; diff --git a/include/http/message/queue.h b/include/http/message/queue.h index b4d6052..7b024e0 100644 --- a/include/http/message/queue.h +++ b/include/http/message/queue.h @@ -7,7 +7,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/http/parser.h b/include/http/parser.h index 81a16c0..7ca76e2 100644 --- a/include/http/parser.h +++ b/include/http/parser.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -58,9 +58,12 @@ CLASS(HttpParser) { }; ssize_t httpParserParse(void *, Stream); +void httpParserRequestVars(HttpParser); void httpParserHeader(HttpParser, const char *, const char *); void httpParserNewMessage(HttpParser, const char *, const char * lend); size_t httpParserBody(HttpParser, const char *, size_t); +void httpParserRequestVars(HttpParser); +void httpParserPostVars(HttpParser); #endif // __HTTP_PARSER_H__ diff --git a/include/http/request.h b/include/http/request.h index ba1f979..45eac76 100644 --- a/include/http/request.h +++ b/include/http/request.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -26,6 +26,7 @@ #include "class.h" #include "http/message.h" +#include "hash.h" #define N_HTTP_METHOD 8 @@ -36,6 +37,11 @@ CLASS(HttpRequest) { char * method; char * uri; + char * path; + + Hash get; + Hash post; + Hash cookies; }; int httpRequestHasValidMethod(HttpRequest); diff --git a/include/http/response.h b/include/http/response.h index 67b206e..98818ff 100644 --- a/include/http/response.h +++ b/include/http/response.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -29,6 +29,7 @@ #include "class.h" #include "http/message.h" +#include "session.h" CLASS(HttpResponse) { @@ -47,6 +48,7 @@ HttpResponse httpResponse403(); HttpResponse httpResponseMe(); HttpResponse httpResponseLoginForm(); HttpResponse httpResponseRandval(time_t, int); +HttpResponse httpResponseSession(Session); HttpResponse httpResponseAsset( const char *, const char *, size_t, diff --git a/include/http/worker.h b/include/http/worker.h index 3ffc348..cfc7255 100644 --- a/include/http/worker.h +++ b/include/http/worker.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -52,6 +52,8 @@ CLASS(HttpWorker) { HttpWriter writer; Session session; Session * sroot; + + void * auth; }; #endif // __HTTP_WORKER_H__ diff --git a/include/http/writer.h b/include/http/writer.h index 9c1d143..3fb91bb 100644 --- a/include/http/writer.h +++ b/include/http/writer.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * - Copyright (C) 2012 Georg Hopp + * - 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 diff --git a/include/interface.h b/include/interface.h index f3e3c56..bebe14c 100644 --- a/include/interface.h +++ b/include/interface.h @@ -8,7 +8,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/auth.h b/include/interface/auth.h new file mode 100644 index 0000000..ed552e7 --- /dev/null +++ b/include/interface/auth.h @@ -0,0 +1,49 @@ +/** + * \file + * The authentication interface. + * + * This is the authentication interface. It's only pupose is to + * authenticate someone or somewhat. It is called AUTH. + * The concrete access rights are managed within a class called ACL. + * + * \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 . + */ + +#ifndef __INTERFACE_AUTH_H__ +#define __INTERFACE_AUTH_H__ + +#include + +#include "interface.h" +#include "credential.h" + +typedef int (* fptr_authenticate)(void *, Credential); + +extern const struct interface i_Auth; + +struct i_Auth { + const struct interface * const _; + fptr_authenticate authenticate; +}; + +extern int authenticate(void *, Credential); + +#endif // __INTERFACE_AUTH_H__ + +// vim: set ts=4 sw=4: diff --git a/include/interface/class.h b/include/interface/class.h index 748b16b..b0fd2ad 100644 --- a/include/interface/class.h +++ b/include/interface/class.h @@ -7,7 +7,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/hashable.h b/include/interface/hashable.h new file mode 100644 index 0000000..18b9ae3 --- /dev/null +++ b/include/interface/hashable.h @@ -0,0 +1,45 @@ +/** + * \file + * The logger interface. + * + * \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 . + */ + +#ifndef __INTERFACE_HASHABLE_H__ +#define __INTERFACE_HASHABLE_H__ + +#include "interface.h" + +typedef unsigned long (* fptr_hashableGetHash)(void *); +typedef void (* fptr_hashableHandleDouble)(void *, void *); + +extern const struct interface i_Hashable; + +struct i_Hashable { + const struct interface * const _; + fptr_hashableGetHash getHash; + fptr_hashableHandleDouble handleDouble; +}; + +extern unsigned long hashableGetHash(void *); +extern void hashableHandleDouble(void *, void *); + +#endif // __INTERFACE_HASHABLE_H__ + +// vim: set ts=4 sw=4: diff --git a/include/interface/http_intro.h b/include/interface/http_intro.h index 81b6afe..60edf93 100644 --- a/include/interface/http_intro.h +++ b/include/interface/http_intro.h @@ -7,7 +7,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/logger.h b/include/interface/logger.h index 769ecb4..c55876c 100644 --- a/include/interface/logger.h +++ b/include/interface/logger.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/observer.h b/include/interface/observer.h index ffb1840..077823c 100644 --- a/include/interface/observer.h +++ b/include/interface/observer.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/stream_reader.h b/include/interface/stream_reader.h index 1478e5d..e353a12 100644 --- a/include/interface/stream_reader.h +++ b/include/interface/stream_reader.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/stream_writer.h b/include/interface/stream_writer.h index 3229705..bbca388 100644 --- a/include/interface/stream_writer.h +++ b/include/interface/stream_writer.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/interface/subject.h b/include/interface/subject.h index 62c3b3a..16ef044 100644 --- a/include/interface/subject.h +++ b/include/interface/subject.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/logger.h b/include/logger.h index 26ee2bf..a9577db 100644 --- a/include/logger.h +++ b/include/logger.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/server.h b/include/server.h index 198aac1..0468e39 100644 --- a/include/server.h +++ b/include/server.h @@ -7,7 +7,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/session.h b/include/session.h index 67571f3..02f7aec 100644 --- a/include/session.h +++ b/include/session.h @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/socket.h b/include/socket.h index af68ac9..7e1442f 100644 --- a/include/socket.h +++ b/include/socket.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/stream.h b/include/stream.h index 0bae954..d7cffed 100644 --- a/include/stream.h +++ b/include/stream.h @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #ifndef __STREAM_H__ #define __STREAM_H__ diff --git a/include/utils/hash.h b/include/utils/hash.h index a247590..27c36c5 100644 --- a/include/utils/hash.h +++ b/include/utils/hash.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/utils/http.h b/include/utils/http.h index 38d4511..5e877bb 100644 --- a/include/utils/http.h +++ b/include/utils/http.h @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #ifndef __UTILS_HTTP_H__ #define __UTILS_HTTP_H__ diff --git a/include/utils/memory.h b/include/utils/memory.h index 4755cac..2e9b65a 100644 --- a/include/utils/memory.h +++ b/include/utils/memory.h @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/include/utils/signalHandling.h b/include/utils/signalHandling.h index 269d1fe..cd1c973 100644 --- a/include/utils/signalHandling.h +++ b/include/utils/signalHandling.h @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/Makefile.am b/src/Makefile.am index 7830b46..3abd3d2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,8 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ interface/subject.c interface/observer.c interface.c SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c STREAM = stream.c stream/read.c stream/write.c +HASH = hash.c hash/add.c hash/get.c hash/delete.c \ + hash/each.c interface/hashable.c hash_value.c SERVER = server.c server/run.c server/close_conn.c server/poll.c \ server/handle_accept.c server/read.c server/write.c LOGGER = logger.c logger/stderr.c logger/syslog.c @@ -30,13 +32,15 @@ RESP = http/response.c \ http/response/403.c \ http/response/login_form.c \ http/response/asset.c \ - http/response/me.c \ - http/response/randval.c + http/response/randval.c \ + http/response/session.c PARSER = http/parser.c \ http/parser/parse.c \ http/parser/new_message.c \ http/parser/header.c \ - http/parser/body.c + http/parser/body.c \ + http/parser/request_vars.c \ + http/parser/post_vars.c WRITER = http/writer.c \ http/writer/write.c WORKER = http/worker.c \ @@ -44,7 +48,7 @@ WORKER = http/worker.c \ http/worker/write.c \ http/worker/get_asset.c \ http/worker/add_common_header.c -HEADER = http/header.c http/header/get.c http/header/add.c \ +HEADER = http/header.c \ http/header/to_string.c SESSION = session.c session/add.c session/get.c session/delete.c UTILS = utils/hash.c \ @@ -52,15 +56,16 @@ UTILS = utils/hash.c \ utils/http.c \ utils/daemonize.c \ utils/signalHandling.c +AUTH = interface/auth.c auth/ldap.c credential.c AM_CFLAGS = -Wall -I ../include/ -bin_PROGRAMS = taskrambler +bin_PROGRAMS = webgameserver -taskrambler_SOURCES = taskrambler.c \ +webgameserver_SOURCES = webgameserver.c \ $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ - $(UTILS) $(MSGQ) $(SESSION) $(STREAM) -taskrambler_CFLAGS = -Wall -I ../include/ -taskrambler_LDFLAGS = -lrt -lssl + $(UTILS) $(MSGQ) $(SESSION) $(STREAM) $(HASH) $(AUTH) +webgameserver_CFLAGS = -Wall -I ../include/ +webgameserver_LDFLAGS = -lrt -lssl -lldap diff --git a/src/auth/ldap.c b/src/auth/ldap.c new file mode 100644 index 0000000..fb7bf0e --- /dev/null +++ b/src/auth/ldap.c @@ -0,0 +1,110 @@ +/** + * \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 . + */ + +#include +#include +#include +#include +#include + +#include "auth/ldap.h" +#include "class.h" +#include "credential.h" +#include "interface/class.h" +#include "interface/auth.h" + +#include "utils/memory.h" +#include "commons.h" + +static +int +authLdapCtor(void * _this, va_list * params) +{ + AuthLdap this = _this; + char * url = va_arg(*params, char*); + char * base_dn; + + this->url = malloc(strlen(url) + 1); + strcpy(this->url, url); + + this->version = 3; + + base_dn = va_arg(* params, char *); + this->nbase_dn = va_arg(* params, size_t); + + this->base_dn = malloc(this->nbase_dn + 1); + this->base_dn[this->nbase_dn] = 0; + memcpy(this->base_dn, base_dn, this->nbase_dn); + + return 0; +} + +static +void +authLdapDtor(void * _this) +{ + AuthLdap this = _this; + + FREE(this->base_dn); + FREE(this->url); +} + +static +int +authLdapAuthenticate(void * _this, Credential cred) +{ + AuthLdap this = _this; + char who[256]; + char * who_ptr = who; + int ldap_err; + + if (CRED_PASSWORD != cred->type) { + return FALSE; + } + + ldap_initialize(&(this->ldap), this->url); + ldap_set_option(this->ldap, LDAP_OPT_PROTOCOL_VERSION, &(this->version)); + + memcpy(who_ptr, "cn=", sizeof("cn=") - 1); + who_ptr += sizeof("cn=") - 1; + memcpy(who_ptr, CRED_PWD(cred).user, CRED_PWD(cred).nuser); + who_ptr += CRED_PWD(cred).nuser; + *who_ptr++ = ','; + memcpy(who_ptr, this->base_dn, this->nbase_dn); + who_ptr[this->nbase_dn] = 0; + + ldap_err = ldap_simple_bind_s(this->ldap, who, CRED_PWD(cred).pass); + if (0 == ldap_err) { + ldap_unbind_s(this->ldap); + //! \todo here we need to get and return the user id + return TRUE; + } + + fprintf(stderr, "%s\n", ldap_err2string(ldap_err)); + return FALSE; +} + +INIT_IFACE(Class, authLdapCtor, authLdapDtor, NULL); +INIT_IFACE(Auth, authLdapAuthenticate); +CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth)); + +// vim: set ts=4 sw=4: diff --git a/src/cbuf.c b/src/cbuf.c index 4d05fa5..fdf77db 100644 --- a/src/cbuf.c +++ b/src/cbuf.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/addr_index.c b/src/cbuf/addr_index.c index f66df81..69eb5e6 100644 --- a/src/cbuf/addr_index.c +++ b/src/cbuf/addr_index.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/empty.c b/src/cbuf/empty.c index 02196de..b41a6a3 100644 --- a/src/cbuf/empty.c +++ b/src/cbuf/empty.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/get_data.c b/src/cbuf/get_data.c index c5191f3..b41f5b8 100644 --- a/src/cbuf/get_data.c +++ b/src/cbuf/get_data.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/get_free.c b/src/cbuf/get_free.c index eb747fd..1117081 100644 --- a/src/cbuf/get_free.c +++ b/src/cbuf/get_free.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/get_line.c b/src/cbuf/get_line.c index 526c22a..647722f 100644 --- a/src/cbuf/get_line.c +++ b/src/cbuf/get_line.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/get_read.c b/src/cbuf/get_read.c index 6464b08..cbbbdc3 100644 --- a/src/cbuf/get_read.c +++ b/src/cbuf/get_read.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/get_write.c b/src/cbuf/get_write.c index dbd2dc3..02f1a55 100644 --- a/src/cbuf/get_write.c +++ b/src/cbuf/get_write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/inc_read.c b/src/cbuf/inc_read.c index 869a780..bfd6dab 100644 --- a/src/cbuf/inc_read.c +++ b/src/cbuf/inc_read.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/inc_write.c b/src/cbuf/inc_write.c index 1221493..f329813 100644 --- a/src/cbuf/inc_write.c +++ b/src/cbuf/inc_write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/is_empty.c b/src/cbuf/is_empty.c index 799ced6..849c032 100644 --- a/src/cbuf/is_empty.c +++ b/src/cbuf/is_empty.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/is_locked.c b/src/cbuf/is_locked.c index fa5a2c3..097bd69 100644 --- a/src/cbuf/is_locked.c +++ b/src/cbuf/is_locked.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/lock.c b/src/cbuf/lock.c index e1579e4..df5256f 100644 --- a/src/cbuf/lock.c +++ b/src/cbuf/lock.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/memchr.c b/src/cbuf/memchr.c index 58894fa..0183351 100644 --- a/src/cbuf/memchr.c +++ b/src/cbuf/memchr.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/read.c b/src/cbuf/read.c index a68eb07..1e49fa0 100644 --- a/src/cbuf/read.c +++ b/src/cbuf/read.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/release.c b/src/cbuf/release.c index 8a3edac..eb32a73 100644 --- a/src/cbuf/release.c +++ b/src/cbuf/release.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/set_data.c b/src/cbuf/set_data.c index 0e3055e..514d052 100644 --- a/src/cbuf/set_data.c +++ b/src/cbuf/set_data.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/skip_non_alpha.c b/src/cbuf/skip_non_alpha.c index 72ef3aa..395c296 100644 --- a/src/cbuf/skip_non_alpha.c +++ b/src/cbuf/skip_non_alpha.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/cbuf/write.c b/src/cbuf/write.c index 01c26b1..03820aa 100644 --- a/src/cbuf/write.c +++ b/src/cbuf/write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/credential.c b/src/credential.c new file mode 100644 index 0000000..873877b --- /dev/null +++ b/src/credential.c @@ -0,0 +1,86 @@ +/** + * \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 . + */ + +#include +#include +#include +#include + +#include "credential.h" +#include "class.h" +#include "interface/class.h" + +#include "utils/memory.h" + +static +int +credentialCtor(void * _this, va_list * params) +{ + Credential this = _this; + + this->type = va_arg(* params, CredentialType); + + switch(this->type) { + case CRED_PASSWORD: + { + char * user, *pass; + + user = va_arg(* params, char*); + CRED_PWD(this).nuser = va_arg(* params, size_t); + pass = va_arg(* params, char*); + CRED_PWD(this).npass = va_arg(* params, size_t); + + CRED_PWD(this).user = malloc(CRED_PWD(this).nuser + 1); + CRED_PWD(this).user[CRED_PWD(this).nuser] = 0; + memcpy(CRED_PWD(this).user, user, CRED_PWD(this).nuser); + + CRED_PWD(this).pass = malloc(CRED_PWD(this).npass + 1); + CRED_PWD(this).pass[CRED_PWD(this).npass] = 0; + memcpy(CRED_PWD(this).pass, pass, CRED_PWD(this).npass); + } + break; + + default: + return -1; + } + + return 0; +} + +static +void +credentialDtor(void * _this) +{ + Credential this = _this; + + switch(this->type) { + case CRED_PASSWORD: + FREE(CRED_PWD(this).user); + FREE(CRED_PWD(this).pass); + break; + } +} + +INIT_IFACE(Class, credentialCtor, credentialDtor, NULL); +CREATE_CLASS(Credential, NULL, IFACE(Class)); + +// vim: set ts=4 sw=4: diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..8956144 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,64 @@ +/** + * \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 . + */ + +#define _GNU_SOURCE + +#include +#include + +#include "hash.h" +#include "class.h" +#include "interface/class.h" + +static +int +hashCtor(void * _this, va_list * params) +{ + return 0; +} + +static +inline +void +tDelete(void * node) +{ + delete(node); +} + +static +void +hashDtor(void * _this) +{ + Hash this = _this; + + /** + * this is a GNU extension...anyway on most non + * GNUish systems i would not use tsearch anyway + * as the trees will be unbalanced. + */ + tdestroy(this->root, tDelete); +} + +INIT_IFACE(Class, hashCtor, hashDtor, NULL); +CREATE_CLASS(Hash, NULL, IFACE(Class)); + +// vim: set ts=4 sw=4: diff --git a/src/http/header/add.c b/src/hash/add.c similarity index 50% rename from src/http/header/add.c rename to src/hash/add.c index b8eda6a..5226862 100644 --- a/src/http/header/add.c +++ b/src/hash/add.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -21,49 +21,34 @@ */ #include -#include -#include -#include -#include "class.h" +#include "hash.h" +#include "interface/hashable.h" #include "interface/class.h" -#include "http/header.h" -#include "utils/hash.h" static inline int -comp(const void * _a, const void * _b) +hashAddComp(const void * a, const void * b) { - HttpHeader a = (HttpHeader)_a; - HttpHeader b = (HttpHeader)_b; - return (a->hash < b->hash)? -1 : (a->hash > b->hash)? 1 : 0; + return hashableGetHash((void*)b) - hashableGetHash((void*)a); } -HttpHeader -httpHeaderAdd(const HttpHeader * root, HttpHeader header) +void * +hashAdd(Hash this, void * operand) { - HttpHeader * _found = tsearch(header, (void **)root, comp); - HttpHeader found; + void * found = tsearch(operand, &(this->root), hashAddComp); - if (NULL == _found) { + if (NULL == found) { return NULL; } - found = *_found; - - if (found != header) { - if (found->cvalue >= N_VALUES) { - return NULL; - } - (found->nvalue)[found->cvalue] = (header->nvalue)[0]; - (found->value)[(found->cvalue)++] = (header->value)[0]; - found->size += header->size; - (header->value)[0] = NULL; - delete(header); + if (operand != *(void**)found) { + hashableHandleDouble(*(void**)found, operand); + delete(operand); } - return found; + return *(void**)found; } // vim: set ts=4 sw=4: diff --git a/src/hash/delete.c b/src/hash/delete.c new file mode 100644 index 0000000..76dd374 --- /dev/null +++ b/src/hash/delete.c @@ -0,0 +1,47 @@ +/** + * \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 . + */ + +#include +#include + +#include "hash.h" +#include "interface/hashable.h" +#include "utils/hash.h" + +static +inline +int +hashDeleteComp(const void * a, const void * b) +{ + return hashableGetHash((void*)b) - *(const unsigned long*)a; +} + +void * +hashDelete(Hash this, const char * search, size_t nsearch) +{ + unsigned long hash = sdbm((const unsigned char *)search, nsearch); + void * found = tfind(&hash, &(this->root), hashDeleteComp); + + return (NULL != found)? *(void**)found : NULL; +} + +// vim: set ts=4 sw=4: diff --git a/src/hash/each.c b/src/hash/each.c new file mode 100644 index 0000000..4df28d1 --- /dev/null +++ b/src/hash/each.c @@ -0,0 +1,47 @@ +/** + * \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 . + */ + +#include + +#include "hash.h" + +static void (*cb)(void*); + +static +inline +void +walk(const void * node, const VISIT which, const int depth) +{ + if (endorder == which || leaf == which) { + cb(*(void**)node); + } +} + +void +hashEach(Hash this, void (*callback)(const void*)) +{ + cb = callback; + + twalk(this->root, walk); +} + +// vim: set ts=4 sw=4: diff --git a/src/http/header/get.c b/src/hash/get.c similarity index 56% rename from src/http/header/get.c rename to src/hash/get.c index 81c24ba..44240cb 100644 --- a/src/http/header/get.c +++ b/src/hash/get.c @@ -1,12 +1,10 @@ /** * \file - * Get a header from a tree containing headers by its name. - * The key for the tree is the hased lowercase header identifier. * * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -23,29 +21,27 @@ */ #include -#include +#include -#include "http/header.h" +#include "hash.h" +#include "interface/hashable.h" #include "utils/hash.h" static inline int -comp(const void * _a, const void * _b) +hashGetComp(const void * a, const void * b) { - const unsigned long * a = _a; - HttpHeader b = (HttpHeader)_b; - return (*a < b->hash)? -1 : (*a > b->hash)? 1 : 0; + return hashableGetHash((void*)b) - *(const unsigned long*)a; } -HttpHeader -httpHeaderGet(const HttpHeader * root, const char * name, size_t nname) +void * +hashGet(Hash this, const char * search, size_t nsearch) { - unsigned long hash = sdbm((const unsigned char*)name, nname); + unsigned long hash = sdbm((const unsigned char *)search, nsearch); + void * found = tfind(&hash, &(this->root), hashGetComp); - HttpHeader * found = tfind(&hash, (void**)root, comp); - - return (NULL != found)? *found : NULL; + return (NULL != found)? *(void**)found : NULL; } // vim: set ts=4 sw=4: diff --git a/src/hash_value.c b/src/hash_value.c new file mode 100644 index 0000000..c69a169 --- /dev/null +++ b/src/hash_value.c @@ -0,0 +1,109 @@ +/** + * \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 . + */ + +#include +#include +#include +#include + +#include "hash_value.h" +#include "utils/hash.h" +#include "utils/memory.h" +#include "commons.h" +#include "interface/class.h" +#include "interface/hashable.h" + +static +int +hashValueCtor(void * _this, va_list * params) +{ + HashValue this = _this; + char * key = va_arg(* params, char*); + void * value; + + this->nkey = va_arg(* params, size_t); + value = va_arg(* params, void*); + this->nvalue = va_arg(* params, size_t); + + this->key = malloc(this->nkey + 1); + this->key[this->nkey] = 0; + memcpy(this->key, key, this->nkey); + + this->hash = sdbm((unsigned char *)this->key, this->nkey); + + if (NULL != value) { + this->value = malloc(this->nvalue + 1); + ((char*)this->value)[this->nvalue] = 0; + memcpy(this->value, value, this->nvalue); + } + + return 0; +} + +static +void +hashValueDtor(void * _this) +{ + HashValue this = _this; + + FREE(this->key); + FREE(this->value); +} + +static +unsigned long +hashValueGetHash(void * _this) +{ + HashValue this = _this; + + return this->hash; +} + +static +void +hashValueHandleDouble(void * _this, void * _double) +{ + HashValue this = _this; + HashValue doub = _double; + void * tmp_value; + size_t tmp_nvalue; + + /** + * here we swap the internal data of both objects, + * effectively overwriting the old entry. We need not + * to free anything here as _double will be deleted + * afterwards anyway (\see hash/add.c). + */ + tmp_value = this->value; + this->value = doub->value; + doub->value = tmp_value; + + tmp_nvalue = this->nvalue; + this->nvalue = doub->nvalue; + doub->nvalue = tmp_nvalue; +} + +INIT_IFACE(Class, hashValueCtor, hashValueDtor, NULL); +INIT_IFACE(Hashable, hashValueGetHash, hashValueHandleDouble); +CREATE_CLASS(HashValue, NULL, IFACE(Class), IFACE(Hashable)); + +// vim: set ts=4 sw=4: diff --git a/src/http/cookie.c b/src/http/cookie.c new file mode 100644 index 0000000..412f463 --- /dev/null +++ b/src/http/cookie.c @@ -0,0 +1,104 @@ +/** + * \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 . + */ + +#include +#include +#include +#include + +#include "cookie.h" +#include "interface/class.h" +#include "interface/hashable" + +#include "utils/hash.h" +#include "utils/memory.h" +#include "commons.h" + + +static +int +httpCookieCtor(void * _this, va_list * params) +{ + HttpCookie this = _this; + char * key = va_arg(* params, char*); + char * value; + + this->nkey = va_arg(* params, size_t); + value = va_arg(* params, char*); + this->nvalue = va_arg(* params, size_t); + + this->key = malloc(this->nkey + 1); + this->key[this->nkey] = 0; + memcpy(this->key, key, this->nkey); + + this->value = malloc(this->nvalue + 1); + this->value[this->nvalue] = 0; + memcpy(this->value, value, this->nvalue); + + this->hash = sdbm((unsigned char *)key, nkey); + + return 0; +} + +static +void +httpCookieDtor(void * _this, va_list * params) +{ + HttpCookie this = _this; + + FREE(this->key); + FREE(this->value); + FREE(this->domain); + FREE(this->path); +} + +static +unsigned long +httpCookieGetHash(void * _this) +{ + HttpCookie this = _this; + + return this->hash; +} + +static +void +httpCookieHandleDouble(void * _this, void * _double) +{ + HttpCookie this = _this; + HttpCookie doub = _double; + + SWAP(char*, this->key, doub->key); + SWAP(char*, this->value, doub->value); + SWAP(char*, this->domain, doub->domain); + SWAP(char*, this->path, doub->path); + + SWAP(char*, this->nkey, doub->nkey); + SWAP(char*, this->nvalue, doub->nvalue); +} + + +INIT_IFACE(Class, httpCookieCtor, httpCookieDtor, NULL); +INIT_IFACE(Hashable, httpCookieGetHash, httpCookieHandleDouble); +CREATE_CLASS(HttpCookie, NULL, IFACE(Class), IFACE(Hashable)); + +// vim: set ts=4 sw=4: diff --git a/src/http/header.c b/src/http/header.c index 344ee87..999b878 100644 --- a/src/http/header.c +++ b/src/http/header.c @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -27,6 +27,7 @@ #include "class.h" #include "interface/class.h" #include "http/header.h" +#include "interface/hashable.h" #include "utils/hash.h" #include "utils/memory.h" @@ -72,7 +73,35 @@ httpHeaderDtor(void * _this) } } +static +unsigned long +httpHeaderGetHash(void * _this) +{ + HttpHeader this = _this; + + return this->hash; +} + +static +void +httpHeaderHandleDouble(void * _this, void * _double) +{ + HttpHeader this = _this; + HttpHeader doub = _double; + + if (this->cvalue >= N_VALUES) { + //! \todo do dome error logging...or change to HEAP + return; + } + + (this->nvalue)[this->cvalue] = (doub->nvalue)[0]; + (this->value)[(this->cvalue)++] = (doub->value)[0]; + this->size += doub->size; + (doub->value)[0] = NULL; +} + INIT_IFACE(Class, httpHeaderCtor, httpHeaderDtor, NULL); -CREATE_CLASS(HttpHeader, NULL, IFACE(Class)); +INIT_IFACE(Hashable, httpHeaderGetHash, httpHeaderHandleDouble); +CREATE_CLASS(HttpHeader, NULL, IFACE(Class), IFACE(Hashable)); // vim: set ts=4 sw=4: diff --git a/src/http/header/to_string.c b/src/http/header/to_string.c index 124e804..089ee51 100644 --- a/src/http/header/to_string.c +++ b/src/http/header/to_string.c @@ -5,7 +5,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/message.c b/src/http/message.c index 3b3c063..611f9de 100644 --- a/src/http/message.c +++ b/src/http/message.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -32,19 +32,11 @@ #include "class.h" #include "interface/class.h" - +#include "hash.h" #include "http/message.h" #include "utils/memory.h" -static -inline -void -tDelete(void * node) -{ - delete(node); -} - static int httpMessageCtor(void * _this, va_list * params) @@ -55,6 +47,9 @@ httpMessageCtor(void * _this, va_list * params) this->version = calloc(1, strlen(version)+1); strcpy(this->version, version); + this->header = new(Hash); + this->cookies = new(Hash); + return 0; } @@ -64,14 +59,10 @@ httpMessageDtor(void * _this) { HttpMessage this = _this; - FREE(this->version); + delete(this->header); + delete(this->cookies); - /** - * this is a GNU extension...anyway on most non - * GNUish systems i would not use tsearch anyway - * as the trees will be unbalanced. - */ - tdestroy(this->header, tDelete); + FREE(this->version); switch (this->type) { case HTTP_MESSAGE_BUFFERED: diff --git a/src/http/message/get_version.c b/src/http/message/get_version.c index be83528..0db7d99 100644 --- a/src/http/message/get_version.c +++ b/src/http/message/get_version.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/message/has_keep_alive.c b/src/http/message/has_keep_alive.c index 24623e4..136b673 100644 --- a/src/http/message/has_keep_alive.c +++ b/src/http/message/has_keep_alive.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -31,6 +31,7 @@ #include "utils/memory.h" #include "commons.h" +#include "hash.h" char httpMessageHasKeepAlive(HttpMessage message) @@ -39,7 +40,7 @@ httpMessageHasKeepAlive(HttpMessage message) size_t size; char * value; - header = httpHeaderGet(&(message->header), CSTRA("connection")); + header = hashGet(message->header, CSTRA("connection")); if (NULL == header) { return 0; diff --git a/src/http/message/has_valid_version.c b/src/http/message/has_valid_version.c index 1195995..c72e7b1 100644 --- a/src/http/message/has_valid_version.c +++ b/src/http/message/has_valid_version.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/message/header_size_get.c b/src/http/message/header_size_get.c index e46ab25..5388db5 100644 --- a/src/http/message/header_size_get.c +++ b/src/http/message/header_size_get.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -28,17 +28,16 @@ #include "http/response.h" #include "http/header.h" #include "interface/http_intro.h" +#include "hash.h" static size_t size; static inline void -addHeaderSize(const void * node, const VISIT which, const int depth) +addHeaderSize(const void * node) { - if (endorder == which || leaf == which) { - size += (*(HttpHeader *)node)->size; - } + size += ((HttpHeader)node)->size; } size_t @@ -46,7 +45,7 @@ httpMessageHeaderSizeGet(HttpMessage message) { size = httpIntroSizeGet(message); - twalk(message->header, addHeaderSize); + hashEach(message->header, addHeaderSize); size += 2; return size; diff --git a/src/http/message/header_to_string.c b/src/http/message/header_to_string.c index ba223ab..1b615cb 100644 --- a/src/http/message/header_to_string.c +++ b/src/http/message/header_to_string.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -27,15 +27,16 @@ #include "http/response.h" #include "http/header.h" #include "interface/http_intro.h" +#include "hash.h" static char * string; +static +inline void -addHeaderString(const void * node, const VISIT which, const int depth) +addHeaderString(const void * node) { - if (endorder == which || leaf == which) { - string += httpHeaderToString(*(HttpHeader *)node, string); - } + string += httpHeaderToString((HttpHeader)node, string); } char * @@ -45,7 +46,7 @@ httpMessageHeaderToString(HttpMessage response, char * _string) string = httpIntroToString(response, _string); - twalk(message->header, addHeaderString); + hashEach(message->header, addHeaderString); *string++ = '\r'; *string++ = '\n'; diff --git a/src/http/message/queue.c b/src/http/message/queue.c index f6c3ff5..ceb4496 100644 --- a/src/http/message/queue.c +++ b/src/http/message/queue.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/parser.c b/src/http/parser.c index d294a67..be34cce 100644 --- a/src/http/parser.c +++ b/src/http/parser.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/parser/body.c b/src/http/parser/body.c index 6901c8d..07b3162 100644 --- a/src/http/parser/body.c +++ b/src/http/parser/body.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -28,7 +28,7 @@ #include "http/parser.h" #include "cbuf.h" -#define MIN(a,b) (((a) < (b))? (a) : (b)) +#include "commons.h" size_t httpParserBody(HttpParser this, const char * buf, size_t nbuf) diff --git a/src/http/parser/header.c b/src/http/parser/header.c index 320f1cc..0944305 100644 --- a/src/http/parser/header.c +++ b/src/http/parser/header.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -29,6 +29,9 @@ #include "http/header.h" #include "http/parser.h" #include "http/message.h" +#include "http/request.h" +#include "hash.h" +#include "hash_value.h" void httpParserHeader( @@ -59,8 +62,43 @@ httpParserHeader( current->dbody = 0; } - httpHeaderAdd( - &(current->header), + if (0 == strncasecmp("cookie", name, nname-1)) { + HttpRequest request = (HttpRequest)this->current; + char * pair = value; + ssize_t togo = lend - value; + + while(NULL != pair && 0 < togo) { + char * key = pair; + char * eqsign; + char * val; + size_t nval; + + for (; *key == ' ' && key < lend; key++, togo--); + eqsign = memchr(key, '=', togo); + + if (NULL == eqsign) { + break; + } + + togo -= (eqsign - key); + pair = memchr(eqsign, ';', togo); + + if (NULL == pair) { + pair = (char *)lend; + } + + nval = pair-eqsign-1; + val = (0 != nval)? eqsign+1 : NULL; + + hashAdd(request->cookies, + new(HashValue, key, eqsign-key, val, nval)); + + pair++; + togo -= (pair - eqsign); + } + } + + hashAdd(current->header, new(HttpHeader, name, nname, value, lend - value)); } diff --git a/src/http/parser/new_message.c b/src/http/parser/new_message.c index 5c392fc..e265a9c 100644 --- a/src/http/parser/new_message.c +++ b/src/http/parser/new_message.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include "http/parser.h" #include "utils/http.h" diff --git a/src/http/parser/parse.c b/src/http/parser/parse.c index 9bef455..397c352 100644 --- a/src/http/parser/parse.c +++ b/src/http/parser/parse.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -23,11 +23,18 @@ #include #include "http/parser.h" +#include "http/header.h" #include "interface/class.h" #include "interface/http_intro.h" #include "cbuf.h" #include "stream.h" +#include "utils/memory.h" +#include "commons.h" + +#define MIN(a,b) ((a)<(b)? (a) : (b)) + + ssize_t httpParserParse(void * _this, Stream st) { @@ -92,6 +99,7 @@ httpParserParse(void * _this, Stream st) this->ourLock = FALSE; return -1; } + httpParserRequestVars(this); this->state = HTTP_MESSAGE_INTRO_DONE; break; @@ -141,6 +149,23 @@ httpParserParse(void * _this, Stream st) break; case HTTP_MESSAGE_DONE: + { + HttpHeader enc = hashGet( + this->current->header, + CSTRA("content-type")); + + /** + * do we have form data?? + */ + if (NULL != enc && 0 == strncasecmp( + "application/x-www-form-urlencoded", + enc->value[0], + MIN(sizeof("application/x-www-form-urlencoded")-1, + enc->nvalue[0]))) { + //!> then parse them... + httpParserPostVars(this); + } + /** * enqueue current request */ @@ -151,7 +176,7 @@ httpParserParse(void * _this, Stream st) * prepare for next request */ this->state = HTTP_MESSAGE_GARBAGE; - + } break; default: diff --git a/src/http/parser/post_vars.c b/src/http/parser/post_vars.c new file mode 100644 index 0000000..517133f --- /dev/null +++ b/src/http/parser/post_vars.c @@ -0,0 +1,72 @@ +/** + * \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 . + */ + +#include +#include + +#include "http/parser.h" +#include "http/request.h" +#include "hash_value.h" +#include "hash.h" +#include "interface/class.h" + +/** + * \todo this is very similar to other pair parsing + * things... key1=val1key2=val2...keyn=valn + * Generalize this!!!! + */ +void +httpParserPostVars(HttpParser this) +{ + HttpRequest request = (HttpRequest)this->current; + char * pair = this->current->body; + ssize_t togo = this->current->nbody; + + while(NULL != pair && 0 < togo) { + char * key = pair; + char * eqsign = memchr(key, '=', togo); + char * value; + size_t nvalue; + + if (NULL == eqsign) { + return; + } + + togo -= (eqsign - key); + pair = memchr(eqsign, '&', togo); + + if (NULL == pair) { + pair = &(this->current->body[this->current->nbody]); + } + + nvalue = pair-eqsign-1; + value = (0 != nvalue)? eqsign+1 : NULL; + + hashAdd(request->post, + new(HashValue, key, eqsign-key, value, nvalue)); + + pair++; + togo -= (pair - eqsign); + } +} + +// vim: set ts=4 sw=4: diff --git a/src/http/parser/request_vars.c b/src/http/parser/request_vars.c new file mode 100644 index 0000000..d0b47a1 --- /dev/null +++ b/src/http/parser/request_vars.c @@ -0,0 +1,71 @@ +/** + * \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 . + */ + +#include +#include +#include + +#include "http/parser.h" +#include "http/request.h" +#include "hash_value.h" +#include "hash.h" +#include "interface/class.h" + +void +httpParserRequestVars(HttpParser this) +{ + HttpRequest request = (HttpRequest)this->current; + char * delim = strchr(request->uri, '?'); + + if (NULL == delim) { + delim = request->uri + strlen(request->uri); + } + + request->path = malloc(delim - request->uri + 1); + request->path[delim - request->uri] = 0; + memcpy(request->path, request->uri, delim - request->uri); + + while(NULL != delim && 0 != *delim) { + char * key = delim + 1; + char * eqsign = strchr(key, '='); + char * value; + size_t nvalue; + + if (NULL == eqsign) { + return; + } + + delim = strchr(eqsign, '&'); + + if (NULL == delim) { + delim = key + strlen(key); + } + + nvalue = delim-eqsign-1; + value = (0 != nvalue)? eqsign+1 : NULL; + + hashAdd(request->get, + new(HashValue, key, eqsign-key, value, nvalue)); + } +} + +// vim: set ts=4 sw=4: diff --git a/src/http/request.c b/src/http/request.c index 4299a27..4cd4c14 100644 --- a/src/http/request.c +++ b/src/http/request.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -56,6 +56,10 @@ httpRequestCtor(void * _this, va_list * params) this->uri[ulen] = 0; memcpy(this->uri, uri, ulen); + this->get = new(Hash); + this->post = new(Hash); + this->cookies = new(Hash); + return 0; } @@ -65,8 +69,13 @@ httpRequestDtor(void * _this) { HttpRequest this = _this; + delete(this->get); + delete(this->post); + delete(this->cookies); + FREE(this->uri); FREE(this->method); + FREE(this->path); PARENTCALL(_this, Class, dtor); } diff --git a/src/http/request/has_valid_method.c b/src/http/request/has_valid_method.c index 08a02be..c44dcc3 100644 --- a/src/http/request/has_valid_method.c +++ b/src/http/request/has_valid_method.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/response.c b/src/http/response.c index b35e7b9..a53cd0d 100644 --- a/src/http/response.c +++ b/src/http/response.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/response/304.c b/src/http/response/304.c index 28fa1aa..a5ea639 100644 --- a/src/http/response/304.c +++ b/src/http/response/304.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -30,6 +30,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" HttpResponse httpResponse304( @@ -47,11 +48,11 @@ httpResponse304( message->nbody = 0; message->body = NULL; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("ETag"), etag, netag)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); return response; diff --git a/src/http/response/403.c b/src/http/response/403.c index ded0e4d..7256a3e 100644 --- a/src/http/response/403.c +++ b/src/http/response/403.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/response/404.c b/src/http/response/404.c index eb76df7..fd3fae3 100644 --- a/src/http/response/404.c +++ b/src/http/response/404.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -33,6 +33,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "\n" \ "header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/asset.c b/src/http/response/asset.c index 3ee1a01..1ab4937 100644 --- a/src/http/response/asset.c +++ b/src/http/response/asset.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -36,7 +36,7 @@ #include "http/header.h" #include "utils/memory.h" - +#include "hash.h" HttpResponse httpResponseAsset( @@ -74,11 +74,11 @@ httpResponseAsset( message->handle = new(Stream, STREAM_FD, handle); message->nbody = st.st_size; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("ETag"), etag, netag)); - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); return response; diff --git a/src/http/response/login_form.c b/src/http/response/login_form.c index c2f790b..e0e6374 100644 --- a/src/http/response/login_form.c +++ b/src/http/response/login_form.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -34,6 +34,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "
" \ "" \ @@ -49,7 +50,7 @@ httpResponseLoginForm() response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/me.c b/src/http/response/me.c deleted file mode 100644 index 47bff72..0000000 --- a/src/http/response/me.c +++ /dev/null @@ -1,151 +0,0 @@ -/** - * \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 . - */ - -#include -#include -#include -#include -#include - -#include "class.h" -#include "interface/class.h" - -#include "http/response.h" -#include "http/message.h" -#include "http/header.h" - -#include "utils/memory.h" - - -#define RESP_DATA "\n" \ - "\n" \ - "" \ - "" \ - "My own little Web-App" \ - "" \ - "" \ - "" \ - "" \ - "" \ - "
" \ - "" \ - "Value created at:

" \ - "Next value in:
" \ - "
" \ - "Value: " \ - "
" \ - "
" \ - "

Testpage

" \ - "Welcome %s
" \ - "" \ - "
Link" \ - "
" \ - "
" \ - "" \ - "" - -HttpResponse -httpResponseMe(char * uname) -{ - HttpResponse response; - HttpMessage message; - - response = new(HttpResponse, "HTTP/1.1", 200, "OK"); - message = (HttpMessage)response; - - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("name=Georg+Hopp"))); - httpHeaderAdd(&(message->header), - new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("profession=coder"))); - - message->type = HTTP_MESSAGE_BUFFERED; - message->nbody = sizeof(RESP_DATA)-1-2+strlen(uname); //!< the two are the %s - message->body = malloc(message->nbody+1); - sprintf(message->body, RESP_DATA, uname); - //memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)-1); - - return response; -} - -// vim: set ts=4 sw=4: diff --git a/src/http/response/randval.c b/src/http/response/randval.c index 059effa..d9eb0b7 100644 --- a/src/http/response/randval.c +++ b/src/http/response/randval.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -34,6 +34,7 @@ #include "http/header.h" #include "utils/memory.h" +#include "hash.h" #define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}" @@ -49,7 +50,7 @@ httpResponseRandval(time_t ctime, int value) response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; - httpHeaderAdd(&(message->header), + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); message->type = HTTP_MESSAGE_BUFFERED; diff --git a/src/http/response/session.c b/src/http/response/session.c new file mode 100644 index 0000000..924fa87 --- /dev/null +++ b/src/http/response/session.c @@ -0,0 +1,71 @@ +/** + * \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 . + */ + +#include +#include +#include +#include +#include + +#include "class.h" +#include "interface/class.h" + +#include "http/response.h" +#include "http/message.h" +#include "http/header.h" +#include "session.h" + +#include "utils/memory.h" +#include "hash.h" + +#define RESP_DATA "{\"id\":\"%lu\",\"timeout\":%d,\"timeleft\":%ld,\"username\":\"%s\"}" + +HttpResponse +httpResponseSession(Session session) +{ + char buffer[200]; + HttpResponse response; + HttpMessage message; + size_t nbuf; + + response = new(HttpResponse, "HTTP/1.1", 200, "OK"); + message = (HttpMessage)response; + + hashAdd(message->header, + new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); + + message->type = HTTP_MESSAGE_BUFFERED; + + nbuf = sprintf(buffer, RESP_DATA, + (NULL != session)? session->id : 0, + (NULL != session)? SESSION_LIVETIME : 0, + (NULL != session)? session->livetime - time(NULL) : 0, + (NULL != session)? session->username : ""); + + message->nbody = nbuf; + message->body = malloc(nbuf); + memcpy(message->body, buffer, nbuf); + + return response; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/worker.c b/src/http/worker.c index b4dfc1a..5b8c36d 100644 --- a/src/http/worker.c +++ b/src/http/worker.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -64,6 +64,8 @@ httpWorkerCtor(void * _this, va_list * params) this->sroot = &(this->session); + this->auth = va_arg(* params, void *); + return 0; } @@ -106,6 +108,7 @@ httpWorkerClone(void * _this, void * _base) this->writer = new(HttpWriter, base->wbuf); this->sroot = &(base->session); + this->auth = base->auth; } ssize_t httpWorkerProcess(void *, Stream); diff --git a/src/http/worker/add_common_header.c b/src/http/worker/add_common_header.c index 36fbf6d..99472cf 100644 --- a/src/http/worker/add_common_header.c +++ b/src/http/worker/add_common_header.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include @@ -5,9 +27,11 @@ #include "interface/class.h" #include "http/message.h" +#include "http/header.h" #include "http/response.h" #include "utils/memory.h" +#include "hash.h" void httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) @@ -18,17 +42,15 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) size_t nbuf; if (httpMessageHasKeepAlive(request)) { - httpHeaderAdd( - &(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Connection"), CSTRA("Keep-Alive"))); } else { - httpHeaderAdd( - &(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Connection"), CSTRA("Close"))); } - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Server"), CSTRA("testserver"))); switch(((HttpResponse)response)->status) { @@ -37,14 +59,14 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) default: nbuf = sprintf(buffer, "%d", response->nbody); - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf)); } t = time(NULL); tmp = localtime(&t); nbuf = strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); - httpHeaderAdd(&(response->header), + hashAdd(response->header, new(HttpHeader, CSTRA("Date"), buffer, nbuf)); } diff --git a/src/http/worker/get_asset.c b/src/http/worker/get_asset.c index 180729b..36f69ce 100644 --- a/src/http/worker/get_asset.c +++ b/src/http/worker/get_asset.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include "http/header.h" @@ -6,6 +28,7 @@ #include "http/response.h" #include "utils/memory.h" +#include "hash.h" HttpMessage httpWorkerGetAsset( @@ -18,8 +41,8 @@ httpWorkerGetAsset( size_t nmatch; HttpHeader header; - header = httpHeaderGet( - &(((HttpMessage)request)->header), + header = hashGet( + ((HttpMessage)request)->header, CSTRA("If-None-Match")); if (NULL == header) { diff --git a/src/http/worker/process.c b/src/http/worker/process.c index f7a65e9..29ccbd7 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -24,11 +24,15 @@ #include #include #include +#include +#include #include "class.h" #include "interface/class.h" +#include "interface/auth.h" #include "http/worker.h" +#include "http/header.h" #include "http/message.h" #include "http/request.h" #include "http/response.h" @@ -36,12 +40,19 @@ #include "http/parser.h" #include "session.h" #include "stream.h" +#include "hash_value.h" +#include "hash.h" +#include "credential.h" #include "utils/memory.h" +#include "hash.h" +#include "commons.h" + HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); + ssize_t httpWorkerProcess(HttpWorker this, Stream st) { @@ -56,25 +67,20 @@ httpWorkerProcess(HttpWorker this, Stream st) HttpMessage rmessage = reqq->msgs[i]; HttpRequest request = (HttpRequest)(reqq->msgs[i]); HttpMessage response = NULL; - HttpHeader cookie = httpHeaderGet( - &(rmessage->header), - CSTRA("cookie")); - - if (NULL == this->session && NULL != cookie) { - int i; - for (i=0; icvalue; i++) { - char * sidstr = strstr(cookie->value[i], "sid"); + /** + * \todo store the cookie count in the request to make a simple + * check possible to prevent this lookup if no cookies exists + * at all + */ + if (NULL == this->session) { + HashValue sidstr = hashGet(request->cookies, CSTRA("sid")); if (NULL != sidstr) { unsigned long sid; - sidstr = strchr(sidstr, '=')+1; - sid = strtoul(sidstr, NULL, 10); - + sid = strtoul((char*)(sidstr->value), NULL, 10); this->session = sessionGet(this->sroot, sid); - break; - } } } @@ -88,43 +94,92 @@ httpWorkerProcess(HttpWorker this, Stream st) } if (0 == strcmp("POST", request->method)) { - if (0 == strcmp("/me/", request->uri)) { - char * delim = memchr(rmessage->body, '=', rmessage->nbody); - char * val; - size_t nkey, nval; + if (0 == strcmp("/login/", request->path)) { char buffer[200]; size_t nbuf; - nkey = delim - rmessage->body - 1; - *delim = 0; - val = delim + 1; - nval = rmessage->nbody - (val - rmessage->body); + HashValue username = hashGet(request->post, CSTRA("username")); + HashValue password = hashGet(request->post, CSTRA("password")); + + /** + * \todo This is an application authorization not an HTTP + * authorization...anyway think about sending HTTP 401 + * messages if authorization is required and think about + * sending the credentials via header as described in the + * HTTP protocol. Most likely this will lead to hacky thing + * with javascript as i am not sure how far this is implemented + * within browsers. + * Anyway, for now we simply ignore a failed login within the + * response except that no session is initialized. We send + * an empty 200 OK + */ + if (NULL == password || NULL == username) { + response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); + } + + if (NULL == response) { + Credential cred = new(Credential, + CRED_PASSWORD, + (char*)(username->value), username->nvalue, + (char*)(password->value), password->nvalue); + if (!authenticate(this->auth, cred)) { + response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); + } else { + if (NULL == this->session) { this->session = sessionAdd( this->sroot, - new(Session, val, nval)); - nbuf = sprintf(buffer, "sid=%lu;Path=/", this->session->id); - - response = (HttpMessage)httpResponseMe(this->session->username); - - httpHeaderAdd( - &(response->header), - new(HttpHeader, CSTRA("Set-Cookie"), buffer, nbuf)); + new(Session, + username->value, + username->nvalue)); + } else { + this->session->username = malloc(username->nvalue + 1); + this->session->username[username->nvalue] = 0; + memcpy(this->session->username, + username->value, + username->nvalue); + } + + nbuf = sprintf(buffer, + "sid=%lu;Path=/", + this->session->id); + + response = (HttpMessage)httpResponseSession( + this->session); + + hashAdd(response->header, + new(HttpHeader, + CSTRA("Set-Cookie"), + buffer, nbuf)); + } + delete(cred); + } } } if (0 == strcmp("GET", request->method)) { - if (0 == strcmp("/login/", request->uri)) { - response = (HttpMessage)httpResponseLoginForm(); + if (0 == strcmp("/", request->path)) { + response = httpWorkerGetAsset( + request, + "./assets/html/main.html", + CSTRA("text/html")); + } + + if (0 == strcmp("/sessinfo/", request->path)) { + response = (HttpMessage)httpResponseSession(this->session); } - if (0 == strcmp("/me/", request->uri)) { - char * uname = (NULL != this->session)? this->session->username : ""; - response = (HttpMessage)httpResponseMe(uname); + if (0 == strcmp("/sess/", request->path)) { + if (NULL == this->session) { + this->session = sessionAdd( + this->sroot, + new(Session, NULL, 0)); + } + response = (HttpMessage)httpResponseSession(this->session); } - if (0 == strcmp("/randval/", request->uri)) { + if (0 == strcmp("/randval/", request->path)) { if (NULL != this->session) { response = (HttpMessage)httpResponseRandval( this->val->timestamp, @@ -134,19 +189,47 @@ httpWorkerProcess(HttpWorker this, Stream st) } } - if (0 == strcmp("/image/", request->uri)) { + if (0 == strcmp("/image/me", request->path)) { response = httpWorkerGetAsset( request, - "./assets/waldschrat.jpg", + "./assets/image/waldschrat.jpg", CSTRA("image/jpeg")); } - if (0 == strcmp("/jquery/", request->uri)) { + if (0 == strcmp("/assets/js/jquery", request->path)) { + response = httpWorkerGetAsset( + request, + "./assets/js/jquery-1.7.1.min.js", + CSTRA("text/javascript")); + } + + if (0 == strcmp("/assets/js/serverval", request->path)) { response = httpWorkerGetAsset( request, - "./assets/jquery-1.7.1.min.js", + "./assets/js/serverval.js", CSTRA("text/javascript")); } + + if (0 == strcmp("/assets/js/session", request->path)) { + response = httpWorkerGetAsset( + request, + "./assets/js/session.js", + CSTRA("text/javascript")); + } + + if (0 == strcmp("/assets/js/init", request->path)) { + response = httpWorkerGetAsset( + request, + "./assets/js/init.js", + CSTRA("text/javascript")); + } + + if (0 == strcmp("/assets/style/common", request->path)) { + response = httpWorkerGetAsset( + request, + "./assets/style/common.css", + CSTRA("text/css")); + } } if (NULL == response) { diff --git a/src/http/worker/write.c b/src/http/worker/write.c index ca1889c..1316414 100644 --- a/src/http/worker/write.c +++ b/src/http/worker/write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/writer.c b/src/http/writer.c index 00104e9..a2f94ca 100644 --- a/src/http/writer.c +++ b/src/http/writer.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/http/writer/write.c b/src/http/writer/write.c index abf46d8..9639eea 100644 --- a/src/http/writer/write.c +++ b/src/http/writer/write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -30,8 +30,8 @@ #include "cbuf.h" #include "stream.h" -#define MIN(x,y) ((x) < (y) ? (x) : (y)) -#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#include "commons.h" + ssize_t httpWriterWrite(void * _this, Stream st) diff --git a/src/interface.c b/src/interface.c index 04ef14a..3131ab7 100644 --- a/src/interface.c +++ b/src/interface.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/auth.c b/src/interface/auth.c new file mode 100644 index 0000000..bc2e1bc --- /dev/null +++ b/src/interface/auth.c @@ -0,0 +1,42 @@ +/** + * \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 . + */ + +#include "auth.h" +#include "credential.h" +#include "interface/auth.h" + +const struct interface i_Auth = { + "auth", + 1 +}; + +int +authenticate(void * auth, Credential cred) +{ + int ret; + + RETCALL(auth, Auth, authenticate, ret, cred); + + return ret; +} + +// vim: set ts=4 sw=4: diff --git a/src/interface/class.c b/src/interface/class.c index 8327e89..2909a68 100644 --- a/src/interface/class.c +++ b/src/interface/class.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/hashable.c b/src/interface/hashable.c new file mode 100644 index 0000000..7e4b280 --- /dev/null +++ b/src/interface/hashable.c @@ -0,0 +1,51 @@ +/** + * \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 . + */ + +#include +#include +#include + +#include "class.h" +#include "interface/hashable.h" + +const struct interface i_Hashable = { + "hashable", + 2 +}; + +unsigned long +hashableGetHash(void * hashable) +{ + unsigned long ret; + + RETCALL(hashable, Hashable, getHash, ret); + + return ret; +} + +void +hashableHandleDouble(void * hashable, void * new_hashable) +{ + CALL(hashable, Hashable, handleDouble, new_hashable); +} + +// vim: set ts=4 sw=4: diff --git a/src/interface/http_intro.c b/src/interface/http_intro.c index 2f12eb0..bf14c4d 100644 --- a/src/interface/http_intro.c +++ b/src/interface/http_intro.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/logger.c b/src/interface/logger.c index bb2a75e..84e3dd9 100644 --- a/src/interface/logger.c +++ b/src/interface/logger.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/observer.c b/src/interface/observer.c index b2424e7..7775c6b 100644 --- a/src/interface/observer.c +++ b/src/interface/observer.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/stream_reader.c b/src/interface/stream_reader.c index c23895e..ffaf49c 100644 --- a/src/interface/stream_reader.c +++ b/src/interface/stream_reader.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/stream_writer.c b/src/interface/stream_writer.c index 8489a0d..d3153dc 100644 --- a/src/interface/stream_writer.c +++ b/src/interface/stream_writer.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/interface/subject.c b/src/interface/subject.c index 4ee3f4a..831553e 100644 --- a/src/interface/subject.c +++ b/src/interface/subject.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/logger.c b/src/logger.c index 859ceed..d7237fd 100644 --- a/src/logger.c +++ b/src/logger.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/logger/stderr.c b/src/logger/stderr.c index dd26e1b..104714b 100644 --- a/src/logger/stderr.c +++ b/src/logger/stderr.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/logger/syslog.c b/src/logger/syslog.c index 3667606..75fcd40 100644 --- a/src/logger/syslog.c +++ b/src/logger/syslog.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/server.c b/src/server.c index a8e7951..e01f044 100644 --- a/src/server.c +++ b/src/server.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -35,6 +35,10 @@ #include "utils/memory.h" + +void serverCloseConn(Server, unsigned int); + + static int serverCtor(void * _this, va_list * params) @@ -102,18 +106,9 @@ serverDtor(void * _this) int i; for (i=0; infds; i++) { - if (this->sock->handle != (this->fds)[i].fd) { - Stream st = (this->conns[(this->fds)[i].fd]).stream; - - delete((this->conns[(this->fds)[i].fd]).sock); - delete((this->conns[(this->fds)[i].fd]).worker); - - if (NULL != st && STREAM_SSL == st->type) { - SSL_shutdown((st->handle).ssl); - SSL_free((st->handle).ssl); - } - - delete((this->conns[(this->fds)[i].fd]).stream); + if (this->sock->handle != (this->fds)[i].fd && + this->sockSSL->handle != (this->fds)[i].fd) { + serverCloseConn(this, i); } } @@ -122,6 +117,7 @@ serverDtor(void * _this) delete(this->sock); delete(this->sockSSL); + SSL_CTX_free(this->ctx); ERR_free_strings(); } diff --git a/src/server/close_conn.c b/src/server/close_conn.c index dcab329..7347669 100644 --- a/src/server/close_conn.c +++ b/src/server/close_conn.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -31,16 +31,17 @@ void serverCloseConn(Server this, unsigned int i) { int fd = (this->fds)[i].fd; - Stream st = (this->conns[(this->fds)[i].fd]).stream; + Stream st = (this->conns[fd]).stream; delete((this->conns)[fd].sock); delete((this->conns)[fd].worker); if (NULL != st && STREAM_SSL == st->type) { SSL_shutdown((st->handle).ssl); + SSL_free((st->handle).ssl); } - delete((this->conns)[fd].stream); + delete(st); memset(&(this->fds[i]), 0, sizeof(struct pollfd)); } diff --git a/src/server/handle_accept.c b/src/server/handle_accept.c index 8235e30..bd10a93 100644 --- a/src/server/handle_accept.c +++ b/src/server/handle_accept.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -43,10 +43,12 @@ serverHandleAccept(Server this, unsigned int i) return -1; } + acc = socketAccept((0 == i)? this->sock : this->sockSSL, &remoteAddr); + + if (-1 != acc->handle) { switch(i) { case 0: // no SSL - acc = socketAccept(this->sock, &remoteAddr); st = new(Stream, STREAM_FD, acc->handle); break; @@ -54,7 +56,6 @@ serverHandleAccept(Server this, unsigned int i) // SSL { SSL * ssl = SSL_new(this->ctx); - acc = socketAccept(this->sockSSL, &remoteAddr); SSL_set_fd(ssl, acc->handle); SSL_accept(ssl); st = new(Stream, STREAM_SSL, ssl); @@ -65,7 +66,6 @@ serverHandleAccept(Server this, unsigned int i) break; } - if (-1 != acc->handle) { // save the socket handle (this->conns)[acc->handle].sock = acc; diff --git a/src/server/poll.c b/src/server/poll.c index 8c1d76c..77cfe07 100644 --- a/src/server/poll.c +++ b/src/server/poll.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -29,7 +29,6 @@ #include "utils/signalHandling.h" #define POLLFD(ptr) ((struct pollfd *)(ptr)) -#define SWAP(a, b) ((a)^=(b),(b)^=(a),(a)^=(b)) int serverPoll(Server this) { diff --git a/src/server/read.c b/src/server/read.c index 0eda967..5eb1adc 100644 --- a/src/server/read.c +++ b/src/server/read.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/server/run.c b/src/server/run.c index 950a292..6702be6 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/server/write.c b/src/server/write.c index aa5e69a..bacbf81 100644 --- a/src/server/write.c +++ b/src/server/write.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/session.c b/src/session.c index b3b4e37..6b205d0 100644 --- a/src/session.c +++ b/src/session.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include #include @@ -22,7 +44,7 @@ sessionCtor(void * _this, va_list * params) size_t nuname = va_arg(* params, size_t); this->livetime = time(NULL) + SESSION_LIVETIME; - this->id = sdbm((unsigned char *)uname, nuname) & this->livetime; + this->id = sdbm((unsigned char *)uname, nuname) ^ this->livetime; this->username = malloc(nuname + 1); this->username[nuname] = 0; diff --git a/src/session/add.c b/src/session/add.c index 07adfa2..ae635eb 100644 --- a/src/session/add.c +++ b/src/session/add.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include "session.h" diff --git a/src/session/delete.c b/src/session/delete.c index ef560ee..f8a8ba4 100644 --- a/src/session/delete.c +++ b/src/session/delete.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include "session.h" diff --git a/src/session/get.c b/src/session/get.c index 2b622ee..ed3734e 100644 --- a/src/session/get.c +++ b/src/session/get.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include diff --git a/src/socket.c b/src/socket.c index 3603513..f3e2a27 100644 --- a/src/socket.c +++ b/src/socket.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/socket/accept.c b/src/socket/accept.c index e1ff313..a51cae5 100644 --- a/src/socket/accept.c +++ b/src/socket/accept.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/socket/connect.c b/src/socket/connect.c index 66aa19c..7e0d19b 100644 --- a/src/socket/connect.c +++ b/src/socket/connect.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/socket/listen.c b/src/socket/listen.c index 4992058..0e54132 100644 --- a/src/socket/listen.c +++ b/src/socket/listen.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/stream.c b/src/stream.c index 49cf067..3168d92 100644 --- a/src/stream.c +++ b/src/stream.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include diff --git a/src/stream/read.c b/src/stream/read.c index b7bb058..9050e65 100644 --- a/src/stream/read.c +++ b/src/stream/read.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include diff --git a/src/stream/write.c b/src/stream/write.c index 475e9e9..540fade 100644 --- a/src/stream/write.c +++ b/src/stream/write.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include diff --git a/src/taskrambler.c b/src/taskrambler.c index 5125956..faf9888 100644 --- a/src/taskrambler.c +++ b/src/taskrambler.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 @@ -38,11 +38,13 @@ #include "server.h" #include "logger.h" #include "http/worker.h" +#include "auth/ldap.h" #include "interface/class.h" #include "interface/logger.h" #include "utils/signalHandling.h" +#include "utils/memory.h" #define DEFAULT_SECS 10 //#define DEFAULT_USECS (1000000 / HZ * 2) @@ -126,6 +128,7 @@ main() default: { Logger logger; + AuthLdap auth; HttpWorker worker; Server server; @@ -135,8 +138,11 @@ main() shm_unlink("/fooshm"); close(shm); - logger = new(LoggerStderr, LOGGER_DEBUG); - worker = new(HttpWorker, "testserver", value); + logger = new(LoggerSyslog, LOGGER_DEBUG); + auth = new(AuthLdap, + "ldap://localhost/", + CSTRA("ou=user,dc=yabrog,dc=weird-web-workers,dc=org")); + worker = new(HttpWorker, "testserver", value, auth); server = new(Server, logger, worker, 11212, SOMAXCONN); //daemonize(); @@ -184,6 +190,7 @@ main() if (NULL != server) delete(server); if (NULL != worker) delete(worker); + if (NULL != auth) delete(auth); if (NULL != logger) delete(logger); } diff --git a/src/utils/daemonize.c b/src/utils/daemonize.c index bdd61f5..6dd8318 100644 --- a/src/utils/daemonize.c +++ b/src/utils/daemonize.c @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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 diff --git a/src/utils/hash.c b/src/utils/hash.c index dc6dcea..ea46085 100644 --- a/src/utils/hash.c +++ b/src/utils/hash.c @@ -1,3 +1,26 @@ +/** + * \file + * + * \author Georg Hopp + * \author Unknown (find out) + * + * \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 . + */ + #include #include diff --git a/src/utils/http.c b/src/utils/http.c index 61e833b..36ab048 100644 --- a/src/utils/http.c +++ b/src/utils/http.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include #include diff --git a/src/utils/memory.c b/src/utils/memory.c index 853b613..8784759 100644 --- a/src/utils/memory.c +++ b/src/utils/memory.c @@ -1,3 +1,25 @@ +/** + * \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 . + */ + #include #include "utils/memory.h" diff --git a/src/utils/signalHandling.c b/src/utils/signalHandling.c index 4771fa0..a0f11c0 100644 --- a/src/utils/signalHandling.c +++ b/src/utils/signalHandling.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright (C) 2012 Georg Hopp + * 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