Skip to content

Commit 023ae95

Browse files
committed
add local support
1 parent 1e96e1f commit 023ae95

File tree

7 files changed

+79
-38
lines changed

7 files changed

+79
-38
lines changed

Diff for: inc/ctorm.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@
263263
*/
264264
#define REQ_PARAM(param) ctorm_req_param(req, param)
265265

266+
/*!
267+
268+
* Get or set a local
269+
* @param[in] name Local name
270+
* @param[in] ... Local value
271+
272+
*/
273+
#define REQ_LOCAL(local, ...) ctorm_req_local(req, local, ##__VA_ARGS__)
274+
266275
/*!
267276
268277
* Parse URL form encoded request body
@@ -348,7 +357,7 @@
348357
349358
* Set response body to a formatted string
350359
* @param[in] fmt Format string
351-
* @return Format string arguments
360+
* @param[in] ... Arguments for the format string
352361
353362
*/
354363
#define RES_FMT(fmt, ...) ctorm_res_fmt(res, fmt, __VA_ARGS__)

Diff for: inc/errors.h

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ typedef enum {
5050
PortTooLarge = 9937,
5151
NameTooLarge = 9938,
5252
BadName = 9939,
53+
BadLocalPointer = 9940,
54+
BadQueryPointer = 9941,
55+
BadParamPointer = 9942,
5356
} ctorm_error_t;
5457

5558
/*!

Diff for: inc/log.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void ctorm_debug(const char *fmt, ...);
3737
* Print messages marked as "info", you can use this
3838
* to print informative messages
3939
40-
* @param[in] fmt Message format string
41-
* @param[in] ... Arguments for the formatted string
40+
* @param[in] fmt Format string
41+
* @param[in] ... Arguments for the format string
4242
4343
*/
4444
void ctorm_info(const char *fmt, ...);
@@ -48,8 +48,8 @@ void ctorm_info(const char *fmt, ...);
4848
* Print messages marked as "warn", you can use this
4949
* to print warnings
5050
51-
* @param[in] fmt Message format string
52-
* @param[in] ... Arguments for the formatted string
51+
* @param[in] fmt Format string
52+
* @param[in] ... Arguments for the format string
5353
5454
*/
5555
void ctorm_warn(const char *fmt, ...);
@@ -59,8 +59,8 @@ void ctorm_warn(const char *fmt, ...);
5959
* Print messages marked as "fail", you can use this
6060
* to print failures
6161
62-
* @param[in] fmt Message format string
63-
* @param[in] ... Arguments for the formatted string
62+
* @param[in] fmt Format string
63+
* @param[in] ... Arguments for the format string
6464
6565
*/
6666
void ctorm_fail(const char *fmt, ...);

Diff for: inc/req.h

+17-14
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@ typedef struct {
3333
bool received_headers; /// did we receive all the HTTP headers
3434
ctorm_url_t *queries; /// HTTP queries (for example "?key=1")
3535
ctorm_pair_t *params; /// HTTP path params (for example "/blog/:slug")
36+
ctorm_pair_t *locals; /// Local variables to pass along with the request
3637
int64_t bodysize; /// size of the HTTP body
3738
} ctorm_req_t;
3839

3940
#ifndef CTORM_EXPORT
4041

4142
#define ctorm_req_is_valid(req) \
4243
(NULL != (req)->version && NULL != (req)->encpath && NULL != (req)->path) // check if the request is valid
43-
void ctorm_req_init(ctorm_req_t *, connection_t *); // setup a request
44-
void ctorm_req_free(ctorm_req_t *); // cleanup a request
45-
bool ctorm_req_start(ctorm_req_t *); // receive the (at least the first part) of the HTTP request
46-
void ctorm_req_end(ctorm_req_t *); // completely receive the HTTP request
44+
void ctorm_req_init(ctorm_req_t *req, connection_t *con); // setup a request
45+
void ctorm_req_free(ctorm_req_t *req); // cleanup a request
46+
bool ctorm_req_start(ctorm_req_t *req); // receive the (at least the first part) of the HTTP request
47+
void ctorm_req_end(ctorm_req_t *req); // completely receive the HTTP request
4748

4849
#endif
4950

50-
const char *ctorm_req_method(ctorm_req_t *); // get the request method (GET, POST, PUT etc.)
51-
char *ctorm_req_query(ctorm_req_t *, char *name); // get a request URL query
52-
char *ctorm_req_param(ctorm_req_t *, char *name); // get a request URL param
53-
char *ctorm_req_get(ctorm_req_t *, char *header); // get a request header
51+
const char *ctorm_req_method(ctorm_req_t *req); // get the request method (GET, POST, PUT etc.)
52+
char *ctorm_req_query(ctorm_req_t *req, char *name); // get a request URL query
53+
char *ctorm_req_param(ctorm_req_t *req, char *name); // get a request URL param
54+
void *ctorm_req_local(ctorm_req_t *req, char *name, ...); // get or set a local by name
55+
char *ctorm_req_get(ctorm_req_t *req, char *header); // get a request header
5456

55-
uint64_t ctorm_req_body(ctorm_req_t *, char *buf, uint64_t size); // copy given amount of bytes from body to the buffer
56-
uint64_t ctorm_req_body_size(ctorm_req_t *); // get the body size
57+
uint64_t ctorm_req_body(
58+
ctorm_req_t *req, char *buf, uint64_t size); // copy given amount of bytes from body to the buffer
59+
uint64_t ctorm_req_body_size(ctorm_req_t *req); // get the body size
5760

