commit 5ac127c29cd64a2ed079568789323c15a7a0753f
parent 7b8331c5839aff13f698489f54a06b61be506b5c
Author: pyratebeard <root@pyratebeard.net>
Date: Fri, 6 Aug 2021 14:53:44 +0100
serve index.html file if no redirect link
Diffstat:
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/bitreich-httpd.c b/bitreich-httpd.c
@@ -15,7 +15,7 @@
#include <sys/socket.h>
#include <netdb.h>
-#define WWWBASE "/var/cache/shorturl"
+#define WWWBASE "/var/www/html"
#define CHARSET "abcdefghijklmnopqrstuvwxyz"
#define NAMELEN 3
@@ -161,7 +161,7 @@ saveurl(char *wwwbase, char *location)
return 1;
asprintf(&path, "%s/%s", wwwbase, name);
- asprintf(&url, "http://%s/%s", host, name);
+ asprintf(&url, "http://%s/%s\r\n", host, name);
if ((fd = open(path, O_WRONLY|O_CREAT, 0644)) < 0)
return 1;
@@ -186,13 +186,14 @@ saveurl(char *wwwbase, char *location)
int
main(int argc, char *argv[])
{
- char *wwwbase, request[512], *path, *url,
+ char *wwwbase, *wwwindex, request[512], *ctype, *path, *url,
clienth[NI_MAXHOST], clientp[NI_MAXSERV];
int rlen;
struct sockaddr_storage clt;
socklen_t cltlen = sizeof(clt);
wwwbase = WWWBASE;
+ wwwindex = "index.html";
if (!getpeername(0, (struct sockaddr *)&clt, &cltlen)) {
if (getnameinfo((struct sockaddr *)&clt, cltlen, clienth,
@@ -222,8 +223,11 @@ main(int argc, char *argv[])
return 0;
} else if (!strncmp(request, "GET ", 4)) {
if (!strncmp(request + 4, "/ ", 2) || strstr(request, "/../")) {
- print404();
- return 1;
+ asprintf(&path, "%s/%s", wwwbase, wwwindex);
+ ctype = "text/html";
+ rlen = servefile(path, ctype, 1);
+ free(path);
+ return rlen;
} else {
char *p = strstr(request + 4, " ");
*p = '\0';