static void httpadmin_run(void *arg) { HTTPClient *client; Octstr *ip, *url, *body; List *headers, *cgivars; while(bb_status != BB_DEAD) { if (bb_status == BB_SHUTDOWN) bb_shutdown(); client = http_accept_request(ha_port, &ip, &url, &headers, &body, &cgivars); if (client == NULL) break; if (is_allowed_ip(ha_allow_ip, ha_deny_ip, ip) == 0) { info(0, "HTTP admin tried from denied host <%s>, disconnected", octstr_get_cstr(ip)); http_close_client(client); continue; } httpd_serve(client, url, headers, body, cgivars); octstr_destroy(ip); } httpadmin_running = 0; }
static void http_thread(void *arg) { HTTPClient *client; Octstr *ip; Octstr *url; List *headers; Octstr *body; List *cgivars; Octstr *reply_body = octstr_create( "<?xml version=\"1.0\"?>\n" "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n" " \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n" "<wml>\n" "<card id=\"main\" title=\"Hello, world\" newcontext=\"true\">\n" " <p>Hello, world.</p>\n" "</card></wml>\n"); List *reply_headers = list_create(); int port; port = *(int *) arg; gw_free(arg); list_append(reply_headers, octstr_create("Content-Type: text/vnd.wap.wml")); for (;!dying;) { client = http_accept_request(port, &ip, &url, &headers, &body, &cgivars); if (client == NULL) break; http_send_reply(client, HTTP_OK, reply_headers, reply_body); http_destroy_headers(headers); octstr_destroy(ip); octstr_destroy(url); octstr_destroy(body); http_destroy_cgiargs(cgivars); } octstr_destroy(reply_body); http_destroy_headers(reply_headers); }
/* * Thread to listen to HTTP requests from SMSC entity */ static void httpsmsc_receiver(void *arg) { SMSCConn *conn = arg; ConnData *conndata = conn->data; HTTPClient *client; Octstr *ip, *url, *body; List *headers, *cgivars; /* Make sure we log into our own log-file if defined */ log_thread_to(conn->log_idx); while (conndata->shutdown == 0) { /* reset */ ip = url = body = NULL; headers = cgivars = NULL; /* XXX if conn->is_stopped, do not receive new messages.. */ client = http_accept_request(conndata->port, &ip, &url, &headers, &body, &cgivars); if (client == NULL) break; if (cgivars != NULL) { octstr_append_char(url, '?'); http_cgivar_dump_into(cgivars, url); } debug("smsc.http", 0, "HTTP[%s]: Got request `%s'", octstr_get_cstr(conn->id), octstr_get_cstr(url)); if (connect_denied(conndata->allow_ip, ip)) { info(0, "HTTP[%s]: Connection `%s' tried from denied " "host %s, ignored", octstr_get_cstr(conn->id), octstr_get_cstr(url), octstr_get_cstr(ip)); http_close_client(client); } else conndata->callbacks->receive_sms(conn, client, headers, body, cgivars); debug("smsc.http", 0, "HTTP[%s]: Destroying client information", octstr_get_cstr(conn->id)); octstr_destroy(url); octstr_destroy(ip); octstr_destroy(body); http_destroy_headers(headers); http_destroy_cgiargs(cgivars); } debug("smsc.http", 0, "HTTP[%s]: httpsmsc_receiver dying", octstr_get_cstr(conn->id)); conndata->shutdown = 1; http_close_port(conndata->port); /* unblock http_receive_result() if there are no open sends */ if (counter_value(conndata->open_sends) == 0) http_caller_signal_shutdown(conndata->http_ref); if (conndata->sender_thread != -1) { gwthread_wakeup(conndata->sender_thread); gwthread_join(conndata->sender_thread); } if (conndata->send_cb_thread != -1) { gwthread_wakeup(conndata->send_cb_thread); gwthread_join(conndata->send_cb_thread); } mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_DEAD; mutex_unlock(conn->flow_mutex); if (conndata->callbacks != NULL && conndata->callbacks->destroy != NULL) conndata->callbacks->destroy(conn); conn->data = NULL; conndata_destroy(conndata); bb_smscconn_killed(); }
static void client_thread(void *arg) { HTTPClient *client; Octstr *body, *url, *ip; List *headers, *resph, *cgivars; HTTPCGIVar *v; Octstr *reply_body, *reply_type; unsigned long n = 0; int status, i; while (run) { client = http_accept_request(port, &ip, &url, &headers, &body, &cgivars); n++; if (client == NULL) break; info(0, "Request for <%s> from <%s>", octstr_get_cstr(url), octstr_get_cstr(ip)); if (verbose) debug("test.http", 0, "CGI vars were"); /* * Don't use gwlist_extract() here, otherwise we don't have a chance * to re-use the cgivars later on. */ for (i = 0; i < gwlist_len(cgivars); i++) { if ((v = gwlist_get(cgivars, i)) != NULL && verbose) { octstr_dump(v->name, 0); octstr_dump(v->value, 0); } } if (arg == NULL) { reply_body = octstr_duplicate(reply_text); reply_type = octstr_create("Content-Type: text/plain; " "charset=\"UTF-8\""); } else { reply_body = octstr_duplicate(arg); reply_type = octstr_create("Content-Type: text/vnd.wap.wml"); } resph = gwlist_create(); gwlist_append(resph, reply_type); status = HTTP_OK; /* check for special URIs and handle those */ if (octstr_compare(url, octstr_imm("/quit")) == 0) { run = 0; } else if (octstr_compare(url, octstr_imm("/whitelist")) == 0) { octstr_destroy(reply_body); if (whitelist != NULL) { if (verbose) { debug("test.http.server", 0, "we send a white list"); octstr_dump(whitelist, 0); } reply_body = octstr_duplicate(whitelist); } else { reply_body = octstr_imm(""); } } else if (octstr_compare(url, octstr_imm("/blacklist")) == 0) { octstr_destroy(reply_body); if (blacklist != NULL) { if (verbose) { debug("test.http.server", 0, "we send a blacklist"); octstr_dump(blacklist, 0); } reply_body = octstr_duplicate(blacklist); } else { reply_body = octstr_imm(""); } } else if (octstr_compare(url, octstr_imm("/save")) == 0) { /* safe the body into a temporary file */ pid_t pid = getpid(); FILE *f = fopen(octstr_get_cstr(octstr_format("/tmp/body.%ld.%ld", pid, n)), "w"); octstr_print(f, body); fclose(f); } else if (octstr_compare(url, octstr_imm("/redirect/")) == 0) { /* provide us with a HTTP 302 redirection response * will return /redirect/<pid> for the location header * and will return /redirect/ if cgivar loop is set to allow looping */ Octstr *redirect_header, *scheme, *uri, *l; pid_t pid = getpid(); uri = ((l = http_cgi_variable(cgivars, "loop")) != NULL) ? octstr_format("%s?loop=%s", octstr_get_cstr(url), octstr_get_cstr(l)) : octstr_format("%s%ld", octstr_get_cstr(url), pid); octstr_destroy(reply_body); reply_body = octstr_imm("Here you got a redirection URL that you should follow."); scheme = ssl ? octstr_imm("https://") : octstr_imm("http://"); redirect_header = octstr_format("Location: %s%s%s", octstr_get_cstr(scheme), octstr_get_cstr(http_header_value(headers, octstr_imm("Host"))), octstr_get_cstr(uri)); gwlist_append(resph, redirect_header); status = HTTP_FOUND; /* will provide 302 */ octstr_destroy(uri); } else if (octstr_compare(url, octstr_imm("/mmsc")) == 0) { /* fake a M-Send.conf PDU which is using MMSEncapsulation as body */ pid_t pid = getpid(); FILE *f; gwlist_destroy(resph, octstr_destroy_item); octstr_destroy(reply_body); reply_type = octstr_create("Content-Type: application/vnd.wap.mms-message"); reply_body = octstr_create(""); octstr_append_from_hex(reply_body, "8c81" /* X-Mms-Message-Type: m-send-conf */ "98632d3862343300" /* X-Mms-Transaction-ID: c-8b43 */ "8d90" /* X-Mms-MMS-Version: 1.0 */ "9280" /* Response-status: Ok */ "8b313331373939353434393639383434313731323400" ); /* Message-Id: 13179954496984417124 */ resph = gwlist_create(); gwlist_append(resph, reply_type); /* safe the M-Send.req body into a temporary file */ f = fopen(octstr_get_cstr(octstr_format("/tmp/mms-body.%ld.%ld", pid, n)), "w"); octstr_print(f, body); fclose(f); } if (verbose) { debug("test.http", 0, "request headers were"); http_header_dump(headers); if (body != NULL) { debug("test.http", 0, "request body was"); octstr_dump(body, 0); } } if (extra_headers != NULL) http_header_combine(resph, extra_headers); /* return response to client */ http_send_reply(client, status, resph, reply_body); octstr_destroy(ip); octstr_destroy(url); octstr_destroy(body); octstr_destroy(reply_body); http_destroy_cgiargs(cgivars); gwlist_destroy(headers, octstr_destroy_item); gwlist_destroy(resph, octstr_destroy_item); } octstr_destroy(whitelist); octstr_destroy(blacklist); debug("test.http", 0, "Working thread 'client_thread' terminates"); http_close_all_ports(); }
void mmsc_receive_func(MmscGrp *m) { int i; MmsBoxHTTPClientInfo h = {NULL}; List *mmsc_incoming_reqs = gwlist_create(); long *thids = gw_malloc((maxthreads + 1)*sizeof thids[0]); gwlist_add_producer(mmsc_incoming_reqs); for (i = 0; i<maxthreads; i++) thids[i] = gwthread_create((gwthread_func_t *)dispatch_mm7_recv, mmsc_incoming_reqs); h.m = m; while(rstop == 0 && (h.client = http_accept_request(m->incoming.port, &h.ip, &h.url, &h.headers, &h.body, &h.cgivars)) != NULL) if (is_allowed_ip(m->incoming.allow_ip, m->incoming.deny_ip, h.ip)) { MmsBoxHTTPClientInfo *hx = gw_malloc(sizeof hx[0]); h.ua = http_header_value(h.headers, octstr_imm("User-Agent")); *hx = h; debug("mmsbox", 0, " MM7 Incoming, IP=[%s], MMSC=[%s], dest_port=[%ld] ", h.ip ? octstr_get_cstr(h.ip) : "", octstr_get_cstr(m->id), m->incoming.port); /* Dump headers, url etc. */ #if 0 http_header_dump(h.headers); if (h.body) octstr_dump(h.body, 0); if (h.ip) octstr_dump(h.ip, 0); #endif gwlist_produce(mmsc_incoming_reqs, hx); } else { h.ua = http_header_value(h.headers, octstr_imm("User-Agent")); mms_error_ex("auth",0, "MM7", m->id, "HTTP: Incoming IP denied MMSC[%s] ip=[%s], ua=[%s], disconnected", m->id ? octstr_get_cstr(m->id) : "(none)", h.ip ? octstr_get_cstr(h.ip) : "(none)", h.ua ? octstr_get_cstr(h.ua) : "(none)"); http_send_reply(h.client, HTTP_FORBIDDEN, NULL, octstr_imm("Access denied.")); free_mmsbox_http_clientInfo(&h, 0); } debug("proxy", 0, "MMSBox: MM7 receiver [mmc=%s] Shutting down...", octstr_get_cstr(m->id)); gwlist_remove_producer(mmsc_incoming_reqs); for (i = 0; i<maxthreads; i++) if (thids[i] >= 0) gwthread_join(thids[i]); gwlist_destroy(mmsc_incoming_reqs, NULL); gw_free(thids); debug("proxy", 0, "MMSBox: MM7 receiver [mmc=%s] Shutting down complete.", octstr_get_cstr(m->id)); }