Browse Source

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).

release0.1.5
Georg Hopp 14 years ago
parent
commit
61f8dc58e1
  1. 3
      .gitignore
  2. 8
      TODO
  3. 50
      assets/html/main.html
  4. 0
      assets/image/waldschrat.jpg
  5. 37
      assets/js/init.js
  6. 0
      assets/js/jquery-1.7.1.js
  7. 0
      assets/js/jquery-1.7.1.min.js
  8. 78
      assets/js/serverval.js
  9. 78
      assets/js/session.js
  10. 58
      assets/style/common.css
  11. 45
      include/auth.h
  12. 41
      include/auth/ldap.h
  13. 2
      include/cbuf.h
  14. 2
      include/class.h
  15. 38
      include/commons.h
  16. 54
      include/credential.h
  17. 42
      include/hash.h
  18. 42
      include/hash_value.h
  19. 51
      include/http/cookie.h
  20. 4
      include/http/header.h
  21. 7
      include/http/message.h
  22. 2
      include/http/message/queue.h
  23. 5
      include/http/parser.h
  24. 8
      include/http/request.h
  25. 4
      include/http/response.h
  26. 4
      include/http/worker.h
  27. 2
      include/http/writer.h
  28. 2
      include/interface.h
  29. 49
      include/interface/auth.h
  30. 2
      include/interface/class.h
  31. 45
      include/interface/hashable.h
  32. 2
      include/interface/http_intro.h
  33. 2
      include/interface/logger.h
  34. 2
      include/interface/observer.h
  35. 2
      include/interface/stream_reader.h
  36. 2
      include/interface/stream_writer.h
  37. 2
      include/interface/subject.h
  38. 2
      include/logger.h
  39. 2
      include/server.h
  40. 2
      include/session.h
  41. 2
      include/socket.h
  42. 22
      include/stream.h
  43. 2
      include/utils/hash.h
  44. 22
      include/utils/http.h
  45. 2
      include/utils/memory.h
  46. 2
      include/utils/signalHandling.h
  47. 23
      src/Makefile.am
  48. 110
      src/auth/ldap.c
  49. 2
      src/cbuf.c
  50. 2
      src/cbuf/addr_index.c
  51. 2
      src/cbuf/empty.c
  52. 2
      src/cbuf/get_data.c
  53. 2
      src/cbuf/get_free.c
  54. 2
      src/cbuf/get_line.c
  55. 2
      src/cbuf/get_read.c
  56. 2
      src/cbuf/get_write.c
  57. 2
      src/cbuf/inc_read.c
  58. 2
      src/cbuf/inc_write.c
  59. 2
      src/cbuf/is_empty.c
  60. 2
      src/cbuf/is_locked.c
  61. 2
      src/cbuf/lock.c
  62. 2
      src/cbuf/memchr.c
  63. 2
      src/cbuf/read.c
  64. 2
      src/cbuf/release.c
  65. 2
      src/cbuf/set_data.c
  66. 2
      src/cbuf/skip_non_alpha.c
  67. 2
      src/cbuf/write.c
  68. 86
      src/credential.c
  69. 64
      src/hash.c
  70. 41
      src/hash/add.c
  71. 47
      src/hash/delete.c
  72. 47
      src/hash/each.c
  73. 26
      src/hash/get.c
  74. 109
      src/hash_value.c
  75. 104
      src/http/cookie.c
  76. 33
      src/http/header.c
  77. 2
      src/http/header/to_string.c
  78. 25
      src/http/message.c
  79. 2
      src/http/message/get_version.c
  80. 5
      src/http/message/has_keep_alive.c
  81. 2
      src/http/message/has_valid_version.c
  82. 11
      src/http/message/header_size_get.c
  83. 13
      src/http/message/header_to_string.c
  84. 2
      src/http/message/queue.c
  85. 2
      src/http/parser.c
  86. 4
      src/http/parser/body.c
  87. 44
      src/http/parser/header.c
  88. 22
      src/http/parser/new_message.c
  89. 29
      src/http/parser/parse.c
  90. 72
      src/http/parser/post_vars.c
  91. 71
      src/http/parser/request_vars.c
  92. 11
      src/http/request.c
  93. 2
      src/http/request/has_valid_method.c
  94. 2
      src/http/response.c
  95. 9
      src/http/response/304.c
  96. 2
      src/http/response/403.c
  97. 5
      src/http/response/404.c
  98. 10
      src/http/response/asset.c
  99. 5
      src/http/response/login_form.c
  100. 151
      src/http/response/me.c

3
.gitignore

@ -4,10 +4,9 @@
.deps/
Makefile
Makefile.in
/config*
m4/
/docs/
/config.*
/configure
/INSTALL
*.m4
/autom4te.cache/

8
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

50
assets/html/main.html

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>My own little Web-App</title>
<link rel="stylesheet" type="text/css" href="/assets/style/common">
<script type="text/javascript" src="/assets/js/jquery"></script>
<script type="text/javascript" src="/assets/js/serverval"></script>
<script type="text/javascript" src="/assets/js/session"></script>
<script type="text/javascript" src="/assets/js/init"></script>
</head>
<body>
<ul id="menu">
<li>random Value</li>
<li>start session</li>
<li>login</li>
</ul>
<div id="sessinfo" class="x-small">
Session: <span></span><br />
<canvas width="100px" height="3px"></canvas>
</div>
<div id="login" class="hide">
<form>
<label for="username">username</label>
<input type="text" name="username" /><br />
<label for="password">password</label>
<input type="password" name="password" /><br />
<input type="submit" />
</form>
</div>
<div id="randval" class="hide">
<span class=\"small">
Value created at: <br />
<span></span><br>
Next value in: <span></span><br />
</span>
Value: <span></span>
</div>
<div id="main">
<h1>Testpage</h1>
Welcome<span></span>!!!<br />
<img src="/image/me" />
</div>
<hr />
<div id="msg"></div>
</body>
</html>
<!-- vim: set ts=4 sw=4: -->

0
assets/waldschrat.jpg → assets/image/waldschrat.jpg

Before

Width: 250  |  Height: 250  |  Size: 78 KiB

After

Width: 250  |  Height: 250  |  Size: 78 KiB

37
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:

0
assets/jquery-1.7.1.js → assets/js/jquery-1.7.1.js

0
assets/jquery-1.7.1.min.js → assets/js/jquery-1.7.1.min.js

78
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.<br />");
break;
default:
$("#msg").append(
"Unhandled - " + xhr.responseText + "<br />");
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:

78
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:

58
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: */

45
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 <http://www.gnu.org/licenses/>.
*/
#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:

41
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __AUTH_LDAP_H__
#define __AUTH_LDAP_H__
#include <ldap.h>
#include <sys/types.h>
#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:

2
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

2
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

38
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 <http://www.gnu.org/licenses/>.
*/
#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:

54
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __CREDENTIAL_H__
#define __CREDENTIAL_H__
#include <sys/types.h>
#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:

42
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __HASH_H__
#define __HASH_H__
#include <sys/types.h>
#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:

42
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __HASH_VALUE_H__
#define __HASH_VALUE_H__
#include <sys/types.h>
#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:

51
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __HTTP_COOKIE_H__
#define __HTTP_COOKIE_H__
#include <time.h>
#include <sys/types.h>
#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:

4
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__

7
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;

2
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

5
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__

8
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);

4
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,

4
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__

2
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

2
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

49
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __INTERFACE_AUTH_H__
#define __INTERFACE_AUTH_H__
#include <stdarg.h>
#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:

2
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

45
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 <http://www.gnu.org/licenses/>.
*/
#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:

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

22
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __STREAM_H__
#define __STREAM_H__

2
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

22
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __UTILS_HTTP_H__
#define __UTILS_HTTP_H__

2
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

2
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

23
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

110
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 <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ldap.h>
#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:

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

2
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

86
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 <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#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:

64
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 <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <search.h>
#include <stdarg.h>
#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:

41
src/http/header/add.c → 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 <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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:

47
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 <http://www.gnu.org/licenses/>.
*/
#include <search.h>
#include <sys/types.h>
#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:

47
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 <http://www.gnu.org/licenses/>.
*/
#include <search.h>
#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:

