txcap_action.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 txcap_action.h
  23. * @brief XCAP actions.
  24. *
  25. * @author Mamadou Diop <diopmamadou [at) doubango (DOT) org>
  26. *
  27. */
  28. #ifndef TINYXCAP_TXCAP_ACTION_H
  29. #define TINYXCAP_TXCAP_ACTION_H
  30. #include "tinyxcap_config.h"
  31. #include "txcap.h"
  32. #include "tinyhttp/thttp_action.h"
  33. TXCAP_BEGIN_DECLS
  34. #define TXCAP_MIME_TYPE_ELEMENT "application/xcap-el+xml" /**< as per rfc 4825 subclause 15.2.1 */
  35. #define TXCAP_MIME_TYPE_ATTRIBUTE "application/xcap-att+xml" /**< as per rfc 4825 subclause 15.2.2 */
  36. #define TXCAP_MIME_TYPE_NS "application/xcap-ns+xml" /**< as per rfc 4825 subclause 15.2.3 */
  37. #define TXCAP_MIME_TYPE_ERROR "application/xcap-error+xml" /**< as per rfc 4825 subclause 15.2.4 */
  38. /** List of all supported types.
  39. */
  40. typedef enum txcap_action_type_e {
  41. txcap_atp_create,
  42. txcap_atp_replace,
  43. txcap_atp_fetch,
  44. txcap_atp_delete
  45. }
  46. txcap_action_type_t;
  47. /** List of all supported targets.
  48. */
  49. typedef enum txcap_action_target_e {
  50. txcap_atg_element,
  51. txcap_atg_document,
  52. txcap_atg_attribute
  53. }
  54. txcap_action_target_t;
  55. /** List of all supported options.
  56. * To pass an option to the sesion, use @ref TXCAP_ACTION_SET_OPTION() macro.
  57. */
  58. typedef enum txcap_action_option_e {
  59. TXCAP_ACTION_OPTION_TIMEOUT = THTTP_ACTION_OPTION_TIMEOUT,
  60. //TXCAP_ACTION_OPTION_*** = 0xFF,
  61. //TXCAP_ACTION_OPTION_****,
  62. }
  63. txcap_action_option_t;
  64. typedef enum txcap_action_param_type_e {
  65. txcap_apt_null = 0,
  66. txcap_apt_option,
  67. txcap_apt_header,
  68. txcap_apt_payload,
  69. txcap_apt_selector,
  70. txcap_apt_urlstring
  71. }
  72. txcap_action_param_type_t;
  73. /**@ingroup txcap_action_group
  74. * @def TXCAP_ACTION_SET_OPTION
  75. * Adds or updates an option.
  76. * This is a helper macro for @a txcap_action_*() functions.
  77. * @param ID_INT The id of the option to add/update (@ref txcap_action_option_t).
  78. * @param VALUE_STR The new value of the option (<i>const char*</i>).
  79. *
  80. * @code
  81. int ret = txcap_action_fetch_document(stack,
  82. // action-level options
  83. TXCAP_ACTION_SET_OPTION(TXCAP_ACTION_OPTION_TIMEOUT, "6000"),
  84. // selector
  85. TXCAP_ACTION_SET_SELECTOR("resource-lists",
  86. TXCAP_SELECTOR_NODE_SET_NULL()),
  87. // ends parameters
  88. TXCAP_ACTION_SET_NULL()
  89. );
  90. * @endcode
  91. */
  92. /**@ingroup txcap_action_group
  93. * @def TXCAP_ACTION_SET_HEADER
  94. * Adds new XCAP headers to the request.
  95. * This is a helper macro for @a txcap_action_*() functions.
  96. * @param NAME_STR The name of the header (<i>const char*</i>).
  97. * @param VALUE_STR The value of the header (<i>const char*</i>). Should not contains the trailing CRLF.
  98. *
  99. * @code
  100. int ret = txcap_action_fetch_element(stack,
  101. // action-level headers
  102. TXCAP_ACTION_SET_HEADER("Pragma", "No-Cache"),
  103. // selector
  104. TXCAP_ACTION_SET_SELECTOR("resource-lists",
  105. TXCAP_SELECTOR_NODE_SET_ATTRIBUTE("list", "name", "rcs"),
  106. TXCAP_SELECTOR_NODE_SET_NULL()),
  107. // ends parameters
  108. TXCAP_ACTION_SET_NULL()
  109. );
  110. * @endcode
  111. *
  112. * @sa @ref TXCAP_STACK_SET_HEADER
  113. */
  114. /**@ingroup txcap_action_group
  115. * @def TXCAP_ACTION_SET_PAYLOAD
  116. * Adds a content (or payload) to the request. You should also add a content-type header by using
  117. * @ref TXCAP_ACTION_SET_HEADER() macro. You should not add the content-length header.
  118. * This is a helper macro for @a txcap_action_*() functions.
  119. * @param PAY_PTR A pointer to the payload (<i>const void*</i>).
  120. * @param PAY_SIZE The size of the payload (<i>size_t</i>).
  121. *
  122. * @code
  123. const char* PAYLOAD = "....";
  124. int ret = txcap_action_create_element(stack,
  125. // selector
  126. TXCAP_ACTION_SET_SELECTOR("resource-lists",
  127. TXCAP_SELECTOR_NODE_SET_ATTRIBUTE("list", "name", "newlist"),
  128. TXCAP_SELECTOR_NODE_SET_NULL()),
  129. // payload
  130. TXCAP_ACTION_SET_PAYLOAD(PAYLOAD, strlen(PAYLOAD)),
  131. // ends parameters
  132. TXCAP_ACTION_SET_NULL()
  133. );
  134. * @endcode
  135. */
  136. /**@ingroup txcap_action_group
  137. * @def TXCAP_ACTION_SET_SELECTOR
  138. * Helps building the XCAP Request-URI. You should use @ref TXCAP_ACTION_SET_REQUEST_URI if you want to provide
  139. * your own URI.
  140. * @param AUID_STR The AUID (<i>const char*</i>) to use. You should use @ref TXCAP_STACK_SET_AUID macro to register the the AUID
  141. * if it's missing. This parameter is mandatory.
  142. * @param ... Node selection steps. You must use @a TXCAP_SELECTOR_NODE_SET_*() macros to set these parameters.
  143. * MUST always ends with @ref TXCAP_SELECTOR_NODE_SET_NULL.
  144. *
  145. * @code
  146. int ret = txcap_action_fetch_attribute(stack,
  147. // action-level options
  148. TXCAP_ACTION_SET_OPTION(TXCAP_ACTION_OPTION_TIMEOUT, "6000"),
  149. // headers
  150. TXCAP_ACTION_SET_HEADER("Pragma", "No-Cache"),
  151. // action-level selector
  152. TXCAP_ACTION_SET_SELECTOR("resource-lists",
  153. TXCAP_SELECTOR_NODE_SET_ATTRIBUTE("list", "name", "rcs"),
  154. TXCAP_SELECTOR_NODE_SET_POS("entry", 1),
  155. TXCAP_SELECTOR_NODE_SET_NAME("display-name"),
  156. TXCAP_SELECTOR_NODE_SET_NULL()),
  157. // ends parameters
  158. TXCAP_ACTION_SET_NULL()
  159. );
  160. * @endcode
  161. *
  162. * @sa @ref TXCAP_ACTION_SET_REQUEST_URI
  163. */
  164. /**@ingroup txcap_action_group
  165. * @def TXCAP_ACTION_SET_REQUEST_URI
  166. * Sets the request URI. This macro is useful if you want to provide your own request URI instead of using @ref TXCAP_ACTION_SET_SELECTOR.
  167. * @param URI_STR Fully Qualified HTTP/HTTPS URI.
  168. *
  169. * @code
  170. const char* PAYLOAD = "....";
  171. int ret = txcap_action_create_element(stack,
  172. // custom Request URI
  173. TXCAP_ACTION_SET_REQUEST_URI("http://doubango.org:8080/services/mycustom/uri"),
  174. // payload
  175. TXCAP_ACTION_SET_PAYLOAD(PAYLOAD, strlen(PAYLOAD)),
  176. // ends parameters
  177. TXCAP_ACTION_SET_NULL()
  178. );
  179. getchar();
  180. * @endcode
  181. *
  182. * @sa @ref TXCAP_ACTION_SET_SELECTOR
  183. */
  184. /**@ingroup txcap_action_group
  185. * @def TXCAP_ACTION_SET_NULL
  186. * Ends action parameters. Must always be the last one.
  187. */
  188. #define TXCAP_ACTION_SET_OPTION(ID_INT, VALUE_STR) txcap_apt_option, (thttp_action_option_t)ID_INT, (const char*)VALUE_STR
  189. #define TXCAP_ACTION_SET_HEADER(NAME_STR, VALUE_STR) txcap_apt_header, (const char*)NAME_STR, (const char*)VALUE_STR
  190. #define TXCAP_ACTION_UNSET_HEADER(NAME_STR) TXCAP_ACTION_SET_HEADER(NAME_STR, (const char*)-1)
  191. #define TXCAP_ACTION_SET_PAYLOAD(PAY_PTR, PAY_SIZE) txcap_apt_payload, (const void*)PAY_PTR, (size_t)PAY_SIZE
  192. #define TXCAP_ACTION_SET_SELECTOR(AUID_STR, ...) txcap_apt_selector, (const char*)AUID_STR, ##__VA_ARGS__
  193. #define TXCAP_ACTION_SET_REQUEST_URI(URI_STR) txcap_apt_urlstring, (const char*)URI_STR
  194. #define TXCAP_ACTION_SET_NULL() txcap_apt_null
  195. /**@ingroup txcap_action_group
  196. * @def txcap_action_create_element
  197. * Creates new element by sending a <i>HTTP/HTTPS PUT</i> request.
  198. * The default Content-Type will be "application/xcap-el+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  199. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  200. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  201. */
  202. /**@ingroup txcap_action_group
  203. * @def txcap_action_create_document
  204. * Creates new document by sending a <i>HTTP/HTTPS PUT</i> request.
  205. * The default Content-Type will be the one associated with the AUID of the document, unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  206. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  207. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  208. */
  209. /**@ingroup txcap_action_group
  210. * @def txcap_action_create_attribute
  211. * Creates new attribute by sending a <i>HTTP/HTTPS PUT</i> request.
  212. * The default Content-Type will be "application/xcap-att+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  213. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  214. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  215. */
  216. /**@ingroup txcap_action_group
  217. * @def txcap_action_replace_element
  218. * Replaces an element by sending a <i>HTTP/HTTPS PUT</i> request.
  219. * The default Content-Type will be "application/xcap-el+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  220. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  221. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  222. */
  223. /**@ingroup txcap_action_group
  224. * @def txcap_action_replace_document
  225. * Replaces a document by sending a <i>HTTP/HTTPS PUT</i> request.
  226. * The default Content-Type will be the one associated with the AUID of the document, unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  227. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  228. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  229. */
  230. /**@ingroup txcap_action_group
  231. * @def txcap_action_replace_attribute
  232. * Replaces an attribute by sending a <i>HTTP/HTTPS PUT</i> request.
  233. * The default Content-Type will be "application/xcap-att+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  234. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  235. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  236. */
  237. /**@ingroup txcap_action_group
  238. * @def txcap_action_fetch_element
  239. * Retrieves an element from the XDMS by sending a <i>HTTP/HTTPS GET</i> request.
  240. * The default Content-Type will be "application/xcap-el+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  241. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  242. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  243. */
  244. /**@ingroup txcap_action_group
  245. * @def txcap_action_fetch_document
  246. * Retrieves a document from the XDMS sending a <i>HTTP/HTTPS GET</i> request.
  247. * The default Content-Type will be the one associated with the AUID of the document, unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  248. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  249. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  250. */
  251. /**@ingroup txcap_action_group
  252. * @def txcap_action_fetch_attribute
  253. * Retrieves an attribute from the XDMS by sending a <i>HTTP/HTTPS GET</i> request.
  254. * The default Content-Type will be "application/xcap-att+xml", unless you provide your own Content-Type by using @ref TXCAP_ACTION_SET_HEADER().
  255. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  256. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  257. */
  258. /**@ingroup txcap_action_group
  259. * @def txcap_action_delete_element
  260. * Deletes an element from the XDMS by sending a <i>HTTP/HTTPS DELETE</i> request.
  261. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  262. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  263. */
  264. /**@ingroup txcap_action_group
  265. * @def txcap_action_delete_document
  266. * Deletes a document from the XDMS sending a <i>HTTP/HTTPS DELETE</i> request.
  267. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  268. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  269. */
  270. /**@ingroup txcap_action_group
  271. * @def txcap_action_delete_attribute
  272. * Deletes an attribute from the XDMS by sending a <i>HTTP/HTTPS DELETE</i> request.
  273. * @param stack The HTTP/HTTPS stack created using @ref txcap_stack_create().
  274. * @param ... Any @a TXCAP_ACTION_SET_*() macros. MUST ends with @ref TXCAP_ACTION_SET_NULL().
  275. */
  276. TINYXCAP_API int txcap_action_perform(txcap_stack_handle_t* stack, txcap_action_type_t type, txcap_action_target_t target, ...);
  277. #define txcap_action_create_element(stack, ...) txcap_action_perform(stack, txcap_atp_create, txcap_atg_element, __VA_ARGS__)
  278. #define txcap_action_create_document(stack, ...) txcap_action_perform(stack, txcap_atp_create, txcap_atg_document, __VA_ARGS__)
  279. #define txcap_action_create_attribute(stack, ...) txcap_action_perform(stack, txcap_atp_create, txcap_atg_attribute, __VA_ARGS__)
  280. #define txcap_action_replace_element(stack, ...) txcap_action_perform(stack, txcap_atp_replace, txcap_atg_element, __VA_ARGS__)
  281. #define txcap_action_replace_document(stack, ...) txcap_action_perform(stack, txcap_atp_replace, txcap_atg_document, __VA_ARGS__)
  282. #define txcap_action_replace_attribute(stack, ...) txcap_action_perform(stack, txcap_atp_replace, txcap_atg_attribute, __VA_ARGS__)
  283. #define txcap_action_fetch_element(stack, ...) txcap_action_perform(stack, txcap_atp_fetch, txcap_atg_element, __VA_ARGS__)
  284. #define txcap_action_fetch_document(stack, ...) txcap_action_perform(stack, txcap_atp_fetch, txcap_atg_document, __VA_ARGS__)
  285. #define txcap_action_fetch_attribute(stack, ...) txcap_action_perform(stack, txcap_atp_fetch, txcap_atg_attribute, __VA_ARGS__)
  286. #define txcap_action_delete_element(stack, ...) txcap_action_perform(stack, txcap_atp_delete, txcap_atg_element, __VA_ARGS__)
  287. #define txcap_action_delete_document(stack, ...) txcap_action_perform(stack, txcap_atp_delete, txcap_atg_document, __VA_ARGS__)
  288. #define txcap_action_delete_attribute(stack, ...) txcap_action_perform(stack, txcap_atp_delete, txcap_atg_attribute, __VA_ARGS__)
  289. TXCAP_END_DECLS
  290. #endif /* TINYXCAP_TXCAP_ACTION_H */