From a5dff5e08afab1ca9749ca086384a914f04d7c7d Mon Sep 17 00:00:00 2001 From: oliverschmidt Date: Mon, 30 Aug 2010 19:44:38 +0000 Subject: [PATCH] Up to now the root of the webserver filesystem was always the current directory. Now an optional file 'wwwroot.cfg' is read. If present its content is used as prefix in the conversion of http paths to local paths. This prefix may be a relative path as well as an absolute path. This is especially useful for the cc65-based targets as it ain't possible there to "just change into the wwwroot directory and start the webserver using its pathname". --- apps/webserver/httpd-cfs.c | 5 +++- apps/webserver/urlconv.c | 23 +++++++++++++++---- apps/webserver/urlconv.h | 3 ++- .../ipv6/rpl-border-router/httpd-simple.c | 5 +++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/webserver/httpd-cfs.c b/apps/webserver/httpd-cfs.c index 8f3ef910d..f4021a0a8 100644 --- a/apps/webserver/httpd-cfs.c +++ b/apps/webserver/httpd-cfs.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: httpd-cfs.c,v 1.22 2010/04/11 20:54:39 oliverschmidt Exp $ + * $Id: httpd-cfs.c,v 1.23 2010/08/30 19:44:38 oliverschmidt Exp $ */ #include @@ -273,5 +273,8 @@ httpd_init(void) { tcp_listen(HTONS(80)); memb_init(&conns); +#if URLCONV + urlconv_init(); +#endif /* URLCONV */ } /*---------------------------------------------------------------------------*/ diff --git a/apps/webserver/urlconv.c b/apps/webserver/urlconv.c index 22d5010df..3693ce178 100644 --- a/apps/webserver/urlconv.c +++ b/apps/webserver/urlconv.c @@ -30,11 +30,12 @@ * * Author: Kajtar Zsolt * - * $Id: urlconv.c,v 1.1 2010/04/11 19:18:47 oliverschmidt Exp $ + * $Id: urlconv.c,v 1.2 2010/08/30 19:44:38 oliverschmidt Exp $ */ #include +#include "cfs/cfs.h" #include "http-strings.h" #define ISO_number 0x23 @@ -43,9 +44,22 @@ #define ISO_slash 0x2f #define ISO_question 0x3f +static char wwwroot[40]; +static unsigned char wwwrootlen; + +void +urlconv_init(void) +{ + int fd = cfs_open("wwwroot.cfg", CFS_READ); + int rd = cfs_read(fd, wwwroot, sizeof(wwwroot)); + cfs_close(fd); + if(rd != -1) wwwrootlen = rd; +} + /*---------------------------------------------------------------------------*/ /* URL to filename conversion * + * prepends wwwroot prefix * normalizes path by removing "/./" * interprets "/../" and calculates path accordingly * resulting path is always absolute @@ -64,10 +78,11 @@ urlconv_tofilename(char *dest, char *source, unsigned char maxlen) static unsigned char c, hex1; static unsigned char *from, *to; + *dest = ISO_slash; + strncpy(dest + 1, wwwroot, wwwrootlen); len = 0; - from = source; to = dest; - *to = ISO_slash; - maxlen -= 2; + from = source; to = dest + wwwrootlen; + maxlen -= 2 + wwwrootlen; do { c = *(from++); switch(c) { diff --git a/apps/webserver/urlconv.h b/apps/webserver/urlconv.h index 302c1cb99..23d6bfe51 100644 --- a/apps/webserver/urlconv.h +++ b/apps/webserver/urlconv.h @@ -28,12 +28,13 @@ * * This file is part of the Contiki operating system. * - * $Id: urlconv.h,v 1.2 2010/04/11 20:16:56 oliverschmidt Exp $ + * $Id: urlconv.h,v 1.3 2010/08/30 19:44:38 oliverschmidt Exp $ */ #ifndef __URLCONV_H__ #define __URLCONV_H__ +void urlconv_init(void); void urlconv_tofilename(char *dest, char *source, unsigned char maxlen); #endif /* __URLCONV_H__ */ diff --git a/examples/ipv6/rpl-border-router/httpd-simple.c b/examples/ipv6/rpl-border-router/httpd-simple.c index f216facb1..d7a1db628 100644 --- a/examples/ipv6/rpl-border-router/httpd-simple.c +++ b/examples/ipv6/rpl-border-router/httpd-simple.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: httpd-simple.c,v 1.2 2010/05/09 13:29:33 nifi Exp $ + * $Id: httpd-simple.c,v 1.3 2010/08/30 19:44:38 oliverschmidt Exp $ */ /** @@ -251,5 +251,8 @@ httpd_init(void) { tcp_listen(HTONS(80)); memb_init(&conns); +#if URLCONV + urlconv_init(); +#else /* URLCONV */ } /*---------------------------------------------------------------------------*/