You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

603 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <article>
  2. <articleinfo>
  3. <title>cSOAP Implementation Guide $Revision: 1.3 $</title>
  4. <author><firstname>Ferhat</firstname><surname>Ayaz</surname></author>
  5. <copyright><year>2004</year><holder>csoap</holder></copyright>
  6. </articleinfo>
  7. <section>
  8. <title>Introduction</title>
  9. <para>
  10. This document shows in short examples how to use cSOAP
  11. for the client and server side implementation.
  12. </para>
  13. <para>
  14. cSOAP is a simple client/server SOAP library to implement standalone
  15. and embedded SOAP applications. cSOAP was implemented in pure C to
  16. support platforms as far as possible.
  17. </para>
  18. <para>
  19. The underlying xml layer is libxml2 (http://xmlsoft.org). We used
  20. this library because it is fast and still maintained. You can find also
  21. some documentations about using libxml2.
  22. </para>
  23. <para>
  24. The transport of SOAP messages will be established via HTTP. cSOAP
  25. contains the subproject "nanohttp" which will do all the HTTP specific
  26. stuff. It implements a simple HTTP client and a server which can also
  27. be used outside the cSOAP project.
  28. </para>
  29. </section>
  30. <section>
  31. <title>Nameconvention in cSOAP</title>
  32. <para>
  33. cSOAP contains the following simple modules:
  34. </para>
  35. <synopsis>
  36. + client : soap-client.h
  37. + server : soap-server.h
  38. + ctx : soap-ctx.h
  39. + env : soap-env.h
  40. + fault : soap-fault.h
  41. + router : soap-router.h
  42. + service : soap-service.h
  43. </synopsis>
  44. <para>
  45. Each module declares the functions in the following name convention:
  46. </para>
  47. <synopsis><![CDATA[
  48. herror_t soap_<module>_<operation>();
  49. ]]></synopsis>
  50. <para>
  51. For example: The function to invoke a SOAP call from the client side:
  52. </para>
  53. <synopsis>
  54. herror_t soap_client_invoke(....);
  55. </synopsis>
  56. </section>
  57. <section>
  58. <title>Error handling</title>
  59. <para>
  60. Almost all function will return a "herror_t" object. If the function
  61. returns with success this object is H_OK. Another herror_t object
  62. will be returned otherwise. Following functions can be used with a
  63. returned herror_t object:
  64. </para>
  65. <synopsis>
  66. herror_code() : Returns the error code
  67. herror_func() : Returns the function name, where the error occured
  68. herror_message() : Returns the human readable error message
  69. herror_release() : Frees the herror_t object.
  70. </synopsis>
  71. <example>
  72. <programlisting>
  73. herror_t err = soap_client_invoke(...);
  74. if (err != H_OK) {
  75. printf("Message: %s\n", herror_message(err));
  76. printf("Error code: %d\n", herror_code(err));
  77. printf("In function: %s\n", herror_func(err));
  78. herror_release(err);
  79. }
  80. </programlisting>
  81. </example>
  82. <note>
  83. Note that you "must" call herror_release() to free the resources.
  84. </note>
  85. <note>
  86. The error codes are defined in "nanohttp-common.h". Here some examples:
  87. </note>
  88. <synopsis>
  89. #define HSOCKET_ERROR_CREATE 1001
  90. #define URL_ERROR_UNKNOWN_PROTOCOL 1101
  91. #define XML_ERROR_PARSE 1601
  92. </synopsis>
  93. <para>
  94. You can also create your own herror_t object with herror_new().
  95. </para>
  96. <synopsis>
  97. #define MY_ERROR_TEST 5001
  98. herror_t err = herror_new("my_func()",
  99. MY_ERROR_TEST,
  100. "Size %d is greater then %d", 17, 13);
  101. </synopsis>
  102. <note>
  103. User defined error codes must be greater then 5000.
  104. </note>
  105. </section>
  106. <section>
  107. <title>Implementing a simple client</title>
  108. <para>
  109. One of the advantages of cSOAP is its simplicity. You can implement
  110. a client or a server in a few minutes. The steps are always indentical.
  111. </para>
  112. <synopsis>
  113. 1. Initialize cSOAP client
  114. soap_client_init_args()
  115. 2. Create a SoapCtx object
  116. soap_ctx_new_with_method()
  117. 3. Fill the envelope
  118. soap_env_add_item()
  119. soap_env_add_itemf()
  120. soap_env_add_attachment()
  121. soap_env_add_custom()
  122. soap_env_push_item()
  123. soap_env_pop_item()
  124. 4. Invoke
  125. soap_client_invoke()
  126. 5. Process returned SoapCtx object
  127. 6. Clean up cSOAP client
  128. </synopsis>
  129. <example>
  130. <programlisting><![CDATA[
  131. #include <libcsoap/soap-client.h>
  132. static const char *url = "http://localhost:10000/csoapserver";
  133. static const char *urn = "urn:examples";
  134. static const char *method = "sayHello";
  135. int main(int argc, char *argv[])
  136. {
  137. SoapCtx *ctx, *ctx2;
  138. herror_t err;
  139. /* 1. Initialize cSOAP client */
  140. soap_client_init_args(argc, argv);
  141. /* 2. Create a SoapCtx object */
  142. soap_ctx_new_with_method(urn, method, &ctx);
  143. /* 3. Fill the envelope */
  144. soap_env_add_item(ctx->env, "xsd:string", "name", "Jonny B. Good");
  145. /* 4. Invoke */
  146. soap_client_invoke(ctx, &ctx2, url, "");
  147. if (err != H_OK) {
  148. log_error4("[%d] %s(): %s ",
  149. herror_code(err),
  150. herror_func(err),
  151. herror_message(err));
  152. herror_release(err);
  153. soap_ctx_free(ctx);
  154. return 1;
  155. }
  156. /* 5. Process returned SoapCtx object */
  157. soap_xml_doc_print(ctx2->env->root->doc);
  158. /* 6. Clean up cSOAP client */
  159. soap_ctx_free(ctx2);
  160. soap_ctx_free(ctx);
  161. soap_client_destroy();
  162. return 0;
  163. }
  164. ]]>
  165. </programlisting>
  166. </example>
  167. <note>
  168. Important: Note that we have omitted error handling. The complete
  169. code can be found under examples/csoap/simpleclient.c
  170. </note>
  171. </section>
  172. <section>
  173. <title>Implementing a simple server</title>
  174. <para>
  175. Before you start to implement a SOAP server, you must understand
  176. the architecture how you publish you web service .
  177. </para>
  178. <synopsis>
  179. * Each URL represent a router service.
  180. * Each router service contains one or more user services.
  181. * Each user service is a C function with the following signature:
  182. typedef herror_t (*SoapServiceFunc)(SoapCtx*, SoapCtx*);
  183. </synopsis>
  184. <para>
  185. The first SoapCtx object is the request, the second SoapCtx object
  186. is the response, which must be filled by the service function
  187. "SoapServiceFunc". The function must return H_OK if success.
  188. </para>
  189. <para>
  190. The steps to implement a SOAP server described below:
  191. </para>
  192. <synopsis>
  193. 1. Init cSOAP server
  194. 2. Create and register a router
  195. 3. Register you service (C function)
  196. 4. Enter server loop
  197. 5. Clean up cSOAP server
  198. </synopsis>
  199. <para>
  200. The above steps can be shown in the the following example:
  201. </para>
  202. <example>
  203. <programlisting><![CDATA[
  204. herror_t say_hello(SoapCtx *req, SoapCtx* res)
  205. {
  206. printf("Returning empty envelope\n");
  207. soap_env_new_with_response(req->env, &res->env);
  208. return H_OK;
  209. }
  210. int main(int argc, char *argv[])
  211. {
  212. herror_t err;
  213. SoapRouter *router;
  214. /* 1. Init cSOAP server */
  215. soap_server_init_args(argc, argv);
  216. /* 2. Create and register a router */
  217. router = soap_router_new();
  218. soap_server_register_router(router, url);
  219. /* 3. Register you service (C function) */
  220. soap_router_register_service(router, say_hello, method, urn);
  221. /* 4. Enter server loop */
  222. soap_server_run();
  223. /* 5. Clean up cSOAP server */
  224. soap_server_destroy();
  225. return 0;
  226. }
  227. ]]></programlisting>
  228. </example>
  229. <para>
  230. You can see how to implement a soap server easily!
  231. </para>
  232. <note>
  233. Important: Note that we have omitted error handling. The complete
  234. code can be found under examples/csoap/simpleserver.c
  235. </note>
  236. </section>
  237. <section>
  238. <title>Implementing a simple client with attachment</title>
  239. <para>
  240. File are added to the context using soap_ctx_add_file().
  241. The following example is a little bit modified version of the above
  242. simpleclient example.
  243. </para>
  244. <example>
  245. <programlisting><![CDATA[
  246. int main(int argc, char *argv[])
  247. {
  248. SoapCtx *ctx, *ctx2;
  249. char href[MAX_HREF_SIZE];
  250. xmlNodePtr fault;
  251. /* Initialize soap client */
  252. soap_client_init_args(argc, argv);
  253. /* Create a context object */
  254. soap_ctx_new_with_method(urn, method, &ctx);
  255. /* Add file to the context */
  256. soap_ctx_add_file(ctx, argv[1], "application/octet-stream", href);
  257. /* Add file reference to the envelope */
  258. soap_env_add_attachment(ctx->env,"source", href);
  259. /* Send soap request to the server */
  260. soap_client_invoke(ctx, &ctx2, url, "");
  261. /* Handle response (just print to the screen) */
  262. fault = soap_env_get_fault(ctx2->env);
  263. if (fault) {
  264. ...
  265. } else {
  266. ...
  267. }
  268. /* Clean up */
  269. soap_ctx_free(ctx2);
  270. soap_ctx_free(ctx);
  271. soap_client_destroy();
  272. return 0;
  273. }
  274. ]]></programlisting>
  275. </example>
  276. <para>
  277. The difference between soap_ctx_add_file() and soap_env_add_attachment()
  278. is, that soap_ctx_add_file() adds the file to the context. In our case,
  279. this is a MIME message. soap_ctx_add_file() fills the "href" field with
  280. a generated reference id. (Content-ID: [href]) This id can be used to
  281. point to the added file from a SOAP message using soap_env_add_attachment().
  282. </para>
  283. <para>
  284. Here a simple example to understand. Look at the following MIME message:
  285. </para>
  286. <example>
  287. <programlisting><![CDATA[
  288. POST /csoapsever HTTP/1.1
  289. Content-type: multipart/related; boundary="--.Part1234" ...
  290. ----.Part1234
  291. <SOAP-ENV:Envelope ...>
  292. <SOAP-ENV:Body ...>
  293. <m:echo xmlns:m="urn:example">
  294. <source href="cid:12345abcdef/>
  295. </m:echo>
  296. </SOAP-ENV:Body>
  297. </SOAP-ENV:Envelope>
  298. ----.Part1234
  299. Content-type: apptication/octet-stream
  300. Content-ID: <12345abcdef>
  301. binary data ...
  302. ----.Part1234--
  303. ]]></programlisting>
  304. </example>
  305. <para>
  306. soap_env_add_attachment() will produce the following xml tag:
  307. </para>
  308. <synopsis>
  309. <![CDATA[
  310. <source href="cid:12345abcdef/>
  311. ]]>
  312. </synopsis>
  313. </section>
  314. <section>
  315. <title>Implementing a simple server with attachment</title>
  316. <para>
  317. Attachments are represented as an "attachments_t" object. An
  318. "attachments_t" object is a list of parts. A part is the physical file
  319. on the filesystem.
  320. </para>
  321. <synopsis>
  322. typedef struct _attachments
  323. {
  324. part_t *parts;
  325. part_t *last;
  326. part_t *root_part;
  327. }attachments_t;
  328. </synopsis>
  329. <para>
  330. You don't have to care about this object becaus it is a part of
  331. SoapCtx.
  332. </para>
  333. <synopsis>
  334. typedef struct _SoapCtx
  335. {
  336. SoapEnv *env;
  337. attachments_t *attachments;
  338. }SoapCtx;
  339. </synopsis>
  340. <para>
  341. The following example shows how to use attachment in a service function
  342. (C function).
  343. </para>
  344. <example>
  345. <programlisting><![CDATA[
  346. herror_t echo_attachments(SoapCtx *req, SoapCtx* res)
  347. {
  348. herror_t err;
  349. part_t *part;
  350. char href[MAX_HREF_SIZE];
  351. err = soap_env_new_with_response(req->env, &res->env);
  352. if (err != H_OK) {
  353. return err;
  354. }
  355. if (req->attachments)
  356. {
  357. for (part = req->attachments->parts; part != NULL; part = part->next)
  358. {
  359. soap_ctx_add_file(res, part->filename, part->content_type, href);
  360. soap_env_add_attachment(res->env, "echoFile", href);
  361. }
  362. }
  363. return H_OK;
  364. }
  365. ]]></programlisting>
  366. </example>
  367. </section>
  368. <section>
  369. <title>Building an envelope using the libxml2 API</title>
  370. <para>
  371. One of the ways building a xml tree into an SOAP envelope is to use
  372. directly the libxml2 API. You can obtain the xmlNodePtr of an envelope
  373. using the SoapEnv structure.
  374. </para>
  375. <synopsis>
  376. typedef struct _SoapEnv
  377. {
  378. xmlNodePtr root;
  379. xmlNodePtr cur;
  380. }SoapEnv;
  381. </synopsis>
  382. <para>
  383. Here is "root" your xml node to <![CDATA[ <SOAP-ENV:Envelope> ]]>.
  384. </para>
  385. </section>
  386. <section>
  387. <title>Building an envelope using envelope functions</title>
  388. <para>
  389. You can build a xml tree using following functions
  390. </para>
  391. <synopsis>
  392. xmlNodePtr
  393. soap_env_add_item(SoapEnv* env, const char *type,
  394. const char *name, const char *value);
  395. xmlNodePtr
  396. soap_env_add_itemf(SoapEnv* env, const char *type,
  397. const char *name, const char *value, ...);
  398. xmlNodePtr
  399. soap_env_push_item(SoapEnv *env, const char *type,
  400. const char *name);
  401. void
  402. soap_env_pop_item(SoapEnv* env);
  403. </synopsis>
  404. <para>
  405. soap_env_add_itemf() does the same thing like soap_env_add_item()
  406. but with a C style argument list. (Max buffer for value is 1054)
  407. </para>
  408. <para>
  409. The next example shows, how to use the stack pattern of cSOAP.
  410. </para>
  411. <example>
  412. <programlisting><![CDATA[
  413. SoapEnv *env = ctx->env;
  414. soap_env_push_item(env, "my:user", "User");
  415. soap_env_add_item(env, "xsd:string", "id", "09189");
  416. soap_env_push_item(env, "my:adress", "Adress");
  417. soap_env_add_item(env, "xsd:string", "City", "MyCity");
  418. soap_env_add_item(env, "xsd:int", "Zip", "%d", 12456);
  419. soap_env_pop_item(env);
  420. soap_env_add_item(env, "xsd:string", "name", "snowdrop");
  421. soap_env_add_item(env, "xsd:string", "passwd", "passphrase64");
  422. soap_env_pop_item(env);
  423. ]]></programlisting>
  424. </example>
  425. <para>
  426. This will create the following xml structure
  427. </para>
  428. <example>
  429. <programlisting><![CDATA[
  430. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  431. xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  432. xmlns:xsd="http://www.w3.org/1999/XMLSchema"
  433. SOAP-ENV:encoding="http://schemas.xmlsoap.org/soap/encoding/">
  434. <SOAP-ENV:Body>
  435. <m:CreateUser xmlns:m="urn:examples">
  436. <User type="my:user">
  437. <id type="xsd:string">09189</id>
  438. <Adress type="my:adress">
  439. <City type="my:adress">MyCity</City>
  440. <Zip type="xsd:int">12456</Zip>
  441. </Adress>
  442. <name type="xsd:string">snowdrop</name>
  443. <passwd type="xsd:string">passphrase64</passwd>
  444. </User>
  445. </m:CreateUser>
  446. </SOAP-ENV:Body>
  447. </SOAP-ENV:Envelope>
  448. ]]></programlisting>
  449. </example>
  450. </section>
  451. </article>