58-
char *ctorm_req_ip(ctorm_req_t *, char *); // get the requester IPv4/IPv6 address as string
59-
#define ctorm_req_addr(r) ((r)->con->addr) // get the requester address as sockaddr
61+
char *ctorm_req_ip(ctorm_req_t *req, char *); // get the requester IPv4/IPv6 address as string
62+
#define ctorm_req_addr(req) ((req)->con->addr) // get the requester address as sockaddr
6063

61-
ctorm_url_t *ctorm_req_form(ctorm_req_t *); // parse URL encoded form body
62-
cJSON *ctorm_req_json(ctorm_req_t *); // parse JSON encoded form body
64+
ctorm_url_t *ctorm_req_form(ctorm_req_t *req); // parse URL encoded form body
65+
cJSON *ctorm_req_json(ctorm_req_t *req); // parse JSON encoded form body

Diff for: src/errors.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "errors.h"
2-
32
#include <string.h>
4-
#include <errno.h>
53

64
struct ctorm_error_desc descs[] = {
75
{BadTcpTimeout, "invalid TCP timeout" },
@@ -24,6 +22,9 @@ struct ctorm_error_desc descs[] = {
2422
{BadFmtPointer, "invalid string format pointer" },
2523
{BadPathPointer, "invalid path pointer" },
2624
{BadDataPointer, "invalid data pointer" },
25+
{BadLocalPointer, "invalid local name pointer" },
26+
{BadParamPointer, "invalid URL parameter name pointer" },
27+
{BadQueryPointer, "invalid URL query name pointer" },
2728
{BadHeaderPointer, "invalid header name/value pointer" },
2829
{BadMaxConnCount, "invalid max connection count" },
2930
{NoJSONSupport, "library not compiled with JSON support" },

Diff for: src/pair.c

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "pair.h"
2+
#include "errors.h"
23
#include "util.h"
34

45
#include <string.h>
@@ -12,6 +13,12 @@ ctorm_pair_t *ctorm_pair_add(ctorm_pair_t **head, char *key, char *value) {
1213
}
1314

1415
ctorm_pair_t *new = malloc(sizeof(ctorm_pair_t));
16+
17+
if (NULL == new) {
18+
errno = AllocFailed;
19+
return NULL;
20+
}
21+
1522
bzero(new, sizeof(ctorm_pair_t));
1623

1724
if (NULL != head) {

Diff for: src/req.c

+33-15
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
#include "errors.h"
33

44
#include "http.h"
5+
#include "pair.h"
56
#include "util.h"
67

78
#include "req.h"
89
#include "log.h"
910

1011
#include <arpa/inet.h>
1112

13+
#include <stdarg.h>
1214
#include <stdlib.h>
1315
#include <string.h>
1416

@@ -92,22 +94,16 @@ void ctorm_req_init(ctorm_req_t *req, connection_t *con) {
9294

9395
ctorm_headers_init(&req->headers);
9496
req->received_headers = false;
95-
96-
req->queries = NULL;
97-
req->params = NULL;
98-
req->bodysize = -1;
99-
100-
req->con = con;
101-
req->cancel = false;
102-
req->version = NULL;
103-
req->encpath = NULL;
104-
req->path = NULL;
97+
req->con = con;
98+
req->cancel = false;
99+
req->bodysize = -1;
105100
}
106101

107102
void ctorm_req_free(ctorm_req_t *req) {
108103
ctorm_headers_free(req->headers);
109104
ctorm_url_free(req->queries);
110105
ctorm_pair_free(req->params);
106+
ctorm_pair_free(req->locals);
111107

112108
free(req->encpath);
113109
free(req->path);
@@ -194,21 +190,43 @@ void ctorm_req_end(ctorm_req_t *req) {
194190
}
195191

196192
char *ctorm_req_query(ctorm_req_t *req, char *name) {
197-
if (NULL == name)
193+
if (NULL == name) {
194+
errno = BadQueryPointer;
198195
return NULL;
196+
}
197+
199198
return ctorm_url_get(req->queries, name);
200199
}
201200

202201
char *ctorm_req_param(ctorm_req_t *req, char *name) {
203-
if (NULL == name)
202+
if (NULL == name) {
203+
errno = BadParamPointer;
204204
return NULL;
205+
}
205206

206-
ctorm_pair_t *pair = ctorm_pair_find(req->params, name);
207+
ctorm_pair_t *param = ctorm_pair_find(req->params, name);
208+
return NULL == param ? NULL : param->value;
209+
}
207210

208-
if (NULL == pair)
211+
void *ctorm_req_local(ctorm_req_t *req, char *name, ...) {
212+
if (NULL == name) {
213+
errno = BadLocalPointer;
209214
return NULL;
215+
}
216+
217+
ctorm_pair_t *local = NULL;
218+
void *value = NULL;
219+
va_list args;
220+
221+
va_start(args, name);
222+
223+
if (NULL == (value = va_arg(args, void *)))
224+
local = ctorm_pair_find(req->locals, name);
225+
else
226+
local = ctorm_pair_add(&req->locals, name, value);
210227

211-
return pair->value;
228+
va_end(args);
229+
return NULL == local ? NULL : local->value;
212230
}
213231

214232
ctorm_url_t *ctorm_req_form(ctorm_req_t *req) {

0 commit comments

Comments
 (0)