test_xmlrpc.c
资源名称:gateway-1.2.1 [点击查看]
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:9k
源码类别:
手机WAP编程
开发平台:
WINDOWS
- /*
- * test_xmlrpc.c: A simple program to test XML-RPC parsing
- *
- * Stipe Tolj <tolj@wapme-systems.de>
- */
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include "gwlib/gwlib.h"
- #include "gwlib/http.h"
- #include "gwlib/xmlrpc.h"
- #define MAX_THREADS 1024
- #define MAX_IN_QUEUE 128
- static Counter *counter = NULL;
- static long max_requests = 1;
- static int verbose = 1;
- static Octstr *auth_username = NULL;
- static Octstr *auth_password = NULL;
- static Octstr *ssl_client_certkey_file = NULL;
- static Octstr *extra_headers = NULL;
- static Octstr *content_file = NULL;
- static Octstr *url = NULL;
- static int file = 0;
- static XMLRPCMethodCall *msg;
- static void start_request(HTTPCaller *caller, List *reqh, long i)
- {
- long *id;
- int ret;
- if ((i % 1000) == 0)
- info(0, "Starting fetch %ld", i);
- id = gw_malloc(sizeof(long));
- *id = i;
- /*
- * not semd the XML-RPC document contained in msg to
- * the URL 'url' using the POST method
- */
- ret = xmlrpc_call_send(msg, caller, url, reqh, id);
- debug("", 0, "Started request %ld with url:", *id);
- octstr_url_decode(url);
- octstr_dump(url, 0);
- }
- static int receive_reply(HTTPCaller *caller)
- {
- void *id;
- int ret;
- Octstr *final_url;
- List *replyh;
- Octstr *replyb;
- Octstr *type;
- Octstr *charset;
- Octstr *os;
- id = http_receive_result(caller, &ret, &final_url, &replyh, &replyb);
- octstr_destroy(final_url);
- if (id == NULL || ret == -1) {
- error(0, "http POST failed");
- gw_free(id);
- return -1;
- }
- debug("", 0, "Done with request %ld", *(long *) id);
- gw_free(id);
- http_header_get_content_type(replyh, &type, &charset);
- debug("", 0, "Content-type is <%s>, charset is <%s>",
- octstr_get_cstr(type), octstr_get_cstr(charset));
- octstr_destroy(type);
- octstr_destroy(charset);
- if (verbose)
- debug("", 0, "Reply headers:");
- while ((os = list_extract_first(replyh)) != NULL) {
- if (verbose)
- octstr_dump(os, 1);
- octstr_destroy(os);
- }
- list_destroy(replyh, NULL);
- if (verbose) {
- debug("", 0, "Reply body:");
- octstr_dump(replyb, 1);
- }
- octstr_destroy(replyb);
- return 0;
- }
- static void client_thread(void *arg)
- {
- List *reqh;
- long i, succeeded, failed;
- HTTPCaller *caller;
- char buf[1024];
- long in_queue;
- caller = arg;
- succeeded = 0;
- failed = 0;
- reqh = list_create();
- sprintf(buf, "%ld", (long) gwthread_self());
- http_header_add(reqh, "X-Thread", buf);
- if (auth_username != NULL && auth_password != NULL)
- http_add_basic_auth(reqh, auth_username, auth_password);
- in_queue = 0;
- for (;;) {
- while (in_queue < MAX_IN_QUEUE) {
- i = counter_increase(counter);
- if (i >= max_requests)
- goto receive_rest;
- start_request(caller, reqh, i);
- #if 0
- gwthread_sleep(0.1);
- #endif
- ++in_queue;
- }
- while (in_queue >= MAX_IN_QUEUE) {
- if (receive_reply(caller) == -1)
- ++failed;
- else
- ++succeeded;
- --in_queue;
- }
- }
- receive_rest:
- while (in_queue > 0) {
- if (receive_reply(caller) == -1)
- ++failed;
- else
- ++succeeded;
- --in_queue;
- }
- http_caller_destroy(caller);
- info(0, "This thread: %ld succeeded, %ld failed.", succeeded, failed);
- }
- static void help(void)
- {
- info(0, "Usage: test_xmlrpc [options] xml_source");
- info(0, "where options are:");
- info(0, "-u URL");
- info(0, " send XML-RPC source as POST HTTP request to URL");
- info(0, "-v number");
- info(0, " set log level for stderr logging");
- info(0, "-q");
- info(0, " don't print the body or headers of the HTTP response");
- info(0, "-r number");
- info(0, " make `number' requests, repeating URLs as necessary");
- info(0, "-p domain.name");
- info(0, " use `domain.name' as a proxy");
- info(0, "-P portnumber");
- info(0, " connect to proxy at port `portnumber'");
- info(0, "-e domain1:domain2:...");
- info(0, " set exception list for proxy use");
- info(0, "-s");
- info(0, " use HTTPS scheme to access SSL-enabled HTTP server");
- info(0, "-c ssl_client_cert_key_file");
- info(0, " use this file as the SSL certificate and key file");
- }
- int main(int argc, char **argv)
- {
- int i, opt, num_threads;
- Octstr *proxy;
- List *exceptions;
- long proxy_port;
- Octstr *proxy_username;
- Octstr *proxy_password;
- char *p;
- long threads[MAX_THREADS];
- time_t start, end;
- double run_time;
- FILE *fp;
- Octstr *output, *xml_doc;
- int ssl = 0;
- gwlib_init();
- proxy = NULL;
- proxy_port = -1;
- exceptions = list_create();
- proxy_username = NULL;
- proxy_password = NULL;
- num_threads = 0;
- file = 0;
- fp = NULL;
- while ((opt = getopt(argc, argv, "hvr:t:p:u:P:e:a:sc:")) != EOF) {
- switch (opt) {
- case 'h':
- help();
- exit(1);
- break;
- case 'v':
- log_set_output_level(atoi(optarg));
- break;
- case 'r':
- max_requests = atoi(optarg);
- break;
- case 't':
- num_threads = atoi(optarg);
- if (num_threads > MAX_THREADS)
- num_threads = MAX_THREADS;
- break;
- case 'p':
- proxy = octstr_create(optarg);
- break;
- case 'u':
- url = octstr_create(optarg);
- break;
- case 'P':
- proxy_port = atoi(optarg);
- break;
- case 'e':
- p = strtok(optarg, ":");
- while (p != NULL) {
- list_append(exceptions, octstr_create(p));
- p = strtok(NULL, ":");
- }
- break;
- case 'a':
- p = strtok(optarg, ":");
- if (p != NULL) {
- auth_username = octstr_create(p);
- p = strtok(NULL, "");
- if (p != NULL)
- auth_password = octstr_create(p);
- }
- break;
- case 's':
- ssl = 1;
- break;
- case 'c':
- octstr_destroy(ssl_client_certkey_file);
- ssl_client_certkey_file = octstr_create(optarg);
- break;
- case '?':
- default:
- error(0, "Invalid option %c", opt);
- help();
- panic(0, "Stopping");
- break;
- }
- }
- if (optind >= argc) {
- error(0, "Missing arguments");
- help();
- panic(0, "Stopping");
- }
- #ifdef HAVE_LIBSSL
- /*
- * check if we are doing a SSL-enabled client version here
- * load the required cert and key file
- */
- if (ssl) {
- if (ssl_client_certkey_file != NULL) {
- use_global_client_certkey_file(ssl_client_certkey_file);
- } else {
- panic(0, "client certkey file need to be given!");
- }
- }
- #endif
- if (proxy != NULL && proxy_port > 0) {
- http_use_proxy(proxy, proxy_port, exceptions,
- proxy_username, proxy_password);
- }
- octstr_destroy(proxy);
- octstr_destroy(proxy_username);
- octstr_destroy(proxy_password);
- list_destroy(exceptions, octstr_destroy_item);
- counter = counter_create();
- xml_doc = octstr_read_file(argv[optind]);
- if (xml_doc == NULL)
- panic(0, "Cannot read the XML document");
- /*
- * parse the XML source
- */
- msg = xmlrpc_call_parse(xml_doc);
- if ((xmlrpc_parse_status(msg) != XMLRPC_COMPILE_OK) &&
- ((output = xmlrpc_parse_error(msg)) != NULL)) {
- /* parse failure */
- error(0, "%s", octstr_get_cstr(output));
- octstr_destroy(output);
- }
- /*
- * if no POST is desired then dump the re-formated XML
- */
- if (url != NULL) {
- time(&start);
- if (num_threads == 0)
- client_thread(http_caller_create());
- else {
- for (i = 0; i < num_threads; ++i)
- threads[i] = gwthread_create(client_thread, http_caller_create());
- for (i = 0; i < num_threads; ++i)
- gwthread_join(threads[i]);
- }
- time(&end);
- run_time = difftime(end, start);
- info(0, "%ld requests in %f seconds, %f requests/s.",
- max_requests, run_time, max_requests / run_time);
- octstr_destroy(url);
- } else {
- output = xmlrpc_call_octstr(msg);
- octstr_dump(output, 0);
- octstr_destroy(output);
- }
- counter_destroy(counter);
- octstr_destroy(auth_username);
- octstr_destroy(auth_password);
- octstr_destroy(ssl_client_certkey_file);
- octstr_destroy(extra_headers);
- octstr_destroy(content_file);
- xmlrpc_call_destroy(msg);
- octstr_destroy(xml_doc);
- gwlib_shutdown();
- return 0;
- }