From eccad460d6c1779daaab1a492b714261c36e1075 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sat, 9 Dec 2017 20:22:50 -0500
Subject: [PATCH] Implement basic response with threading

---
 Main.c            | 47 ++++++++++++++++++++++++++++-------------------
 Main_No_Threads.c |  2 ++
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/Main.c b/Main.c
index 9acd308..4f4bf76 100644
--- a/Main.c
+++ b/Main.c
@@ -186,7 +186,7 @@ int main(int argc, char* argv[]) {
 
             // A connection has been established. Open new thread.
             char* ip = inet_ntoa(client_address->sin_addr);
-            printf("Client IP: %s\n", ip);
+            printf("Client IP: %s     Conn FD: %d\n", ip, *conn_fd);
             pthread_create(&thread_array[0], &attr, serve_request, (void*) &((struct conn_obj) {copy_int(&index), conn_fd, client_address}));
         }
 
@@ -211,6 +211,7 @@ void* serve_request(struct conn_obj* connection) {
     thread_joinable[*connection->index] = 1;
     free(connection->index);
     free(connection->client_address);
+    close(*connection->conn_fd);
 
     // Exit thread.
     pthread_exit((void*) NULL);
@@ -240,23 +241,31 @@ void join_threads() {
 /**
  * Generates and sends http response to requesting client.
  */
-void send_response(struct conn_obj connection) {
+void send_response(struct conn_obj* connection) {
     char* temp_string;
-    char* response_buffer = calloc(BUFFER_SIZE, sizeof(char*));
-
-    strcat(response_buffer, "HTTP/1.2 200 OK\r\n");
-
-    strcat(response_buffer, "Host: ");
-    temp_string = inet_ntoa(server_address.sin_addr);
-    strcat(response_buffer, temp_string);
-    strcat(response_buffer, ":");
-    temp_string = calloc(10, sizeof(char*));
-    sprintf(temp_string, "%u", port_number);
-    strcat(response_buffer, temp_string);
-    strcat(response_buffer, "\r\n");
-    strcat(response_buffer, "Content-Type: text/html\r\n");
-    strcat(response_buffer, "\r\n");
-
-    printf("%s\n", response_buffer);
-    // write(*connection.conn_fd, response_buffer, (strlen(response_buffer) + 1));
+    char* response_buffer_1 = calloc(BUFFER_SIZE, sizeof(char*));
+    char* response_buffer_2 = calloc(BUFFER_SIZE, sizeof(char*));
+
+    printf("Creating and sending response...\n\n");
+
+    // First setup desired content.
+    strcat(response_buffer_2, "<body><h1>This is a response.</h1></body>");
+
+    // Then set up response header.
+    strcat(response_buffer_1, "HTTP/1.2 200 Okay\r\n");
+    strcat(response_buffer_1, "Server: Cat/1.0.0");
+    strcat(response_buffer_1, "\r\n");
+    strcat(response_buffer_1, "Content-Type: text/html\r\n");
+    strcat(response_buffer_1, "Content-Length: ");
+    temp_string = calloc(BUFFER_SIZE, sizeof(char));
+    sprintf(temp_string, "%ld", strlen(response_buffer_2));
+    strcat(response_buffer_1, temp_string);
+    strcat(response_buffer_1, "\r\n\r\n");
+
+
+    printf("%s\n", response_buffer_1);
+    // Send header.
+    send(*connection->conn_fd, response_buffer_1, (strlen(response_buffer_1) + 1), 0);
+    // Send actual content.
+    send(*connection->conn_fd, response_buffer_2, (strlen(response_buffer_2) + 1), 0);
 }
diff --git a/Main_No_Threads.c b/Main_No_Threads.c
index 826662d..18b541e 100644
--- a/Main_No_Threads.c
+++ b/Main_No_Threads.c
@@ -155,6 +155,8 @@ int main(int argc, char* argv[]) {
             send(*conn_fd, response_buffer_1, (strlen(response_buffer_1) + 1), 0);
             // Send actual content.
             send(*conn_fd, response_buffer_2, (strlen(response_buffer_2) + 1), 0);
+
+            close(*conn_fd);
         }
     }
 }
-- 
GitLab