initial load of upstream version 1.06.32
[xmlrpc-c] / examples / auth_client.c
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
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include <xmlrpc-c/base.h>
15 #include <xmlrpc-c/client.h>
16
17 #include "config.h"  /* information about this build environment */
18
19 #define NAME       "XML-RPC C Auth Client"
20 #define VERSION    "1.0"
21 #define SERVER_URL "http://localhost:8080/RPC2"
22
23 static void die_if_fault_occurred (xmlrpc_env *env)
24 {
25     if (env->fault_occurred) {
26         fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
27                 env->fault_string, env->fault_code);
28         exit(1);
29     }
30 }
31
32 int 
33 main(int           const argc, 
34      const char ** const argv ATTR_UNUSED) {
35
36     xmlrpc_env env;
37     xmlrpc_server_info * server;
38     xmlrpc_value * result;    
39     xmlrpc_int sum;
40     
41     if (argc-1 > 0) {
42         fprintf(stderr, "There are no arguments.  You specified %d", argc-1);
43         exit(1);
44     }
45
46     /* Start up our XML-RPC client library. */
47     xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION);
48     xmlrpc_env_init(&env);
49
50     /* Make a new object to represent our XML-RPC server. */
51     server = xmlrpc_server_info_new(&env, SERVER_URL);
52     die_if_fault_occurred(&env);
53
54     /* Set up our authentication information. */
55     xmlrpc_server_info_set_basic_auth(&env, server, "jrandom", "secret");
56     die_if_fault_occurred(&env);
57
58     result = 
59         xmlrpc_client_call_server(
60             &env, server, "sample.add", "(ii)", 
61             (xmlrpc_int32) 5, (xmlrpc_int32) 7);
62     die_if_fault_occurred(&env);
63
64     /* Dispose of our server object. */
65     xmlrpc_server_info_free(server);
66     
67     /* Get the authentication information and print it out. */
68     xmlrpc_read_int(&env, result, &sum);
69     die_if_fault_occurred(&env);
70     printf("The sum is %d\n", sum);
71     
72     /* Dispose of our result value. */
73     xmlrpc_DECREF(result);
74
75     /* Shut down our XML-RPC client library. */
76     xmlrpc_env_clean(&env);
77     xmlrpc_client_cleanup();
78
79     return 0;
80 }