http_client.c 7.1 KB

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