📄 xml2wbxml.c
字号:
/* * A very simple push initiator for testing a push proxy gateway * * Transfer XML to WBXML, this app is for sending wap push message. * */#define MAX_THREADS 1024#define MAX_IN_QUEUE 128#include <unistd.h>#include <stdlib.h>#include <string.h>#include "gwlib/gwlib.h"#include "gw/wap_push_pap_compiler.h"static long max_pushes = 1;static int verbose = 1, use_hardcoded = 0, num_urls = 0, use_headers = 0, use_config = 0, accept_binary = 0, use_numeric = 0, use_string = 0, use_content_header = 0, add_epilogue = 0, add_preamble = 0, use_dlr_mask = 0, use_dlr_url = 0;static double wait_seconds = 0.0;static Counter *counter = NULL;static char **push_data = NULL;static char *boundary = NULL;static Octstr *content_flag = NULL;static Octstr *appid_flag = NULL;static Octstr *appid_string = NULL;static Octstr *content_header = NULL;static Octstr *content_transfer_encoding = NULL;static Octstr *connection = NULL;static Octstr *delimiter = NULL;static Octstr *initiator_uri = NULL;static Octstr *dlr_mask = NULL;static Octstr *dlr_url = NULL;enum { SSL_CONNECTION_OFF = 0, DEFAULT_NUMBER_OF_RELOGS = 2};/* * Configuration variables */static int pi_ssl = SSL_CONNECTION_OFF;static long retries = DEFAULT_NUMBER_OF_RELOGS;static Octstr *ssl_client_certkey_file = NULL;static Octstr *push_url = NULL;static Octstr *pap_file = NULL;static Octstr *content_file = NULL;static Octstr *username = NULL;static Octstr *password = NULL;static void add_delimiter(Octstr **content){ if (octstr_compare(delimiter, octstr_imm("crlf")) == 0) { octstr_format_append(*content, "%c", '\r'); } octstr_format_append(*content, "%c", '\n');}static void add_push_application_id(List **push_headers, Octstr *appid_flag, int use_string){ if (use_string) { gwlist_append(*push_headers, appid_string); return; } if (octstr_compare(appid_flag, octstr_imm("any")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "http://www.wiral.com:*"); else http_header_add(*push_headers, "X-WAP-Application-Id", "0"); } else if (octstr_compare(appid_flag, octstr_imm("ua")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "http://www.wiral.com:wml.ua"); else http_header_add(*push_headers, "X-WAP-Application-Id", "2"); } else if (octstr_compare(appid_flag, octstr_imm("mms")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "mms.ua"); else http_header_add(*push_headers, "X-WAP-Application-Id", "4"); } else if (octstr_compare(appid_flag, octstr_imm("scrap")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "no appid at all"); else http_header_add(*push_headers, "X-WAP-Application-Id", "this is not a numeric header"); }}static void read_test_ppg_config(Octstr *name){ Cfg *cfg; CfgGroup *grp; cfg = cfg_create(name); if (cfg_read(cfg) == -1) panic(0, "Cannot read a configuration file %s, exiting", octstr_get_cstr(name)); cfg_dump(cfg); grp = cfg_get_single_group(cfg, octstr_imm("test-ppg")); cfg_get_integer(&retries, grp, octstr_imm("retries")); cfg_get_bool(&pi_ssl, grp, octstr_imm("pi-ssl"));#ifdef HAVE_LIBSSL if (pi_ssl) { ssl_client_certkey_file = cfg_get(grp, octstr_imm("ssl-client-certkey-file")); if (ssl_client_certkey_file != NULL) { use_global_client_certkey_file(ssl_client_certkey_file); } else { error(0, "cannot set up SSL without client certkey file"); exit(1); } }#endif grp = cfg_get_single_group(cfg, octstr_imm("configuration")); push_url = cfg_get(grp, octstr_imm("push-url")); pap_file = cfg_get(grp, octstr_imm("pap-file")); content_file = cfg_get(grp, octstr_imm("content-file")); if (!use_hardcoded) { username = cfg_get(grp, octstr_imm("username")); password = cfg_get(grp, octstr_imm("password")); } cfg_destroy(cfg);}static void add_dlr_mask(List **push_headers, Octstr *value){ http_header_add(*push_headers, "X-Kannel-DLR-Mask", octstr_get_cstr(value));}static void add_dlr_url(List **push_headers, Octstr *value){ http_header_add(*push_headers, "X-Kannel-DLR-Url", octstr_get_cstr(value));}static void add_part_header(Octstr *content_keader, Octstr **wap_content){ if (use_content_header) { octstr_append(*wap_content, content_header); } add_delimiter(wap_content);}static void add_content_type(Octstr *content_flag, Octstr **wap_content){ if (octstr_compare(content_flag, octstr_imm("wml")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.wml"); else if (octstr_compare(content_flag, octstr_imm("si")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.si"); else if (octstr_compare(content_flag, octstr_imm("sl")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.sl"); else if (octstr_compare(content_flag, octstr_imm("multipart")) == 0) *wap_content = octstr_format("%s", "Content-Type: multipart/related; boundary=fsahgwruijkfldsa"); else if (octstr_compare(content_flag, octstr_imm("mms")) == 0) *wap_content = octstr_format("%s", "Content-Type: application/vnd.wap.mms-message"); else if (octstr_compare(content_flag, octstr_imm("scrap")) == 0) *wap_content = octstr_format("%s", "no type at all"); else if (octstr_compare(content_flag, octstr_imm("nil")) == 0) *wap_content = octstr_create(""); if (octstr_len(*wap_content) > 0) add_delimiter(wap_content);}static void add_content_transfer_encoding_type(Octstr *content_flag, Octstr *wap_content){ if (!content_flag) return; if (octstr_compare(content_flag, octstr_imm("base64")) == 0) octstr_append_cstr(wap_content, "Content-transfer-encoding: base64"); add_delimiter(&wap_content);}static void add_connection_header(List **push_headers, Octstr *connection){ if (!connection) return; if (octstr_compare(connection, octstr_imm("close")) == 0) http_header_add(*push_headers, "Connection", "close"); else if (octstr_compare(connection, octstr_imm("keep-alive")) == 0) http_header_add(*push_headers, "Connection", "keep-alive");}static void transfer_encode (Octstr *cte, Octstr *content){ if (!cte) return; if (octstr_compare(cte, octstr_imm("base64")) == 0) { octstr_binary_to_base64(content); }}/* * Add boundary value to the multipart header. */static Octstr *make_multipart_value(const char *boundary){ Octstr *hos; hos = octstr_format("%s", "multipart/related; boundary="); octstr_append(hos, octstr_imm(boundary)); octstr_append(hos, octstr_imm("; type=\"application/xml\"")); return hos;}static Octstr *make_part_delimiter(Octstr *boundary){ Octstr *part_delimiter; part_delimiter = octstr_create(""); add_delimiter(&part_delimiter); octstr_format_append(part_delimiter, "%s", "--"); octstr_append(part_delimiter, boundary); add_delimiter(&part_delimiter); return part_delimiter;}static Octstr *make_close_delimiter(Octstr *boundary){ Octstr *close_delimiter; close_delimiter = octstr_create(""); add_delimiter(&close_delimiter); octstr_format_append(close_delimiter, "%s", "--"); octstr_append(close_delimiter, boundary); octstr_format_append(close_delimiter, "%s", "--"); /*add_delimiter(&close_delimiter);*/ return close_delimiter;}/* * Try log in defined number of times, when got response 401 and authentica- * tion info is in headers. */static int receive_push_reply(HTTPCaller *caller){ void *id; long *trid; int http_status, tries; List *reply_headers; Octstr *final_url, *auth_url, *reply_body, *os, *push_content, *auth_reply_body; WAPEvent *e; List *retry_headers; http_status = HTTP_UNAUTHORIZED; tries = 0; id = http_receive_result(caller, &http_status, &final_url, &reply_headers, &reply_body); if (id == NULL || http_status == -1 || final_url == NULL) { error(0, "push failed, no reason found"); goto push_failed; } while (use_headers && http_status == HTTP_UNAUTHORIZED && tries < retries) { debug("test.ppg", 0, "try number %d", tries); debug("test.ppg", 0, "authentication failure, get a challenge"); http_destroy_headers(reply_headers); push_content = push_content_create(); retry_headers = push_headers_create(octstr_len(push_content)); http_add_basic_auth(retry_headers, username, password); trid = gw_malloc(sizeof(long)); *trid = tries; http_start_request(caller, HTTP_METHOD_POST, final_url, retry_headers, push_content, 0, trid, NULL); debug("test.ppg ", 0, "TEST_PPG: doing response to %s", octstr_get_cstr(final_url)); octstr_destroy(push_content); http_destroy_headers(retry_headers); trid = http_receive_result(caller, &http_status, &auth_url, &reply_headers, &auth_reply_body); if (trid == NULL || http_status == -1 || auth_url == NULL) { error(0, "unable to send authorisation, no reason found"); goto push_failed; } debug("test.ppg", 0, "TEST_PPG: send authentication to %s, retry %ld", octstr_get_cstr(auth_url), *(long *) trid); gw_free(trid); octstr_destroy(auth_reply_body); octstr_destroy(auth_url); ++tries; } if (http_status == HTTP_NOT_FOUND) { error(0, "push failed, service not found"); goto push_failed; } if (http_status == HTTP_FORBIDDEN) { error(0, "push failed, service forbidden"); goto push_failed; } if (http_status == HTTP_UNAUTHORIZED) { if (use_headers) error(0, "tried %ld times, stopping", retries); else error(0, "push failed, authorisation failure"); goto push_failed; } debug("test.ppg", 0, "TEST_PPG: push %ld done: reply from, %s", *(long *) id, octstr_get_cstr(final_url)); gw_free(id); octstr_destroy(final_url); if (verbose) debug("test.ppg", 0, "TEST_PPG: reply headers were"); while ((os = gwlist_extract_first(reply_headers)) != NULL) { if (verbose) octstr_dump(os, 0); octstr_destroy(os); } if (verbose) { debug("test.ppg", 0, "TEST_PPG: reply body was"); octstr_dump(reply_body, 0); } e = NULL; if (pap_compile(reply_body, &e) < 0) { warning(0, "TEST_PPG: receive_push_reply: cannot compile pap message"); goto parse_error; } switch (e->type) { case Push_Response: debug("test.ppg", 0, "TEST_PPG: and type push response"); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -