mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
parse the filename out from the url of the request
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user