Xcap.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.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. #include "Xcap.h"
  23. #include "Common.h"
  24. unsigned XcapStack::count = 0;
  25. /* === ANSI-C functions (local use) === */
  26. static int stack_callback(const thttp_event_t *httpevent);
  27. /* =================================== XCAP Event ==================================== */
  28. typedef enum twrap_xcap_step_type_e {
  29. txst_name,
  30. txst_pos,
  31. txst_att,
  32. txst_pos_n_att,
  33. txst_ns
  34. }
  35. twrap_xcap_step_type_t;
  36. typedef struct twrap_xcap_step_s {
  37. TSK_DECLARE_OBJECT;
  38. twrap_xcap_step_type_t type;
  39. char* qname;
  40. char* att_qname;
  41. char* att_value;
  42. unsigned pos;
  43. struct {
  44. char* prefix;
  45. char* value;
  46. } ns;
  47. }
  48. twrap_xcap_step_t;
  49. static tsk_object_t* twrap_xcap_step_ctor(tsk_object_t * self, va_list * app)
  50. {
  51. twrap_xcap_step_t *step = (twrap_xcap_step_t *)self;
  52. if(step) {
  53. }
  54. return self;
  55. }
  56. static tsk_object_t* twrap_xcap_step_dtor(tsk_object_t * self)
  57. {
  58. twrap_xcap_step_t *step = (twrap_xcap_step_t *)self;
  59. if(step) {
  60. TSK_FREE(step->qname);
  61. TSK_FREE(step->att_qname);
  62. TSK_FREE(step->att_value);
  63. TSK_FREE(step->ns.prefix);
  64. TSK_FREE(step->ns.value);
  65. }
  66. return self;
  67. }
  68. static const tsk_object_def_t twrap_xcap_step_def_s = {
  69. sizeof(twrap_xcap_step_t),
  70. twrap_xcap_step_ctor,
  71. twrap_xcap_step_dtor,
  72. tsk_null,
  73. };
  74. const tsk_object_def_t *twrap_xcap_step_def_t = &twrap_xcap_step_def_s;
  75. twrap_xcap_step_t* twrap_xcap_step_create(twrap_xcap_step_type_t type)
  76. {
  77. twrap_xcap_step_t* step;
  78. if((step = (twrap_xcap_step_t*)tsk_object_new(twrap_xcap_step_def_t))) {
  79. step->type = type;
  80. }
  81. return step;
  82. }
  83. XcapSelector::XcapSelector(XcapStack* stack)
  84. : auid(tsk_null)
  85. {
  86. if(stack) {
  87. this->stack_handle = tsk_object_ref(stack->getHandle());
  88. }
  89. this->steps = tsk_list_create();
  90. }
  91. XcapSelector* XcapSelector::setAUID(const char* auid)
  92. {
  93. tsk_strupdate(&this->auid, auid);
  94. return this;
  95. }
  96. XcapSelector* XcapSelector::setName(const char* qname)
  97. {
  98. twrap_xcap_step_t* step;
  99. if((step = twrap_xcap_step_create(txst_name))) {
  100. step->qname = tsk_strdup(qname);
  101. tsk_list_push_back_data(this->steps, (void**)&step);
  102. }
  103. return this;
  104. }
  105. XcapSelector* XcapSelector::setAttribute(const char* qname, const char* att_qname, const char* att_value)
  106. {
  107. twrap_xcap_step_t* step;
  108. if((step = twrap_xcap_step_create(txst_att))) {
  109. step->qname = tsk_strdup(qname);
  110. step->att_qname = tsk_strdup(att_qname);
  111. step->att_value = tsk_strdup(att_value);
  112. tsk_list_push_back_data(this->steps, (void**)&step);
  113. }
  114. return this;
  115. }
  116. XcapSelector* XcapSelector::setPos(const char* qname, unsigned pos)
  117. {
  118. twrap_xcap_step_t* step;
  119. if((step = twrap_xcap_step_create(txst_pos))) {
  120. step->qname = tsk_strdup(qname);
  121. step->pos = pos;
  122. tsk_list_push_back_data(this->steps, (void**)&step);
  123. }
  124. return this;
  125. }
  126. XcapSelector* XcapSelector::setPosAttribute(const char* qname, unsigned pos, const char* att_qname, const char* att_value)
  127. {
  128. twrap_xcap_step_t* step;
  129. if((step = twrap_xcap_step_create(txst_pos))) {
  130. step->qname = tsk_strdup(qname);
  131. step->pos = pos;
  132. step->att_qname = tsk_strdup(att_qname);
  133. step->att_value = tsk_strdup(att_value);
  134. tsk_list_push_back_data(this->steps, (void**)&step);
  135. }
  136. return this;
  137. }
  138. XcapSelector* XcapSelector::setNamespace(const char* prefix, const char* value)
  139. {
  140. twrap_xcap_step_t* step;
  141. if((step = twrap_xcap_step_create(txst_ns))) {
  142. step->ns.prefix = tsk_strdup(prefix);
  143. step->ns.value = tsk_strdup(value);
  144. tsk_list_push_back_data(this->steps, (void**)&step);
  145. }
  146. return this;
  147. }
  148. /* From tinyXCAP::txcap_selector_get_node_2() */
  149. char* XcapSelector::getString()
  150. {
  151. char* node = tsk_null;
  152. char* temp = tsk_null;
  153. char* _namespace = tsk_null;
  154. tsk_buffer_t* buffer = tsk_buffer_create_null();
  155. const tsk_list_item_t* item;
  156. const twrap_xcap_step_t* step;
  157. /* Node */
  158. tsk_list_foreach(item, this->steps) {
  159. step = (twrap_xcap_step_t*)item->data;
  160. switch(step->type) {
  161. case txst_name:
  162. if(tsk_buffer_append_2(buffer, "/%s", step->qname)) {
  163. goto bail;
  164. }
  165. break;
  166. case txst_pos:
  167. tsk_buffer_append_2(buffer, "/%s%%5B%u%%5D",
  168. step->att_qname, step->pos);
  169. break;
  170. case txst_att:
  171. tsk_buffer_append_2(buffer, "/%s%%5B@%s=%%22%s%%22%%5D",
  172. step->qname, step->att_qname, step->att_value);
  173. break;
  174. case txst_pos_n_att:
  175. tsk_buffer_append_2(buffer, "/%s%%5B%u%%5D%%5B@%s=%%22%s%%22%%5D",
  176. step->qname, step->pos, step->att_qname, step->att_value);
  177. break;
  178. case txst_ns:
  179. tsk_sprintf(&temp, "%sxmlns(%s=%%22%s%%22)",
  180. _namespace?"":"%3F", step->ns.prefix, step->ns.value);
  181. tsk_strcat(&_namespace, temp);
  182. TSK_FREE(temp);
  183. break;
  184. } /* switch */
  185. } /* for */
  186. /* append the namespace */
  187. if(_namespace) {
  188. tsk_buffer_append(buffer, _namespace, (tsk_size_t)tsk_strlen(_namespace));
  189. TSK_FREE(_namespace);
  190. }
  191. bail:
  192. if(TSK_BUFFER_DATA(buffer) && TSK_BUFFER_SIZE(buffer)) {
  193. node = tsk_strndup((const char*)TSK_BUFFER_DATA(buffer), TSK_BUFFER_SIZE(buffer));
  194. }
  195. TSK_OBJECT_SAFE_FREE(buffer);
  196. /* Document */
  197. if(this->auid) {
  198. char* document;
  199. if((document = txcap_selector_get_document(this->stack_handle, this->auid))) {
  200. if(node) {
  201. tsk_strcat_2(&document, "/~~/%s%s", this->auid, node);
  202. TSK_FREE(node);
  203. }
  204. return document;
  205. }
  206. }
  207. return node;
  208. }
  209. void XcapSelector::reset()
  210. {
  211. TSK_FREE(this->auid);
  212. tsk_list_clear_items(this->steps);
  213. }
  214. XcapSelector::~XcapSelector()
  215. {
  216. this->reset();
  217. TSK_OBJECT_SAFE_FREE(this->steps);
  218. tsk_object_unref(this->stack_handle);
  219. }
  220. /* =================================== XCAP Message ==================================== */
  221. XcapMessage::XcapMessage() :
  222. httpmessage(tsk_null)
  223. {
  224. }
  225. XcapMessage::XcapMessage(const thttp_message_t *_httpmessage)
  226. {
  227. this->httpmessage = _httpmessage;
  228. }
  229. XcapMessage::~XcapMessage()
  230. {
  231. }
  232. short XcapMessage::getCode() const
  233. {
  234. if(this->httpmessage) {
  235. return this->httpmessage->line.response.status_code;
  236. }
  237. return 0;
  238. }
  239. const char* XcapMessage::getPhrase() const
  240. {
  241. if(this->httpmessage) {
  242. return this->httpmessage->line.response.reason_phrase;
  243. }
  244. return tsk_null;
  245. }
  246. char* XcapMessage::getXcapHeaderValue(const char* name, unsigned index /*= 0*/)
  247. {
  248. const thttp_header_t* header;
  249. if((header = thttp_message_get_headerByName(this->httpmessage, name))) {
  250. return thttp_header_value_tostring(header);
  251. }
  252. return tsk_null;
  253. }
  254. char* XcapMessage::getXcapHeaderParamValue(const char* name, const char* pname, unsigned index /*= 0*/)
  255. {
  256. const thttp_header_t* header;
  257. if((header = thttp_message_get_headerByName(this->httpmessage, name))) {
  258. const tsk_param_t* param;
  259. if((param = tsk_params_get_param_by_name(header->params, pname))) {
  260. return tsk_strdup(param->value);
  261. }
  262. }
  263. return tsk_null;
  264. }
  265. unsigned XcapMessage::getXcapContentLength()
  266. {
  267. if(this->httpmessage && this->httpmessage->Content) {
  268. return this->httpmessage->Content->size;
  269. }
  270. return 0;
  271. }
  272. unsigned XcapMessage::getXcapContent(void* output, unsigned maxsize)
  273. {
  274. unsigned retsize = 0;
  275. if(output && maxsize && this->httpmessage->Content) {
  276. retsize = (this->httpmessage->Content->size > maxsize) ? maxsize : this->httpmessage->Content->size;
  277. memcpy(output, this->httpmessage->Content->data, retsize);
  278. }
  279. return retsize;
  280. }
  281. /* =================================== XCAP Event ==================================== */
  282. XcapEvent::XcapEvent(const thttp_event_t *_httpevent)
  283. {
  284. this->httpevent = _httpevent;
  285. if(_httpevent) {
  286. this->httpmessage = new XcapMessage(_httpevent->message);
  287. }
  288. else {
  289. this->httpmessage = tsk_null;
  290. }
  291. }
  292. XcapEvent::~XcapEvent()
  293. {
  294. if(this->httpmessage) {
  295. delete this->httpmessage;
  296. }
  297. }
  298. thttp_event_type_t XcapEvent::getType()
  299. {
  300. return this->httpevent->type;
  301. }
  302. const XcapMessage* XcapEvent::getXcapMessage() const
  303. {
  304. return this->httpmessage;
  305. }
  306. /* =================================== XCAP Callback ==================================== */
  307. XcapCallback::XcapCallback()
  308. {
  309. }
  310. XcapCallback::~XcapCallback()
  311. {
  312. }
  313. /* =================================== XCAP Stack ==================================== */
  314. XcapStack::XcapStack(XcapCallback* _callback, const char* xui, const char* password, const char* xcap_root)
  315. {
  316. /* Initialize network layer */
  317. if(XcapStack::count == 0) {
  318. tnet_startup();
  319. }
  320. this->callback = _callback;
  321. this->handle = txcap_stack_create(stack_callback, xui, password, xcap_root,
  322. TXCAP_STACK_SET_USERDATA(this),
  323. TXCAP_STACK_SET_NULL());
  324. }
  325. XcapStack::~XcapStack()
  326. {
  327. TSK_OBJECT_SAFE_FREE(this->handle);
  328. /* DeInitialize the network layer (only if last stack) */
  329. if(--XcapStack::count == 0) {
  330. tnet_cleanup();
  331. }
  332. }
  333. bool XcapStack::registerAUID(const char* id, const char* mime_type, const char* ns, const char* document_name, bool is_global)
  334. {
  335. txcap_stack_t* stack = (txcap_stack_t*)this->handle;
  336. if(stack) {
  337. tsk_bool_t _global = is_global?tsk_true:tsk_false; // 32bit <-> 64bit workaround
  338. return (txcap_auid_register(stack->auids, id, mime_type, ns, document_name, _global) == 0);
  339. }
  340. return false;
  341. }
  342. bool XcapStack::start()
  343. {
  344. return (txcap_stack_start(this->handle) == 0);
  345. }
  346. bool XcapStack::setCredentials(const char* xui, const char* password)
  347. {
  348. return txcap_stack_set(this->handle,
  349. TXCAP_STACK_SET_XUI(xui),
  350. TXCAP_STACK_SET_PASSWORD(password),
  351. TXCAP_STACK_SET_NULL()) == 0;
  352. }
  353. bool XcapStack::setXcapRoot(const char* xcap_root)
  354. {
  355. return txcap_stack_set(this->handle,
  356. TXCAP_STACK_SET_ROOT(xcap_root),
  357. TXCAP_STACK_SET_NULL()) == 0;
  358. }
  359. bool XcapStack::setLocalIP(const char* ip)
  360. {
  361. return txcap_stack_set(this->handle,
  362. TXCAP_STACK_SET_LOCAL_IP(ip),
  363. TXCAP_STACK_SET_NULL()) == 0;
  364. }
  365. bool XcapStack::setLocalPort(unsigned port)
  366. {
  367. tsk_istr_t port_str;
  368. tsk_itoa(port, &port_str);
  369. return txcap_stack_set(this->handle,
  370. TXCAP_STACK_SET_LOCAL_PORT(port_str),
  371. TXCAP_STACK_SET_NULL()) == 0;
  372. }
  373. bool XcapStack::addHeader(const char* name, const char* value)
  374. {
  375. return txcap_stack_set(this->handle,
  376. TXCAP_STACK_SET_HEADER(name, value),
  377. TXCAP_STACK_SET_NULL()) == 0;
  378. }
  379. bool XcapStack::removeHeader(const char* name)
  380. {
  381. return txcap_stack_set(this->handle,
  382. TXCAP_STACK_UNSET_HEADER(name),
  383. TXCAP_STACK_SET_NULL()) == 0;
  384. }
  385. bool XcapStack::setTimeout(unsigned timeout)
  386. {
  387. tsk_istr_t timeout_str;
  388. tsk_itoa(timeout, &timeout_str);
  389. return txcap_stack_set(this->handle,
  390. TXCAP_STACK_SET_TIMEOUT(timeout_str),
  391. TXCAP_STACK_SET_NULL()) == 0;
  392. }
  393. bool XcapStack::getDocument(const char* url)
  394. {
  395. return txcap_action_fetch_document(this->handle,
  396. TXCAP_ACTION_SET_REQUEST_URI(url),
  397. TXCAP_ACTION_SET_NULL()) == 0;
  398. }
  399. bool XcapStack::getElement(const char* url)
  400. {
  401. return txcap_action_fetch_element(this->handle,
  402. TXCAP_ACTION_SET_REQUEST_URI(url),
  403. TXCAP_ACTION_SET_NULL()) == 0;
  404. }
  405. bool XcapStack::getAttribute(const char* url)
  406. {
  407. return txcap_action_fetch_attribute(this->handle,
  408. TXCAP_ACTION_SET_REQUEST_URI(url),
  409. TXCAP_ACTION_SET_NULL()) == 0;
  410. }
  411. bool XcapStack::deleteDocument(const char* url)
  412. {
  413. return txcap_action_delete_document(this->handle,
  414. TXCAP_ACTION_SET_REQUEST_URI(url),
  415. TXCAP_ACTION_SET_NULL()) == 0;
  416. }
  417. bool XcapStack::deleteElement(const char* url)
  418. {
  419. return txcap_action_delete_element(this->handle,
  420. TXCAP_ACTION_SET_REQUEST_URI(url),
  421. TXCAP_ACTION_SET_NULL()) == 0;
  422. }
  423. bool XcapStack::deleteAttribute(const char* url)
  424. {
  425. return txcap_action_delete_attribute(this->handle,
  426. TXCAP_ACTION_SET_REQUEST_URI(url),
  427. TXCAP_ACTION_SET_NULL()) == 0;
  428. }
  429. bool XcapStack::putDocument(const char* url, const void* payload, unsigned len, const char* contentType)
  430. {
  431. return txcap_action_create_document(this->handle,
  432. TXCAP_ACTION_SET_REQUEST_URI(url),
  433. TXCAP_ACTION_SET_PAYLOAD(payload, len),
  434. TXCAP_ACTION_SET_HEADER("Content-Type", contentType),
  435. TXCAP_ACTION_SET_NULL()) == 0;
  436. }
  437. bool XcapStack::putElement(const char* url, const void* payload, unsigned len)
  438. {
  439. return txcap_action_create_element(this->handle,
  440. TXCAP_ACTION_SET_REQUEST_URI(url),
  441. TXCAP_ACTION_SET_PAYLOAD(payload, len),
  442. TXCAP_ACTION_SET_NULL()) == 0;
  443. }
  444. bool XcapStack::putAttribute(const char* url, const void* payload, unsigned len)
  445. {
  446. return txcap_action_create_attribute(this->handle,
  447. TXCAP_ACTION_SET_REQUEST_URI(url),
  448. TXCAP_ACTION_SET_PAYLOAD(payload, len),
  449. TXCAP_ACTION_SET_NULL()) == 0;
  450. }
  451. bool XcapStack::stop()
  452. {
  453. return (txcap_stack_stop(this->handle) == 0);
  454. }
  455. int stack_callback(const thttp_event_t *httpevent)
  456. {
  457. const XcapStack* stack = tsk_null;
  458. XcapEvent* e = tsk_null;
  459. const txcap_stack_handle_t* stack_handle = thttp_session_get_userdata(httpevent->session);
  460. if(!stack_handle || !(stack = dyn_cast<const XcapStack*>((const XcapStack*)stack_handle))) {
  461. TSK_DEBUG_ERROR("Invalid user data");
  462. return -1;
  463. }
  464. if(stack->getCallback()) {
  465. if((e = new XcapEvent(httpevent))) {
  466. stack->getCallback()->onEvent(e);
  467. delete e;
  468. }
  469. }
  470. return 0;
  471. }