]> git.seodisparate.com - c_simple_http/commitdiff
WIP some work on http req/response handling
authorStephen Seo <seo.disparate@gmail.com>
Mon, 2 Sep 2024 04:51:10 +0000 (13:51 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 2 Sep 2024 04:51:10 +0000 (13:51 +0900)
src/http.c
src/main.c

index 7e99e121643fd493cbcd488c7f23fc89eb64e45f..b6a20772efe6fe1eeaf9b9826d71e4a6aefd296a 100644 (file)
 // Standard library includes.
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 
 // Local includes
 #include "constants.h"
 
+#define REQUEST_TYPE_BUFFER_SIZE 16
+#define REQUEST_PATH_BUFFER_SIZE 256
+#define REQUEST_PROTO_BUFFER_SIZE 16
+
 char *c_simple_http_request_response(
     const char *request,
     unsigned int size,
     const C_SIMPLE_HTTP_HTTPTemplates *templates) {
+  // parse first line.
+  unsigned int idx = 0;
+  char request_type[REQUEST_TYPE_BUFFER_SIZE] = {0};
+  unsigned int request_type_idx = 0;
+  for (; idx < size
+      && request[idx] != ' '
+      && request[idx] != '\n'
+      && request[idx] != '\r'
+      && request[idx] != '\t'
+      && request_type_idx < REQUEST_TYPE_BUFFER_SIZE - 1;
+        ++idx) {
+    request_type[request_type_idx++] = request[idx];
+  }
+  if (request_type_idx == 0) {
+    return NULL;
+  }
+#ifndef NDEBUG
+  fprintf(stderr, "Parsing request: got type \"%s\"\n", request_type);
+#endif
+  // skip whitespace until next part.
+  for (; idx < size
+      && (request[idx] == ' ' ||
+          request[idx] == '\n' ||
+          request[idx] == '\r' ||
+          request[idx] == '\t');
+      ++idx) {}
+  // parse request path.
+  char request_path[REQUEST_PATH_BUFFER_SIZE] = {0};
+  unsigned int request_path_idx = 0;
+  for (; idx < size
+      && request[idx] != ' '
+      && request[idx] != '\n'
+      && request[idx] != '\r'
+      && request[idx] != '\t'
+      && request_path_idx < REQUEST_PATH_BUFFER_SIZE - 1;
+        ++idx) {
+    request_path[request_path_idx++] = request[idx];
+  }
+  if (request_path_idx == 0) {
+    return NULL;
+  }
+#ifndef NDEBUG
+  fprintf(stderr, "Parsing request: got path \"%s\"\n", request_path);
+#endif
+  // skip whitespace until next part.
+  for (; idx < size
+      && (request[idx] == ' ' ||
+          request[idx] == '\n' ||
+          request[idx] == '\r' ||
+          request[idx] == '\t');
+      ++idx) {}
+  // parse request http protocol.
+  char request_proto[REQUEST_PROTO_BUFFER_SIZE] = {0};
+  unsigned int request_proto_idx = 0;
+  for (; idx < size
+      && request[idx] != ' '
+      && request[idx] != '\n'
+      && request[idx] != '\r'
+      && request[idx] != '\t'
+      && request_proto_idx < REQUEST_PROTO_BUFFER_SIZE - 1;
+        ++idx) {
+    request_proto[request_proto_idx++] = request[idx];
+  }
+  if (request_proto_idx == 0) {
+    return NULL;
+  }
+#ifndef NDEBUG
+  fprintf(stderr, "Parsing request: got http protocol \"%s\"\n", request_proto);
+#endif
+
   // TODO
   return NULL;
 }
index 0f0730a25b64b073ba3409410cf864c1c5d72b11..ae0e1dc234dd58682a7df980a425a198c9710805 100644 (file)
@@ -34,6 +34,7 @@
 #include "signal_handling.h"
 #include "globals.h"
 #include "constants.h"
+#include "http.h"
 
 int main(int argc, char **argv) {
   Args args = parse_args(argc, argv);
@@ -83,17 +84,24 @@ int main(int argc, char **argv) {
       int connection_fd = ret;
       read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE);
       // DEBUG print received buf.
-      // TODO Validate request and send response.
       for (unsigned int idx = 0;
           idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret;
           ++idx) {
-        if (recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E) {
+        if ((recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E)
+            || recv_buf[idx] == '\n' || recv_buf[idx] == '\r') {
           printf("%c", recv_buf[idx]);
         } else {
           break;
         }
       }
       puts("");
+      // TODO Validate request and send response.
+      // TODO WIP
+      const char *response = c_simple_http_request_response(
+        (const char*)recv_buf, read_ret, NULL);
+      if (response) {
+        free((void*)response);
+      }
       close(connection_fd);
     } else {
       printf("WARNING: accept: Unknown invalid state!\n");