wn_module_alias.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * File : wn_module_alias.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. */
  23. #include <webnet.h>
  24. #include <wn_module.h>
  25. #include <wn_utils.h>
  26. #ifdef WEBNET_USING_ALIAS
  27. struct webnet_alias_item
  28. {
  29. char* old_path;
  30. char* new_path;
  31. };
  32. static struct webnet_alias_item *_alias_items = RT_NULL;
  33. static rt_uint32_t _alias_item_count = 0;
  34. void webnet_alias_set(char* old_path, char* new_path)
  35. {
  36. if (_alias_items == RT_NULL)
  37. {
  38. _alias_item_count = 1;
  39. _alias_items = (struct webnet_alias_item*)wn_malloc (sizeof(struct webnet_alias_item) *
  40. _alias_item_count);
  41. }
  42. else
  43. {
  44. _alias_item_count += 1;
  45. _alias_items = (struct webnet_alias_item*) wn_realloc (_alias_items, sizeof(struct webnet_alias_item) *
  46. _alias_item_count);
  47. }
  48. RT_ASSERT(_alias_items != RT_NULL);
  49. _alias_items[_alias_item_count - 1].old_path = wn_strdup(old_path);
  50. RT_ASSERT(_alias_items[_alias_item_count - 1].old_path != RT_NULL);
  51. _alias_items[_alias_item_count - 1].new_path = wn_strdup(new_path);
  52. RT_ASSERT(_alias_items[_alias_item_count - 1].new_path != RT_NULL);
  53. }
  54. RTM_EXPORT(webnet_alias_set);
  55. int webnet_module_alias(struct webnet_session* session, int event)
  56. {
  57. if (event == WEBNET_EVENT_URI_PHYSICAL)
  58. {
  59. int index;
  60. struct webnet_request* request;
  61. RT_ASSERT(session != RT_NULL);
  62. request = session->request;
  63. RT_ASSERT(request != RT_NULL);
  64. /* check whether the uri is a alias */
  65. for (index = 0; index < _alias_item_count; index ++)
  66. {
  67. if (str_path_with(request->path, _alias_items[index].old_path))
  68. {
  69. char* map_path;
  70. map_path = (char*) wn_malloc (WEBNET_PATH_MAX);
  71. RT_ASSERT(map_path != RT_NULL);
  72. rt_snprintf(map_path, WEBNET_PATH_MAX, "%s/%s",
  73. _alias_items[index].new_path,
  74. request->path + strlen(_alias_items[index].old_path));
  75. /* set new path */
  76. wn_free(request->path);
  77. request->path = map_path;
  78. return WEBNET_MODULE_CONTINUE;
  79. }
  80. }
  81. }
  82. return WEBNET_MODULE_CONTINUE;
  83. }
  84. #endif /* WEBNET_USING_ALIAS */