Commit 07316ef7b2bdca89db8da79a01cd3c505dd07a65

Authored by Georg Hopp
1 parent e59dc48f

created and used a more generalized httpResponseJson

... ... @@ -48,12 +48,7 @@ HttpResponse httpResponse304(
48 48 HttpResponse httpResponse403();
49 49 HttpResponse httpResponse404();
50 50 HttpResponse httpResponse500();
51   -HttpResponse httpResponseMe();
52   -HttpResponse httpResponseLoginForm();
53   -HttpResponse httpResponseRandval(time_t, int);
54   -HttpResponse httpResponseSession(Session);
55   -HttpResponse httpResponseUser(User);
56   -HttpResponse httpResponseVersion(const char *);
  51 +HttpResponse httpResponseJson(const char *, size_t);
57 52 HttpResponse httpResponseAsset(const char *, size_t);
58 53
59 54 #endif // __HTTP_RESPONSE_H__
... ...
... ... @@ -41,8 +41,12 @@
41 41
42 42
43 43 #define NO_SESSION_SID NULL
44   -#define SESS_HEADER "{\"id\":\"%s\",\"timeout\":%d,\"timeleft\":%ld}"
45 44
  45 +#define RANDVAL_JSON "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}"
  46 +#define SESSION_JSON "{\"id\":\"%s\",\"timeout\":%d,\"timeleft\":%ld}"
  47 +#define USER_JSON \
  48 + "{\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}"
  49 +#define VERSION_JSON "{\"version\":\"%s\"}"
46 50
47 51 static
48 52 inline
... ... @@ -58,6 +62,63 @@ getSessionId(Hash cookies)
58 62 return NO_SESSION_SID;
59 63 }
60 64
  65 +HttpMessage
  66 +responseVersion(const char * version)
  67 +{
  68 + char buffer[200];
  69 + size_t nbuf;
  70 +
  71 + nbuf = sprintf(buffer, VERSION_JSON, version? version : "");
  72 + return (HttpMessage)httpResponseJson(buffer, nbuf);
  73 +}
  74 +
  75 +HttpMessage
  76 +responseRandval(struct randval * val)
  77 +{
  78 + char buffer[200];
  79 + size_t nbuf;
  80 + time_t remaining;
  81 +
  82 + remaining = 10 - (time(NULL) - val->timestamp);
  83 +
  84 + nbuf = sprintf(
  85 + buffer,
  86 + RANDVAL_JSON,
  87 + val->timestamp,
  88 + remaining,
  89 + val->value);
  90 +
  91 + return (HttpMessage)httpResponseJson(buffer, nbuf);
  92 +}
  93 +
  94 +HttpMessage
  95 +responseSession(Session session)
  96 +{
  97 + char buffer[200];
  98 + size_t nbuf;
  99 +
  100 + nbuf = sprintf(buffer, SESSION_JSON,
  101 + (NULL != session)? session->id : "",
  102 + (NULL != session)? SESSION_LIVETIME : 0,
  103 + (NULL != session)? session->livetime - time(NULL) : 0);
  104 +
  105 + return (HttpMessage)httpResponseJson(buffer, nbuf);
  106 +}
  107 +
  108 +HttpMessage
  109 +responseUser(User user)
  110 +{
  111 + char buffer[200];
  112 + size_t nbuf;
  113 +
  114 + nbuf = sprintf(buffer, USER_JSON,
  115 + (NULL != user)? user->email : "",
  116 + (NULL != user)? user->firstname : "",
  117 + (NULL != user)? user->surname : "");
  118 +
  119 + return (HttpMessage)httpResponseJson(buffer, nbuf);
  120 +}
  121 +
