SDL_net: Added simple-http-get example program.

From ed8f7fd3785a90bb907e5696f5f664c53d5c6e22 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 11 Sep 2023 11:29:19 -0400
Subject: [PATCH] Added simple-http-get example program.

---
 CMakeLists.txt             |  3 ++
 SDL_net.c                  | 36 ---------------------
 examples/simple-http-get.c | 65 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 36 deletions(-)
 create mode 100644 examples/simple-http-get.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5fa7bdd..743fab9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,3 +26,6 @@ endif()
 
 add_executable(voipchat examples/voipchat.c)
 target_link_libraries(voipchat PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
+
+add_executable(simple-http-get examples/simple-http-get.c)
+target_link_libraries(simple-http-get PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
diff --git a/SDL_net.c b/SDL_net.c
index c0aea4d..4aac5c4 100644
--- a/SDL_net.c
+++ b/SDL_net.c
@@ -1508,42 +1508,6 @@ int main(int argc, char **argv)
     }
 #endif
 
-#if 0
-    for (int i = 1; i < argc; i++) {
-        SDL_Log("Looking up %s ...", argv[i]);
-        SDLNet_Address *addr = SDLNet_ResolveHostname(argv[i]);
-        if (SDLNet_WaitUntilResolved(addr, -1) == -1) {
-            SDL_Log("Failed to lookup %s: %s", argv[i], SDL_GetError());
-        } else {
-            SDL_Log("%s is %s", argv[i], SDLNet_GetAddressString(addr));
-            const char *req = "GET / HTTP/1.0\r\n\r\n";
-            SDLNet_StreamSocket *sock = SDLNet_CreateClient(addr, 80);
-            if (!sock) {
-                SDL_Log("Failed to create stream socket to %s: %s\n", argv[i], SDL_GetError());
-            } else if (SDLNet_WaitUntilConnected(sock, -1) < 0) {
-                SDL_Log("Failed to connect to %s: %s", argv[i], SDL_GetError());
-            } else if (SDLNet_WriteToStreamSocket(sock, req, SDL_strlen(req)) < 0) {
-                SDL_Log("Failed to write to %s: %s", argv[i], SDL_GetError());
-            } else if (SDLNet_WaitStreamSocketPendingWrites(sock) < 0) {
-                SDL_Log("Failed to finish write to %s: %s", argv[i], SDL_GetError());
-            } else {
-                char buf[512];
-                int br;
-                while ((br = SDLNet_ReadStreamSocket(sock, buf, sizeof (buf))) >= 0) {
-                    fwrite(buf, 1, br, stdout);
-                }
-
-                printf("\n\n\n%s\n\n\n", SDL_GetError());
-                fflush(stdout);
-            }
-
-            if (sock) {
-                SDLNet_DestroyStreamSocket(sock);
-            }
-        }
-    }
-#endif
-
 #if 0
     //SDLNet_SimulateAddressResolutionLoss(3000, 30);
 
diff --git a/examples/simple-http-get.c b/examples/simple-http-get.c
new file mode 100644
index 0000000..015fcc5
--- /dev/null
+++ b/examples/simple-http-get.c
@@ -0,0 +1,65 @@
+/*
+ * This is just for demonstration purposes! A real HTTP solution would
+ * be WAY more complicated, support HTTPS, cookies, etc. Use curl or
+ * wget for real stuff, not this.
+ *
+ * All this to say: don't use this for anything serious!
+ */
+
+#include <stdio.h>
+#include <SDL3/SDL.h>
+#include <SDL3/SDL_main.h>
+#include "SDL_net.h"
+
+int main(int argc, char **argv)
+{
+    if (SDLNet_Init() < 0) {
+        SDL_Log("SDLNet_Init() failed: %s", SDL_GetError());
+        return 1;
+    }
+
+    for (int i = 1; i < argc; i++) {
+        SDL_Log("Looking up %s ...", argv[i]);
+        SDLNet_Address *addr = SDLNet_ResolveHostname(argv[i]);
+        if (SDLNet_WaitUntilResolved(addr, -1) == -1) {
+            SDL_Log("Failed to lookup %s: %s", argv[i], SDL_GetError());
+        } else {
+            SDL_Log("%s is %s", argv[i], SDLNet_GetAddressString(addr));
+            char *req = NULL;
+            SDL_asprintf(&req, "GET / HTTP/1.0\r\nHost: %s\r\n\r\n", argv[i]);
+            SDLNet_StreamSocket *sock = req ? SDLNet_CreateClient(addr, 80) : NULL;
+            if (!req) {
+                SDL_Log("Out of memory!");
+            } else if (!sock) {
+                SDL_Log("Failed to create stream socket to %s: %s\n", argv[i], SDL_GetError());
+            } else if (SDLNet_WaitUntilConnected(sock, -1) < 0) {
+                SDL_Log("Failed to connect to %s: %s", argv[i], SDL_GetError());
+            } else if (SDLNet_WriteToStreamSocket(sock, req, SDL_strlen(req)) < 0) {
+                SDL_Log("Failed to write to %s: %s", argv[i], SDL_GetError());
+            } else if (SDLNet_WaitUntilStreamSocketDrained(sock, -1) < 0) {
+                SDL_Log("Failed to finish write to %s: %s", argv[i], SDL_GetError());
+            } else {
+                char buf[512];
+                int br;
+                while ((br = SDLNet_ReadStreamSocket(sock, buf, sizeof (buf))) >= 0) {
+                    fwrite(buf, 1, br, stdout);
+                }
+
+                printf("\n\n\n%s\n\n\n", SDL_GetError());
+                fflush(stdout);
+            }
+
+            if (sock) {
+                SDLNet_DestroyStreamSocket(sock);
+            }
+
+            SDL_free(req);
+        }
+    }
+
+
+    SDLNet_Quit();
+
+    return 0;
+}
+