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
|
#define REQ_READ_SUCCESS 0
#define REQ_READ_ERROR -1
int get_request_data( http_request_data_t *hdt, char *req_contents ){
if ( req_contents == NULL || (size_t)strnlen(req_contents, (size_t)MAX_RECV_BYTES) <= 0 )
return REQ_READ_ERROR;
hdt->request_size = (size_t)strnlen( req_contents, MAX_RECV_BYTES );
hdt->request_copy = (char*)calloc( hdt->request_size, sizeof(char) );
if ( hdt->request_copy == NULL )
return REQ_READ_ERROR;
strncpy( hdt->request_copy, req_contents, hdt->request_size );
if ( (size_t)strnlen(hdt->request_copy, (size_t)MAX_RECV_BYTES) != hdt->request_size )
return REQ_READ_ERROR;
hdt->tok_current = strtok_r( hdt->request_copy, "\r\n", &hdt->tok_prev );
if ( hdt->tok_current == NULL || (size_t)strnlen(hdt->tok_current, (size_t)(MAX_HEADER_LENGTH + 1)) > (size_t)MAX_HEADER_LENGTH )
return REQ_READ_ERROR;
if ( strstr(hdt->tok_current, "GET /") == NULL && strstr(hdt->tok_current, "POST /") == NULL )
return REQ_READ_ERROR;
hdt->tok_current = strtok_r( hdt->request_copy, " ", &hdt->tok_prev );
if ( hdt->tok_current == NULL )
return REQ_READ_ERROR;
hdt->request_method = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH), sizeof(char) );
if ( hdt->request_method == NULL )
return REQ_READ_ERROR;
strncpy( hdt->request_method, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH) );
if ( (size_t)strnlen(hdt->request_method, MAX_HEADER_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_HEADER_LENGTH + 1)) )
return REQ_READ_ERROR;
hdt->tok_current = strtok_r( NULL, " ", &hdt->tok_prev );
if ( hdt->tok_current == NULL )
return REQ_READ_ERROR;
hdt->request_path = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH), sizeof(char) );
if ( hdt->request_path == NULL )
return REQ_READ_ERROR;
strncpy( hdt->request_path, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_HEADER_LENGTH) );
if ( (size_t)strnlen(hdt->request_path, (size_t)MAX_HEADER_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_HEADER_LENGTH + 1)) )
return REQ_READ_ERROR;
memset( hdt->request_copy, 0, hdt->request_size );
strncpy( hdt->request_copy, req_contents, hdt->request_size );
if ( (size_t)strnlen(hdt->request_copy, (size_t)(MAX_RECV_BYTES + 1)) != hdt->request_size )
return REQ_READ_ERROR;
char *request_body_begin = strstr( hdt->request_copy, "\r\n\r\n" );
if ( request_body_begin == NULL || (size_t)strnlen(request_body_begin, (size_t)(MAX_CONTENT_LENGTH + 1)) > (size_t)MAX_CONTENT_LENGTH ){
hdt->request_body = NULL;
return REQ_READ_SUCCESS;
}
hdt->tok_current = strtok_r( request_body_begin, "\r\n\r\n", &hdt->tok_prev );
if ( hdt->tok_current == NULL || (size_t)strnlen(hdt->tok_current, (size_t)(MAX_CONTENT_LENGTH + 1)) > (size_t)MAX_CONTENT_LENGTH ){
hdt->request_body = NULL;
return REQ_READ_SUCCESS;
}
hdt->request_body = (char*)calloc( (size_t)strnlen(hdt->tok_current, (size_t)MAX_CONTENT_LENGTH), sizeof(char) );
if ( hdt->request_body == NULL )
return REQ_READ_ERROR;
strncpy( hdt->request_body, hdt->tok_current, (size_t)strnlen(hdt->tok_current, (size_t)MAX_CONTENT_LENGTH) );
if ( (size_t)strnlen(hdt->request_body, (size_t)MAX_CONTENT_LENGTH) != (size_t)strnlen(hdt->tok_current, (size_t)(MAX_CONTENT_LENGTH + 1)) )
return REQ_READ_ERROR;
return REQ_READ_SUCCESS;
}
void clear_request_copy( http_request_data_t *hrq ){
if ( hrq->request_copy != NULL ){
free( hrq->request_copy );
hrq->request_copy = NULL;
}
return;
}
void clear_request_data( http_request_data_t *hreq_data ){
if ( hreq_data->request_path != NULL ){
free( hreq_data->request_path );
hreq_data->request_path = NULL;
}
if ( hreq_data->request_body != NULL ){
free( hreq_data->request_body );
hreq_data->request_body = NULL;
}
if ( hreq_data->request_method != NULL ){
free( hreq_data->request_method );
hreq_data->request_method = NULL;
}
if ( hreq_data->request_size > 0 )
hreq_data->request_size = 0;
if ( hreq_data->tok_prev != NULL )
hreq_data->tok_prev = NULL;
if ( hreq_data->tok_current != NULL )
hreq_data->tok_current = NULL;
return;
}
|