]> git.seodisparate.com - c_simple_http/commitdiff
Add README.md, ensure listening port is printed
authorStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 04:11:00 +0000 (13:11 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 04:11:00 +0000 (13:11 +0900)
README.md [new file with mode: 0644]
src/main.c

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..71d081a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+# C Simple HTTP
+
+A simple HTTP/1.1 server written in C.
+
+## Usage
+
+    Usage:
+      -p <port> | --port <port>
+      --config=<config_file>
+
+## Example
+
+    # Build the project.
+    make c_simple_http
+    
+    # Run it with the example config.
+    ./c_simple_http --config=example_config/example.config
+    
+    # If port is not specified, the server picks a random port.
+    # This program should print which TCP port it is listening on.
+    # Sometimes the program will fail to rebind to the same port due to how TCP
+    # works. Either wait some time or choose a different port.
+    
+    # Access the website.
+    # This assumes the server is hosted on port 3000.
+    curl localhost:3000
index de9211b5f392c7cfed8d9da78c0aae04ca364081..17ab2943bbb536d68c41bf9e8b7f5ac0b16c59bd 100644 (file)
@@ -48,8 +48,7 @@ int main(int argc, char **argv) {
     return 2;
   }
 
-  printf("listening on port: %u\n", args.port);
-  printf("config file is: %s\n", args.config_file);
+  printf("Config file is: %s\n", args.config_file);
 
   __attribute__((cleanup(c_simple_http_clean_up_parsed_config)))
   C_SIMPLE_HTTP_ParsedConfig parsed_config = c_simple_http_parse_config(
@@ -64,6 +63,20 @@ int main(int argc, char **argv) {
     return 1;
   }
 
+  {
+    struct sockaddr_in6 ipv6_addr;
+    memset(&ipv6_addr, 0, sizeof(struct sockaddr_in6));
+    socklen_t size = sizeof(ipv6_addr);
+    int ret = getsockname(tcp_socket, (struct sockaddr*)&ipv6_addr, &size);
+    if (ret == 0) {
+      printf("Listening on port: %u\n", u16_be_swap(ipv6_addr.sin6_port));
+    } else {
+      fprintf(
+        stderr,
+        "WARNING Failed to get info on tcp socket to print port number!\n");
+    }
+  }
+
   struct timespec sleep_time;
   sleep_time.tv_sec = 0;
   sleep_time.tv_nsec = C_SIMPLE_HTTP_SLEEP_NANOS;