wn_module_index.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 webnet_request *request;
  42. static const char* header = "<html><head><title>Index of %s</title></head><body bgcolor=\"white\"><h1>Index of %s</h1><hr><pre>";
  43. static const char* foot = "</pre><hr>WebNet/%s (RT-Thread)</body></html>";
  44. RT_ASSERT(session != RT_NULL);
  45. request = session->request;
  46. RT_ASSERT(request != RT_NULL);
  47. dir = opendir(request->path);
  48. if (dir != RT_NULL)
  49. {
  50. struct stat s;
  51. struct dirent* dirent;
  52. const char* sub_path;
  53. char *fullpath;
  54. char *delim;
  55. dirent = RT_NULL;
  56. fullpath = wn_malloc (WEBNET_PATH_MAX);
  57. if (fullpath == RT_NULL)
  58. {
  59. request->result_code = 500;
  60. return WEBNET_MODULE_FINISHED;
  61. }
  62. webnet_session_set_header(session, "text/html", 200, "OK", -1);
  63. /* get sub path */
  64. sub_path = request->path + strlen(webnet_get_root());
  65. delim = strrchr(sub_path, '/');
  66. rt_snprintf(fullpath, delim - sub_path + 1, "%s", sub_path);
  67. webnet_session_printf(session, header, sub_path, sub_path);
  68. /* display parent directory */
  69. webnet_session_printf(session, "<a href=\"../\">..</a>\n");
  70. /* list directory */
  71. do
  72. {
  73. dirent = readdir(dir);
  74. if (dirent == RT_NULL) break;
  75. rt_memset(&s, 0, sizeof(struct stat));
  76. /* build full path for each file */
  77. rt_sprintf(fullpath, "%s/%s", request->path, dirent->d_name);
  78. str_normalize_path(fullpath);
  79. stat(fullpath, &s);
  80. rt_sprintf(fullpath, "%s/%s", sub_path, dirent->d_name);
  81. if ( s.st_mode & S_IFDIR )
  82. {
  83. webnet_session_printf(session, "<a href=\"%s/\">%s/</a>\n", fullpath, dirent->d_name);
  84. }
  85. else
  86. {
  87. webnet_session_printf(session, "<a href=\"%s\">%s</a>\t\t\t\t\t%d\n", fullpath, dirent->d_name, s.st_size);
  88. }
  89. }
  90. while (dirent != RT_NULL);
  91. closedir(dir);
  92. wn_free(fullpath);
  93. /* set foot */
  94. webnet_session_printf(session, foot, WEBNET_VERSION);
  95. return WEBNET_MODULE_FINISHED;
  96. }
  97. }
  98. return WEBNET_MODULE_CONTINUE;
  99. }
  100. #endif /* WEBNET_USING_INDEX */