Commit 36612df0da046892186d9d07cc26365de912221f
1 parent
04250fb4
implemented an application class as well as an http adapter for it and use it to…
… start application logic by http requests as well as creating a fitting repsonse. Not perfect, but a start. This code is not finish and will not work...in fact it won't even compile i think. refs #24
Showing
13 changed files
with
485 additions
and
201 deletions
include/application/adapter/http.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2012 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#ifndef __APPLICATION_ADAPTER_HTTP_H__ | |
24 | +#define __APPLICATION_ADAPTER_HTTP_H__ | |
25 | + | |
26 | +#include "class.h" | |
27 | +#include "application.h" | |
28 | + | |
29 | + | |
30 | +CLASS(ApplicationAdapterHttp) { | |
31 | + Application application; | |
32 | +}; | |
33 | + | |
34 | +#endif // __APPLICATION_ADAPTER_HTTP_H__ | |
35 | + | |
36 | +// vim: set ts=4 sw=4: | ... | ... |
include/application/application.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2012 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#ifndef __APPLICATION_H__ | |
24 | +#define __APPLICATION_H__ | |
25 | + | |
26 | +#include "class.h" | |
27 | + | |
28 | +#include "auth/credential.h" | |
29 | + | |
30 | +struct randval { | |
31 | + time_t timestamp; | |
32 | + int value; | |
33 | +} | |
34 | + | |
35 | +CLASS(Application) { | |
36 | + Hash active_sessions; | |
37 | + void * auth; | |
38 | + struct randval * val; | |
39 | +}; | |
40 | + | |
41 | +// this should return a user account....now it only return success or failure. | |
42 | +int applicationLogin(Application, Credential); | |
43 | +unsigned long applicationSessionStart(Application); | |
44 | +void applicationSessionStop(Application, unsigned long); | |
45 | +Session applicationSessionGet(Application, unsigned long); | |
46 | + | |
47 | +#endif // __HTTP_HEADER_H__ | |
48 | + | |
49 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -34,28 +34,24 @@ |
34 | 34 | #include "cbuf.h" |
35 | 35 | #include "session.h" |
36 | 36 | |
37 | +#include "http/request.h" | |
38 | +#include "http/response.h" | |
37 | 39 | #include "commons.h" |
38 | 40 | |
39 | -struct randval { | |
40 | - time_t timestamp; | |
41 | - int value; | |
42 | -}; | |
43 | - | |
44 | 41 | |
45 | 42 | CLASS(HttpWorker) { |
46 | - char * id; | |
47 | - struct randval * val; | |
43 | + char * id; | |
48 | 44 | |
49 | - Cbuf pbuf; | |
45 | + Cbuf pbuf; | |
46 | + Hash asset_pool; | |
50 | 47 | |
51 | - Hash asset_pool; | |
48 | + void * application_adapter; | |
49 | + | |
50 | + HttpRequest current_request; | |
51 | + HttpMessage current_response; | |
52 | 52 | |
53 | 53 | HttpParser parser; |
54 | 54 | HttpWriter writer; |
55 | - Session session; | |
56 | - Session * sroot; | |
57 | - | |
58 | - void * auth; | |
59 | 55 | }; |
60 | 56 | |
61 | 57 | #endif // __HTTP_WORKER_H__ | ... | ... |
... | ... | @@ -24,16 +24,16 @@ |
24 | 24 | #ifndef __OBSERVER_H__ |
25 | 25 | #define __OBSERVER_H__ |
26 | 26 | |
27 | -typedef void (* fptr_observerNotify)(void *, void*); | |
27 | +typedef void (* fptr_observerUpdate)(void *, void*); | |
28 | 28 | |
29 | 29 | extern const struct interface i_Observer; |
30 | 30 | |
31 | 31 | struct i_Observer { |
32 | 32 | const struct interface * const _; |
33 | - fptr_observerNotify notify; | |
33 | + fptr_observerUpdate update; | |
34 | 34 | }; |
35 | 35 | |
36 | -extern void observerNotify(void *, void *); | |
36 | +extern void observerUpdate(void *, void *); | |
37 | 37 | |
38 | 38 | #endif // __OBSERVER_H__ |
39 | 39 | ... | ... |
src/application/adapter/http/http.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2012 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#define _GNU_SOURCE | |
24 | + | |
25 | +#include <stdarg.h> | |
26 | + | |
27 | +#include "class.h" | |
28 | +#include "application/application.h" | |
29 | +#include "application/adapter/http.h" | |
30 | + | |
31 | +#include "utils/memory.h" | |
32 | +#include "interface/observer.h" | |
33 | + | |
34 | +static | |
35 | +int | |
36 | +applicationAdapterHttpCtor(void * _this, va_list * params) | |
37 | +{ | |
38 | + ApplicationAdapterHttp this = _this; | |
39 | + | |
40 | + this->application = va_arg(*params, Application); | |
41 | + | |
42 | + return 0; | |
43 | +} | |
44 | + | |
45 | +static | |
46 | +void | |
47 | +applicationAdapterHttpDtor(void * _this) | |
48 | +{ | |
49 | +} | |
50 | + | |
51 | + | |
52 | +void applicationAdapterHttpUpdate(ApplicationAdapterHttp, void *); | |
53 | + | |
54 | + | |
55 | +INIT_IFACE(Class, applicationAdapterHttpCtor, applicationAdapterHttpDtor); | |
56 | +INIT_IFACE(Observer, applicationAdapterHttpUpdate); | |
57 | +CREATE_CLASS( | |
58 | + ApplicationAdapterHttp, | |
59 | + NULL, | |
60 | + IFACE(Class), | |
61 | + IFACE(Observer)); | |
62 | + | |
63 | +// vim: set ts=4 sw=4: | ... | ... |
src/application/adapter/http/update.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2012 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#define _GNU_SOURCE | |
24 | + | |
25 | +#include <stdio,h> | |
26 | +#include <stdlib,h> | |
27 | +#include <sys/types.h> | |
28 | + | |
29 | +#include "class.h" | |
30 | +#include "application/application.h" | |
31 | +#include "application/adapter/http.h" | |
32 | +#include "hash.h" | |
33 | +#include "http/worker.h" | |
34 | +#include "http/header.h" | |
35 | +#include "http/response.h" | |
36 | +#include "auth/credential.h" | |
37 | + | |
38 | +#include "utils/memory.h" | |
39 | + | |
40 | + | |
41 | +#define NO_SESSION_SID 0 | |
42 | + | |
43 | + | |
44 | +static | |
45 | +inline | |
46 | +unsigned long | |
47 | +getSessionId(Hash cookies) | |
48 | +{ | |
49 | + HashValue sidstr = hashGet(cookies, CSTRA("sid")); | |
50 | + | |
51 | + if (NULL != sidstr) { | |
52 | + return strtoul((char*)(sidstr->value), NULL, 10); | |
53 | + } | |
54 | + | |
55 | + return NO_SESSION_SID; | |
56 | +} | |
57 | + | |
58 | +static | |
59 | +void | |
60 | +loginAdapter(Application application, HttpWorker worker) | |
61 | +{ | |
62 | + HashValue username; | |
63 | + HashValue passeord; | |
64 | + Credential credential; | |
65 | + | |
66 | + username = hashGet( | |
67 | + worker->current_reqeust->post, | |
68 | + CSTRA("username")); | |
69 | + password = hashGet( | |
70 | + worker->current_reqeust->post, | |
71 | + CSTRA("password")); | |
72 | + | |
73 | + if (NULL == username || NULL == password) { | |
74 | + worker->current_response = | |
75 | + new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); | |
76 | + return; | |
77 | + } | |
78 | + | |
79 | + credential = new(Credential, | |
80 | + CRED_PASSWORD, | |
81 | + (char *)(username->value), username->nvalue, | |
82 | + (char *)(password->value), password->nvalue); | |
83 | + | |
84 | + if (applicationLogin(application, credential)) { | |
85 | + char buffer[200]; | |
86 | + size_t nbuf; | |
87 | + | |
88 | + if (NO_SESSION_SID == sid) { | |
89 | + sid = applicationSessionStart(application); | |
90 | + } else { | |
91 | + applicationSessionUpdate( | |
92 | + application, | |
93 | + sid, | |
94 | + username->value, | |
95 | + username->nvalue); | |
96 | + } | |
97 | + | |
98 | + nbuf = sprintf(buffer, "sid=%lu;Path=/", sid); | |
99 | + | |
100 | + worker->current_response = | |
101 | + (HttpMessage)httpResponseSession( | |
102 | + applicationSessionGet(sid)); | |
103 | + | |
104 | + hashAdd( | |
105 | + worker->current_response->header, | |
106 | + new(HttpHeader. CSTRA("Set-Cookie"), buffer, nbuf)); | |
107 | + } else { | |
108 | + worker->current_response = | |
109 | + new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); | |
110 | + } | |
111 | + | |
112 | + delete(credential) | |
113 | +} | |
114 | + | |
115 | + | |
116 | +void | |
117 | +applicationAdapterHttpUpdate(ApplicationAdapterHttp this, void * subject) | |
118 | +{ | |
119 | + HttpWorker worker = (HttpWorker)subject; | |
120 | + unsigned long sid = getSessionId(worker->current_request->cookies); | |
121 | + | |
122 | + if (0 == strcmp("POST", worker->current_request->method)) { | |
123 | + if (0 == strcmp("/login/", worker->current_request->path)) { | |
124 | + loginAdapter(this->application, worker); | |
125 | + return; | |
126 | + } | |
127 | + } | |
128 | + | |
129 | + if (0 == strcmp("GET", worker->current_request->method)) { | |
130 | + if (0 == strcmp("/sessinfo/", worker->current_request->path)) { | |
131 | + worker->current_response = | |
132 | + (HttpMessage)httpResponseSession( | |
133 | + applicationSessionGet(sid)); | |
134 | + return; | |
135 | + } | |
136 | + | |
137 | + if (0 == strcmp("/sess/", worker->current_request->path)) { | |
138 | + if (NO_SESSION_SID == sid) { | |
139 | + sid = applicationSessionStart(application); | |
140 | + } | |
141 | + | |
142 | + worker->current_response = | |
143 | + (HttpMessage)httpResponseSession( | |
144 | + applicationSessionGet(sid)); | |
145 | + return; | |
146 | + } | |
147 | + | |
148 | + if (0 == strcmp("/randval/", worker->current_request->path)) { | |
149 | + if (NO_SESSION_SID != sid) { | |
150 | + worker->current_response = | |
151 | + (HttpMessage)httpResponseRandval( | |
152 | + this->application->val->timestamp, | |
153 | + this->application->val->value); | |
154 | + } else { | |
155 | + worker->current_response = (HttpMessage)httpResponse403(); | |
156 | + } | |
157 | + } | |
158 | + } | |
159 | +} | |
160 | + | |
161 | +// vim: set ts=4 sw=4: | ... | ... |
src/application/application.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2012 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#define _GNU_SOURCE | |
24 | + | |
25 | +#include <stdarg.h> | |
26 | + | |
27 | +#include "class.h" | |
28 | +#include "application/application.h" | |
29 | + | |
30 | +#include "utils/memory.h" | |
31 | + | |
32 | +static | |
33 | +int | |
34 | +applicationCtor(void * _this, va_list * params) | |
35 | +{ | |
36 | + Application this = _this; | |
37 | + | |
38 | + this->val = va_arg(*params, struct randval *); | |
39 | + this->auth = va_arg(* params, void *); | |
40 | + | |
41 | + return 0; | |
42 | +} | |
43 | + | |
44 | +static | |
45 | +void | |
46 | +applicationDtor(void * _this) | |
47 | +{ | |
48 | +} | |
49 | + | |
50 | + | |
51 | +INIT_IFACE(Class, applicationCtor, applicationDtor); | |
52 | +CREATE_CLASS(Application, NULL, IFACE(Class)); | |
53 | + | |
54 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -38,6 +38,8 @@ |
38 | 38 | #include "http/writer.h" |
39 | 39 | |
40 | 40 | #include "utils/memory.h" |
41 | +#include "interface/subject.h" | |
42 | +#include "interface/observer.h" | |
41 | 43 | |
42 | 44 | static |
43 | 45 | int |
... | ... | @@ -50,8 +52,6 @@ httpWorkerCtor(void * _this, va_list * params) |
50 | 52 | this->id = memMalloc(strlen(id) + 1); |
51 | 53 | strcpy(this->id, id); |
52 | 54 | |
53 | - this->val = va_arg(*params, struct randval *); | |
54 | - | |
55 | 55 | this->asset_pool = new(Hash); |
56 | 56 | |
57 | 57 | sprintf(cbuf_id, "%s_%s", "parser", id); |
... | ... | @@ -60,21 +60,10 @@ httpWorkerCtor(void * _this, va_list * params) |
60 | 60 | this->parser = new(HttpParser, this->pbuf); |
61 | 61 | this->writer = new(HttpWriter); |
62 | 62 | |
63 | - this->sroot = &(this->session); | |
64 | - this->auth = va_arg(* params, void *); | |
65 | - | |
66 | 63 | return 0; |
67 | 64 | } |
68 | 65 | |
69 | 66 | static |
70 | -inline | |
71 | -void | |
72 | -tDelete(void * node) | |
73 | -{ | |
74 | - delete(node); | |
75 | -} | |
76 | - | |
77 | -static | |
78 | 67 | void |
79 | 68 | httpWorkerDtor(void * _this) |
80 | 69 | { |
... | ... | @@ -88,7 +77,6 @@ httpWorkerDtor(void * _this) |
88 | 77 | if (NULL != this->pbuf) { |
89 | 78 | delete(this->asset_pool); |
90 | 79 | delete(this->pbuf); //!< cloned workers have NULL, so delete won't do anything |
91 | - tdestroy(*(this->sroot), tDelete); | |
92 | 80 | } |
93 | 81 | } |
94 | 82 | |
... | ... | @@ -99,9 +87,8 @@ httpWorkerClone(void * _this, void * _base) |
99 | 87 | HttpWorker this = _this; |
100 | 88 | HttpWorker base = _base; |
101 | 89 | |
102 | - this->val = base->val; | |
103 | - | |
104 | - this->asset_pool = base->asset_pool; | |
90 | + this->asset_pool = base->asset_pool; | |
91 | + this->application_adapter = base->application_adapter; | |
105 | 92 | |
106 | 93 | this->parser = new(HttpParser, base->pbuf); |
107 | 94 | /* |
... | ... | @@ -124,22 +111,59 @@ httpWorkerClone(void * _this, void * _base) |
124 | 111 | * it is. |
125 | 112 | */ |
126 | 113 | this->writer = new(HttpWriter); |
127 | - | |
128 | - this->sroot = &(base->session); | |
129 | - this->auth = base->auth; | |
130 | 114 | } |
131 | 115 | |
132 | 116 | ssize_t httpWorkerProcess(void *, Stream); |
133 | 117 | ssize_t httpWorkerWrite(void *, Stream); |
134 | 118 | |
119 | +static | |
120 | +void | |
121 | +httpWorkerDetach(void * _this, void * adapter) | |
122 | +{ | |
123 | + HttpWorker this = (HttpWorker)_this; | |
124 | + | |
125 | + if (NULL != this->application_adapter) { | |
126 | + delete(this->application_adapter); | |
127 | + } | |
128 | +} | |
129 | + | |
130 | +static | |
131 | +void | |
132 | +httpWorkerAttach(void * _this, void * adapter) | |
133 | +{ | |
134 | + HttpWorker this = (HttpWorker)_this; | |
135 | + | |
136 | + /* | |
137 | + * right now only one adapter is allowed and the last | |
138 | + * added will be used....all others will be deleted in | |
139 | + * assumption that no other handle does exist anymore | |
140 | + * (because it was added as an adapter and thus is good | |
141 | + * for nothing else.) | |
142 | + */ | |
143 | + httpWorkerDetach(_this, adapter); | |
144 | + | |
145 | + this->application_adapter = adapter; | |
146 | +} | |
147 | + | |
148 | +static | |
149 | +void | |
150 | +httpWorkerNotify(void * _this) | |
151 | +{ | |
152 | + HttpWorker this = (HttpWorker)_this; | |
153 | + | |
154 | + observerUpdate(this->application_adapter, _this); | |
155 | +} | |
156 | + | |
135 | 157 | INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone); |
136 | 158 | INIT_IFACE(StreamReader, httpWorkerProcess); |
137 | 159 | INIT_IFACE(StreamWriter, httpWorkerWrite); |
160 | +INIT_IFACE(Subject, httpWorkerAttach, httpWorkerDetach, httpWorkerNotify); | |
138 | 161 | CREATE_CLASS( |
139 | 162 | HttpWorker, |
140 | 163 | NULL, |
141 | 164 | IFACE(Class), |
142 | 165 | IFACE(StreamReader), |
143 | - IFACE(StreamWriter)); | |
166 | + IFACE(StreamWriter), | |
167 | + IFACE(Subject)); | |
144 | 168 | |
145 | 169 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -26,7 +26,7 @@ |
26 | 26 | |
27 | 27 | #include "http/message.h" |
28 | 28 | #include "http/header.h" |
29 | -#include "http/response.h" | |
29 | +#include "http/worker.h" | |
30 | 30 | |
31 | 31 | #include "hash.h" |
32 | 32 | |
... | ... | @@ -35,35 +35,35 @@ |
35 | 35 | |
36 | 36 | |
37 | 37 | void |
38 | -httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) | |
38 | +httpWorkerAddCommonHeader(HttpWorker this) | |
39 | 39 | { |
40 | 40 | char buffer[200]; |
41 | 41 | size_t nbuf; |
42 | 42 | |
43 | - if (httpMessageHasKeepAlive(request)) { | |
44 | - hashAdd(response->header, | |
43 | + if (httpMessageHasKeepAlive((HttpMessage)this->current_request)) { | |
44 | + hashAdd(this->current_response->header, | |
45 | 45 | new(HttpHeader, CSTRA("Connection"), CSTRA("Keep-Alive"))); |
46 | 46 | } |
47 | 47 | else { |
48 | - hashAdd(response->header, | |
48 | + hashAdd(this->current_response->header, | |
49 | 49 | new(HttpHeader, CSTRA("Connection"), CSTRA("Close"))); |
50 | 50 | } |
51 | 51 | |
52 | - hashAdd(response->header, | |
52 | + hashAdd(this->current_response->header, | |
53 | 53 | new(HttpHeader, CSTRA("Server"), CSTRA("testserver"))); |
54 | 54 | |
55 | - switch(((HttpResponse)response)->status) { | |
55 | + switch(((HttpResponse)this->current_response)->status) { | |
56 | 56 | case 304: |
57 | 57 | break; |
58 | 58 | |
59 | 59 | default: |
60 | - nbuf = sprintf(buffer, "%d", response->nbody); | |
61 | - hashAdd(response->header, | |
60 | + nbuf = sprintf(buffer, "%d", this->current_response->nbody); | |
61 | + hashAdd(this->current_response->header, | |
62 | 62 | new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf)); |
63 | 63 | } |
64 | 64 | |
65 | 65 | nbuf = rfc1123GmtNow(buffer, sizeof(buffer)); |
66 | - hashAdd(response->header, | |
66 | + hashAdd(this->current_response->header, | |
67 | 67 | new(HttpHeader, CSTRA("Date"), buffer, nbuf)); |
68 | 68 | } |
69 | 69 | ... | ... |
... | ... | @@ -32,10 +32,7 @@ |
32 | 32 | #include "hash.h" |
33 | 33 | |
34 | 34 | HttpMessage |
35 | -httpWorkerGetAsset( | |
36 | - HttpWorker this, | |
37 | - HttpRequest request, | |
38 | - const char * fname) | |
35 | +httpWorkerGetAsset(HttpWorker this, const char * fname) | |
39 | 36 | { |
40 | 37 | char * match; |
41 | 38 | size_t nmatch; |
... | ... | @@ -45,7 +42,7 @@ httpWorkerGetAsset( |
45 | 42 | size_t nfname = strlen(fname); |
46 | 43 | |
47 | 44 | header = hashGet( |
48 | - ((HttpMessage)request)->header, | |
45 | + ((HttpMessage)this->current_request)->header, | |
49 | 46 | CSTRA("If-None-Match")); |
50 | 47 | |
51 | 48 | if (NULL == header) { | ... | ... |
... | ... | @@ -46,9 +46,8 @@ |
46 | 46 | #include "commons.h" |
47 | 47 | |
48 | 48 | |
49 | -HttpMessage httpWorkerGetAsset(HttpWorker, HttpRequest, const char *); | |
50 | -void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); | |
51 | -char * httpWorkerGetMimeType(HttpWorker, const char * extension); | |
49 | +HttpMessage httpWorkerGetAsset(HttpWorker, const char *); | |
50 | +void httpWorkerAddCommonHeader(HttpWorker); | |
52 | 51 | |
53 | 52 | |
54 | 53 | ssize_t |
... | ... | @@ -62,162 +61,61 @@ httpWorkerProcess(HttpWorker this, Stream st) |
62 | 61 | |
63 | 62 | if (0 < requests) { |
64 | 63 | while (! queueEmpty(this->parser->queue)) { |
65 | - HttpRequest request = queueGet(this->parser->queue); | |
66 | - HttpMessage response = NULL; | |
64 | + this->current_request = queueGet(this->parser->queue); | |
65 | + this->current_response = NULL; | |
67 | 66 | |
68 | - /** | |
69 | - * \todo store the cookie count in the request to make a simple | |
70 | - * check possible to prevent this lookup if no cookies exists | |
71 | - * at all | |
67 | + /* | |
68 | + * let our observers...application (or better their | |
69 | + * http adapter) try to create an answer. | |
72 | 70 | */ |
73 | - if (NULL == this->session) { | |
74 | - HashValue sidstr = hashGet(request->cookies, CSTRA("sid")); | |
71 | + subjectNotify(this); | |
75 | 72 | |
76 | - if (NULL != sidstr) { | |
77 | - unsigned long sid; | |
78 | - | |
79 | - sid = strtoul((char*)(sidstr->value), NULL, 10); | |
80 | - this->session = sessionGet(this->sroot, sid); | |
81 | - } | |
82 | - } | |
83 | - | |
84 | - if (NULL != this->session) { | |
85 | - if (time(NULL) < this->session->livetime) { | |
86 | - this->session->livetime = time(NULL) + SESSION_LIVETIME; | |
87 | - } else { | |
88 | - sessionDelete(this->sroot, this->session->id); | |
89 | - delete(this->session); | |
90 | - } | |
73 | + if (0 == strcmp("POST", this->current_request->method)) { | |
74 | + /* | |
75 | + * we can't do post requests on our own... | |
76 | + */ | |
77 | + this->current_response = (HttpMessage)httpResponse500(); | |
91 | 78 | } |
92 | 79 | |
93 | - if (0 == strcmp("POST", request->method)) { | |
94 | - if (0 == strcmp("/login/", request->path)) { | |
95 | - char buffer[200]; | |
96 | - size_t nbuf; | |
97 | - | |
98 | - HashValue username = hashGet(request->post, CSTRA("username")); | |
99 | - HashValue password = hashGet(request->post, CSTRA("password")); | |
100 | - | |
101 | - /** | |
102 | - * \todo This is an application authorization not an HTTP | |
103 | - * authorization...anyway think about sending HTTP 401 | |
104 | - * messages if authorization is required and think about | |
105 | - * sending the credentials via header as described in the | |
106 | - * HTTP protocol. Most likely this will lead to hacky thing | |
107 | - * with javascript as i am not sure how far this is implemented | |
108 | - * within browsers. | |
109 | - * Anyway, for now we simply ignore a failed login within the | |
110 | - * response except that no session is initialized. We send | |
111 | - * an empty 200 OK | |
112 | - */ | |
113 | - if (NULL == password || NULL == username) { | |
114 | - response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); | |
115 | - } | |
116 | - | |
117 | - if (NULL == response) { | |
118 | - Credential cred = new(Credential, | |
119 | - CRED_PASSWORD, | |
120 | - (char*)(username->value), username->nvalue, | |
121 | - (char*)(password->value), password->nvalue); | |
122 | - | |
123 | - if (!authenticate(this->auth, cred)) { | |
124 | - response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); | |
125 | - } else { | |
126 | - if (NULL == this->session) { | |
127 | - this->session = sessionAdd( | |
128 | - this->sroot, | |
129 | - new(Session, | |
130 | - username->value, | |
131 | - username->nvalue)); | |
132 | - } else { | |
133 | - this->session->username = memMalloc(username->nvalue + 1); | |
134 | - this->session->username[username->nvalue] = 0; | |
135 | - memcpy(this->session->username, | |
136 | - username->value, | |
137 | - username->nvalue); | |
138 | - } | |
139 | - | |
140 | - nbuf = sprintf(buffer, | |
141 | - "sid=%lu;Path=/", | |
142 | - this->session->id); | |
143 | - | |
144 | - response = (HttpMessage)httpResponseSession( | |
145 | - this->session); | |
146 | - | |
147 | - hashAdd(response->header, | |
148 | - new(HttpHeader, | |
149 | - CSTRA("Set-Cookie"), | |
150 | - buffer, nbuf)); | |
151 | - } | |
152 | - delete(cred); | |
153 | - } | |
154 | - } | |
155 | - } | |
80 | + if (0 == strcmp("GET", this->current_request->method)) { | |
81 | + char html_asset[2048] = "./assets/html"; | |
82 | + char base_asset[2048] = "./assets"; | |
83 | + char main_asset[] = "/main.html"; | |
156 | 84 | |
157 | - if (0 == strcmp("GET", request->method)) { | |
158 | - if (0 == strcmp("/sessinfo/", request->path)) { | |
159 | - response = (HttpMessage)httpResponseSession(this->session); | |
160 | - } | |
85 | + char * asset_path = base_asset; | |
86 | + char * asset; | |
87 | + char * mime_type; | |
161 | 88 | |
162 | - else if (0 == strcmp("/sess/", request->path)) { | |
163 | - if (NULL == this->session) { | |
164 | - this->session = sessionAdd( | |
165 | - this->sroot, | |
166 | - new(Session, NULL, 0)); | |
167 | - } | |
168 | - response = (HttpMessage)httpResponseSession(this->session); | |
89 | + if (0 == strcmp("/", this->current_request->path)) { | |
90 | + asset = main_asset; | |
91 | + } else { | |
92 | + asset = this->current_request->path; | |
169 | 93 | } |
170 | 94 | |
171 | - else if (0 == strcmp("/randval/", request->path)) { | |
172 | - if (NULL != this->session) { | |
173 | - response = (HttpMessage)httpResponseRandval( | |
174 | - this->val->timestamp, | |
175 | - this->val->value); | |
176 | - } else { | |
177 | - response = (HttpMessage)httpResponse403(); | |
178 | - } | |
95 | + mime_type = strrchr(asset, '.'); | |
96 | + if (NULL != mime_type) { | |
97 | + mime_type++; | |
98 | + mime_type = getMimeType(mime_type, strlen(mime_type)); | |
179 | 99 | } |
180 | 100 | |
181 | - else { | |
182 | - char html_asset[2048] = "./assets/html"; | |
183 | - char base_asset[2048] = "./assets"; | |
184 | - char main_asset[] = "/main.html"; | |
185 | - | |
186 | - char * asset_path = base_asset; | |
187 | - char * asset; | |
188 | - char * mime_type; | |
189 | - | |
190 | - if (0 == strcmp("/", request->path)) { | |
191 | - asset = main_asset; | |
192 | - } else { | |
193 | - asset = request->path; | |
194 | - } | |
195 | - | |
196 | - mime_type = strrchr(asset, '.'); | |
197 | - if (NULL != mime_type) { | |
198 | - mime_type++; | |
199 | - mime_type = getMimeType(mime_type, strlen(mime_type)); | |
200 | - } | |
201 | - | |
202 | - if (NULL != mime_type && | |
203 | - 0 == memcmp(mime_type, CSTRA("text/html"))) { | |
204 | - asset_path = html_asset; | |
205 | - } | |
206 | - | |
207 | - strcat(asset_path, asset); | |
208 | - response = httpWorkerGetAsset(this, request, asset_path); | |
101 | + if (NULL != mime_type && | |
102 | + 0 == memcmp(mime_type, CSTRA("text/html"))) { | |
103 | + asset_path = html_asset; | |
209 | 104 | } |
210 | 105 | |
106 | + strcat(asset_path, asset); | |
107 | + this->current_response = | |
108 | + httpWorkerGetAsset(this, asset_path); | |
211 | 109 | } |
212 | 110 | |
213 | - if (NULL == response) { | |
214 | - response = (HttpMessage)httpResponse404(); | |
111 | + if (NULL == this->current_response) { | |
112 | + this->current_response = (HttpMessage)httpResponse404(); | |
215 | 113 | } |
216 | 114 | |
217 | - httpWorkerAddCommonHeader((HttpMessage)request, response); | |
218 | - delete(request); | |
219 | - queuePut(this->writer->queue, response); | |
220 | - response = NULL; | |
115 | + httpWorkerAddCommonHeader(this); | |
116 | + delete(this->current_request); | |
117 | + queuePut(this->writer->queue, this->current_response); | |
118 | + this->current_response = NULL; | |
221 | 119 | } |
222 | 120 | } |
223 | 121 | ... | ... |
... | ... | @@ -29,9 +29,9 @@ const struct interface i_Observer = { |
29 | 29 | }; |
30 | 30 | |
31 | 31 | void |
32 | -observerNotify(void * observer, void * subject) | |
32 | +observerUpdate(void * observer, void * subject) | |
33 | 33 | { |
34 | - CALL(observer, Observer, notify, subject); | |
34 | + CALL(observer, Observer, update, subject); | |
35 | 35 | } |
36 | 36 | |
37 | 37 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -134,9 +134,11 @@ main() |
134 | 134 | |
135 | 135 | default: |
136 | 136 | { |
137 | - AuthLdap auth; | |
138 | - HttpWorker worker; | |
139 | - Server server; | |
137 | + AuthLdap auth; | |
138 | + //Application application; | |
139 | + //ApplicationAdapterHttp adapterHttp; | |
140 | + HttpWorker worker; | |
141 | + Server server; | |
140 | 142 | |
141 | 143 | value = mmap (0, sizeof(int), PROT_READ|PROT_WRITE, |
142 | 144 | MAP_SHARED, shm, 0); |
... | ... | @@ -146,9 +148,13 @@ main() |
146 | 148 | |
147 | 149 | logger = new(LoggerSyslog, LOGGER_DEBUG); |
148 | 150 | auth = new(AuthLdap, |
149 | - "ldap://localhost/", | |
151 | + "ldap://hosted/", | |
150 | 152 | CSTRA("ou=user,dc=yabrog,dc=weird-web-workers,dc=org")); |
151 | - worker = new(HttpWorker, "testserver", value, auth); | |
153 | + worker = new(HttpWorker, "testserver"); | |
154 | + application = new(Application, value, auth); | |
155 | + adapterHttp = new(ApplicationAdapterHttp, application); | |
156 | + subjectAttach(worker, adapterHttp); | |
157 | + | |
152 | 158 | server = new(Server, logger, worker, 11212, SOMAXCONN); |
153 | 159 | |
154 | 160 | if (NULL != server) { | ... | ... |
Please
register
or
login
to post a comment