process.c
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* \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 <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include "class.h"
#include "auth.h"
#include "queue.h"
#include "session.h"
#include "stream.h"
#include "hash.h"
#include "http/worker.h"
#include "http/header.h"
#include "http/message.h"
#include "http/request.h"
#include "http/response.h"
#include "http/parser.h"
#include "utils/memory.h"
#include "commons.h"
HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t);
void httpWorkerAddCommonHeader(HttpMessage, HttpMessage);
ssize_t
httpWorkerProcess(HttpWorker this, Stream st)
{
time_t t;
struct tm * tmp;
char buffer[200];
ssize_t size;
if (0 < (size = httpParserParse(this->parser, st))) {
while (! queueEmpty(this->parser->queue)) {
HttpRequest request = queueGet(
this->parser->queue);
HttpMessage response = NULL;
/**
* \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;
sid = strtoul((char*)(sidstr->value), NULL, 10);
this->session = sessionGet(this->sroot, sid);
}
}
if (NULL != this->session) {
if (time(NULL) < this->session->livetime) {
this->session->livetime = time(NULL) + SESSION_LIVETIME;
} else {
sessionDelete(this->sroot, this->session->id);
delete(this->session);
}
}
if (0 == strcmp("POST", request->method)) {
if (0 == strcmp("/login/", request->path)) {
char buffer[200];
size_t nbuf;
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,
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("/", 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("/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->path)) {
if (NULL != this->session) {
response = (HttpMessage)httpResponseRandval(
this->val->timestamp,
this->val->value);
} else {
response = (HttpMessage)httpResponse403();
}
}
if (0 == strcmp("/image/me", request->path)) {
response = httpWorkerGetAsset(
request,
"./assets/image/waldschrat.jpg",
CSTRA("image/jpeg"));
}
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/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) {
response = (HttpMessage)httpResponse404();
}
httpWorkerAddCommonHeader((HttpMessage)request, response);
delete(request);
queuePut(this->writer->queue, response);
response = NULL;
}
}
return size;
}
// vim: set ts=4 sw=4: