http_client.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #define LDAP_DEBUG_TRACE 0x00001 /* 1 */
  34. #define LDAP_DEBUG_ANY 0x04000 /* 16384 */
  35. #define LDAP_DEBUG_PLUGIN 0x10000 /* 65536 */
  36. /* debugging stuff */
  37. extern int slapd_ldap_debug;
  38. #define LDAPDebug( level, sev_level, fmt, arg1, arg2, arg3 ) \
  39. { \
  40. if ( slapd_ldap_debug & level ) { \
  41. slapd_log_error_proc( NULL, sev_level, fmt, arg1, arg2, arg3 ); \
  42. } \
  43. }
  44. #ifdef __cplusplus
  45. }
  46. #endif
  47. #endif /* _LDAP_H */
  48. #define HTTP_PLUGIN_SUBSYSTEM "http-client-plugin" /* used for logging */
  49. #define HTTP_PLUGIN_VERSION 0x00050050
  50. #define HTTP_SUCCESS 0
  51. #define HTTP_FAILURE -1
  52. /**
  53. * Implementation functions
  54. */
  55. static void *api[7];
  56. /**
  57. * Plugin identifiers
  58. */
  59. static Slapi_PluginDesc pdesc = { "http-client",
  60. VENDOR,
  61. DS_PACKAGE_VERSION,
  62. "HTTP Client plugin" };
  63. static Slapi_ComponentId *plugin_id = NULL;
  64. /**
  65. **
  66. ** Http plug-in management functions
  67. **
  68. **/
  69. int http_client_init(Slapi_PBlock *pb);
  70. static int http_client_start(Slapi_PBlock *pb);
  71. static int http_client_close(Slapi_PBlock *pb);
  72. /**
  73. * our functions
  74. */
  75. static void _http_init(Slapi_ComponentId *plugin_id);
  76. static int _http_get_text(char *url, char **data, int *bytesRead);
  77. static int _http_get_binary(char *url, char **data, int *bytesRead);
  78. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead);
  79. static int _http_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead);
  80. static void _http_shutdown( void );
  81. /**
  82. *
  83. * Get the presence plug-in version
  84. *
  85. */
  86. int http_client_version(void)
  87. {
  88. return HTTP_PLUGIN_VERSION;
  89. }
  90. int http_client_init(Slapi_PBlock *pb)
  91. {
  92. int status = HTTP_SUCCESS;
  93. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> http_client_init -- BEGIN\n",0,0,0);
  94. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  95. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  96. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  97. (void *) http_client_start ) != 0 ||
  98. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  99. (void *) http_client_close ) != 0 ||
  100. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  101. (void *)&pdesc ) != 0 )
  102. {
  103. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  104. "http_client_init: failed to register plugin\n" );
  105. status = HTTP_FAILURE;
  106. }
  107. /* Retrieve and save the plugin identity to later pass to
  108. internal operations */
  109. if (slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id) != 0) {
  110. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  111. "http_client_init: Failed to retrieve SLAPI_PLUGIN_IDENTITY\n");
  112. return HTTP_FAILURE;
  113. }
  114. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- http_client_init -- END\n",0,0,0);
  115. return status;
  116. }
  117. static int http_client_start(Slapi_PBlock *pb)
  118. {
  119. int status = HTTP_SUCCESS;
  120. /**
  121. * do some init work here
  122. */
  123. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> http_client_start -- BEGIN\n",0,0,0);
  124. api[0] = 0; /* reserved for api broker use, must be zero */
  125. api[1] = (void *)_http_init;
  126. api[2] = (void *)_http_get_text;
  127. api[3] = (void *)_http_get_binary;
  128. api[4] = (void *)_http_get_redirected_uri;
  129. api[5] = (void *)_http_shutdown;
  130. api[6] = (void *)_http_post;
  131. if( slapi_apib_register(HTTP_v1_0_GUID, api) ) {
  132. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, HTTP_PLUGIN_SUBSYSTEM,
  133. "http_client_start: failed to register functions\n" );
  134. status = HTTP_FAILURE;
  135. }
  136. _http_init(plugin_id);
  137. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- http_client_start -- END\n",0,0,0);
  138. return status;
  139. }
  140. static int http_client_close(Slapi_PBlock *pb)
  141. {
  142. int status = HTTP_SUCCESS;
  143. /**
  144. * do cleanup
  145. */
  146. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> http_client_close -- BEGIN\n",0,0,0);
  147. slapi_apib_unregister(HTTP_v1_0_GUID);
  148. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- http_client_close -- END\n",0,0,0);
  149. return status;
  150. }
  151. /**
  152. * perform http initialization here
  153. */
  154. static void _http_init(Slapi_ComponentId *plugin_id)
  155. {
  156. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_init -- BEGIN\n",0,0,0);
  157. http_impl_init(plugin_id);
  158. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_init -- END\n",0,0,0);
  159. }
  160. /**
  161. * This method gets the data in a text format based on the
  162. * URL send.
  163. */
  164. static int _http_get_text(char *url, char **data, int *bytesRead)
  165. {
  166. int status = HTTP_SUCCESS;
  167. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_get_text -- BEGIN\n",0,0,0);
  168. status = http_impl_get_text(url, data, bytesRead);
  169. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_get_text -- END\n",0,0,0);
  170. return status;
  171. }
  172. /**
  173. * This method gets the data in a binary format based on the
  174. * URL send.
  175. */
  176. static int _http_get_binary(char *url, char **data, int *bytesRead)
  177. {
  178. int status = HTTP_SUCCESS;
  179. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_get_binary -- BEGIN\n",0,0,0);
  180. status = http_impl_get_binary(url, data, bytesRead);
  181. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_get_binary -- END\n",0,0,0);
  182. return status;
  183. }
  184. /**
  185. * This method intercepts the redirected URI and returns the location
  186. * information.
  187. */
  188. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead)
  189. {
  190. int status = HTTP_SUCCESS;
  191. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_get_redirected_uri -- BEGIN\n",0,0,0);
  192. status = http_impl_get_redirected_uri(url, data, bytesRead);
  193. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_get_redirected_uri -- END\n",0,0,0);
  194. return status;
  195. }
  196. /**
  197. * This method posts the data based on the URL send.
  198. */
  199. static int _http_post(char *url, httpheader ** httpheaderArray, char *body, char **data, int *bytesRead)
  200. {
  201. int status = HTTP_SUCCESS;
  202. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_post -- BEGIN\n",0,0,0);
  203. status = http_impl_post(url, httpheaderArray, body, data, bytesRead);
  204. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_post -- END\n",0,0,0);
  205. return status;
  206. }
  207. /**
  208. * perform http shutdown here
  209. */
  210. static void _http_shutdown( void )
  211. {
  212. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "--> _http_shutdown -- BEGIN\n",0,0,0);
  213. http_impl_shutdown();
  214. LDAPDebug(LDAP_DEBUG_PLUGIN, LOG_DEBUG, "<-- _http_shutdown -- END\n",0,0,0);
  215. }