Showing
8 changed files
with
222 additions
and
0 deletions
include/http/request.h
0 → 100644
1 | +#ifndef __HTTP_REQUEST_H__ | ||
2 | +#define __HTTP_REQUEST_H__ | ||
3 | + | ||
4 | +#include "cclass.h" | ||
5 | + | ||
6 | +CLASS(HTTP_REQUEST) { | ||
7 | + char * http_version; | ||
8 | + char * uri; | ||
9 | + char * method; | ||
10 | + | ||
11 | + struct { | ||
12 | + char * name; | ||
13 | + char * value; | ||
14 | + } header[128]; | ||
15 | + | ||
16 | + char * body; | ||
17 | + | ||
18 | + unsigned char done; | ||
19 | +}; | ||
20 | + | ||
21 | +#endif /* __HTTP_REQUEST_H__ */ | ||
22 | + | ||
23 | +// vim: set ts=4 sw=4: |
include/http/request_parser.h
0 → 100644
1 | +#ifndef __HTTP_REQUEST_PARSER_H__ | ||
2 | +#define __HTTP_REQUEST_PARSER_H__ | ||
3 | + | ||
4 | +#include "cclass.h" | ||
5 | +#include "server.h" | ||
6 | +#include "http/request.h" | ||
7 | +#include "http/request_queue.h" | ||
8 | + | ||
9 | +#define HTTP_REQUEST_PARSER_READ_CHUNK 1024 | ||
10 | + | ||
11 | +#define HTTP_REQUEST_PARSER_START 0 | ||
12 | +#define HTTP_REQUEST_PARSER_REQUEST_LINE_DONE 1 | ||
13 | +#define HTTP_REQUEST_PARSER_HEADERS_DONE 2 | ||
14 | +#define HTTP_REQUEST_PARSER_DONE 3 | ||
15 | + | ||
16 | + | ||
17 | +CLASS(HTTP_REQUEST_PARSER) { | ||
18 | + server_read_hook get_data; | ||
19 | + | ||
20 | + char * buffer; | ||
21 | + size_t buffer_used; | ||
22 | + | ||
23 | + HTTP_REQUEST_QUEUE request_queue; | ||
24 | + | ||
25 | + unsigned char state; | ||
26 | +}; | ||
27 | + | ||
28 | +void http_request_parser_parse(const char * buffer, size_t size); | ||
29 | + | ||
30 | +#endif /* __HTTP_REQUEST_PARSER_H__ */ | ||
31 | + | ||
32 | +// vim: set ts=4 sw=4: |
include/http/request_queue.h
0 → 100644
1 | +#ifndef __HTTP_REQUEST_QUEUE_H__ | ||
2 | +#define __HTTP_REQUEST_QUEUE_H__ | ||
3 | + | ||
4 | +#include "cclass.h" | ||
5 | +#include "http/request.h" | ||
6 | + | ||
7 | +#define HTTP_REQUEST_QUEUE_MAX 1024 | ||
8 | + | ||
9 | + | ||
10 | +CLASS(HTTP_REQUEST_QUEUE) { | ||
11 | + REQUEST requests[HTTP_REQUEST_QUEUE_MAX]; | ||
12 | + size_t nrequests; | ||
13 | +} | ||
14 | + | ||
15 | +#endif /* __HTTP_REQUEST_QUEUE_H__ */ | ||
16 | + | ||
17 | +// vim: set ts=4 sw=4: |
src/http/request.c
0 → 100644
1 | +#include <stdlib.h> | ||
2 | + | ||
3 | +#include "cclass.h" | ||
4 | +#include "http/request.h" | ||
5 | + | ||
6 | +INIT_CLASS(HTTP_REQUEST); | ||
7 | + | ||
8 | +static void | ||
9 | +_free(void ** data) | ||
10 | +{ | ||
11 | + if (NULL != *data) { | ||
12 | + free(*data); | ||
13 | + } | ||
14 | +} | ||
15 | + | ||
16 | +__construct(LOGGER) {} | ||
17 | + | ||
18 | +__destruct(LOGGER) | ||
19 | +{ | ||
20 | + int i; | ||
21 | + | ||
22 | + _free(&(this->http_version)); | ||
23 | + _free(&(this->uri)); | ||
24 | + _free(&(this->method)); | ||
25 | + | ||
26 | + for (i=0; i<128; i++) { | ||
27 | + _free(&((this->header)[i].name)); | ||
28 | + _free(&((this->header)[i].value)); | ||
29 | + } | ||
30 | + | ||
31 | + _free(&(this->body)); | ||
32 | +} | ||
33 | + | ||
34 | +__jsonConst(LOGGER) {} | ||
35 | +__toJson(LOGGER) {} | ||
36 | +__clear(LOGGER) {} | ||
37 | + | ||
38 | +// vim: set ts=4 sw=4: |
src/http/request_parser.c
0 → 100644
1 | +#include <stdlib.h> | ||
2 | + | ||
3 | +#include "cclass.h" | ||
4 | +#include "http/request.h" | ||
5 | +#include "http/request_parser.h" | ||
6 | + | ||
7 | + | ||
8 | +INIT_CLASS(HTTP_REQUEST_PARSER); | ||
9 | + | ||
10 | + | ||
11 | +__construct(LOGGER) | ||
12 | +{ | ||
13 | + this->request_queue = va_arg(*params, HTTP_REQUEST_QUEUE); | ||
14 | + | ||
15 | + this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK); | ||
16 | +} | ||
17 | + | ||
18 | +__destruct(LOGGER) | ||
19 | +{ | ||
20 | + free(this->buffer); | ||
21 | +} | ||
22 | + | ||
23 | +__jsonConst(LOGGER) {} | ||
24 | +__toJson(LOGGER) {} | ||
25 | +__clear(LOGGER) {} | ||
26 | + | ||
27 | + | ||
28 | +static void | ||
29 | +get_data(HTTP_REQUEST_PARSER this, const char * data, size_t size) | ||
30 | +{ | ||
31 | + size_t remaining, chunks; | ||
32 | + | ||
33 | + remaining = this->buffer_used % HTTP_REQUEST_PARSER_READ_CHUNK; | ||
34 | + chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; | ||
35 | + | ||
36 | + chunks = (0 == remaining) ? chunks++ : chunks; | ||
37 | + | ||
38 | + if (size > remaining) { | ||
39 | + this->buffer = | ||
40 | + realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK); | ||
41 | + } | ||
42 | + | ||
43 | + memcpy(this->buffer + this->buffer_used, data, size); | ||
44 | + this->buffer_used += size; | ||
45 | +} | ||
46 | + | ||
47 | +void http_request_parser_parse(const char * data, size_t size) | ||
48 | +{ | ||
49 | +} | ||
50 | + | ||
51 | +// vim: set ts=4 sw=4: |
src/logger/add.c
0 → 100644
src/logger/log.c
0 → 100644
1 | +#define _ISOC99_SOURCE | ||
2 | + | ||
3 | +#include <stdio.h> | ||
4 | +#include <string.h> | ||
5 | + | ||
6 | +#include "logger.h" | ||
7 | + | ||
8 | +void | ||
9 | +logger_log(LOGGER this, int level, const char * message, ...) { | ||
10 | + va_list args; | ||
11 | + char buffer[1025]; | ||
12 | + logger_logfnct * logfnct; | ||
13 | + | ||
14 | + int maxBuf = sizeof(buffer)/sizeof(buffer[0]); | ||
15 | + | ||
16 | + memset(buffer, 0, maxBuf); | ||
17 | + | ||
18 | + va_start(args, message); | ||
19 | + vsnprintf(buffer, 1024, message, args); | ||
20 | + va_end(args); | ||
21 | + | ||
22 | + logfnct = this->logfncts; | ||
23 | + | ||
24 | + while (NULL != *logfnct) { | ||
25 | + (*logfnct)(level, buffer); | ||
26 | + logfnct++; | ||
27 | + } | ||
28 | +} | ||
29 | + | ||
30 | +// vim: set ts=4 sw=4: |
src/logger/syslog.c
0 → 100644
1 | +#include <syslog.h> | ||
2 | + | ||
3 | +const int priority[] = { | ||
4 | + LOG_USER | LOG_EMERG, | ||
5 | + LOG_USER | LOG_ALERT, | ||
6 | + LOG_USER | LOG_CRIT, | ||
7 | + LOG_USER | LOG_ERR, | ||
8 | + LOG_USER | LOG_WARNING, | ||
9 | + LOG_USER | LOG_NOTICE, | ||
10 | + LOG_USER | LOG_INFO, | ||
11 | + LOG_USER | LOG_DEBUG | ||
12 | +}; | ||
13 | + | ||
14 | +void | ||
15 | +logger_syslog(int level, const char * msg) | ||
16 | +{ | ||
17 | + syslog(priority[level], "%s", msg); | ||
18 | +} | ||
19 | + | ||
20 | +// vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment