http_client.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * --- END COPYRIGHT BLOCK --- */
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /**
  42. * Simple Http Client API broker plugin
  43. */
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include "portable.h"
  47. #include "nspr.h"
  48. #include "slapi-plugin.h"
  49. #include "slapi-private.h"
  50. #include "dirlite_strings.h"
  51. #include "dirver.h"
  52. #include "http_client.h"
  53. #include "http_impl.h"
  54. /* get file mode flags for unix */
  55. #ifndef _WIN32
  56. #include <sys/stat.h>
  57. #endif
  58. /*** from proto-slap.h ***/
  59. int slapd_log_error_proc( char *subsystem, char *fmt, ... );
  60. /*** from ldaplog.h ***/
  61. /* edited ldaplog.h for LDAPDebug()*/
  62. #ifndef _LDAPLOG_H
  63. #define _LDAPLOG_H
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. #define LDAP_DEBUG_TRACE 0x00001 /* 1 */
  68. #define LDAP_DEBUG_ANY 0x04000 /* 16384 */
  69. #define LDAP_DEBUG_PLUGIN 0x10000 /* 65536 */
  70. /* debugging stuff */
  71. # ifdef _WIN32
  72. extern int *module_ldap_debug;
  73. # define LDAPDebug( level, fmt, arg1, arg2, arg3 ) \
  74. { \
  75. if ( *module_ldap_debug & level ) { \
  76. slapd_log_error_proc( NULL, fmt, arg1, arg2, arg3 ); \
  77. } \
  78. }
  79. # else /* _WIN32 */
  80. extern int slapd_ldap_debug;
  81. # define LDAPDebug( level, fmt, arg1, arg2, arg3 ) \
  82. { \
  83. if ( slapd_ldap_debug & level ) { \
  84. slapd_log_error_proc( NULL, fmt, arg1, arg2, arg3 ); \
  85. } \
  86. }
  87. # endif /* Win32 */
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. #endif /* _LDAP_H */
  92. #define HTTP_PLUGIN_SUBSYSTEM "http-client-plugin" /* used for logging */
  93. #define HTTP_PLUGIN_VERSION 0x00050050
  94. #define HTTP_SUCCESS 0
  95. #define HTTP_FAILURE -1
  96. /**
  97. * Implementation functions
  98. */
  99. static void *api[7];
  100. /**
  101. * Plugin identifiers
  102. */
  103. static Slapi_PluginDesc pdesc = { "http-client",
  104. PLUGIN_MAGIC_VENDOR_STR,
  105. PRODUCTTEXT,
  106. "HTTP Client plugin" };
  107. static Slapi_ComponentId *plugin_id = NULL;
  108. /**
  109. **
  110. ** Http plug-in management functions
  111. **
  112. **/
  113. int http_client_init(Slapi_PBlock *pb);
  114. static int http_client_start(Slapi_PBlock *pb);
  115. static int http_client_close(Slapi_PBlock *pb);
  116. /**
  117. * our functions
  118. */
  119. static void _http_init(Slapi_ComponentId *plugin_id);
  120. static int _http_get_text(char *url, char **data, int *bytesRead);
  121. static int _http_get_binary(char *url, char **data, int *bytesRead);
  122. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead);
  123. static int _http_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead);
  124. static void _http_shutdown( void );
  125. #ifdef _WIN32
  126. int *module_ldap_debug = 0;
  127. void plugin_init_debug_level(int *level_ptr)
  128. {
  129. module_ldap_debug = level_ptr;
  130. }
  131. #endif
  132. /**
  133. *
  134. * Get the presence plug-in version
  135. *
  136. */
  137. int http_client_version()
  138. {
  139. return HTTP_PLUGIN_VERSION;
  140. }
  141. int http_client_init(Slapi_PBlock *pb)
  142. {
  143. int status = HTTP_SUCCESS;
  144. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> http_client_init -- BEGIN\n",0,0,0);
  145. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  146. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  147. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  148. (void *) http_client_start ) != 0 ||
  149. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  150. (void *) http_client_close ) != 0 ||
  151. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  152. (void *)&pdesc ) != 0 )
  153. {
  154. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  155. "http_client_init: failed to register plugin\n" );
  156. status = HTTP_FAILURE;
  157. }
  158. /* Retrieve and save the plugin identity to later pass to
  159. internal operations */
  160. if (slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id) != 0) {
  161. slapi_log_error(SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  162. "http_client_init: Failed to retrieve SLAPI_PLUGIN_IDENTITY\n");
  163. return HTTP_FAILURE;
  164. }
  165. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- http_client_init -- END\n",0,0,0);
  166. return status;
  167. }
  168. static int http_client_start(Slapi_PBlock *pb)
  169. {
  170. int status = HTTP_SUCCESS;
  171. /**
  172. * do some init work here
  173. */
  174. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> http_client_start -- BEGIN\n",0,0,0);
  175. api[0] = 0; /* reserved for api broker use, must be zero */
  176. api[1] = (void *)_http_init;
  177. api[2] = (void *)_http_get_text;
  178. api[3] = (void *)_http_get_binary;
  179. api[4] = (void *)_http_get_redirected_uri;
  180. api[5] = (void *)_http_shutdown;
  181. api[6] = (void *)_http_post;
  182. if( slapi_apib_register(HTTP_v1_0_GUID, api) ) {
  183. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  184. "http_client_start: failed to register functions\n" );
  185. status = HTTP_FAILURE;
  186. }
  187. _http_init(plugin_id);
  188. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- http_client_start -- END\n",0,0,0);
  189. return status;
  190. }
  191. static int http_client_close(Slapi_PBlock *pb)
  192. {
  193. int status = HTTP_SUCCESS;
  194. /**
  195. * do cleanup
  196. */
  197. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> http_client_close -- BEGIN\n",0,0,0);
  198. slapi_apib_unregister(HTTP_v1_0_GUID);
  199. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- http_client_close -- END\n",0,0,0);
  200. return status;
  201. }
  202. /**
  203. * perform http initialization here
  204. */
  205. static void _http_init(Slapi_ComponentId *plugin_id)
  206. {
  207. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_init -- BEGIN\n",0,0,0);
  208. http_impl_init(plugin_id);
  209. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_init -- END\n",0,0,0);
  210. }
  211. /**
  212. * This method gets the data in a text format based on the
  213. * URL send.
  214. */
  215. static int _http_get_text(char *url, char **data, int *bytesRead)
  216. {
  217. int status = HTTP_SUCCESS;
  218. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_get_text -- BEGIN\n",0,0,0);
  219. status = http_impl_get_text(url, data, bytesRead);
  220. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_get_text -- END\n",0,0,0);
  221. return status;
  222. }
  223. /**
  224. * This method gets the data in a binary format based on the
  225. * URL send.
  226. */
  227. static int _http_get_binary(char *url, char **data, int *bytesRead)
  228. {
  229. int status = HTTP_SUCCESS;
  230. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_get_binary -- BEGIN\n",0,0,0);
  231. status = http_impl_get_binary(url, data, bytesRead);
  232. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_get_binary -- END\n",0,0,0);
  233. return status;
  234. }
  235. /**
  236. * This method intercepts the redirected URI and returns the location
  237. * information.
  238. */
  239. static int _http_get_redirected_uri(char *url, char **data, int *bytesRead)
  240. {
  241. int status = HTTP_SUCCESS;
  242. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_get_redirected_uri -- BEGIN\n",0,0,0);
  243. status = http_impl_get_redirected_uri(url, data, bytesRead);
  244. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_get_redirected_uri -- END\n",0,0,0);
  245. return status;
  246. }
  247. /**
  248. * This method posts the data based on the URL send.
  249. */
  250. static int _http_post(char *url, httpheader ** httpheaderArray, char *body, char **data, int *bytesRead)
  251. {
  252. int status = HTTP_SUCCESS;
  253. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_post -- BEGIN\n",0,0,0);
  254. status = http_impl_post(url, httpheaderArray, body, data, bytesRead);
  255. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_post -- END\n",0,0,0);
  256. return status;
  257. }
  258. /**
  259. * perform http shutdown here
  260. */
  261. static void _http_shutdown( void )
  262. {
  263. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> _http_shutdown -- BEGIN\n",0,0,0);
  264. http_impl_shutdown();
  265. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- _http_shutdown -- END\n",0,0,0);
  266. }