diff --git a/DesignedWorlds b/DesignedWorlds index 1b9c180..4cfa879 100755 Binary files a/DesignedWorlds and b/DesignedWorlds differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..e69de29 diff --git a/source/http.c b/source/http.c index e963219..b3f1a2d 100644 --- a/source/http.c +++ b/source/http.c @@ -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; diff --git a/source/http.h b/source/http.h index 47ccacd..7af9e56 100644 --- a/source/http.h +++ b/source/http.h @@ -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); diff --git a/source/main.c b/source/main.c index 63b0187..670e2f0 100644 --- a/source/main.c +++ b/source/main.c @@ -17,6 +17,46 @@ #include +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, - "

This is a test to see what happends

", 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; } + diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..8bb9404 --- /dev/null +++ b/web/index.html @@ -0,0 +1,34 @@ + + + + My Simple Website + + + +

Welcome to my website!

+ + + + + +

This is some sample text.

+ +