wn_session.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * File : wn_session.h
  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. #ifndef __WN_SESSION_H__
  24. #define __WN_SESSION_H__
  25. #include <wn_request.h>
  26. #ifdef RT_USING_SAL
  27. #include <sys/socket.h>
  28. #else
  29. #include <lwip/sockets.h>
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #define WEBNET_SESSION_BUFSZ (4 * 1024)
  35. /* close session */
  36. #define WEBNET_EVENT_CLOSE (1 << 5)
  37. /* read session */
  38. #define WEBNET_EVENT_READ (1 << 6)
  39. /* write session */
  40. #define WEBNET_EVENT_WRITE (1 << 7)
  41. struct webnet_session_ops
  42. {
  43. void (*session_handle)(struct webnet_session* session, int event);
  44. void (*session_close) (struct webnet_session* session);
  45. };
  46. enum webnet_session_phase
  47. {
  48. WEB_PHASE_METHOD = 0, /* parse method */
  49. WEB_PHASE_HEADER, /* handle web request header */
  50. WEB_PHASE_QUERY, /* handle web query */
  51. WEB_PHASE_RESPONSE, /* handle web response */
  52. WEB_PHASE_CLOSE, /* to close session */
  53. };
  54. struct webnet_session
  55. {
  56. struct webnet_session *next;
  57. /* socket information */
  58. int socket;
  59. struct sockaddr_in cliaddr;
  60. /* webnet request */
  61. struct webnet_request* request;
  62. /* session buffer */
  63. rt_uint16_t buffer_length;
  64. rt_uint16_t buffer_offset;
  65. rt_uint8_t buffer[WEBNET_SESSION_BUFSZ];
  66. /* session phase */
  67. rt_uint32_t session_phase;
  68. rt_uint32_t session_event_mask;
  69. const struct webnet_session_ops* session_ops;
  70. rt_uint32_t user_data;
  71. };
  72. struct webnet_session* webnet_session_create(int listenfd);
  73. int webnet_session_read(struct webnet_session *session, char *buffer, int length);
  74. void webnet_session_close(struct webnet_session *session);
  75. void webnet_session_printf(struct webnet_session *session, const char* fmt, ...);
  76. int webnet_session_write(struct webnet_session *session, const rt_uint8_t* data, rt_size_t size);
  77. int webnet_session_redirect(struct webnet_session *session, const char* url);
  78. int webnet_session_get_physical_path(struct webnet_session *session, const char* virtual_path, char* full_path);
  79. void webnet_session_set_header(struct webnet_session *session, const char* mimetype, int code, const char* status, int length);
  80. void webnet_session_set_header_status_line(struct webnet_session *session, int code, const char * reason_phrase);
  81. int webnet_sessions_set_fds(fd_set *readset, fd_set *writeset);
  82. void webnet_sessions_handle_fds(fd_set *readset, fd_set *writeset);
  83. void webnet_sessions_set_err_callback(void (*callback)(struct webnet_session *session));
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* __WN_SESSION_H__ */