thttp_action.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file thttp_action.h
  23. * @brief HTTP action.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #ifndef THTTP_ACTION_H
  29. #define THTTP_ACTION_H
  30. #include "tinyhttp_config.h"
  31. #include "tinyhttp/thttp_session.h"
  32. #include "tsk_buffer.h"
  33. #include "tsk_list.h"
  34. #include "tsk_params.h"
  35. #include "tsk_options.h"
  36. THTTP_BEGIN_DECLS
  37. typedef uint64_t thttp_action_id_t;
  38. #define THTTP_ACTION_INVALID_ID 0
  39. #define THTTP_ACTION_INVALID_HANDLE tsk_null
  40. /** List of all supported options.
  41. * To pass an option to the sesion, use @ref THTTP_ACTION_SET_OPTION() macro.
  42. */
  43. typedef enum thttp_action_option_e {
  44. THTTP_ACTION_OPTION_TIMEOUT,
  45. }
  46. thttp_action_option_t;
  47. /** List of actions.
  48. */
  49. typedef enum thttp_action_type_e {
  50. /* Outgoing GET, PUT, HEAD, DELETE, .... */
  51. thttp_atype_o_request,
  52. /* Incoming message */
  53. thttp_atype_i_message,
  54. /* common */
  55. thttp_thttp_atype_closed,
  56. thttp_atype_error,
  57. thttp_atype_close,
  58. thttp_atype_cancel,
  59. thttp_atype_timedout,
  60. }
  61. thttp_action_type_t;
  62. typedef enum thttp_action_param_type_e {
  63. thttp_aptype_null = 0,
  64. thttp_aptype_option,
  65. thttp_aptype_header,
  66. thttp_aptype_payload,
  67. }
  68. thttp_action_param_type_t;
  69. /**@ingroup thttp_action_group
  70. * @def THTTP_ACTION_SET_OPTION
  71. * Adds or updates an option.
  72. * This is a helper macro for @a thttp_action_*() functions.
  73. * @param ID_ENUM The id of the option to add/update (@ref thttp_action_option_t).
  74. * @param VALUE_STR The new value of the parameter (<i>const char*</i>).
  75. *
  76. * @code
  77. thttp_action_GET(session, "http://www.google.com",
  78. THTTP_ACTION_SET_PARAM("timeout", "6000"),
  79. THTTP_ACTION_SET_NULL());
  80. * @endcode
  81. */
  82. /**@ingroup thttp_action_group
  83. * @def THTTP_ACTION_SET_HEADER
  84. * Adds new HTTP headers to the request.
  85. * This is a helper macro for @a thttp_action_*() functions.
  86. * @param NAME_STR The name of the header (<i>const char*</i>).
  87. * @param VALUE_STR The value of the header (<i>const char*</i>). Should not contains the trailing CRLF.
  88. *
  89. * @code
  90. thttp_action_GET(session, "http://www.doubango.org"
  91. THTTP_ACTION_SET_HEADER("Pragma", "No-Cache"),
  92. THTTP_ACTION_SET_HEADER("Connection", "Keep-Alive"),
  93. THTTP_ACTION_SET_NULL());
  94. * @endcode
  95. */
  96. /**@ingroup thttp_action_group
  97. * @def THTTP_ACTION_SET_PAYLOAD
  98. * Adds a content (or payload) to the request. You should also add a content-type header by using
  99. * @ref THTTP_ACTION_SET_HEADER() macro. You should not add the content-length header.
  100. * This is a helper macro for @a thttp_action_*() functions.
  101. * @param PAY_PTR A pointer to the payload (<i>const void*</i>).
  102. * @param PAY_SIZE The size of the payload (<i>tsk_size_t</i>).
  103. *
  104. * @code
  105. thttp_action_PUT(session, "http://www.doubango.org"
  106. THTTP_ACTION_SET_HEADER("Pragma", "No-Cache"),
  107. THTTP_ACTION_SET_HEADER("Connection", "Keep-Alive"),
  108. THTTP_ACTION_SET_HEADER("Content-length", "application/mytype"),
  109. THTTP_ACTION_SET_PAYLOAD("Salut", 5),
  110. THTTP_ACTION_SET_NULL());
  111. * @endcode
  112. */
  113. /**@ingroup thttp_action_group
  114. * @def THTTP_ACTION_SET_NULL
  115. * Ends action parameters. Must always be the last one.
  116. */
  117. #define THTTP_ACTION_SET_OPTION(ID_ENUM, VALUE_STR) thttp_aptype_option, (thttp_action_option_t)ID_ENUM, (const char*)VALUE_STR
  118. #define THTTP_ACTION_SET_HEADER(NAME_STR, VALUE_STR) thttp_aptype_header, (const char*)NAME_STR, (const char*)VALUE_STR
  119. #define THTTP_ACTION_SET_PAYLOAD(PAY_PTR, PAY_SIZE) thttp_aptype_payload, (const void*)PAY_PTR, (tsk_size_t)PAY_SIZE
  120. #define THTTP_ACTION_SET_NULL() thttp_aptype_null
  121. typedef struct thttp_action_s {
  122. TSK_DECLARE_OBJECT;
  123. thttp_action_type_t type;
  124. const char* url;
  125. const char* method;
  126. tsk_options_L_t *options;
  127. tsk_params_L_t *headers;
  128. tsk_buffer_t* payload;
  129. }
  130. thttp_action_t;
  131. typedef void thttp_action_handle_t;
  132. /**@ingroup thttp_action_group
  133. * @def thttp_action_CONNECT
  134. * Sends @a CONNECT method request. This function is non-blocking and the result will be posted to the callback function.
  135. * @param session The @a session (or connection) to use.
  136. * @param urlstring The Request-URI of the request.
  137. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  138. * @retval Zero if succeed and non-zero error code otherwise.
  139. */
  140. /**@ingroup thttp_action_group
  141. * @def thttp_action_DELETE
  142. * Sends @a DELETE method request. This function is non-blocking and the result will be posted to the callback function.
  143. * @param session The @a session (or connection) to use.
  144. * @param urlstring The Request-URI of the request.
  145. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  146. * @retval Zero if succeed and non-zero error code otherwise.
  147. */
  148. /**@ingroup thttp_action_group
  149. * @def thttp_action_GET
  150. * Sends @a GET method request. This function is non-blocking and the result will be posted to the callback function.
  151. * @param session The @a session (or connection) to use.
  152. * @param urlstring The Request-URI of the request.
  153. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  154. * @retval Zero if succeed and non-zero error code otherwise.
  155. */
  156. /**@ingroup thttp_action_group
  157. * @def thttp_action_HEAD
  158. * Sends @a HEAD method request. This function is non-blocking and the result will be posted to the callback function.
  159. * @param session The @a session (or connection) to use.
  160. * @param urlstring The Request-URI of the request.
  161. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  162. * @retval Zero if succeed and non-zero error code otherwise.
  163. */
  164. /**@ingroup thttp_action_group
  165. * @def thttp_action_OPTIONS
  166. * Sends @a OPTIONS method request. This function is non-blocking and the result will be posted to the callback function.
  167. * @param session The @a session (or connection) to use.
  168. * @param urlstring The Request-URI of the request.
  169. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  170. * @retval Zero if succeed and non-zero error code otherwise.
  171. */
  172. /**@ingroup thttp_action_group
  173. * @def thttp_action_PATCH
  174. * Sends @a PATCH method request. This function is non-blocking and the result will be posted to the callback function.
  175. * @param session The @a session (or connection) to use.
  176. * @param urlstring The Request-URI of the request.
  177. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  178. * @retval Zero if succeed and non-zero error code otherwise.
  179. */
  180. /**@ingroup thttp_action_group
  181. * @def thttp_action_POST
  182. * Sends @a POST method request. This function is non-blocking and the result will be posted to the callback function.
  183. * @param session The @a session (or connection) to use.
  184. * @param urlstring The Request-URI of the request.
  185. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  186. * @retval Zero if succeed and non-zero error code otherwise.
  187. */
  188. /**@ingroup thttp_action_group
  189. * @def thttp_action_PUT
  190. * Sends @a PUT method request. This function is non-blocking and the result will be posted to the callback function.
  191. * @param session The @a session (or connection) to use.
  192. * @param urlstring The Request-URI of the request.
  193. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  194. * @retval Zero if succeed and non-zero error code otherwise.
  195. */
  196. /**@ingroup thttp_action_group
  197. * @def thttp_action_TRACE
  198. * Sends @a TRACE method request. This function is non-blocking and the result will be posted to the callback function.
  199. * @param session The @a session (or connection) to use.
  200. * @param urlstring The Request-URI of the request.
  201. * @param ... Any @b THTTP_ACTION_SET_*() macros. MUST ends with @ref THTTP_ACTION_SET_NULL().
  202. * @retval Zero if succeed and non-zero error code otherwise.
  203. */
  204. TINYHTTP_API int thttp_action_perform(thttp_session_handle_t *session, const char* urlstring, const char* method, ...);
  205. #define thttp_action_CONNECT(session, urlstring, ...) thttp_action_perform(session, urlstring, "CONNECT", __VA_ARGS__)
  206. #define thttp_action_DELETE(session, urlstring, ...) thttp_action_perform(session, urlstring, "DELETE", __VA_ARGS__)
  207. #define thttp_action_GET(session, urlstring, ...) thttp_action_perform(session, urlstring, "GET", __VA_ARGS__)
  208. #define thttp_action_HEAD(session, urlstring, ...) thttp_action_perform(session, urlstring, "HEAD", __VA_ARGS__)
  209. #define thttp_action_OPTIONS(session, urlstring, ...) thttp_action_perform(session, urlstring, "OPTIONS", __VA_ARGS__)
  210. #define thttp_action_PATCH(session, urlstring, ...) thttp_action_perform(session, urlstring, "PATCH", __VA_ARGS__)
  211. #define thttp_action_POST(session, urlstring, ...) thttp_action_perform(session, urlstring, "POST", __VA_ARGS__)
  212. #define thttp_action_PUT(session, urlstring, ...) thttp_action_perform(session, urlstring, "PUT", __VA_ARGS__)
  213. #define thttp_action_TRACE(session, urlstring, ...) thttp_action_perform(session, urlstring, "TRACE", __VA_ARGS__)
  214. TINYHTTP_API thttp_action_t* thttp_action_create(thttp_action_type_t type, const char* urlstring, const char* method, va_list* app);
  215. typedef tsk_list_t thttp_actions_L_t; /**< List of @ref thttp_action_handle_t elements. */
  216. TINYHTTP_GEXTERN const tsk_object_def_t *thttp_action_def_t;
  217. THTTP_END_DECLS
  218. #endif /* THTTP_ACTION_H */