http_client.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * --- END COPYRIGHT BLOCK --- */
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /**
  13. * Simple Http Client API broker plugin
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "portable.h"
  18. #include "nspr.h"
  19. #include "slapi-plugin.h"
  20. #include "slapi-private.h"
  21. #include "http_client.h"
  22. #include "http_impl.h"
  23. #include <sys/stat.h>
  24. /*** from proto-slap.h ***/
  25. int slapd_log_error_proc( char *subsystem, int sev_level, char *fmt, ... );
  26. /*** from ldaplog.h ***/
  27. /* edited ldaplog.h for LDAPDebug()*/
  28. #ifndef _LDAPLOG_H
  29. #define _LDAPLOG_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #ifdef __cplusplus
  34. }
  35. #endif
  36. #endif /* _LDAP_H */
  37. #define HTTP_PLUGIN_SUBSYSTEM "http-client-plugin" /* used for logging */
  38. #define HTTP_PLUGIN_VERSION 0x00050050
  39. #define HTTP_SUCCESS 0
  40. #define HTTP_FAILURE -1
  41. /**
  42. * Implementation functions
  43. */
  44. static void *api[7];
  45. /**
  46. * Plugin identifiers
  47. */
  48. static Slapi_PluginDesc pdesc = { "http-client",
  49. VENDOR,
  50. DS_PACKAGE_VERSION,
  51. "HTTP Client plugin" };
  52. static Slapi_ComponentId *plugin_id = NULL;
  53. /**
  54. **
  55. ** Http plug-in management functions
  56. **
  57. **/
  58. int http_client_init(Slapi_PBlock *pb);
  59. static int http_client_start(Slapi_PBlock *pb);
  60. static int http_client_close(Slapi_PBlock *pb);
  61. /**
  62. * our functions
  63. */
  64. static void _http_init(Slapi_ComponentId *plugin_id);
  65. static int _http_get_text(char *url, char **data, int *bytesRead);
  66. static int _http_get_binary(char *url, char **data, int *bytesRead);
  67. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead);
  68. static int _http_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead);
  69. static void _http_shutdown( void );
  70. /**
  71. *
  72. * Get the presence plug-in version
  73. *
  74. */
  75. int http_client_version(void)
  76. {
  77. return HTTP_PLUGIN_VERSION;
  78. }
  79. int http_client_init(Slapi_PBlock *pb)
  80. {
  81. int status = HTTP_SUCCESS;
  82. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,"http_client_init - BEGIN\n");
  83. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  84. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  85. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  86. (void *) http_client_start ) != 0 ||
  87. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  88. (void *) http_client_close ) != 0 ||
  89. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  90. (void *)&pdesc ) != 0 )
  91. {
  92. slapi_log_error(SLAPI_LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  93. "http_client_init - Failed to register plugin\n" );
  94. status = HTTP_FAILURE;
  95. }
  96. /* Retrieve and save the plugin identity to later pass to
  97. internal operations */
  98. if (slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id) != 0) {
  99. slapi_log_error(SLAPI_LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  100. "http_client_init - Failed to retrieve SLAPI_PLUGIN_IDENTITY\n");
  101. return HTTP_FAILURE;
  102. }
  103. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "http_client_init - END\n");
  104. return status;
  105. }
  106. static int http_client_start(Slapi_PBlock *pb)
  107. {
  108. int status = HTTP_SUCCESS;
  109. /**
  110. * do some init work here
  111. */
  112. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,"http_client_start - BEGIN\n");
  113. api[0] = 0; /* reserved for api broker use, must be zero */
  114. api[1] = (void *)_http_init;
  115. api[2] = (void *)_http_get_text;
  116. api[3] = (void *)_http_get_binary;
  117. api[4] = (void *)_http_get_redirected_uri;
  118. api[5] = (void *)_http_shutdown;
  119. api[6] = (void *)_http_post;
  120. if( slapi_apib_register(HTTP_v1_0_GUID, api) ) {
  121. slapi_log_error(SLAPI_LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  122. "http_client_start: failed to register functions\n" );
  123. status = HTTP_FAILURE;
  124. }
  125. _http_init(plugin_id);
  126. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "http_client_start - END\n");
  127. return status;
  128. }
  129. static int http_client_close(Slapi_PBlock *pb)
  130. {
  131. int status = HTTP_SUCCESS;
  132. /**
  133. * do cleanup
  134. */
  135. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "http_client_close - BEGIN\n");
  136. slapi_apib_unregister(HTTP_v1_0_GUID);
  137. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "http_client_close - END\n");
  138. return status;
  139. }
  140. /**
  141. * perform http initialization here
  142. */
  143. static void _http_init(Slapi_ComponentId *plugin_id)
  144. {
  145. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_init - BEGIN\n");
  146. http_impl_init(plugin_id);
  147. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_init - END\n");
  148. }
  149. /**
  150. * This method gets the data in a text format based on the
  151. * URL send.
  152. */
  153. static int _http_get_text(char *url, char **data, int *bytesRead)
  154. {
  155. int status = HTTP_SUCCESS;
  156. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_text - BEGIN\n");
  157. status = http_impl_get_text(url, data, bytesRead);
  158. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_text - END\n");
  159. return status;
  160. }
  161. /**
  162. * This method gets the data in a binary format based on the
  163. * URL send.
  164. */
  165. static int _http_get_binary(char *url, char **data, int *bytesRead)
  166. {
  167. int status = HTTP_SUCCESS;
  168. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_binary - BEGIN\n");
  169. status = http_impl_get_binary(url, data, bytesRead);
  170. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_binary - END\n");
  171. return status;
  172. }
  173. /**
  174. * This method intercepts the redirected URI and returns the location
  175. * information.
  176. */
  177. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead)
  178. {
  179. int status = HTTP_SUCCESS;
  180. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_redirected_uri -- BEGIN\n");
  181. status = http_impl_get_redirected_uri(url, data, bytesRead);
  182. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_get_redirected_uri -- END\n");
  183. return status;
  184. }
  185. /**
  186. * This method posts the data based on the URL send.
  187. */
  188. static int _http_post(char *url, httpheader ** httpheaderArray, char *body, char **data, int *bytesRead)
  189. {
  190. int status = HTTP_SUCCESS;
  191. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_post - BEGIN\n");
  192. status = http_impl_post(url, httpheaderArray, body, data, bytesRead);
  193. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_post - END\n");
  194. return status;
  195. }
  196. /**
  197. * perform http shutdown here
  198. */
  199. static void _http_shutdown( void )
  200. {
  201. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_shutdown - BEGIN\n");
  202. http_impl_shutdown();
  203. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM, "_http_shutdown - END\n");
  204. }