wn_session.h 3.4 KB

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