Skip to content

Commit e33da64

Browse files
committed
renamed callbacks in ebb_request. fixed bug in ebb.c.
the callback naming scheme is thus: on_* some event new_* allocation
1 parent c8e48de commit e33da64

File tree

6 files changed

+93
-93
lines changed

6 files changed

+93
-93
lines changed

ebb.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ static ssize_t nosigpipe_push(void *data, const void *buf, size_t len)
4646
return send(fd, buf, len, MSG_NOSIGNAL);
4747
}
4848

49+
static void
50+
close_connection(ebb_connection *connection)
51+
{
52+
#ifdef HAVE_GNUTLS
53+
if(connection->server->secure)
54+
ev_io_stop(connection->server->loop, &connection->handshake_watcher);
55+
#endif
56+
ev_io_stop(connection->server->loop, &connection->read_watcher);
57+
ev_io_stop(connection->server->loop, &connection->write_watcher);
58+
ev_timer_stop(connection->server->loop, &connection->timeout_watcher);
59+
60+
if(0 > close(connection->fd))
61+
error(0, 0, "problem closing connection fd");
62+
63+
connection->open = FALSE;
64+
65+
if(connection->on_close)
66+
connection->on_close(connection);
67+
/* No access to the connection past this point!
68+
* The user is allowed to free in the callback
69+
*/
70+
}
71+
4972
#ifdef HAVE_GNUTLS
5073
#define GNUTLS_NEED_WRITE (gnutls_record_get_direction(connection->session) == 1)
5174
#define GNUTLS_NEED_READ (gnutls_record_get_direction(connection->session) == 0)
@@ -150,29 +173,6 @@ session_cache_remove (void *data, gnutls_datum_t key)
150173
return 0;
151174
}
152175

