wn_module_index.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * File : wn_module_index.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2011-08-02 Bernard the first version
  22. * 2012-12-21 aozima fixed version print.
  23. */
  24. #include <webnet.h>
  25. #include <wn_module.h>
  26. #include <wn_utils.h>
  27. #ifdef RT_USING_DFS
  28. #include <dfs_posix.h>
  29. #endif
  30. #ifdef WEBNET_USING_INDEX
  31. int webnet_module_dirindex(struct webnet_session* session, int event)
  32. {
  33. if( (session->request->method != WEBNET_GET)
  34. && (session->request->method != WEBNET_POST) )
  35. {
  36. return WEBNET_MODULE_CONTINUE;
  37. }
  38. if (event == WEBNET_EVENT_URI_POST)
  39. {
  40. DIR *dir;
  41. struct stat file_stat;
  42. struct webnet_request *request;
  43. static const char* header = "<html><head><title>Index of %s</title></head><body bgcolor=\"white\"><h1>Index of %s</h1><hr><pre>";
  44. static const char* foot = "</pre><hr>WebNet/%s (RT-Thread)</body></html>";
  45. RT_ASSERT(session != RT_NULL);
  46. request = session->request;
  47. RT_ASSERT(request != RT_NULL);
  48. if (stat(request->path, &file_stat) < 0 || !S_ISDIR(file_stat.st_mode))
  49. {
  50. return WEBNET_MODULE_CONTINUE;
  51. }
  52. dir = opendir(request->path);
  53. if (dir != RT_NULL)
  54. {
  55. struct stat s;
  56. struct dirent* dirent;
  57. const char* sub_path;
  58. char *fullpath;
  59. char *delim;
  60. dirent = RT_NULL;
  61. fullpath = wn_malloc (WEBNET_PATH_MAX);
  62. if (fullpath == RT_NULL)
  63. {
  64. request->result_code = 500;
  65. return WEBNET_MODULE_FINISHED;
  66. }
  67. webnet_session_set_header(session, "text/html", 200, "OK", -1);
  68. /* get sub path */
  69. sub_path = request->path + strlen(webnet_get_root());
  70. delim = strrchr(sub_path, '/');
  71. rt_snprintf(fullpath, delim - sub_path + 1, "%s", sub_path);
  72. webnet_session_printf(session, header, sub_path, sub_path);
  73. /* display parent directory */
  74. webnet_session_printf(session, "<a href=\"../\">..</a>\n");
  75. /* list directory */
  76. do
  77. {
  78. dirent = readdir(dir);
  79. if (dirent == RT_NULL) break;
  80. rt_memset(&s, 0, sizeof(struct stat));
  81. /* build full path for each file */
  82. rt_sprintf(fullpath, "%s/%s", request->path, dirent->d_name);
  83. str_normalize_path(fullpath);
  84. stat(fullpath, &s);
  85. rt_sprintf(fullpath, "%s/%s", sub_path, dirent->d_name);
  86. if ( s.st_mode & S_IFDIR )
  87. {
  88. webnet_session_printf(session, "<a href=\"%s/\">%s/</a>\n", fullpath, dirent->d_name);
  89. }
  90. else
  91. {
  92. webnet_session_printf(session, "<a href=\"%s\">%s</a>\t\t\t\t\t%d\n", fullpath, dirent->d_name, s.st_size);
  93. }
  94. }
  95. while (dirent != RT_NULL);
  96. closedir(dir);
  97. wn_free(fullpath);
  98. /* set foot */
  99. webnet_session_printf(session, foot, WEBNET_VERSION);
  100. return WEBNET_MODULE_FINISHED;
  101. }
  102. }
  103. return WEBNET_MODULE_CONTINUE;
  104. }
  105. #endif /* WEBNET_USING_INDEX */