commit 480252f

uint  ·  2026-01-25 18:30:14 +0000 UTC
parent 2b570ac
move http stuff->http.h/c

.
4 files changed,  +142, -104
+5, -0
 1@@ -1,3 +1,6 @@
 2+#ifndef CONFIG_H
 3+#define CONFIG_H
 4+
 5 #include <stdbool.h>
 6 
 7 static const char*  media_dir     = "/path/to/media";
 8@@ -11,3 +14,5 @@ enum {
 9 	LISTEN_BACKLOG = 64,
10 };
11 
12+#endif /* CONFIG_H */
13+
+113, -0
  1@@ -0,0 +1,113 @@
  2+#include <errno.h>
  3+#include <stddef.h>
  4+#include <stdio.h>
  5+#include <string.h>
  6+#include <unistd.h>
  7+
  8+#include "config.h"
  9+#include "http.h"
 10+
 11+static int read_reqline(int c, char* method, size_t msz, char* path, size_t psz);
 12+static void reply_text(int c, const char* status, const char* body);
 13+static int write_all(int fd, const void* buf, size_t n);
 14+
 15+int http_handle(char c)
 16+{
 17+	char method[16];
 18+	char path[1024];
 19+
 20+	if (read_reqline(c, method, sizeof(method), path, sizeof(path)) < 0) {
 21+		reply_text(c, HTTP_400, "bad request\n");
 22+		return -1;
 23+	}
 24+
 25+	if (strcmp(method, "GET") != 0) {
 26+		reply_text(c, HTTP_405, "method not allowed\n");
 27+		return 0;
 28+	}
 29+
 30+	if (strcmp(path, "/ping") == 0) {
 31+		reply_text(c, HTTP_200, "ok\n");
 32+		return 0;
 33+	}
 34+
 35+	reply_text(c, HTTP_404, "not found\n");
 36+	return 0;
 37+}
 38+
 39+static int read_reqline(int c, char* method, size_t msz, char* path, size_t psz)
 40+{
 41+	char buf[HTTP_REQ_MAX];
 42+	size_t used = 0;
 43+
 44+	for (;;) {
 45+		if (used + 1 >= sizeof(buf))
 46+			return -1;
 47+
 48+		ssize_t r = read(c, buf + used, sizeof(buf) - 1 - used);
 49+		if (r <= 0)
 50+			return -1;
 51+
 52+		used += (size_t)r;
 53+		buf[used] = '\0';
 54+
 55+		char* eol = strstr(buf, "\r\n");
 56+		if (eol) {
 57+			*eol = '\0';
 58+			break;
 59+		}
 60+	}
 61+
 62+	if (sscanf(buf, "%15s %1023s", method, path) != 2)
 63+		return -1;
 64+
 65+	method[msz-1] = '\0';
 66+	path[psz-1] = '\0';
 67+
 68+	return 0;
 69+}
 70+
 71+static void reply_text(int c, const char* status, const char* body)
 72+{
 73+	char resp[HTTP_RESP_MAX];
 74+	int n = snprintf(
 75+		resp, sizeof(resp),
 76+
 77+		"%s"
 78+		HTTP_TEXT
 79+		HTTP_LENGTH
 80+		HTTP_CLOSE
 81+		"\r\n"
 82+		"%s",
 83+
 84+		status,
 85+		strlen(body), body
 86+	);
 87+
 88+	if (n < 0)
 89+		return;
 90+
 91+	if ((size_t)n >= sizeof(resp))
 92+		n = (int)(sizeof(resp) - 1);
 93+
 94+	(void)write_all(c, resp, (size_t)n);
 95+}
 96+
 97+static int write_all(int fd, const void* buf, size_t n)
 98+{
 99+	const char* p = buf;
100+
101+	while (n > 0) {
102+		size_t w = write(fd, p, n);
103+		if (w < 0) {
104+			if (errno == EINTR)
105+				continue;
106+			return -1;
107+		}
108+
109+		p += (size_t)w;
110+		n -= (size_t)w;
111+	}
112+
113+	return 0;
114+}
+16, -0
 1@@ -0,0 +1,16 @@
 2+#ifndef HTTP_H
 3+#define HTTP_H
 4+
 5+#define HTTP_200    "HTTP/1.1 200 OK\r\n"
 6+#define HTTP_400    "HTTP/1.1 400 Bad Request\r\n"
 7+#define HTTP_404    "HTTP/1.1 404 Not Found\r\n"
 8+#define HTTP_405    "HTTP/1.1 405 Method Not Allowed\r\n"
 9+
