auth_client.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* A demonstration of using HTTP basic authentication with XML-RPC.
  2. **
  3. ** In general, you shouldn't write XML-RPC servers which require basic
  4. ** authenticaion. (Few XML-RPC clients are capable of it, and it's not part of
  5. ** any standard.) Instead, you should pass any authentication information
  6. ** as a regular XML-RPC parameter (or look into using SSL).
  7. **
  8. ** But certain XML-RPC servers, including Zope, rely heavily on HTTP
  9. ** basic authentication. Here's how to talk to them. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <xmlrpc-c/base.h>
  13. #include <xmlrpc-c/client.h>
  14. #include "config.h" /* information about this build environment */
  15. #define NAME "XML-RPC C Auth Client"
  16. #define VERSION "1.0"
  17. #define SERVER_URL "http://localhost:8080/RPC2"
  18. static void
  19. die_if_fault_occurred(xmlrpc_env * const envP) {
  20. if (envP->fault_occurred) {
  21. fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
  22. envP->fault_string, envP->fault_code);
  23. exit(1);
  24. }
  25. }
  26. int
  27. main(int const argc,
  28. const char ** const argv) {
  29. xmlrpc_env env;
  30. xmlrpc_server_info * serverP;
  31. xmlrpc_value * resultP;
  32. xmlrpc_int sum;
  33. if (argc-1 > 0) {
  34. fprintf(stderr, "There are no arguments. You specified %d", argc-1);
  35. exit(1);
  36. }
  37. /* Start up our XML-RPC client library. */
  38. xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION);
  39. xmlrpc_env_init(&env);
  40. /* Make a new object to represent our XML-RPC server. */
  41. serverP = xmlrpc_server_info_new(&env, SERVER_URL);
  42. die_if_fault_occurred(&env);
  43. /* Set up our authentication information. */
  44. xmlrpc_server_info_set_basic_auth(&env, serverP, "jrandom", "secret");
  45. die_if_fault_occurred(&env);
  46. resultP =
  47. xmlrpc_client_call_server(
  48. &env, serverP, "sample.add", "(ii)",
  49. (xmlrpc_int32) 5, (xmlrpc_int32) 7);
  50. die_if_fault_occurred(&env);
  51. /* Dispose of our server object. */
  52. xmlrpc_server_info_free(serverP);
  53. /* Get the authentication information and print it out. */
  54. xmlrpc_read_int(&env, resultP, &sum);
  55. die_if_fault_occurred(&env);
  56. printf("The sum is %d\n", sum);
  57. /* Dispose of our result value. */
  58. xmlrpc_DECREF(resultP);
  59. /* Shut down our XML-RPC client library. */
  60. xmlrpc_env_clean(&env);
  61. xmlrpc_client_cleanup();
  62. return 0;
  63. }