153-
static void
154-
close_connection(ebb_connection *connection)
155-
{
156-
#ifdef HAVE_GNUTLS
157-
if(connection->server->secure)
158-
ev_io_stop(connection->server->loop, &connection->handshake_watcher);
159-
#endif
160-
ev_io_stop(connection->server->loop, &connection->read_watcher);
161-
ev_io_stop(connection->server->loop, &connection->write_watcher);
162-
ev_timer_stop(connection->server->loop, &connection->timeout_watcher);
163-
164-
if(0 > close(connection->fd))
165-
error(0, 0, "problem closing connection fd");
166-
167-
connection->open = FALSE;
168-
169-
if(connection->on_close)
170-
connection->on_close(connection);
171-
/* No access to the connection past this point!
172-
* The user is allowed to free in the callback
173-
*/
174-
}
175-
176176
static void
177177
on_handshake(struct ev_loop *loop ,ev_io *watcher, int revents)
178178
{
@@ -345,7 +345,7 @@ static void on_writable(struct ev_loop *loop, ev_io *watcher, int revents)
345345
} else {
346346
#endif /* HAVE_GNUTLS */
347347

348-
sent = nosigpipe_push(connection, buf->base + buf->written, buf->len - buf->written);
348+
sent = nosigpipe_push((void*)connection->fd, buf->base + buf->written, buf->len - buf->written);
349349
if(sent < 0) goto error;
350350
if(sent == 0) return;
351351

ebb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define EBB_H
99

1010
/* remove this if you want to embed libebb without GNUTLS */
11-
#define HAVE_GNUTLS 1
11+
//#define HAVE_GNUTLS 1
1212

1313
#include <sys/socket.h>
1414
#include <netinet/in.h>

ebb_request_parser.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ struct ebb_request {
5353
char multipart_boundary[EBB_MAX_MULTIPART_BOUNDARY_LEN]; /* ro */
5454
unsigned int multipart_boundary_len; /* ro */
5555

56-
/* Public */
57-
ebb_element_cb request_path;
58-
ebb_element_cb query_string;
59-
ebb_element_cb request_uri;
60-
ebb_element_cb fragment;
61-
ebb_header_cb header_field;
62-
ebb_header_cb header_value;
63-
void (*headers_complete)(ebb_request *);
64-
ebb_element_cb body_handler;
65-
void (*request_complete)(ebb_request *);
56+
/* Public - ordered list of callbacks */
57+
ebb_element_cb on_path;
58+
ebb_element_cb on_query_string;
59+
ebb_element_cb on_uri;
60+
ebb_element_cb on_fragment;
61+
ebb_header_cb on_header_field;
62+
ebb_header_cb on_header_value;
63+
void (*on_headers_complete)(ebb_request *);
64+
ebb_element_cb on_body;
65+
void (*on_complete)(ebb_request *);
6666
void *data;
6767
};
6868

@@ -76,8 +76,8 @@ struct ebb_request_parser {
7676
const char *header_field_mark;
7777
const char *header_value_mark;
7878
const char *query_string_mark;
79-
const char *request_path_mark;
80-
const char *request_uri_mark;
79+
const char *path_mark;
80+
const char *uri_mark;
8181
const char *fragment_mark;
8282

8383
/* Public */

ebb_request_parser.rl

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
#define CONTENT_LENGTH (parser->current_request->content_length)
2222

2323
#define LEN(FROM) (p - parser->FROM##_mark)
24-
#define CALLBACK(FOR) \
25-
if(parser->FOR##_mark && CURRENT->FOR) { \
26-
CURRENT->FOR( CURRENT \
27-
, parser->FOR##_mark \
28-
, p - parser->FOR##_mark \
29-
); \
24+
#define CALLBACK(FOR) \
25+
if(parser->FOR##_mark && CURRENT->on_##FOR) { \
26+
CURRENT->on_##FOR( CURRENT \
27+
, parser->FOR##_mark \
28+
, p - parser->FOR##_mark \
29+
); \
3030
}
31-
#define HEADER_CALLBACK(FOR) \
32-
if(parser->FOR##_mark && CURRENT->FOR) { \
33-
CURRENT->FOR( CURRENT \
34-
, parser->FOR##_mark \
35-
, p - parser->FOR##_mark \
36-
, CURRENT->number_of_headers \
37-
); \
31+
#define HEADER_CALLBACK(FOR) \
32+
if(parser->FOR##_mark && CURRENT->on_##FOR) { \
33+
CURRENT->on_##FOR( CURRENT \
34+
, parser->FOR##_mark \
35+
, p - parser->FOR##_mark \
36+
, CURRENT->number_of_headers \
37+
); \
3838
}
39-
#define END_REQUEST \
40-
if(CURRENT->request_complete) \
41-
CURRENT->request_complete(CURRENT); \
39+
#define END_REQUEST \
40+
if(CURRENT->on_complete) \
41+
CURRENT->on_complete(CURRENT); \
4242
CURRENT = NULL;
4343

4444
%%{
@@ -48,8 +48,8 @@
4848
action mark_header_value { parser->header_value_mark = p; }
4949
action mark_fragment { parser->fragment_mark = p; }
5050
action mark_query_string { parser->query_string_mark = p; }
51-
action mark_request_path { parser->request_path_mark = p; }
52-
action mark_request_uri { parser->request_uri_mark = p; }
51+
action mark_request_path { parser->path_mark = p; }
52+
action mark_request_uri { parser->uri_mark = p; }
5353

5454
action method_copy { CURRENT->method = EBB_COPY; }
5555
action method_delete { CURRENT->method = EBB_DELETE; }
@@ -80,8 +80,8 @@
8080

8181
action request_uri {
8282
//printf("request uri\n");
83-
CALLBACK(request_uri);
84-
parser->request_uri_mark = NULL;
83+
CALLBACK(uri);
84+
parser->uri_mark = NULL;
8585
}
8686

8787
action fragment {
@@ -98,8 +98,8 @@
9898

9999
action request_path {
100100
//printf("request path\n");
101-
CALLBACK(request_path);
102-
parser->request_path_mark = NULL;
101+
CALLBACK(path);
102+
parser->path_mark = NULL;
103103
}
104104

105105
action content_length {
@@ -146,8 +146,8 @@
146146
}
147147

148148
action end_headers {
149-
if(CURRENT->headers_complete)
150-
CURRENT->headers_complete(CURRENT);
149+
if(CURRENT->on_headers_complete)
150+
CURRENT->on_headers_complete(CURRENT);
151151
}
152152

153153
action add_to_chunk_size {
@@ -169,12 +169,12 @@
169169
//printf("chunk_size: %d\n", parser->chunk_size);
170170
if(parser->chunk_size > REMAINING) {
171171
parser->eating = TRUE;
172-
CURRENT->body_handler(CURRENT, p, REMAINING);
172+
CURRENT->on_body(CURRENT, p, REMAINING);
173173
parser->chunk_size -= REMAINING;
174174
fhold;
175175
fbreak;
176176
} else {
177-
CURRENT->body_handler(CURRENT, p, parser->chunk_size);
177+
CURRENT->on_body(CURRENT, p, parser->chunk_size);
178178
p += parser->chunk_size;
179179
parser->chunk_size = 0;
180180
parser->eating = FALSE;
@@ -216,8 +216,8 @@
216216
*
217217
*/
218218
p += 1;
219-
if( CURRENT->body_handler )
220-
CURRENT->body_handler(CURRENT, p, CURRENT->content_length);
219+
if( CURRENT->on_body )
220+
CURRENT->on_body(CURRENT, p, CURRENT->content_length);
221221

222222
p += CURRENT->content_length;
223223
CURRENT->body_read = CURRENT->content_length;
@@ -240,8 +240,8 @@
240240
p += 1;
241241
size_t eat = REMAINING;
242242

243-
if( CURRENT->body_handler && eat > 0)
244-
CURRENT->body_handler(CURRENT, p, eat);
243+
if( CURRENT->on_body && eat > 0)
244+
CURRENT->on_body(CURRENT, p, eat);
245245

246246
p += eat;
247247
CURRENT->body_read += eat;
@@ -385,8 +385,8 @@ void ebb_request_parser_init(ebb_request_parser *parser)
385385
parser->current_request = NULL;
386386

387387
parser->header_field_mark = parser->header_value_mark =
388-
parser->query_string_mark = parser->request_path_mark =
389-
parser->request_uri_mark = parser->fragment_mark = NULL;
388+
parser->query_string_mark = parser->path_mark =
389+
parser->uri_mark = parser->fragment_mark = NULL;
390390

391391
parser->new_request = NULL;
392392
}
@@ -418,7 +418,7 @@ size_t ebb_request_parser_execute(ebb_request_parser *parser, const char *buffer
418418
if(eat == parser->chunk_size) {
419419
parser->eating = FALSE;
420420
}
421-
CURRENT->body_handler(CURRENT, p, eat);
421+
CURRENT->on_body(CURRENT, p, eat);
422422
p += eat;
423423
parser->chunk_size -= eat;
424424
//printf("eat: %d\n", eat);
@@ -431,7 +431,7 @@ size_t ebb_request_parser_execute(ebb_request_parser *parser, const char *buffer
431431
//printf("eat normal body (before parse)\n");
432432
size_t eat = MIN(len, CURRENT->content_length - CURRENT->body_read);
433433

434-
CURRENT->body_handler(CURRENT, p, eat);
434+
CURRENT->on_body(CURRENT, p, eat);
435435
p += eat;
436436
CURRENT->body_read += eat;
437437

@@ -444,8 +444,8 @@ size_t ebb_request_parser_execute(ebb_request_parser *parser, const char *buffer
444444
if(parser->header_value_mark) parser->header_value_mark = buffer;
445445
if(parser->fragment_mark) parser->fragment_mark = buffer;
446446
if(parser->query_string_mark) parser->query_string_mark = buffer;
447-
if(parser->request_path_mark) parser->request_path_mark = buffer;
448-
if(parser->request_uri_mark) parser->request_uri_mark = buffer;
447+
if(parser->path_mark) parser->path_mark = buffer;
448+
if(parser->uri_mark) parser->uri_mark = buffer;
449449

450450
%% write exec;
451451

@@ -457,8 +457,8 @@ size_t ebb_request_parser_execute(ebb_request_parser *parser, const char *buffer
457457
HEADER_CALLBACK(header_value);
458458
CALLBACK(fragment);
459459
CALLBACK(query_string);
460-
CALLBACK(request_path);
461-
CALLBACK(request_uri);
460+
CALLBACK(path);
461+
CALLBACK(uri);
462462

463463
assert(p <= pe && "buffer overflow after parsing execute");
464464

@@ -488,15 +488,15 @@ void ebb_request_init(ebb_request *request)
488488
request->multipart_boundary_len = 0;
489489
request->keep_alive = -1;
490490

491-
request->request_complete = NULL;
492-
request->headers_complete = NULL;
493-
request->body_handler = NULL;
494-
request->header_field = NULL;
495-
request->header_value = NULL;
496-
request->request_uri = NULL;
497-
request->fragment = NULL;
498-
request->request_path = NULL;
499-
request->query_string = NULL;
491+
request->on_complete = NULL;
492+
request->on_headers_complete = NULL;
493+
request->on_body = NULL;
494+
request->on_header_field = NULL;
495+
request->on_header_value = NULL;
496+
request->on_uri = NULL;
497+
request->on_fragment = NULL;
498+
request->on_path = NULL;
499+
request->on_query_string = NULL;
500500
}
501501

502502
int ebb_request_should_keep_alive(ebb_request *request)

examples/hello_world.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ int main()
9090
ebb_server server;
9191

9292
ebb_server_init(&server, loop);
93-
ebb_server_set_secure(&server, "examples/ca-cert.pem", "examples/ca-key.pem");
93+
//ebb_server_set_secure(&server, "examples/ca-cert.pem", "examples/ca-key.pem");
9494
server.new_connection = new_connection;
9595

9696
printf("hello_world listening on port 5000\n");

test_request_parser.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,14 @@ ebb_request* new_request ()
417417
ebb_request *r = &requests[num_requests].request;
418418
ebb_request_init(r);
419419

420-
r->request_complete = request_complete;
421-
r->header_field = header_field_cb;
422-
r->header_value = header_value_cb;
423-
r->request_path = request_path_cb;
424-
r->request_uri = request_uri_cb;
425-
r->fragment = fragment_cb;
426-
r->query_string = query_string_cb;
427-
r->body_handler = body_handler;
420+
r->on_complete = request_complete;
421+
r->on_header_field = header_field_cb;
422+
r->on_header_value = header_value_cb;
423+
r->on_path = request_path_cb;
424+
r->on_uri = request_uri_cb;
425+
r->on_fragment = fragment_cb;
426+
r->on_query_string = query_string_cb;
427+
r->on_body = body_handler;
428428

429429
r->data = &requests[num_requests];
430430
// printf("new request %d\n", num_requests);

0 commit comments

Comments
 (0)