diff options
Diffstat (limited to 'http_headers.h')
-rw-r--r-- | http_headers.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/http_headers.h b/http_headers.h new file mode 100644 index 0000000..f3aa397 --- /dev/null +++ b/http_headers.h @@ -0,0 +1,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 || (uint64_t)strlen(req_contents) <= 0 ) + return REQ_READ_ERROR; + hdt->request_size = (size_t)strlen( req_contents ); + 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)strlen(hdt->request_copy) != 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 ) + 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)strlen(hdt->tok_current), sizeof(char) ); + if ( hdt->request_method == NULL ) + return REQ_READ_ERROR; + strncpy( hdt->request_method, hdt->tok_current, (size_t)strlen(hdt->tok_current) ); + if ( (size_t)strlen(hdt->request_method) != (size_t)strlen(hdt->tok_current) ) + 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)strlen(hdt->tok_current), sizeof(char) ); + if ( hdt->request_path == NULL ) + return REQ_READ_ERROR; + strncpy( hdt->request_path, hdt->tok_current, (size_t)strlen(hdt->tok_current) ); + if ( (size_t)strlen(hdt->request_path) != (size_t)strlen(hdt->tok_current) ) + return REQ_READ_ERROR; + memset( hdt->request_copy, 0, hdt->request_size ); + strncpy( hdt->request_copy, req_contents, hdt->request_size ); + if ( (size_t)strlen(hdt->request_copy) != hdt->request_size ) + return REQ_READ_ERROR; + char *request_body_begin = strstr( hdt->request_copy, "\r\n\r\n" ); + if ( request_body_begin == NULL ){ + 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 ){ + hdt->request_body = NULL; + return REQ_READ_SUCCESS; + } + hdt->request_body = (char*)calloc( (size_t)strlen(hdt->tok_current), sizeof(char) ); + if ( hdt->request_body == NULL ) + return REQ_READ_ERROR; + strncpy( hdt->request_body, hdt->tok_current, (size_t)strlen(hdt->tok_current) ); + if ( (size_t)strlen(hdt->request_body) != (size_t)strlen(hdt->tok_current) ) + 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; +} |