wn_module_index.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if RT_VER_NUM >= 0x40100
  29. #include <dirent.h> /* fix DIR */
  30. #include <fcntl.h> /* fix S_IFDIR */
  31. #else
  32. #include <dfs_posix.h>
  33. #endif /*RT_VER_NUM >= 0x40100*/
  34. #endif
  35. #ifdef WEBNET_USING_INDEX
  36. int webnet_module_dirindex(struct webnet_session* session, int event)
  37. {
  38. if( (session->request->method != WEBNET_GET)
  39. && (session->request->method != WEBNET_POST) )
  40. {
  41. return WEBNET_MODULE_CONTINUE;
  42. }
  43. if (event == WEBNET_EVENT_URI_POST)
  44. {
  45. DIR *dir;
  46. struct stat file_stat;
  47. struct webnet_request *request;
  48. static const char* header = "<html><head><title>Index of %s</title></head><body bgcolor=\"white\"><h1>Index of %s</h1><hr><pre>";
  49. static const char* foot = "</pre><hr>WebNet/%s (RT-Thread)</body></html>";
  50. RT_ASSERT(session != RT_NULL);
  51. request = session->request;
  52. RT_ASSERT(request != RT_NULL);
  53. if (stat(request->path, &file_stat) < 0 || !S_ISDIR(file_stat.st_mode))
  54. {
  55. return WEBNET_MODULE_CONTINUE;
  56. }
  57. dir = opendir(request->path);
  58. if (dir != RT_NULL)
  59. {
  60. struct stat s;
  61. struct dirent* dirent;
  62. const char* sub_path;
  63. char *fullpath;
  64. char *delim;
  65. dirent = RT_NULL;
  66. fullpath = wn_malloc (WEBNET_PATH_MAX);
  67. if (fullpath == RT_NULL)
  68. {
  69. request->result_code = 500;
  70. return WEBNET_MODULE_FINISHED;
  71. }
  72. webnet_session_set_header(session, "text/html", 200, "OK", -1);
  73. /* get sub path */
  74. sub_path = request->path + strlen(webnet_get_root());
  75. delim = strrchr(sub_path, '/');
  76. rt_snprintf(fullpath, delim - sub_path + 1, "%s", sub_path);
  77. webnet_session_printf(session, header, sub_path, sub_path);
  78. /* display parent directory */
  79. webnet_session_printf(session, "<a href=\"../\">..</a>\n");
  80. /* list directory */
  81. do
  82. {
  83. dirent = readdir(dir);
  84. if (dirent == RT_NULL) break;
  85. rt_memset(&s, 0, sizeof(struct stat));
  86. /* build full path for each file */
  87. rt_sprintf(fullpath, "%s/%s", request->path, dirent->d_name);
  88. str_normalize_path(fullpath);
  89. stat(fullpath, &s);
  90. rt_sprintf(fullpath, "%s/%s", sub_path, dirent->d_name);
  91. if ( s.st_mode & S_IFDIR )
  92. {
  93. webnet_session_printf(session, "<a href=\"%s/\">%s/</a>\n", fullpath, dirent->d_name);
  94. }
  95. else
  96. {
  97. webnet_session_printf(session, "<a href=\"%s\">%s</a>\t\t\t\t\t%d\n", fullpath, dirent->d_name, s.st_size);
  98. }
  99. }
  100. while (dirent != RT_NULL);
  101. closedir(dir);
  102. wn_free(fullpath);
  103. /* set foot */
  104. webnet_session_printf(session, foot, WEBNET_VERSION);
  105. return WEBNET_MODULE_FINISHED;
  106. }
  107. }
  108. return WEBNET_MODULE_CONTINUE;
  109. }
  110. #endif /* WEBNET_USING_INDEX */