mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
add enum for request method for easy switch statements
This commit is contained in:
12
http_stuff.c
12
http_stuff.c
@@ -2,7 +2,7 @@
|
||||
|
||||
http_request* create_http_request() {
|
||||
http_request* req = (http_request*)malloc(sizeof(http_request));
|
||||
req->method = NULL;
|
||||
req->method_str = NULL;
|
||||
req->url = NULL;
|
||||
req->num_headers = 0;
|
||||
req->headers = NULL;
|
||||
@@ -28,9 +28,9 @@ void free_http_request(http_request* req) {
|
||||
printf("Attempting to free NULL request\n");
|
||||
return;
|
||||
}
|
||||
if (req->method != NULL) {
|
||||
free(req->method);
|
||||
req->method = NULL;
|
||||
if (req->method_str != NULL) {
|
||||
free(req->method_str);
|
||||
req->method_str = NULL;
|
||||
}
|
||||
if (req->url != NULL) {
|
||||
free(req->url);
|
||||
@@ -88,9 +88,7 @@ void print_http_request(http_request* req) {
|
||||
printf("Attempting to print NULL request\n");
|
||||
return;
|
||||
}
|
||||
if (req->method == NULL) {
|
||||
}
|
||||
printf("Method: %s\n", (req->method == NULL) ? "" : req->method);
|
||||
printf("Method: %s\n", (req->method_str == NULL) ? "" : req->method_str);
|
||||
printf("URL: %s\n", (req->url == NULL) ? "" : req->url);
|
||||
printf("Headers:\n");
|
||||
for (int i = 0; i < req->num_headers; i++) {
|
||||
|
||||
10
http_stuff.h
10
http_stuff.h
@@ -6,6 +6,13 @@
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef enum {
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
DELETE,
|
||||
} http_method;
|
||||
|
||||
/**
|
||||
* Stores a key-value pair for http headers
|
||||
*/
|
||||
@@ -18,7 +25,8 @@ struct header_kv {
|
||||
* Stores an HTTP request
|
||||
*/
|
||||
struct http_request {
|
||||
char* method;
|
||||
http_method method;
|
||||
char* method_str;
|
||||
char* url;
|
||||
int num_headers;
|
||||
struct header_kv* headers;
|
||||
|
||||
@@ -22,6 +22,19 @@ void handle_request(char* request, int length, char* response,
|
||||
// request_info_print(req);
|
||||
free_http_request(req);
|
||||
|
||||
// Switch statement to handle different request types
|
||||
// switch (req->method)
|
||||
// {
|
||||
// case GET:
|
||||
// /* code */
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
|
||||
// Create response
|
||||
|
||||
// Create reponse string
|
||||
char* ptr_temp = response;
|
||||
char* temp =
|
||||
@@ -43,16 +56,31 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract request type
|
||||
// Extract request method
|
||||
char* type_start = request;
|
||||
char* type_end = strstr(type_start, " ");
|
||||
if (type_end == NULL) {
|
||||
printf("Invalid packet (method)\n");
|
||||
return;
|
||||
}
|
||||
req->method = (char*)malloc(type_end - type_start + 1);
|
||||
strncpy(req->method, type_start, type_end - type_start);
|
||||
req->method[type_end - type_start] = '\0';
|
||||
char* type = (char*)malloc(type_end - type_start + 1);
|
||||
strncpy(type, type_start, type_end - type_start);
|
||||
type[type_end - type_start] = '\0';
|
||||
if (strcmp(type, "GET") == 0) {
|
||||
req->method = GET;
|
||||
} else if (strcmp(type, "POST") == 0) {
|
||||
req->method = POST;
|
||||
} else if (strcmp(type, "PUT") == 0) {
|
||||
req->method = PUT;
|
||||
} else if (strcmp(type, "DELETE") == 0) {
|
||||
req->method = DELETE;
|
||||
} else {
|
||||
printf("Invalid packet (method)\n");
|
||||
return;
|
||||
}
|
||||
req->method_str = (char*)malloc(type_end - type_start + 1);
|
||||
strncpy(req->method_str, type_start, type_end - type_start);
|
||||
req->method_str[type_end - type_start] = '\0';
|
||||
|
||||
// Extract URL
|
||||
char* url_start = type_end + 1;
|
||||
|
||||
Reference in New Issue
Block a user