tinywebd

changeset 16:2873d3ec8c78

starting page generator
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Apr 2015 10:17:22 +0300
parents 4a25751fe61d
children 2874f61a43b1
files libtinyweb/src/genpage.c libtinyweb/src/genpage.h libtinyweb/src/tinyweb.c
diffstat 3 files changed, 106 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libtinyweb/src/genpage.c	Mon Apr 20 10:17:22 2015 +0300
     1.3 @@ -0,0 +1,50 @@
     1.4 +#include <string.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <stdarg.h>
     1.8 +#include <errno.h>
     1.9 +#include "genpage.h"
    1.10 +#include "http.h"
    1.11 +#include "logger.h"
    1.12 +
    1.13 +static char *footer;
    1.14 +
    1.15 +int set_gen_footer(const char *footer_fmt, ...)
    1.16 +{
    1.17 +	int len;
    1.18 +	char c;
    1.19 +	va_list ap;
    1.20 +
    1.21 +	va_start(ap, footer_fmt);
    1.22 +	len = snprintf(&c, 0, footer_fmt, ap);
    1.23 +	va_end(ap);
    1.24 +
    1.25 +	free(footer);
    1.26 +	if(!(footer = malloc(len + 1))) {
    1.27 +		logmsg("failed to set page generator footer: %s\n", strerror(errno));
    1.28 +		return -1;
    1.29 +	}
    1.30 +
    1.31 +	va_start(ap, footer_fmt);
    1.32 +	sprintf(footer, footer_fmt, ap);
    1.33 +	va_end(ap);
    1.34 +
    1.35 +	return 0;
    1.36 +}
    1.37 +
    1.38 +int gen_index(struct page *pg, const char *path)
    1.39 +{
    1.40 +}
    1.41 +
    1.42 +int gen_error(struct page *pg, int errcode, const char *uri)
    1.43 +{
    1.44 +}
    1.45 +
    1.46 +void destroy_page(struct page *pg)
    1.47 +{
    1.48 +	if(pg) {
    1.49 +		free(pg->text);
    1.50 +		pg->text = 0;
    1.51 +		pg->size = 0;
    1.52 +	}
    1.53 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/libtinyweb/src/genpage.h	Mon Apr 20 10:17:22 2015 +0300
     2.3 @@ -0,0 +1,15 @@
     2.4 +#ifndef GENPAGE_H_
     2.5 +#define GENPAGE_H_
     2.6 +
     2.7 +struct page {
     2.8 +	char *text;
     2.9 +	int size;
    2.10 +};
    2.11 +
    2.12 +int set_gen_footer(const char *footer_fmt, ...);
    2.13 +int gen_index(struct page *pg, const char *path);
    2.14 +int gen_error(struct page *pg, int errcode, const char *uri);
    2.15 +
    2.16 +void destroy_page(struct page *pg);
    2.17 +
    2.18 +#endif	/* GENPAGE_H_ */
     3.1 --- a/libtinyweb/src/tinyweb.c	Sun Apr 19 04:18:08 2015 +0300
     3.2 +++ b/libtinyweb/src/tinyweb.c	Mon Apr 20 10:17:22 2015 +0300
     3.3 @@ -12,6 +12,7 @@
     3.4  #include <errno.h>
     3.5  #include <unistd.h>
     3.6  #include <fcntl.h>
     3.7 +#include <dirent.h>
     3.8  #include <sys/stat.h>
     3.9  #include <sys/select.h>
    3.10  #include <sys/mman.h>
    3.11 @@ -42,6 +43,7 @@
    3.12  static void close_conn(struct client *c);
    3.13  static int handle_client(struct client *c);
    3.14  static int do_get(struct client *c, const char *uri, int with_body);
    3.15 +static int do_get_index(struct client *c, const char *path, int with_body);
    3.16  static void respond_error(struct client *c, int errcode);
    3.17  
    3.18  static int lis = -1;
    3.19 @@ -330,13 +332,13 @@
    3.20  			for(i=0; indexfiles[i]; i++) {
    3.21  				sprintf(path, "%s/%s", uri, indexfiles[i]);
    3.22  				if(stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
    3.23 +					/* found an index file */
    3.24  					break;
    3.25  				}
    3.26  			}
    3.27  
    3.28  			if(indexfiles[i] == 0) {
    3.29 -				respond_error(c, 404);
    3.30 -				return -1;
    3.31 +				return do_get_index(c, uri, with_body);
    3.32  			}
    3.33  		} else {
    3.34  			path = (char*)uri;
    3.35 @@ -368,6 +370,8 @@
    3.36  			}
    3.37  			ptr = cont;
    3.38  
    3.39 +			logmsg("GET %s OK! (%s)\n", uri, path);
    3.40 +
    3.41  			send(c->s, rsphdr, rspsize, 0);
    3.42  			while(cont_left > 0) {
    3.43  				int sz = cont_left < 4096 ? cont_left : 4096;
    3.44 @@ -378,6 +382,7 @@
    3.45  
    3.46  			munmap(cont, st.st_size);
    3.47  		} else {
    3.48 +			logmsg("HEAD %s OK! (%s)\n", uri, path);
    3.49  			send(c->s, rsphdr, rspsize, 0);
    3.50  		}
    3.51  
    3.52 @@ -386,13 +391,47 @@
    3.53  	return 0;
    3.54  }
    3.55  
    3.56 +static void filesizestr(char *str, unsigned long sz)
    3.57 +{
    3.58 +	int uidx = 0;
    3.59 +	static const char *unit[] = {"bytes", "kb", "mb", "gb", "tb", 0};
    3.60 +
    3.61 +	while(sz > 1024 && unit[uidx + 1]) {
    3.62 +		sz /= 1024;
    3.63 +		++uidx;
    3.64 +	}
    3.65 +	sprintf(str, "%lu %s", sz, unit[uidx]);
    3.66 +}
    3.67 +
    3.68 +struct dir_entry {
    3.69 +	char *name;
    3.70 +	unsigned long size;
    3.71 +	int is_dir;
    3.72 +	struct dir_entry *next;
    3.73 +};
    3.74 +
    3.75 +/* precondition: path is a directory */
    3.76 +static int do_get_index(struct client *c, const char *path, int with_body)
    3.77 +{
    3.78 +}
    3.79 +
    3.80 +static const char *error_page_fmt =
    3.81 +	"<html>"
    3.82 +	"<head><title>Error %d</title></head>\n"
    3.83 +	"<body><h1>HTTP Error %d</h1><p>%s</p></body>"
    3.84 +	"</html>";
    3.85 +
    3.86  static void respond_error(struct client *c, int errcode)
    3.87  {
    3.88  	char buf[512];
    3.89  
    3.90  	sprintf(buf, "HTTP/" HTTP_VER_STR " %d %s\r\n\r\n", errcode, http_strmsg(errcode));
    3.91 +	logmsg("error %d: %s\n", errcode, http_strmsg(errcode));
    3.92  
    3.93  	send(c->s, buf, strlen(buf), 0);
    3.94 +
    3.95 +	sprintf(buf, error_page_fmt, errcode, errcode, http_strmsg(errcode));
    3.96 +	send(c->s, buf, strlen(buf), 0);
    3.97  	close_conn(c);
    3.98  }
    3.99