#include #include #include #include "include/http.h" // what we want to do here is start parsing the http message // GET / HTTP/1.1 // Host: localhost:8080 // User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0 // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 // Accept-Language: en-US,en;q=0.5 // Accept-Encoding: gzip, deflate, br, zstd // Connection: keep-alive // Upgrade-Insecure-Requests: 1 // Sec-Fetch-Dest: document // Sec-Fetch-Mode: navigate // Sec-Fetch-Site: none // Sec-Fetch-User: ?1 // Priority: u=0, i http_method_t str_to_http_method(char* input) { if (strncmp(input, "GET", 3) == 0) { return GET; } else if (strncmp(input, "HEAD", 4) == 0) { return HEAD; } return UNKNOWN; } const char* http_method_to_str(http_method_t input) { switch (input) { case GET: return "GET"; case HEAD: return "HEAD"; case UNKNOWN: default: return "UNKNOWN"; } } void parse_request_line(char** line, http_request* request) { char *tok, *ver, *num; tok = strtok(*line, " "); // this is the method if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } request->method = str_to_http_method(tok); tok = strtok(NULL, " "); // this is the request URI if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } request->request_uri = (char*) malloc(strlen(tok) + 1); strncpy(request->request_uri, tok, strlen(tok) + 1); tok = strtok(NULL, " "); // this is the version if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } ver = strtok(tok, "/"); if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } ver = strtok(NULL, "/"); if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } num = strtok(ver, "."); if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } request->version.major = atoi(num); num = strtok(NULL, "."); if (tok == NULL) { perror("strtok"); exit(EXIT_FAILURE); } request->version.minor = atoi(num); } void parse_http_request(char** buffer, http_request* request) { // going to be using strtok here to split up the request // first lets split each line char* line; // parse the first line line = strtok(*buffer, "\n"); if (line == NULL) { perror("strtok"); exit(EXIT_FAILURE); } parse_request_line(&line, request); /** * TODO: start accepting the headers * * for now im going to ignore them since they're not suuuper important for simple * operation * * whats important is that we have the 2 things we need to get started, the method and the URI */ /** // parse the remaining lines while (line != NULL) { // get the rest of them line = strtok(NULL, "\n"); } */ }