-
Notifications
You must be signed in to change notification settings - Fork 7
/
bs_url.c
521 lines (439 loc) · 15.2 KB
/
bs_url.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* file : bs_url.c
* author : bushaofeng
* create : 2014-10-06 23:05
* func :
* history:
*/
#include "bs_url.h"
#include "bs_socket.h"
#include "bs_dnscache.h"
#include "bs_log.h"
void* url_init(void* p){
url_t* url = (url_t*)p;
data_init(&url->url);
data_init(&url->domain);
data_init(&url->host);
data_init(&url->path);
data_init(&url->protocal);
data_init(&url->query);
return url;
}
void url_destroy(void* p){
url_t* url = (url_t*)p;
data_destroy(&url->url);
data_destroy(&url->domain);
data_destroy(&url->host);
data_destroy(&url->path);
data_destroy(&url->protocal);
data_destroy(&url->query);
}
void* http_init(void* p){
http_t* http = (http_t*)p;
url_init(&http->url);
data_init(&http->req);
return http;
}
void http_destroy(void* p){
http_t* http = (http_t*)p;
url_destroy(&http->url);
data_destroy(&http->req);
}
state_t url_parse(url_t* url, const char* url_str){
int i;
char port[8] = {0};
if(url==NULL || url_str==NULL){
return BS_INVALID;
}
char* start;
char* end;
url->url.set(&url->url, url_str, (uint32_t)strlen(url_str));
start = (char*)url_str;
// protocal
end = strstr(start, "://");
if(end != NULL){
url->protocal.set(&url->protocal, start, (uint32_t)(end-start));
start = end+3;
}
// host/port
end = strchr(start, '/');
if(end != NULL){
url->domain.set(&url->domain, start, (uint32_t)(end-start));
url->host.set(&url->host, start, (uint32_t)(end-start));
url->port = 80;
for(i=0; i<end-start; i++){
if (start[i]==':'){
url->domain.set(&url->domain, start, i);
bs_memcpy(port, sizeof(port)-1, start+i+1, (uint32_t)(end-start-i-1));
url->port = atoi(port);
break;
}
}
start = end;
}
url->path.set(&url->path, start, (uint32_t)strlen(url_str)-(uint32_t)(start - url_str));
// path
start = strchr(start, '?');
if (start==NULL){
return BS_SUCCESS;
}
else {
start += 1;
url->query.set(&url->query, start, (uint32_t)strlen(url_str)-(uint32_t)(start - url_str));
}
return BS_SUCCESS;
}
http_t* http_create(const char* url, const char* method){
char buffer[URL_SIZE];
http_t* http = bs_new(http);
url_parse(&http->url, url);
snprintf(buffer, URL_SIZE, "%s %s HTTP/1.1\r\nAccept: */*\r\nHost: %s\r\n", method, http->url.path.mem, http->url.host.mem);
data_set(&http->req, buffer, (uint32_t)strlen(buffer));
return http;
}
void http_set_header(http_t* http, const char* key, const char* value){
char buffer[URL_SIZE];
snprintf(buffer, URL_SIZE, "%s: %s\r\n", key, value);
data_append(&http->req, buffer, (uint32_t)strlen(buffer));
}
void http_add_newline(http_t* http){
char* newline = "\r\n";
data_append(&http->req, newline, (uint32_t)strlen(newline));
}
void* http_res_init(void * p){
http_res_t* res = (http_res_t*)p;
data_init(&res->response);
res->response_code = 0;
res->body = NULL;
res->body_size = 0;
return res;
}
void http_res_destroy(void* p){
http_res_t* res = (http_res_t*)p;
data_destroy(&res->response);
}
/// 设置http body
void http_set_body(http_t* http, const char* body, uint32_t body_size){
char buffer[URL_SIZE];
snprintf(buffer, URL_SIZE, "Content-Length: %d\r\nConnection: Close\r\n\r\n", body_size);
data_append(&http->req, buffer, (uint32_t)strlen(buffer));
if (body!=NULL && body_size!=0) {
http->body = http->req.mem + http->req.len;
data_append(&http->req, body, body_size);
http->body_size = body_size;
}
}
http_res_header parse_header(const char *response)
{
/*获取响应头的信息*/
struct http_res_header response_header;
char *header = strstr(response, "HTTP/1.1");
//获取http状态代码
if (header)
sscanf(header, "%*s %hd", &response_header.status_code);
//获取返回文档类型
header = strstr(response, "Content-Type:");
if (header)
sscanf(header, "%*s %s", response_header.content_type);
//获取返回文档长度
header = strstr(response, "Content-Length:");
if (header)
sscanf(header, "%*s %lld", &response_header.content_length);
return response_header;
}
http_res_t* http_perform(http_t* http) {
int socket;
http_res_t *res = bs_new(http_res);
res->response_code = connect_socket(&socket, http);
if (res->response_code == BS_SUCCESS) {
http_send_request(socket, res, http);
if (res->response_code == BS_SUCCESS) {
http_receive_response(socket, res, http);
}
}
if (res->response_code == -1 && errno == ETIMEDOUT) {
res->response_code = BS_TIMEOUT;
}
close(socket);
return res;
}
http_res_t* http_download(http_t* http, const char* path, void* download, http_progress progressCallback){
int socket;
char buf[1024];
uint64_t len = 0;
uint64_t hasrecieve = 0;
http_res_t *res = bs_new(http_res);
res->response_code = connect_socket(&socket, http);
if (res->response_code == BS_SUCCESS) {
http_send_request(socket, res, http);
if (res->response_code == BS_SUCCESS) {
// http response header解析
http_receive_header(socket, res, http);
if (res->response_code == BS_SUCCESS) {
FILE *file = fopen(path, "wb+");
if(file == NULL){
res->response_code = BS_CREATERR;
} else {
// 构建请求成功后的http状态消息
http_response_parse(res);
http_res_header res_header = parse_header(res->response.mem);
while (hasrecieve < res_header.content_length) {
len = read(socket, buf, sizeof(buf));
fwrite(buf, (size_t)len, 1, file);
hasrecieve += len; //更新已经下载的长度
progressCallback(len, hasrecieve, res_header.content_length, download); // 回调下载文件的进度值
}
fclose(file);
}
}
}
}
if (res->response_code == -1 && errno == ETIMEDOUT) {
res->response_code = BS_TIMEOUT;
}
close(socket);
return res;
}
http_res_t* http_upload(http_t *http, const char *path, void *upload, http_progress progressCallback){
int socket;
char buf[1024];
uint64_t hassend = 0;
uint64_t len = 0;
uint64_t contentLength = 0;
http_res_t *res = bs_new(http_res);
res->response_code = connect_socket(&socket, http);
if (res->response_code == BS_SUCCESS) {
http_send_request(socket, res, http);
if (res->response_code == BS_SUCCESS) {
FILE *file = fopen(path, "rb");
if(file == NULL){
res->response_code = BS_CREATERR;
} else {
// 获取文件的大小
fseek (file , 0 , SEEK_END);
contentLength = ftell (file);
rewind (file);
}
// 设置socket的缓冲区
int sock_buf_size = 10000;
setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));
//上传http文件
while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
if (write(socket, buf, (size_t)len) <= -1) {
res->response_code = BS_SENDERR;
} else {
hassend += len;
if (hassend / len % 10 == 1 || hassend >= contentLength) {
progressCallback(len, hassend, contentLength, upload); // 回调上传文件的进度值
}
}
}
http_receive_response(socket, res, http);
fclose(file);
}
}
if (res->response_code == -1 && errno == ETIMEDOUT) {
res->response_code = BS_TIMEOUT;
}
close(socket);
return res;
}
http_res_t* http_post_data(http_t *http, uint8_t *data, void *upload, http_progress progressCallback) {
int socket;
char buf[1024];
uint64_t hassend = 0;
uint32_t bufferSize = 1024;
uint64_t len = 0;
http_res_t* res = bs_new(http_res);
res->response_code = connect_socket(&socket, http);
if (res->response_code == BS_SUCCESS) {
http_send_request(socket, res, http);
if (res->response_code == BS_SUCCESS) {
//上传二进制数据
size_t sock_buf_size = 10000;
setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));
uint64_t totalSize = http->body_size;
len = bufferSize;
while (hassend < totalSize) {
memset(buf, 0, bufferSize);
uint64_t remainSize = totalSize - hassend;
len = remainSize > bufferSize ? bufferSize : remainSize;
memcpy(buf, data + hassend, len);
if (write(socket, buf, (size_t)len) <= -1) {
close(socket);
res->response_code = BS_SENDERR;
return res;
} else {
hassend += len;
if (hassend / len % 10 == 1 || hassend >= totalSize) {
progressCallback(len, hassend, totalSize, upload);
}
}
}
http_receive_response(socket, res, http);
}
}
if (res->response_code == -1 && errno == ETIMEDOUT) {
res->response_code = BS_TIMEOUT;
}
close(socket);
return res;
}
void http_send_request(int socket, http_res_t* http_res, http_t* http) {
socket_block(socket);
http_res->response_code = write_timeout(socket, http->time_out);
if (http_res->response_code == BS_SUCCESS) {
if (write(socket, http->req.mem, http->req.len) <= 0) {
http_res->response_code = BS_SENDERR;
}
}
}
void http_receive_response(int socket, http_res_t *http_res, http_t *http) {
int len = 0;
int bufferSize = 1024;
int receveLength = 0;
char buf[bufferSize];
http_receive_header(socket, http_res, http);
http_res_header res_header = parse_header(http_res->response.mem);
if (res_header.content_length > 0) {
if (http_res->response_code == BS_SUCCESS) {
while((len = (int)read(socket, buf, sizeof(buf))) > 0) {
data_append(&http_res->response, buf, len);
receveLength += len;
if (receveLength >= res_header.content_length) {
break;
}
}
if (len == -1) {
http_res->response_code = BS_SENDERR;
} else {
http_response_parse(http_res);
}
}
} else {
http_response_parse(http_res);
}
}
void http_receive_header(int socket, http_res_t *http_res, http_t* http) {
char buf[1024];
int len = 0;
int flag = 0;
http_res->response_code = read_timeout(socket, http->time_out);
if (http_res->response_code == BS_SUCCESS) {
while ((len = read(socket, buf, 1)) != 0) {
data_append(&http_res->response, buf, (uint32_t)len);
if (buf[0] == '\r' || buf[0] == '\n') {
flag ++;
} else {
flag = 0;
}
if (flag == 4)
break;
}
}
}
state_t connect_socket(int *sock, http_t *http) {
int err = bs_tcp_sock_connect(sock, http->url.domain.mem, http->url.port, BS_TRUE);
if (!(err == -1 || errno == EINPROGRESS) || err == BS_HOSTERR) {
return BS_CONNERR;
}
return connect_timeout(*sock, http->time_out);
}
state_t http_response_parse(http_res_t* res){
if (res == NULL) {
return BS_PARANULL;
}
http_res_header res_header = parse_header(res->response.mem);
sscanf(res->response.mem, "HTTP/1.1 %d", &res->response_code);
res->body = bs_strrstr(res->response.mem, "\r\n") + 2;
res->body_size = res->response.len - (uint32_t)(res->body-res->response.mem);
if (res_header.status_code == HTTP_NOTFOUND) {
dns_cache_clear();
}
return BS_SUCCESS;
}
state_t read_timeout(int socket, uint32_t wait_seconds)
{
int ret = 0;
if (wait_seconds > 0) {
fd_set read_fdset;
struct timeval timeout;
FD_ZERO(&read_fdset);
FD_SET(socket, &read_fdset);
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do {
ret = select(socket + 1, &read_fdset, NULL, NULL, &timeout); //select会阻塞直到检测到事件或者超时
// 如果select检测到可读事件发送,则此时调用read不会阻塞
} while (ret < 0 && errno == EINTR);
if (ret == 0) {
ret = -1;
errno = ETIMEDOUT;
} else if (ret == 1) {
return 0;
}
}
return ret;
}
state_t write_timeout(int socket, uint32_t wait_seconds)
{
int ret = 0;
if (wait_seconds > 0) {
fd_set write_fdset;
struct timeval timeout;
FD_ZERO(&write_fdset);
FD_SET(socket, &write_fdset);
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do {
ret = select(socket + 1, NULL, &write_fdset, NULL, &timeout);
} while (ret < 0 && errno == EINTR);
if (ret == 0) {
ret = -1;
errno = ETIMEDOUT;
} else if (ret == 1) {
return 0;
}
}
return ret;
}
state_t connect_timeout(int socket, uint32_t wait_seconds)
{
int ret;
fd_set connect_fdset;
struct timeval timeout;
FD_ZERO(&connect_fdset);
FD_SET(socket, &connect_fdset);
timeout.tv_sec = wait_seconds;
timeout.tv_usec = 0;
do {
/* 一旦连接建立,套接字就可写 */
ret = select(socket + 1, NULL, &connect_fdset, NULL, &timeout);
} while (ret < 0 && errno == EINTR);
if (ret == 0) {
errno = ETIMEDOUT;
return -1;
} else if (ret < 0) {
return -1;
} else if (ret == 1) {
/* ret返回为1,可能有两种情况,一种是连接建立成功,一种是套接字产生错误
* 此时错误信息不会保存至errno变量中(select没出错),因此,需要调用
* getsockopt来获取 */
int err;
socklen_t socklen = sizeof(err);
int sockoptret = getsockopt(socket, SOL_SOCKET, SO_ERROR, &err, &socklen);
if (sockoptret == -1) {
return -1;
}
if (err == 0) {
ret = 0;
} else {
errno = err;
ret = -1;
}
}
return ret;
}
void url_print(url_t* url){
fprintf(stdout, "protocal[%s] host[%s] domain[%s] port[%d] path[%s] query[%s]\n", url->protocal.mem, url->host.mem, url->domain.mem, url->port, url->path.mem, url->query.mem);
}