From e029cf489e4da83b4ff09abeb562246e23644e47 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Tue, 2 Feb 2010 18:17:55 +0000 Subject: [PATCH] When a file was not found, and no notfound.html was found, send directory contents instead --- apps/webserver/httpd-cfs.c | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/apps/webserver/httpd-cfs.c b/apps/webserver/httpd-cfs.c index 6f10a07ae..e49444f9e 100644 --- a/apps/webserver/httpd-cfs.c +++ b/apps/webserver/httpd-cfs.c @@ -30,9 +30,13 @@ * * Author: Adam Dunkels * - * $Id: httpd-cfs.c,v 1.11 2009/02/09 13:04:37 fros4943 Exp $ + * $Id: httpd-cfs.c,v 1.12 2010/02/02 18:17:55 adamdunkels Exp $ */ +#include +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t size, const char *format, ...); +#endif /* HAVE_SNPRINTF */ #include #include "contiki-net.h" @@ -83,6 +87,34 @@ PT_THREAD(send_file(struct httpd_state *s)) } /*---------------------------------------------------------------------------*/ static +PT_THREAD(send_dir(struct httpd_state *s)) +{ + static char buf[80]; + static int r, num; + static struct cfs_dir dir; + static struct cfs_dirent dirent; + PSOCK_BEGIN(&s->sout); + + SEND_STRING(&s->sout, "File system contents:
\r\n"); + + r = cfs_opendir(&dir, "/"); + if(r == 0) { + num = 0; + while(cfs_readdir(&dir, &dirent) >= 0) { + snprintf(buf, sizeof(buf), "%s
", dirent.name, dirent.name); + SEND_STRING(&s->sout, buf); + ++num; + } + snprintf(buf, sizeof(buf), "%d files", num); + SEND_STRING(&s->sout, buf); + cfs_closedir(&dir); + } else { + SEND_STRING(&s->sout, "Could not open directory"); + } + PSOCK_END(&s->sout); +} +/*---------------------------------------------------------------------------*/ +static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { char *ptr; @@ -119,8 +151,11 @@ PT_THREAD(handle_output(struct httpd_state *s)) if(s->fd < 0) { s->fd = cfs_open("notfound.html", CFS_READ); if(s->fd < 0) { - uip_abort(); - memb_free(&conns, s); + PT_WAIT_THREAD(&s->outputpt, + send_headers(s, http_header_200)); + PT_WAIT_THREAD(&s->outputpt, + send_dir(s)); + uip_close(); webserver_log_file(&uip_conn->ripaddr, "reset (no notfound.html)"); PT_EXIT(&s->outputpt); }