tinywebd

diff libtinyweb/src/tinyweb.c @ 16:2873d3ec8c78

starting page generator
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Apr 2015 10:17:22 +0300
parents 86f703031228
children
line diff
     1.1 --- a/libtinyweb/src/tinyweb.c	Sun Apr 19 04:18:08 2015 +0300
     1.2 +++ b/libtinyweb/src/tinyweb.c	Mon Apr 20 10:17:22 2015 +0300
     1.3 @@ -12,6 +12,7 @@
     1.4  #include <errno.h>
     1.5  #include <unistd.h>
     1.6  #include <fcntl.h>
     1.7 +#include <dirent.h>
     1.8  #include <sys/stat.h>
     1.9  #include <sys/select.h>
    1.10  #include <sys/mman.h>
    1.11 @@ -42,6 +43,7 @@
    1.12  static void close_conn(struct client *c);
    1.13  static int handle_client(struct client *c);
    1.14  static int do_get(struct client *c, const char *uri, int with_body);
    1.15 +static int do_get_index(struct client *c, const char *path, int with_body);
    1.16  static void respond_error(struct client *c, int errcode);
    1.17  
    1.18  static int lis = -1;
    1.19 @@ -330,13 +332,13 @@
    1.20  			for(i=0; indexfiles[i]; i++) {
    1.21  				sprintf(path, "%s/%s", uri, indexfiles[i]);
    1.22  				if(stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
    1.23 +					/* found an index file */
    1.24  					break;
    1.25  				}
    1.26  			}
    1.27  
    1.28  			if(indexfiles[i] == 0) {
    1.29 -				respond_error(c, 404);
    1.30 -				return -1;
    1.31 +				return do_get_index(c, uri, with_body);
    1.32  			}
    1.33  		} else {
    1.34  			path = (char*)uri;
    1.35 @@ -368,6 +370,8 @@
    1.36  			}
    1.37  			ptr = cont;
    1.38  
    1.39 +			logmsg("GET %s OK! (%s)\n", uri, path);
    1.40 +
    1.41  			send(c->s, rsphdr, rspsize, 0);
    1.42  			while(cont_left > 0) {
    1.43  				int sz = cont_left < 4096 ? cont_left : 4096;
    1.44 @@ -378,6 +382,7 @@
    1.45  
    1.46  			munmap(cont, st.st_size);
    1.47  		} else {
    1.48 +			logmsg("HEAD %s OK! (%s)\n", uri, path);
    1.49  			send(c->s, rsphdr, rspsize, 0);
    1.50  		}
    1.51  
    1.52 @@ -386,13 +391,47 @@
    1.53  	return 0;
    1.54  }
    1.55  
    1.56 +static void filesizestr(char *str, unsigned long sz)
    1.57 +{
    1.58 +	int uidx = 0;
    1.59 +	static const char *unit[] = {"bytes", "kb", "mb", "gb", "tb", 0};
    1.60 +
    1.61 +	while(sz > 1024 && unit[uidx + 1]) {
    1.62 +		sz /= 1024;
    1.63 +		++uidx;
    1.64 +	}
    1.65 +	sprintf(str, "%lu %s", sz, unit[uidx]);
    1.66 +}
    1.67 +
    1.68 +struct dir_entry {
    1.69 +	char *name;
    1.70 +	unsigned long size;
    1.71 +	int is_dir;
    1.72 +	struct dir_entry *next;
    1.73 +};
    1.74 +
    1.75 +/* precondition: path is a directory */
    1.76 +static int do_get_index(struct client *c, const char *path, int with_body)
    1.77 +{
    1.78 +}
    1.79 +
    1.80 +static const char *error_page_fmt =
    1.81 +	"<html>"
    1.82 +	"<head><title>Error %d</title></head>\n"
    1.83 +	"<body><h1>HTTP Error %d</h1><p>%s</p></body>"
    1.84 +	"</html>";
    1.85 +
    1.86  static void respond_error(struct client *c, int errcode)
    1.87  {
    1.88  	char buf[512];
    1.89  
    1.90  	sprintf(buf, "HTTP/" HTTP_VER_STR " %d %s\r\n\r\n", errcode, http_strmsg(errcode));
    1.91 +	logmsg("error %d: %s\n", errcode, http_strmsg(errcode));
    1.92  
    1.93  	send(c->s, buf, strlen(buf), 0);
    1.94 +
    1.95 +	sprintf(buf, error_page_fmt, errcode, errcode, http_strmsg(errcode));
    1.96 +	send(c->s, buf, strlen(buf), 0);
    1.97  	close_conn(c);
    1.98  }
    1.99