parse the filename out from the url of the request

This commit is contained in:
Nayan
2025-05-05 21:24:56 -04:00
parent ec28990794
commit 3f6a2fbd7f
2 changed files with 48 additions and 3 deletions

View File

@@ -8,20 +8,64 @@ void* client_handler(void* args) {
ssize_t bytes_read = recv(client, buffer, BUFFER_SIZE, 0);
if (bytes_read > 0) {
char* ptr = buffer;
char* buf_temp[10];
char* ptr_temp = buf_temp;
// Null terminate buffer
buffer[bytes_read] = '\0';
// Check if request is GET
char* ptr = buffer; // Modifiable pointer to buffer
char* buf_temp =
(char*)malloc(10); // Temporary buffer to store request code
char* ptr_temp = buf_temp; // Modifiable pointer to temporary buffer
// Copy request code to temporary buffer
while (*ptr != ' ') {
*ptr_temp = *ptr;
ptr++;
ptr_temp++;
}
// Null terminate temporary buffer
*ptr_temp = '\0';
ptr++; // Skip over space
if (strcmp(buf_temp, "GET") == 0) {
printf("Received a GET request!!\n");
} else {
printf("Received a non-GET request!!\n");
return NULL;
}
// Get filename
char* filename = (char*)malloc(100); // Buffer to store filename
ptr_temp = filename; // Modifiable pointer to filename
*ptr_temp = '.'; // Prepend . to filename
ptr_temp++;
// Copy filename to temporary buffer
while (*ptr != ' ') {
// Convert %20 to space
if (*ptr == '%' && *(ptr + 1) == '2' && *(ptr + 2) == '0') {
ptr += 3;
*ptr_temp = ' ';
ptr_temp++;
} else {
// Copy character
*ptr_temp = *ptr;
ptr++;
ptr_temp++;
}
}
*ptr_temp = '\0'; // Null terminate filename
// Print filename
printf("Filename: %s\n", filename);
// Send response
char* response = "HTTP/1.1 200 OK\n\n";
send(client, response, strlen(response), 0);
printf("Request:\n");
printf("%s\n", buffer);
free(buf_temp);
free(filename);
return NULL;
}
free(buffer);
return NULL;

View File

@@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
void* client_handler(void* args);