mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
111 lines
3.2 KiB
C
111 lines
3.2 KiB
C
#include "request_handler.h"
|
|
|
|
void handle_request(char* request, int length, char* response,
|
|
size_t* response_length) {
|
|
// Terminate request with EOF so strtok stops at end of string
|
|
request[length] = EOF;
|
|
|
|
printf("Request ---------\n");
|
|
printf("%s\n---------\n", request);
|
|
|
|
// Parse request into struct
|
|
http_request req;
|
|
char* lines[100];
|
|
int num_lines = 0;
|
|
char* tok = strtok(request, "\n");
|
|
|
|
while (tok != NULL) {
|
|
printf("%s\n", tok);
|
|
tok = strtok(NULL, "\n");
|
|
}
|
|
|
|
// printf("tok is %s\n", tok);
|
|
|
|
// // Check if request is GET
|
|
// char* ptr = request; // Modifiable pointer to request
|
|
// char buf_temp[10]; // Temporary buffer to store request code
|
|
// char* ptr_temp =
|
|
// (char*)&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;
|
|
// }
|
|
|
|
// // Get filename from request url
|
|
// char filename[100]; // Buffer to store filename
|
|
// ptr_temp = (char*)&filename; // Modifiable pointer to filename
|
|
// *ptr_temp = '.'; // Prepend . to filename
|
|
// ptr_temp++;
|
|
// // While loop to 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);
|
|
|
|
// Create reponse string
|
|
char* ptr_temp = response;
|
|
char* temp =
|
|
"HTTP/1.1 200 OK\r\nContent-Type: text/html \r\nContent-Length: "
|
|
"0\r\n\r\n\0";
|
|
strcpy(ptr_temp, temp);
|
|
// while (*temp != '\0') {
|
|
// *ptr_temp = *temp;
|
|
// ptr_temp++;
|
|
// temp++;
|
|
// }
|
|
// // Put content length in the html response
|
|
// int content_length = strlen(filename);
|
|
// char content_length_str[10];
|
|
// sprintf(content_length_str, "%d", content_length);
|
|
// temp = (char*)&content_length_str;
|
|
// while (*temp != '\0') {
|
|
// *ptr_temp = *temp;
|
|
// ptr_temp++;
|
|
// temp++;
|
|
// }
|
|
// // Terminate header
|
|
// temp = "\r\n\r\n\0";
|
|
// while (*temp != '\0') {
|
|
// *ptr_temp = *temp;
|
|
// ptr_temp++;
|
|
// temp++;
|
|
// }
|
|
// // Put the filename in the html response
|
|
// temp = filename;
|
|
// while (*temp != '\0') {
|
|
// *ptr_temp = *temp;
|
|
// ptr_temp++;
|
|
// temp++;
|
|
// }
|
|
|
|
// *ptr_temp = '\0'; // Null terminate response
|
|
|
|
printf("Response --------\n");
|
|
printf("%s\n--------\n", response);
|
|
|
|
*response_length = strlen(response);
|
|
} |