diff --git a/include/netutils/httpd.h b/include/netutils/httpd.h index cba69574f..1c981fb85 100644 --- a/include/netutils/httpd.h +++ b/include/netutils/httpd.h @@ -199,6 +199,7 @@ uint16_t httpd_fs_count(char *name); int httpd_send_datachunk(int sockfd, void *data, int len, bool chunked); #ifdef CONFIG_NETUTILS_HTTPD_DIRLIST +bool httpd_is_file(FAR const char *filename); ssize_t httpd_dirlist(int outfd, FAR struct httpd_fs_file *file); #endif diff --git a/netutils/webserver/httpd.c b/netutils/webserver/httpd.c index c497af013..503c0eedf 100644 --- a/netutils/webserver/httpd.c +++ b/netutils/webserver/httpd.c @@ -494,11 +494,7 @@ static int send_headers(struct httpd_state *pstate, int status, int len) ptr = strrchr(pstate->ht_filename, ISO_period); if (ptr == NULL) { -#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST - mime = "text/html"; -#else mime = "application/octet-stream"; -#endif } else { @@ -514,6 +510,15 @@ static int send_headers(struct httpd_state *pstate, int status, int len) } } +#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST + if (false == httpd_is_file(pstate->ht_filename)) + { + /* we assume that it's a directory */ + + mime = "text/html"; + } +#endif + if (len >= 0) { (void)snprintf(contentlen, HTTPD_MAX_CONTENTLEN, diff --git a/netutils/webserver/httpd_dirlist.c b/netutils/webserver/httpd_dirlist.c index 1c48a5691..48226a635 100644 --- a/netutils/webserver/httpd_dirlist.c +++ b/netutils/webserver/httpd_dirlist.c @@ -114,6 +114,38 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: httpd_is_file + ****************************************************************************/ + +bool httpd_is_file(FAR const char *filename) +{ + char *path; + int fd; + bool ret = false; + + path = malloc(CONFIG_NAME_MAX); + ASSERT(path); + + snprintf(path, CONFIG_NAME_MAX, "%s/%s", + CONFIG_NETUTILS_HTTPD_PATH, filename); + + fd = open(path, "O_RDONLY"); + + if (-1 != fd) + { + close(fd); + ret = true; + } + + free(path); + return ret; +} + +/**************************************************************************** + * Name: httpd_dirlist + ****************************************************************************/ + ssize_t httpd_dirlist(int outfd, FAR struct httpd_fs_file *file) { struct dirent *dent;