working on the web side of things

This commit is contained in:
2025-10-06 10:58:33 -07:00
parent b750f76cd7
commit d3ca8d920a
6 changed files with 125 additions and 27 deletions

Binary file not shown.

0
index.html Normal file
View File

View File

@@ -35,7 +35,7 @@ HttpRequestHeader* createHttpRequest(char* uri, unsigned char method, char* host
// Function to receive an HTTP response
HttpResponseHeader* createHttpResponse(int statusCode,const char* contextType,
char* data, char* buffer, int bs) {
char* data, char* buffer, int bufferSize) {
// TO DO: implement the actual receiving logic
HttpResponseHeader* response = (HttpResponseHeader*)malloc(sizeof(HttpResponseHeader));
response->http_version = (char*) malloc(strlen("HTTP/1.1") + 1);
@@ -76,15 +76,15 @@ HttpResponseHeader* createHttpResponse(int statusCode,const char* contextType,
break;
}
HttpResponseHeaderToS(response, data, buffer, bs);
HttpResponseHeaderToS(response, data, buffer, bufferSize);
return response;
}
void HttpResponseHeaderToS(HttpResponseHeader* rh,char* d, char* buffer, int bs) {
void HttpResponseHeaderToS(HttpResponseHeader* rh,char* d, char* buffer, int bufferSize) {
snprintf(buffer, 512,
snprintf(buffer, bufferSize,
"%s %u %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
@@ -94,6 +94,45 @@ void HttpResponseHeaderToS(HttpResponseHeader* rh,char* d, char* buffer, int bs)
}
void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer) {
int methodLen = 0;
char methodStr[8] = "";
if (strstr(buffer, "GET") != NULL) {
rh->method = HTTP_GET;
} else if (strstr(buffer, "POST") != NULL) {
rh->method = HTTP_POST;
} else if (strstr(buffer, "PUT") != NULL) {
rh->method = HTTP_PUT;
} else if (strstr(buffer, "DELETE") != NULL) {
rh->method = HTTP_DELETE;
} else if (strstr(buffer, "HEAD") != NULL) {
rh->method = HTTP_HEAD;
} else if (strstr(buffer, "OPTIONS") != NULL) {
rh->method = HTTP_OPTIONS;
} else if (strstr(buffer, "PATCH") != NULL) {
rh->method = HTTP_PATCH;
} else if (strstr(buffer, "CONNECT") != NULL) {
rh->method = HTTP_CONNECT;
} else if (strstr(buffer, "CONNECT") != NULL) {
rh->method = HTTP_CONNECT;
} else {
rh->method = HTTP_UNKNOWN;
}
int bufferLen, wordIndex;
// Find the bufferLen of the string
bufferLen = strlen(buffer);
int start, end = 0;
// GET /path/to/file/ HTTP:1.1
}
// Function to free the memory allocated for an HTTP request header
void freeHttpRequest(HttpRequestHeader* request) {
if (request == NULL) return;

View File

@@ -8,7 +8,7 @@
#ifndef HTTP_H
#define HTTP_H
#define HTTP_UNKNOWN 0
#define HTTP_GET 1
#define HTTP_POST 2
#define HTTP_PUT 3
@@ -17,7 +17,6 @@
#define HTTP_OPTIONS 6
#define HTTP_PATCH 7
#define HTTP_CONNECT 8
#define HTTP_TRACE 9
#ifndef HTTP_CONTENT_TYPES_H
@@ -70,7 +69,11 @@ HttpResponseHeader* createHttpResponse(int statusCode,const char* contentType,
void HttpResponseHeaderToS(HttpResponseHeader* rh, char* d, char* buffer, int bs);
void HttpRequestHeaderToS(HttpRequestHeader* rh, char* buffer);
void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer);
int HttpCreate();
int HttpGetReqest(char* buffer);
int HttpSendResponse(int statusCode, const char* type, char* data);
// Freeing Objects
void freeHttpRequest(HttpRequestHeader* request);

View File

@@ -17,6 +17,46 @@
#include <arpa/inet.h>
void handleClient(int sock) {
while (1) {
int client_sock = accept(sock, NULL, NULL);
if (client_sock < 0) {
perror("accept");
continue;
}
FILE* file = fopen("./web/index.html", "r");
if (file == NULL) {
perror("that file dose not exists");
return;
}
char* fileBuffer;
fseek(file, 0, SEEK_END);
int size = ftell(file);
rewind(file);
printf("file size: %d\n", size);
fileBuffer = (char*)malloc(size + 512);
printf("Sent '%s\n' to the client.\n", fileBuffer);
int bytesRead = fread(fileBuffer, sizeof(char), size, file);
char* response = (char*)malloc(512 + size); // Assuming a maximum length of 512 bytes
HttpResponseHeader* header = createHttpResponse(200, "text/html", fileBuffer, response, 512 + size);
fclose(file);
printf("%s\n", response);
send(client_sock, response, strlen(response)+1, 0);
free(fileBuffer);
}
}
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
@@ -37,28 +77,10 @@ int main() {
listen(sock, 3);
printf("Server started. Listening on port 9090.\n");
while (1) {
int client_sock = accept(sock, NULL, NULL);
if (client_sock < 0) {
perror("accept");
continue;
}
char* response = (char*)malloc(512); // Assuming a maximum length of 512 bytes
HttpResponseHeader* header = createHttpResponse(200, HTTP_CONTENT_TYPE_HTML,
"<h1>This is a test to see what happends</h1>", response, 512);
printf("%s\n", response);
printf("Sent 'Hello, World!' to the client.\n");
send(client_sock, response, strlen(response)+1, 0);
}
handleClient(sock);
return 0;
}

34
web/index.html Normal file
View File

@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>My Simple Website</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>
<button id="button-1">Button 1</button>
<button id="button-2">Button 2</button>
<button id="button-3">Button 3</button>
<p>This is some sample text.</p>
</body>
</html>