61 122 static
62 123 void
63 124 loginAdapter(Application application, HttpWorker worker, Session session)
... ... @@ -80,8 +141,7 @@ loginAdapter(Application application, HttpWorker worker, Session session)
80 141 }
81 142
82 143 if (NULL == username || NULL == password) {
83   - worker->current_response =
84   - new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
  144 + worker->current_response = (HttpMessage)httpResponse403();
85 145 return;
86 146 }
87 147
... ... @@ -91,11 +151,9 @@ loginAdapter(Application application, HttpWorker worker, Session session)
91 151 (char *)(password->value), password->nvalue);
92 152
93 153 if (! applicationLogin(application, credential, session)) {
94   - worker->current_response =
95   - new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
  154 + worker->current_response = (HttpMessage)httpResponse403();
96 155 } else {
97   - worker->current_response =
98   - (HttpMessage)httpResponseUser(session->user);
  156 + worker->current_response = responseUser(session->user);
99 157 }
100 158
101 159 delete(credential);
... ... @@ -168,7 +226,6 @@ signupAdapter(Application application, HttpWorker worker, Session session)
168 226 delete(user);
169 227 }
170 228
171   -
172 229 void
173 230 applicationAdapterHttpUpdate(void * _this, void * subject)
174 231 {
... ... @@ -211,36 +268,32 @@ applicationAdapterHttpUpdate(void * _this, void * subject)
211 268 if (0 == strcmp("GET", worker->current_request->method)) {
212 269 if (0 == strcmp("/version/", worker->current_request->path)) {
213 270 worker->current_response =
214   - (HttpMessage)httpResponseVersion(this->application->version);
  271 + responseVersion(this->application->version);
215 272 return;
216 273 }
217 274
218 275 if (0 == strcmp("/user/get/", worker->current_request->path)) {
219   - worker->current_response =
220   - (HttpMessage)httpResponseUser(session->user);
  276 + worker->current_response = responseUser(session->user);
221 277 return;
222 278 }
223 279
224 280 if (0 == strcmp("/logout/", worker->current_request->path)) {
225 281 applicationLogout(this->application, session);
226 282
227   - worker->current_response =
228   - (HttpMessage)httpResponseUser(session->user);
  283 + worker->current_response = responseUser(session->user);
229 284 return;
230 285 }
231 286
232 287 if (0 == strcmp("/sessinfo/", worker->current_request->path)) {
233   - worker->current_response =
234   - (HttpMessage)httpResponseSession(session);
  288 + worker->current_response = responseSession(session);
235 289 return;
236 290 }
237 291
238 292 if (0 == strcmp("/randval/", worker->current_request->path)) {
239 293 if (NULL != session->user) {
240 294 worker->current_response =
241   - (HttpMessage)httpResponseRandval(
242   - this->application->val->timestamp,
243   - this->application->val->value);
  295 + responseRandval(this->application->val);
  296 + return;
244 297 } else {
245 298 worker->current_response = (HttpMessage)httpResponse403();
246 299 }
... ... @@ -248,7 +301,7 @@ applicationAdapterHttpUpdate(void * _this, void * subject)
248 301 }
249 302
250 303 // if (0 < session->livetime - now) {
251   - // nbuf = sprintf(buf, SESS_HEADER,
  304 + // nbuf = sprintf(buf, SESSION_JSON,
252 305 // session->id,
253 306 // SESSION_LIVETIME,
254 307 // session->livetime - now);
... ...
... ... @@ -14,12 +14,8 @@ RESP = response.c \
14 14 response/404.c \
15 15 response/403.c \
16 16 response/500.c \
17   - response/login_form.c \
18 17 response/asset.c \
19   - response/randval.c \
20   - response/session.c \
21   - response/version.c \
22   - response/user.c
  18 + response/json.c
23 19 PARSER = parser.c \
24 20 parser/parse.c \
25 21 parser/new_message.c \
... ...
... ... @@ -36,15 +36,11 @@
36 36 #include "utils/memory.h"
37 37 #include "hash.h"
38 38
39   -#define RESP_DATA "{\"version\":\"%s\"}"
40   -
41 39 HttpResponse
42   -httpResponseVersion(const char * version)
  40 +httpResponseJson(const char * body, size_t nbody)
43 41 {
44   - char buffer[200];
45 42 HttpResponse response;
46 43 HttpMessage message;
47   - size_t nbuf;
48 44
49 45 response = new(HttpResponse, "HTTP/1.1", 200, "OK");
50 46 message = (HttpMessage)response;
... ... @@ -52,11 +48,9 @@ httpResponseVersion(const char * version)
52 48 hashAdd(message->header,
53 49 new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
54 50
55   - nbuf = sprintf(buffer, RESP_DATA, (NULL != version)? version : "");
56   -
57   - message->nbody = nbuf;
58   - message->body = memMalloc(nbuf);
59   - memcpy(message->body, buffer, nbuf);
  51 + message->nbody = nbody;
  52 + message->body = memMalloc(nbody);
  53 + memcpy(message->body, body, nbody);
60 54
61 55 return response;
62 56 }
... ...
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   -#include <stdlib.h>
24   -#include <string.h>
25   -#include <stdio.h>
26   -#include <time.h>
27   -#include <sys/types.h>
28   -
29   -#include "class.h"
30   -
31   -#include "http/response.h"
32   -#include "http/message.h"
33   -#include "http/header.h"
34   -
35   -#include "utils/memory.h"
36   -#include "hash.h"
37   -
38   -#define RESP_DATA "<form action=\"/me/\" method=\"POST\">" \
39   - "<input name=\"username\" type=\"text\" />" \
40   - "<input type=\"submit\">" \
41   -"</form>"
42   -
43   -HttpResponse
44   -httpResponseLoginForm()
45   -{
46   - HttpResponse response;
47   - HttpMessage message;
48   -
49   - response = new(HttpResponse, "HTTP/1.1", 200, "OK");
50   - message = (HttpMessage)response;
51   -
52   - hashAdd(message->header,
53   - new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html")));
54   -
55   - message->nbody = sizeof(RESP_DATA)-1;
56   - message->body = memMalloc(message->nbody);
57   - memcpy(message->body, RESP_DATA, message->nbody);
58   -
59   - return response;
60   -}
61   -
62   -// vim: set ts=4 sw=4:
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   -#include <stdlib.h>
24   -#include <string.h>
25   -#include <stdio.h>
26   -#include <time.h>
27   -#include <sys/types.h>
28   -
29   -#include "class.h"
30   -
31   -#include "http/response.h"
32   -#include "http/message.h"
33   -#include "http/header.h"
34   -
35   -#include "utils/memory.h"
36   -#include "hash.h"
37   -
38   -#define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}"
39   -
40   -HttpResponse
41   -httpResponseRandval(time_t ctime, int value)
42   -{
43   - char buffer[200];
44   - HttpResponse response;
45   - HttpMessage message;
46   - size_t nbuf;
47   - time_t remaining;
48   -
49   - response = new(HttpResponse, "HTTP/1.1", 200, "OK");
50   - message = (HttpMessage)response;
51   -
52   - hashAdd(message->header,
53   - new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
54   -
55   - remaining = 10 - (time(NULL) - ctime);
56   -
57   - nbuf = sprintf(buffer, RESP_DATA, ctime, remaining, value);
58   -
59   - message->nbody = nbuf;
60   - message->body = memMalloc(nbuf);
61   - memcpy(message->body, buffer, nbuf);
62   -
63   - return response;
64   -}
65   -
66   -// vim: set ts=4 sw=4:
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   -#include <stdlib.h>
24   -#include <string.h>
25   -#include <stdio.h>
26   -#include <time.h>
27   -#include <sys/types.h>
28   -
29   -#include "class.h"
30   -
31   -#include "http/response.h"
32   -#include "http/message.h"
33   -#include "http/header.h"
34   -#include "session.h"
35   -
36   -#include "utils/memory.h"
37   -#include "hash.h"
38   -
39   -#define RESP_DATA "{\"id\":\"%s\",\"timeout\":%d,\"timeleft\":%ld}"
40   -
41   -HttpResponse
42   -httpResponseSession(Session session)
43   -{
44   - char buffer[200];
45   - HttpResponse response;
46   - HttpMessage message;
47   - size_t nbuf;
48   -
49   - response = new(HttpResponse, "HTTP/1.1", 200, "OK");
50   - message = (HttpMessage)response;
51   -
52   - hashAdd(message->header,
53   - new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
54   -
55   - nbuf = sprintf(buffer, RESP_DATA,
56   - (NULL != session)? session->id : "",
57   - (NULL != session)? SESSION_LIVETIME : 0,
58   - (NULL != session)? session->livetime - time(NULL) : 0);
59   -
60   - message->nbody = nbuf;
61   - message->body = memMalloc(nbuf);
62   - memcpy(message->body, buffer, nbuf);
63   -
64   - return response;
65   -}
66   -
67   -// vim: set ts=4 sw=4:
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   -#include <stdlib.h>
24   -#include <string.h>
25   -#include <stdio.h>
26   -#include <time.h>
27   -#include <sys/types.h>
28   -
29   -#include "class.h"
30   -
31   -#include "http/response.h"
32   -#include "http/message.h"
33   -#include "http/header.h"
34   -#include "session.h"
35   -
36   -#include "utils/memory.h"
37   -#include "hash.h"
38   -
39   -#define RESP_DATA "{\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}"
40   -
41   -HttpResponse
42   -httpResponseUser(User user)
43   -{
44   - char buffer[200];
45   - HttpResponse response;
46   - HttpMessage message;
47   - size_t nbuf;
48   -
49   - response = new(HttpResponse, "HTTP/1.1", 200, "OK");
50   - message = (HttpMessage)response;
51   -
52   - hashAdd(message->header,
53   - new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
54   -
55   - nbuf = sprintf(buffer, RESP_DATA,
56   - (NULL != user)? user->email : "",
57   - (NULL != user)? user->firstname : "",
58   - (NULL != user)? user->surname : "");
59   -
60   - message->nbody = nbuf;
61   - message->body = memMalloc(nbuf);
62   - memcpy(message->body, buffer, nbuf);
63   -
64   - return response;
65   -}
66   -
67   -// vim: set ts=4 sw=4:
Please register or login to post a comment