-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcshiori.c
354 lines (340 loc) · 12.1 KB
/
cshiori.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "cshiori.h"
bool cshiori_load(const char* dirpath, bool (*load)(const char*)){
return load(dirpath);
}
char* cshiori_request(char** const lines, const size_t lines_length, struct cshiori_response_message* (*request)(struct cshiori_request_message*, struct cshiori_response_message*)){
struct cshiori_request_message* req;
struct cshiori_response_message* res;
char* str;
req = cshiori_shiori_request_parse(lines, lines_length);
if(req == NULL) return cshiori_shiori_response_build_bad_request();
res = cshiori_response_message_new();
res = request(req, res);
cshiori_request_message_delete(req);
if(res == NULL) return cshiori_shiori_response_build_bad_request();
str = cshiori_shiori_response_build(res);
cshiori_response_message_delete(res);
return str;
}
bool cshiori_unload(bool (*unload)(void)){
return unload();
}
struct cshiori_request_message* cshiori_request_message_new(void){
struct cshiori_request_message* req = (struct cshiori_request_message*)malloc(sizeof(struct cshiori_request_message));
size_t i;
req->method = NONE;
req->version = NULL;
req->id = NULL;
req->sender = NULL;
req->charset = NULL;
for(i = 0; i < CSHIORI_REQUEST_MESSAGE_REFERENCE_SIZE; ++i) req->reference[i] = NULL;
req->security_level = NULL;
req->status = NULL;
req->base_id = NULL;
req->names = NULL;
req->values = NULL;
req->header_size = 0;
return req;
}
void cshiori_request_message_delete(struct cshiori_request_message* req){
size_t i;
/* free(req->id);
free(req->sender);
free(req->charset);
for(i = 0; i < CSHIORI_REQUEST_MESSAGE_REFERENCE_SIZE; ++i) free(req->reference[i]);
free(req->security_level);
free(req->status);
free(req->base_id);*/
for(i = 0; i < req->header_size; ++i){
free(*(req->names + i));
free(*(req->values + i));
}
free(req->names);
free(req->values);
free(req);
}
char* cshiori_request_message_get(struct cshiori_request_message* req, const char* const name){
size_t i;
for(i = 0; i < req->header_size; ++i){
char* h_name = *(req->names + i);
if(h_name != NULL && (0 == strcmp(h_name, name))){
return *(req->values + i);
}
}
return NULL;
}
bool cshiori_request_message_set(struct cshiori_request_message* req, char* const name, char* const value){
char** names_new;
char** values_new;
size_t i;
for(i = 0; i < req->header_size; ++i){
char* h_name = *(req->names + i);
if(h_name != NULL && (0 == strcmp(h_name, name))){
free(*(req->values + i));
*(req->values + i) = value;
return true;
}
}
names_new = (char**)realloc(req->names, sizeof(char*) * req->header_size + 1);
if(names_new == NULL){
return false;
}
req->names = names_new;
values_new = (char**)realloc(req->values, sizeof(char*) * req->header_size + 1);
if(values_new == NULL){
return false;
}
req->values = values_new;
*(req->names + req->header_size) = name;
*(req->values + req->header_size) = value;
req->header_size ++;
return true;
}
struct cshiori_response_message* cshiori_response_message_new(void){
struct cshiori_response_message* res = (struct cshiori_response_message*)malloc(sizeof(struct cshiori_response_message));
size_t i;
res->status_code = 0;
res->version = NULL;
res->value = NULL;
res->sender = NULL;
res->charset = NULL;
for(i = 0; i < CSHIORI_RESPONSE_MESSAGE_REFERENCE_SIZE; ++i) res->reference[i] = NULL;
return res;
}
void cshiori_response_message_delete(struct cshiori_response_message* res){
size_t i;
free(res->value);
free(res->sender);
free(res->charset);
for(i = 0; i < CSHIORI_RESPONSE_MESSAGE_REFERENCE_SIZE; ++i) free(res->reference[i]);
free(res);
}
#define CSHIORI_SHIORI_REQUEST_PARSE_HEADER_UNIT_SIZE 10
struct cshiori_request_message* cshiori_shiori_request_parse(char** const lines, const size_t lines_length){
struct cshiori_request_message* req = cshiori_request_message_new();
char** names_new;
char** values_new;
size_t lines_index;
char* line;
char* header_separator;
/* status line */
static const char* const shiori3_get_pre = "GET SHIORI/3.";
static const char* const shiori3_notify_pre = "NOTIFY SHIORI/3.";
static const char* const reference_pre = "Reference";
const size_t shiori3_get_pre_length = strlen(shiori3_get_pre);
const size_t shiori3_notify_pre_length = strlen(shiori3_notify_pre);
const size_t reference_pre_length = strlen(reference_pre);
line = *lines;
if(0 == strncmp(line, shiori3_get_pre, shiori3_get_pre_length)){
req->method = GET;
req->version = (char*)malloc(sizeof(char) * (strlen(line) - shiori3_get_pre_length + 3));
strcpy(req->version, line + shiori3_get_pre_length - 2);
}else if(0 == strncmp(line, shiori3_notify_pre, strlen(shiori3_notify_pre))){
req->method = NOTIFY;
req->version = (char*)malloc(sizeof(char) * (strlen(line) - shiori3_notify_pre_length + 3));
strcpy(req->version, line + shiori3_notify_pre_length - 2);
}else{
return NULL;
}
/* headers */
for(lines_index = 1; lines_index < lines_length - 1; ++lines_index){
char* name;
size_t name_length;
char* value;
size_t value_length;
line = *(lines + lines_index);
header_separator = strstr(line, ": ");
if(header_separator == NULL) return NULL;
name_length = header_separator - line;
name = (char*)malloc(sizeof(char) * (name_length + 1));
strncpy(name, line, name_length);
name[name_length] = '\0';
value_length = strlen(header_separator) - 2;
value = (char*)malloc(sizeof(char) * (value_length + 1));
strncpy(value, header_separator + 2, value_length);
value[value_length] = '\0';
if(0 == strncmp(name, reference_pre, reference_pre_length)){
char* number = name + reference_pre_length;
char nc;
size_t i;
bool is_number = true;
for(i = 0; i < name_length - reference_pre_length; ++i){
nc = *(number + i);
if(!('0' <= nc && nc <= '9')){
is_number = false;
break;
}
}
if(is_number){
size_t index = atoi(number);
if(index < CSHIORI_REQUEST_MESSAGE_REFERENCE_SIZE) req->reference[index] = value;
}
}
else if(0 == strcmp(name, "ID")){ req->id = value; }
else if(0 == strcmp(name, "Charset")){ req->charset = value; }
else if(0 == strcmp(name, "Sender")){ req->sender = value; }
else if(0 == strcmp(name, "SecurityLevel")){ req->security_level = value; }
else if(0 == strcmp(name, "Status")){ req->status = value; }
else if(0 == strcmp(name, "BaseID")){ req->base_id = value; }
if(0 == (req->header_size % CSHIORI_SHIORI_REQUEST_PARSE_HEADER_UNIT_SIZE)){
names_new = (char**)realloc(req->names, sizeof(char*) * (req->header_size + CSHIORI_SHIORI_REQUEST_PARSE_HEADER_UNIT_SIZE));
if(names_new == NULL){
size_t i;
for(i = 0; i < req->header_size; ++i){
free(*(req->names + i));
}
free(req->names);
return NULL;
}
req->names = names_new;
values_new = (char**)realloc(req->values, sizeof(char*) * (req->header_size + CSHIORI_SHIORI_REQUEST_PARSE_HEADER_UNIT_SIZE));
if(values_new == NULL){
size_t i;
for(i = 0; i < req->header_size; ++i){
free(*(req->values + i));
}
free(req->values);
return NULL;
}
req->values = values_new;
}
*(req->names + req->header_size) = name;
*(req->values + req->header_size) = value;
req->header_size ++;
}
return req;
}
char* cshiori_shiori_request_build(const struct cshiori_request_message* const req){
char* str = NULL;
char* str_p;
size_t i;
/* length */
size_t content_length = 0;
size_t request_line_length;
size_t* header_length;
size_t line_length = 1;
char* request_line;
switch(req->method){
case GET:
request_line = (char*)malloc(sizeof(char) * (11 + strlen(req->version) + 1));
sprintf(request_line, "GET SHIORI/%s", req->version);
break;
case NOTIFY:
request_line = (char*)malloc(sizeof(char) * (14 + strlen(req->version) + 1));
sprintf(request_line, "NOTIFY SHIORI/%s", req->version);
break;
default: return NULL;
}
request_line_length = strlen(request_line);
line_length ++;
content_length += request_line_length;
header_length = (size_t*)malloc(sizeof(size_t) * req->header_size);
for(i = 0; i < req->header_size; ++i){
char* name = *(req->names + i);
char* value = *(req->values + i);
if(name != NULL && value != NULL){
*(header_length + i) = strlen(name) + 2 + strlen(value);
line_length ++;
content_length += *(header_length + i);
}
}
content_length += line_length * LF_LEN; /* LF */
/* copy str */
str = (char*)malloc(sizeof(char) * (content_length + 1));
str_p = str;
strcpy(str_p, request_line);
str_p += request_line_length;
strcpy(str_p, LF);
str_p += LF_LEN;
for(i = 0; i < req->header_size; ++i){
char* name = *(req->names + i);
char* value = *(req->values + i);
if(name != NULL && value != NULL){
sprintf(str_p, "%s: %s", name, value);
str_p += *(header_length + i);
strcpy(str_p, LF);
str_p += LF_LEN;
}
}
strcpy(str_p, LF);
free(header_length);
return str;
}
char* cshiori_shiori_response_build(const struct cshiori_response_message* const res){
char* str = NULL;
char* str_p;
size_t i;
/* length */
static const char* const shiori_pre = "SHIORI/";
const size_t shiori_pre_length = strlen(shiori_pre);
size_t content_length = 0;
size_t status_length = 0, value_length = 0, sender_length = 0, charset_length = 0;
size_t reference_length[CSHIORI_REQUEST_MESSAGE_REFERENCE_SIZE];
size_t line_length = 1;
char* status_message;
if(res->version == NULL) return NULL;
switch(res->status_code){
case 200: status_message = "OK"; break;
case 204: status_message = "No Content"; break;
case 311: status_message = "Not Enough"; break;
case 312: status_message = "Advice"; break;
case 400: status_message = "Bad Request"; break;
case 500: status_message = "Internal Server Error"; break;
default: return NULL;
}
status_length = shiori_pre_length + strlen(res->version) + 5 + strlen(status_message); line_length ++;
if(res->value != NULL){ value_length = 5 + 2 + strlen(res->value); line_length ++; }
if(res->sender != NULL){ sender_length = 6 + 2 + strlen(res->sender); line_length ++; }
if(res->charset != NULL){ charset_length = 7 + 2 + strlen(res->charset); line_length ++; }
content_length += status_length + value_length + sender_length + charset_length;
for(i = 0; i < CSHIORI_RESPONSE_MESSAGE_REFERENCE_SIZE; ++i){
if(res->reference[i] != NULL){
reference_length[i] = 9 + (1 + ((size_t) log10((float)i))) + 2 + strlen(res->reference[i]);
line_length ++;
}else{
reference_length[i] = 0;
}
content_length += reference_length[i];
}
content_length += line_length * LF_LEN; /* LF */
/* copy str */
str = (char*)malloc(sizeof(char) * (content_length + 1));
str_p = str;
sprintf(str, "%s%s %03d %s", shiori_pre, res->version, res->status_code, status_message);
str_p += status_length;
strcpy(str_p, LF);
str_p += LF_LEN;
if(res->value != NULL){ sprintf(str_p, "Value: %s", res->value); str_p += value_length; strcpy(str_p, LF); str_p += LF_LEN; }
if(res->sender != NULL){ sprintf(str_p, "Sender: %s", res->sender); str_p += sender_length; strcpy(str_p, LF); str_p += LF_LEN; }
if(res->charset != NULL){ sprintf(str_p, "Charset: %s", res->charset); str_p += charset_length; strcpy(str_p, LF); str_p += LF_LEN; }
for(i = 0; i < CSHIORI_RESPONSE_MESSAGE_REFERENCE_SIZE; ++i){
if(res->reference[i] != NULL){
sprintf(str_p, "Reference%d: %s", i, res->reference[i]);
str_p += reference_length[i];
strcpy(str_p, LF);
str_p += LF_LEN;
}
}
strcpy(str_p, LF);
return str;
}
char* cshiori_shiori_response_build_bad_request(){
struct cshiori_response_message* res = cshiori_response_message_new();
char* str;
res->version = "3.0";
res->status_code = 400;
res->charset = "Shift_JIS";
str = cshiori_shiori_response_build(res);
cshiori_response_message_delete(res);
return str;
}
char* cshiori_shiori_response_build_internal_server_error(){
struct cshiori_response_message* res = cshiori_response_message_new();
char* str;
res->version = "3.0";
res->status_code = 500;
res->charset = "Shift_JIS";
str = cshiori_shiori_response_build(res);
cshiori_response_message_delete(res);
return str;
}