thttp_session.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2010-2015 Mamadou DIOP.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. *
  19. */
  20. /**@file thttp_session.h
  21. * @brief HTTP session.
  22. *
  23. */
  24. #ifndef THTTP_SESSION_H
  25. #define THTTP_SESSION_H
  26. #include "tinyhttp_config.h"
  27. #include "tinyhttp/auth/thttp_challenge.h"
  28. #include "tinyhttp/thttp_message.h"
  29. #include "tinyhttp/thttp_dialog.h"
  30. #include "tnet_types.h"
  31. #include "tsk_object.h"
  32. #include "tsk_list.h"
  33. #include "tsk_params.h"
  34. #include "tsk_options.h"
  35. THTTP_BEGIN_DECLS
  36. //FD
  37. struct thttp_message_s;
  38. typedef uint64_t thttp_session_id_t;
  39. #define THTTP_SESSION_INVALID_ID 0
  40. #define THTTP_SESSION_INVALID_HANDLE tsk_null
  41. /** List of all supported options.
  42. * To pass an option to the sesion, use @ref THTTP_SESSION_SET_OPTION() macro.
  43. */
  44. typedef enum thttp_session_option_e {
  45. THTTP_SESSION_OPTION_TIMEOUT,
  46. THTTP_SESSION_OPTION_TTL,
  47. // To be continued...
  48. }
  49. thttp_session_option_t;
  50. typedef enum thttp_session_param_type_e {
  51. httpp_null = 0,
  52. httpp_option,
  53. httpp_cred,
  54. httpp_header,
  55. httpp_userdata,
  56. }
  57. thttp_session_param_type_t;
  58. /**@ingroup thttp_session_group
  59. * @def THTTP_SESSION_SET_OPTION
  60. * Adds or updates an option.
  61. * This is a helper macro for @ref thttp_session_create and @ref thttp_session_set.
  62. * @param ID_ENUM The id of the option to add/update (@ref thttp_session_option_t).
  63. * @param VALUE_STR The new value of the option (<i>const char*</i>).
  64. *
  65. * @code
  66. // session = thttp_session_create(stack,
  67. thttp_session_set(session,
  68. THTTP_SESSION_SET_PARAM(THTTP_SESSION_OPTION_TIMEOUT, "6000"),
  69. THTTP_SESSION_SET_NULL());
  70. * @endcode
  71. */
  72. /**@ingroup thttp_session_group
  73. * @def THTTP_SESSION_SET_CRED
  74. * Sets the user's creadentials.
  75. * This is a helper macro for @ref thttp_session_create and @ref thttp_session_set.
  76. * @param USERNAME_STR The username (const char*).
  77. * @param PASSWORD_STR The password(const char*).
  78. *
  79. * @code
  80. // session = thttp_session_create(stack,
  81. thttp_session_set(session,
  82. THTTP_SESSION_SET_CRED("ali baba", "open sesame"),
  83. THTTP_SESSION_SET_NULL());
  84. * @endcode
  85. */
  86. /**@ingroup thttp_session_group
  87. * @def THTTP_SESSION_SET_HEADER
  88. * Adds new HTTP headers to the session. The value of the header will be updated if it already exist.
  89. * This is a helper macro for @ref thttp_session_create and @ref thttp_session_set.
  90. * @param NAME_STR The name of the header (<i>const char*</i>) to add or update.
  91. * @param VALUE_STR The value of the header (<i>const char*</i>). Should not contains the trailing CRLF.
  92. * @sa @ref THTTP_SESSION_UNSET_HEADER
  93. *
  94. * @code
  95. // session = thttp_session_create(stack,
  96. thttp_session_set(session,
  97. THTTP_SESSION_SET_HEADER("Pragma", "No-Cache"),
  98. THTTP_SESSION_SET_HEADER("Connection", "Keep-Alive"),
  99. THTTP_SESSION_SET_NULL());
  100. * @endcode
  101. */
  102. /**@ingroup thttp_session_group
  103. * @def THTTP_SESSION_UNSET_HEADER
  104. * Removes a header. This header should be previously added by using @ref THTTP_SESSION_SET_HEADER().
  105. * This is a helper macro for @ref thttp_session_create and @ref thttp_session_set.
  106. * @param NAME_STR The name of the header (<i>const char*</i>) to remove.
  107. * @sa @ref THTTP_SESSION_SET_HEADER
  108. *
  109. * @code
  110. // session = thttp_session_create(stack,
  111. thttp_session_set(session,
  112. THTTP_SESSION_UNSET_HEADER("Pragma"),
  113. THTTP_SESSION_UNSET_HEADER("Connection"),
  114. THTTP_SESSION_SET_NULL());
  115. * @endcode
  116. */
  117. /**@ingroup thttp_session_group
  118. * @def THTTP_SESSION_SET_USERDATA
  119. * Sets user data (context). Will be return to the application layer each time the callback function is called.
  120. * This is a helper macro for @ref thttp_session_create and @ref thttp_session_set.
  121. * @param USERDATA_PTR A pointer to the data(const void*).
  122. *
  123. * @code
  124. // session = thttp_session_create(stack,
  125. thttp_session_set(session,
  126. THTTP_SESSION_SET_USERDATA(ctx),
  127. THTTP_SESSION_SET_NULL());
  128. * @endcode
  129. */
  130. /**@ingroup thttp_session_group
  131. * @def THTTP_SESSION_SET_NULL
  132. * Ends session parameters. Must always be the last one.
  133. */
  134. #define THTTP_SESSION_SET_OPTION(ID_ENUM, VALUE_STR) httpp_option, (thttp_session_option_t)ID_ENUM, (const char*)VALUE_STR
  135. #define THTTP_SESSION_SET_CRED(USERNAME_STR, PASSWORD_STR) httpp_cred, (const char*)USERNAME_STR, (const char*)PASSWORD_STR
  136. #define THTTP_SESSION_SET_HEADER(NAME_STR, VALUE_STR) httpp_header, (const char*)NAME_STR, (const char*)VALUE_STR
  137. #define THTTP_SESSION_UNSET_HEADER(NAME_STR) THTTP_SESSION_SET_HEADER(NAME_STR, (const char*)-1)
  138. #define THTTP_SESSION_SET_USERDATA(USERDATA_PTR) httpp_userdata, (const void*)USERDATA_PTR
  139. #define THTTP_SESSION_SET_NULL() httpp_null
  140. typedef struct thttp_session_s {
  141. TSK_DECLARE_OBJECT;
  142. thttp_session_id_t id;
  143. const struct thttp_stack_s* stack;
  144. const void* userdata; // user's context
  145. tsk_options_L_t *options;
  146. tsk_params_L_t *headers;
  147. tnet_fd_t fd;
  148. thttp_challenges_L_t *challenges;
  149. thttp_dialogs_L_t* dialogs;
  150. struct {
  151. char* usename;
  152. char* password;
  153. } cred;
  154. TSK_DECLARE_SAFEOBJ;
  155. }
  156. thttp_session_t;
  157. typedef tsk_list_t thttp_sessions_L_t; /**< List of @ref thttp_session_handle_t elements. */
  158. /** Pointer to a HTTP/HTTPS session. */
  159. typedef void thttp_session_handle_t;
  160. /** Pointer to a HTTP/HTTPS stack object. */
  161. typedef void thttp_stack_handle_t;
  162. TINYHTTP_API thttp_session_handle_t* thttp_session_create(const thttp_stack_handle_t* stack, ...);
  163. TINYHTTP_API int thttp_session_set(thttp_session_handle_t *self, ...);
  164. TINYHTTP_API thttp_session_id_t thttp_session_get_id(const thttp_session_handle_t *self);
  165. TINYHTTP_API const void* thttp_session_get_userdata(const thttp_session_handle_t *self);
  166. TINYHTTP_API int thttp_session_closefd(thttp_session_handle_t *self);
  167. int thttp_session_update_challenges(thttp_session_t *self, const thttp_response_t* response, tsk_bool_t answered);
  168. int thttp_session_signal_closed(thttp_session_t *self);
  169. int thttp_session_signal_error(thttp_session_t *self);
  170. thttp_session_t* thttp_session_get_by_fd(thttp_sessions_L_t* sessions, tnet_fd_t fd);
  171. TINYHTTP_GEXTERN const tsk_object_def_t *thttp_session_def_t;
  172. THTTP_END_DECLS
  173. #endif /* THTTP_SESSION_H */