-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.c
138 lines (122 loc) · 3.79 KB
/
request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "request.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "error.h"
static int parse_request_line(request_t* req, buffer_t* buf, error_t* err) {
char *start, *end, *iter;
int n, i, ret;
int spaces[3];
start = buf->buf + buf->front;
end = buf->buf + buf->rear;
for (i = 0, n = 0, iter = start; iter != end; iter++) {
if (*iter == ' ') {
if (i < 3) spaces[i] = n;
++i;
}
if (*iter == '\n') break;
++n;
}
if (iter == end) {
if (buffer_avaliable(buf) == 0) goto bad;
return 0;
}
if (i != 3 || iter == start || (iter == start + 1 && *start == '\r'))
goto bad;
buffer_drop(buf, n + 1);
if (*(iter - 1) == '\r') --n;
start[n] = '\0';
req->service_method = malloc(spaces[1] + 1);
ret = sscanf(start, "%s%s%ld%ld", req->service_method,
req->service_method + spaces[0] + 1, &req->argcnt, &req->seq);
req->service_method[spaces[0]] = '.';
if (ret == 4 && req->argcnt < 10) return 1;
bad:
error_put(err, ERR_BAD_RQUEST_LINE);
return -1;
}
static inline void copy(buffer_t* buff, char** dst, int len) {
*dst = malloc(len + 1);
memcpy(*dst, buff->buf + buff->front, len);
(*dst)[len] = '\0';
buffer_drop(buff, len);
}
static int parse_args(request_t* req, buffer_t* buf, error_t* err) {
int i;
char* start;
struct argument *cur_arg, *last_arg;
if (req->argcnt == 0) return 1;
if (req->args == NULL) {
req->args = malloc(sizeof(struct argument) * req->argcnt);
for (i = 0; i < req->argcnt; i++) argument_init(req->args + i);
req->cur_arg = req->args;
}
last_arg = req->args + (req->argcnt - 1);
while (1) {
cur_arg = req->cur_arg;
start = buf->buf + buf->front;
if (!cur_arg->head_bytes_read) {
if (buffer_buffered(buf) < 8) return 0;
cur_arg->type_kind = *(uint16_t*)start;
cur_arg->type_name_len = *(uint16_t*)(start + 2);
cur_arg->data_len = *(uint32_t*)(start + 4);
cur_arg->head_bytes_read = 1;
buffer_drop(buf, 8);
}
if (buffer_buffered(buf) < cur_arg->data_len + cur_arg->type_name_len)
return 0;
if (cur_arg->type_name_len != 0)
copy(buf, &cur_arg->type_name, cur_arg->type_name_len);
if (cur_arg->data_len != 0)
copy(buf, &cur_arg->data, cur_arg->data_len);
if (cur_arg == last_arg) break;
req->cur_arg++;
}
return 1;
}
int read_request(request_t* req, buffer_t* buf, error_t* err) {
int ret;
switch (req->state) {
case READING_REQUEST_LINE:
ret = parse_request_line(req, buf, err);
if (ret == 0 || ret == -1) break;
req->state = READING_ARGS;
case READING_ARGS:
ret = parse_args(req, buf, err);
if (ret == 0 || ret == -1) break;
req->state = READING_REQUEST_LINE;
break;
default:
ret = -1;
}
return ret;
}
request_t* request_new() {
request_t* req;
req = malloc(sizeof(request_t));
req->service_method = NULL;
req->cur_arg = req->args = NULL;
req->argcnt = req->seq = 0;
req->state = READING_REQUEST_LINE;
return req;
}
void request_destroy(request_t* req) {
int i;
free(req->service_method);
if (req->args != NULL) {
for (i = 0; i < req->argcnt; i++) {
argument_destroy(req->args + i);
}
free(req->args);
}
free(req);
}
void request_set_init_state(struct request* req) {
free(req->service_method);
free(req->args);
req->state = READING_REQUEST_LINE;
req->service_method = NULL;
req->seq = req->argcnt = 0;
req->args = req->cur_arg = NULL;
}