10+#define HTTP_TEXT   "Content-Type: text/plain\r\n"
11+#define HTTP_CLOSE  "Connection: close\r\n"
12+#define HTTP_LENGTH "Content-Length: %zu\r\n"
13+
14+int http_handle(char c);
15+
16+#endif /* HTTP_H */
17+
+8, -104
  1@@ -1,3 +1,9 @@
  2+/*  parados - simple media server
  3+	
  4+	this software is licensed under ISC
  5+	check LICENCE for more details
  6+*/
  7+
  8 #include <errno.h>
  9 #include <stddef.h>
 10 #include <stdio.h>
 11@@ -10,10 +16,9 @@
 12 #include <netinet/in.h>
 13 
 14 #include "config.h"
 15+#include "http.h"
 16 
 17 void die(const char* s, int e);
 18-int read_reqline(int c, char* method, size_t msz, char* path, size_t psz);
 19-void reply_text(int c, const char* status, const char* body);
 20 void run(void);
 21 void setup(void);
 22 int write_all(int fd, const void* buf, size_t n);
 23@@ -26,64 +31,6 @@ void die(const char* s, int e)
 24 	exit(e);
 25 }
 26 
 27-int read_reqline(int c, char* method, size_t msz, char* path, size_t psz)
 28-{
 29-	char buf[HTTP_REQ_MAX];
 30-	size_t used = 0;
 31-
 32-	for (;;) {
 33-		if (used + 1 >= sizeof(buf))
 34-			return -1;
 35-
 36-		ssize_t r = read(c, buf + used, sizeof(buf) - 1 - used);
 37-		if (r <= 0)
 38-			return -1;
 39-
 40-		used += (size_t)r;
 41-		buf[used] = '\0';
 42-
 43-		char* eol = strstr(buf, "\r\n");
 44-		if (eol) {
 45-			*eol = '\0';
 46-			break;
 47-		}
 48-	}
 49-
 50-	if (sscanf(buf, "%15s %1023s", method, path) != 2)
 51-		return -1;
 52-
 53-	method[msz-1] = '\0';
 54-	path[psz-1] = '\0';
 55-
 56-	return 0;
 57-}
 58-
 59-void reply_text(int c, const char* status, const char* body)
 60-{
 61-	char resp[HTTP_RESP_MAX];
 62-	int n = snprintf(
 63-		resp, sizeof(resp),
 64-
 65-		"%s"
 66-		"Content-Type: text/plain\r\n"
 67-		"Content-Length: %zu\r\n"
 68-		"Connection: close\r\n"
 69-		"\r\n"
 70-		"%s",
 71-
 72-		status,
 73-		strlen(body), body
 74-	);
 75-
 76-	if (n < 0)
 77-		return;
 78-
 79-	if ((size_t)n >= sizeof(resp))
 80-		n = (int)(sizeof(resp) - 1);
 81-
 82-	(void)write_all(c, resp, (size_t)n);
 83-}
 84-
 85 void run(void)
 86 {
 87 	for (;;) {
 88@@ -94,31 +41,7 @@ void run(void)
 89 			continue;
 90 		}
 91 
 92-		char method[16];
 93-		char path[1024];
 94-
 95-		if (read_reqline(c, method, sizeof(method), path, sizeof(path)) < 0) {
 96-			reply_text(c, "HTTP/1.1 400 Bad Request\r\n", "bad request\n");
 97-			shutdown(c, SHUT_WR);
 98-			close(c);
 99-			continue;
100-		}
101-
102-		if (strcmp(method, "GET") != 0) {
103-			reply_text(c, "HTTP/1.1 405 Method Not Allowed\r\n", "method not allowed\n");
104-			shutdown(c, SHUT_WR);
105-			close(c);
106-			continue;
107-		}
108-
109-		if (strcmp(path, "/ping") == 0) {
110-			reply_text(c, "HTTP/1.1 200 OK\r\n", "ok\n");
111-			shutdown(c, SHUT_WR);
112-			close(c);
113-			continue;
114-		}
115-
116-		reply_text(c, "HTTP/1.1 404 Not Found\r\n", "not found\n");
117+		(void)http_handle(c);
118 		shutdown(c, SHUT_WR);
119 		close(c);
120 	}
121@@ -156,25 +79,6 @@ void setup(void)
122 		printf("listening on %s:%d\n", server_addr, server_port);
123 }
124 
125-int write_all(int fd, const void* buf, size_t n)
126-{
127-	const char* p = buf;
128-
129-	while (n > 0) {
130-		ssize_t w = write(fd, p, n);
131-		if (w < 0) {
132-			if (errno == EINTR)
133-				continue;
134-			return -1;
135-		}
136-
137-		p += (size_t)w;
138-		n -= (size_t)w;
139-	}
140-
141-	return 0;
142-}
143-
144 int main(void)
145 {
146 	setup();