# HG changeset patch # User John Tsiombikas # Date 1429514242 -10800 # Node ID 2873d3ec8c78397066588dc93c907509ecf56a30 # Parent 4a25751fe61d24539a7eacafafda215eed08b750 starting page generator diff -r 4a25751fe61d -r 2873d3ec8c78 libtinyweb/src/genpage.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libtinyweb/src/genpage.c Mon Apr 20 10:17:22 2015 +0300 @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include "genpage.h" +#include "http.h" +#include "logger.h" + +static char *footer; + +int set_gen_footer(const char *footer_fmt, ...) +{ + int len; + char c; + va_list ap; + + va_start(ap, footer_fmt); + len = snprintf(&c, 0, footer_fmt, ap); + va_end(ap); + + free(footer); + if(!(footer = malloc(len + 1))) { + logmsg("failed to set page generator footer: %s\n", strerror(errno)); + return -1; + } + + va_start(ap, footer_fmt); + sprintf(footer, footer_fmt, ap); + va_end(ap); + + return 0; +} + +int gen_index(struct page *pg, const char *path) +{ +} + +int gen_error(struct page *pg, int errcode, const char *uri) +{ +} + +void destroy_page(struct page *pg) +{ + if(pg) { + free(pg->text); + pg->text = 0; + pg->size = 0; + } +} diff -r 4a25751fe61d -r 2873d3ec8c78 libtinyweb/src/genpage.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libtinyweb/src/genpage.h Mon Apr 20 10:17:22 2015 +0300 @@ -0,0 +1,15 @@ +#ifndef GENPAGE_H_ +#define GENPAGE_H_ + +struct page { + char *text; + int size; +}; + +int set_gen_footer(const char *footer_fmt, ...); +int gen_index(struct page *pg, const char *path); +int gen_error(struct page *pg, int errcode, const char *uri); + +void destroy_page(struct page *pg); + +#endif /* GENPAGE_H_ */ diff -r 4a25751fe61d -r 2873d3ec8c78 libtinyweb/src/tinyweb.c --- a/libtinyweb/src/tinyweb.c Sun Apr 19 04:18:08 2015 +0300 +++ b/libtinyweb/src/tinyweb.c Mon Apr 20 10:17:22 2015 +0300 @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ static void close_conn(struct client *c); static int handle_client(struct client *c); static int do_get(struct client *c, const char *uri, int with_body); +static int do_get_index(struct client *c, const char *path, int with_body); static void respond_error(struct client *c, int errcode); static int lis = -1; @@ -330,13 +332,13 @@ for(i=0; indexfiles[i]; i++) { sprintf(path, "%s/%s", uri, indexfiles[i]); if(stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) { + /* found an index file */ break; } } if(indexfiles[i] == 0) { - respond_error(c, 404); - return -1; + return do_get_index(c, uri, with_body); } } else { path = (char*)uri; @@ -368,6 +370,8 @@ } ptr = cont; + logmsg("GET %s OK! (%s)\n", uri, path); + send(c->s, rsphdr, rspsize, 0); while(cont_left > 0) { int sz = cont_left < 4096 ? cont_left : 4096; @@ -378,6 +382,7 @@ munmap(cont, st.st_size); } else { + logmsg("HEAD %s OK! (%s)\n", uri, path); send(c->s, rsphdr, rspsize, 0); } @@ -386,13 +391,47 @@ return 0; } +static void filesizestr(char *str, unsigned long sz) +{ + int uidx = 0; + static const char *unit[] = {"bytes", "kb", "mb", "gb", "tb", 0}; + + while(sz > 1024 && unit[uidx + 1]) { + sz /= 1024; + ++uidx; + } + sprintf(str, "%lu %s", sz, unit[uidx]); +} + +struct dir_entry { + char *name; + unsigned long size; + int is_dir; + struct dir_entry *next; +}; + +/* precondition: path is a directory */ +static int do_get_index(struct client *c, const char *path, int with_body) +{ +} + +static const char *error_page_fmt = + "" + "Error %d\n" + "

HTTP Error %d

%s

" + ""; + static void respond_error(struct client *c, int errcode) { char buf[512]; sprintf(buf, "HTTP/" HTTP_VER_STR " %d %s\r\n\r\n", errcode, http_strmsg(errcode)); + logmsg("error %d: %s\n", errcode, http_strmsg(errcode)); send(c->s, buf, strlen(buf), 0); + + sprintf(buf, error_page_fmt, errcode, errcode, http_strmsg(errcode)); + send(c->s, buf, strlen(buf), 0); close_conn(c); }