26
src/http/header/get.c → 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 <search.h>
#include <stdlib.h>
#include <sys/types.h>
#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:

109
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 <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#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:

104
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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#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:

33
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:

2
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

25
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:

2
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

5
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;

2
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

11
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;

13
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';

2
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

2
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

4
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)

44
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));
}

22
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 <http://www.gnu.org/licenses/>.
*/
#include "http/parser.h"
#include "utils/http.h"

29
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 <stdlib.h>
#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:

72
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 <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <sys/types.h>
#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=val1<delim>key2=val2<delim>...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:

71
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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#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:

11
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);
}

2
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

2
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

9
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;

2
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

5
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 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
@ -52,7 +53,7 @@ httpResponse404()
response = new(HttpResponse, "HTTP/1.1", 404, "Not Found");
message = (HttpMessage)response;
httpHeaderAdd(&(message->header),
hashAdd(message->header,
new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html")));
message->type = HTTP_MESSAGE_BUFFERED;

10
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;

5
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 "<form action=\"/me/\" method=\"POST\">" \
"<input name=\"username\" type=\"text\" />" \
@ -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;

151
src/http/response/me.c

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include "class.h"
#include "interface/class.h"
#include "http/response.h"
#include "http/message.h"
#include "http/header.h"
#include "utils/memory.h"
#define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" \
"<head>" \
"<title>My own little Web-App</title>" \
"<style type=\"text/css\">" \
"div#randval {" \
"left: 200px;" \
"top: 100px;" \
"position: fixed;" \
"background-color: white;" \
"border: 1px solid black;" \
"}" \
"div.hide#randval {" \
"top: -500px;" \
"}" \
".small {" \
"font-size: small;" \
"}" \
"</style>" \
"<script type=\"text/javascript\" src=\"/jquery/\"></script>" \
"<script>" \
"$(document).ready(function() {" \
"var intervalId;" \
"var vnext = 0;" \
"var clickclose = function() {" \
"clearInterval(intervalId);" \
"vnext = 0;" \
"$(\"#randval\").addClass(\"hide\");" \
"};" \
"var counter = function() {" \
"if (0 >= vnext) {" \
"$.getJSON(\"/randval/\", function(data, xhr) {" \
"var date = new Date(data.ctime * 1000);" \
"$(\"#ctime\").empty().append(date.toString());" \
"vnext = data.vnext;" \
"$(\"#value\").empty().append(data.value);" \
"$(\"#vnext\").empty().append(vnext);" \
"$(\"#randval\").on(\"click\", clickclose);" \
"}).error(function(event, request, settings) {" \
"clearInterval(intervalId);" \
"$.get(\"/login/\", function(data) {" \
"$(\"#randval\")" \
".off(\"click\", clickclose)" \
".empty().append(data);" \
"});" \
"});" \
"if ($(\"#randval\").hasClass(\"hide\")) {" \
"$(\"#randval\").removeClass(\"hide\");" \
"}" \
"} else {" \
"vnext--;" \
"$(\"#vnext\").empty().append(vnext);" \
"}" \
"};" \
"$(\"#msg\").ajaxError(function(event, request, settings) {" \
"$(this).append(" \
"\"<li>Error requesting page \" + " \
"settings.url + " \
"\"</li>\");" \
"vnext = 0;" \
"});" \
"$(\"a\").click(function() {" \
"intervalId = setInterval(counter, 1000);" \
"});" \
"});" \
"</script>" \
"</head>" \
"<body>" \
"<div id=\"randval\" class=\"hide\">" \
"<span class=\"small\">" \
"Value created at: <br /><span id=\"ctime\"></span><br>" \
"Next value in: <span id=\"vnext\"></span><br />" \
"</span>" \
"Value: <span id=\"value\"></span>" \
"</div>" \
"<div id=\"main\">" \
"<h1>Testpage</h1>" \
"Welcome %s<br />" \
"<img src=\"/image/\" />" \
"<br /><a href=\"#\">Link</a>" \
"</div>" \
"<hr /><div id=\"msg\"></div>" \
"</body>" \
"</html>"
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:

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save