/*! * Wrapper for HTTP-Query (POST-Variant) */ static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) { int ret = 0; str url = {NULL, 0}; str post = {NULL, 0}; str result = {NULL, 0}; pv_spec_t *dst; pv_value_t val; if (get_str_fparam(&url, _m, (gparam_p)_url) != 0) { LM_ERR("URL has no value\n"); return -1; } if (get_str_fparam(&post, _m, (gparam_p)_post) != 0) { LM_ERR("DATA has no value\n"); return -1; } ret = http_query(_m, url.s, &result, post.s); val.rs = result; val.flags = PV_VAL_STR; dst = (pv_spec_t *)_result; dst->setf(_m, &dst->pvp, (int)EQ_T, &val); if (result.s != NULL) pkg_free(result.s); return (ret==0)?-1:ret; }
Buffer *http_url(const char *url) { char host[BUF_SIZE]; char page_slash[BUF_SIZE]; strncpy(host, url, BUF_SIZE); char *page = strstr(host, "/"); if(page) { page[0] = '\0'; ++page; sprintf(page_slash, "/%s", page); return http_query(host, page_slash, 80); } else { fprintf(stderr, "could not split url into host/page %s\n", url); return NULL; } }
struct catalog_query *catalog_query_create(const char *host, int port, time_t stoptime) { struct catalog_query *q = xxmalloc(sizeof(*q)); char url[1024]; if(!host) host = CATALOG_HOST; if(!port) port = CATALOG_PORT; sprintf(url, "http://%s:%d/query.text", host, port); q->link = http_query(url, "GET", stoptime); if(!q->link) { free(q); return 0; } return q; }
struct jx *catalog_query_send_query(const char *url, time_t stoptime) { struct link *link = http_query(url, "GET", stoptime); if(!link) { return NULL; } struct jx *j = jx_parse_link(link,stoptime); link_close(link); if(!j) { debug(D_DEBUG,"query result failed to parse as JSON"); return NULL; } if(!jx_istype(j,JX_ARRAY)) { debug(D_DEBUG,"query result is not a JSON array"); jx_delete(j); return NULL; } return j; }
int main(int argc, char **argv) { int i, s, done, print_body, gssapi_done, gssapi_started, optidx = 0; const char *host, *page; struct http_req req; char *headers[99]; /* XXX */ int num_headers; krb5_storage *sp; gss_cred_id_t client_cred = GSS_C_NO_CREDENTIAL; gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT; gss_name_t server = GSS_C_NO_NAME; gss_OID mech_oid, cred_mech_oid; OM_uint32 flags; OM_uint32 maj_stat, min_stat; setprogname(argv[0]); if(getarg(http_args, num_http_args, argc, argv, &optidx)) usage(1); if (help_flag) usage (0); if(version_flag) { print_version(NULL); exit(0); } argc -= optidx; argv += optidx; mech_oid = select_mech(mech); if (cred_mech_str) cred_mech_oid = select_mech(cred_mech_str); else cred_mech_oid = mech_oid; if (argc != 1 && argc != 2) errx(1, "usage: %s host [page]", getprogname()); host = argv[0]; if (argc == 2) page = argv[1]; else page = "/"; flags = 0; if (delegate_flag) flags |= GSS_C_DELEG_FLAG; if (policy_flag) flags |= GSS_C_DELEG_POLICY_FLAG; if (mutual_flag) flags |= GSS_C_MUTUAL_FLAG; done = 0; num_headers = 0; gssapi_done = 0; gssapi_started = 0; if (client_str) { gss_buffer_desc name_buffer; gss_name_t name; gss_OID_set mechset = GSS_C_NO_OID_SET; name_buffer.value = client_str; name_buffer.length = strlen(client_str); maj_stat = gss_import_name(&min_stat, &name_buffer, GSS_C_NT_USER_NAME, &name); if (maj_stat) errx(1, "failed to import name"); if (cred_mech_oid) { gss_create_empty_oid_set(&min_stat, &mechset); gss_add_oid_set_member(&min_stat, cred_mech_oid, &mechset); } maj_stat = gss_acquire_cred(&min_stat, name, GSS_C_INDEFINITE, mechset, GSS_C_INITIATE, &client_cred, NULL, NULL); gss_release_name(&min_stat, &name); gss_release_oid_set(&min_stat, &mechset); if (maj_stat) errx(1, "failed to find cred of name %s", client_str); } { gss_buffer_desc name_token; char *name; asprintf(&name, "%s@%s", gss_service, host); name_token.length = strlen(name); name_token.value = name; maj_stat = gss_import_name(&min_stat, &name_token, GSS_C_NT_HOSTBASED_SERVICE, &server); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_inport_name: %s", name); free(name); } s = do_connect(host, port_str); if (s < 0) errx(1, "connection failed"); sp = krb5_storage_from_fd(s); if (sp == NULL) errx(1, "krb5_storage_from_fd"); do { print_body = 0; http_query(sp, host, page, headers, num_headers, &req); for (i = 0 ; i < num_headers; i++) free(headers[i]); num_headers = 0; if (strstr(req.response, " 200 ") != NULL) { print_body = 1; done = 1; } else if (strstr(req.response, " 401 ") != NULL) { if (http_find_header(&req, "WWW-Authenticate:") == NULL) errx(1, "Got %s but missed `WWW-Authenticate'", req.response); } if (!gssapi_done) { const char *h = http_find_header(&req, "WWW-Authenticate:"); if (h == NULL) errx(1, "Got %s but missed `WWW-Authenticate'", req.response); if (strncasecmp(h, "Negotiate", 9) == 0) { gss_buffer_desc input_token, output_token; if (verbose_flag) printf("Negotiate found\n"); i = 9; while(h[i] && isspace((unsigned char)h[i])) i++; if (h[i] != '\0') { size_t len = strlen(&h[i]); int slen; if (len == 0) errx(1, "invalid Negotiate token"); input_token.value = emalloc(len); slen = base64_decode(&h[i], input_token.value); if (slen < 0) errx(1, "invalid base64 Negotiate token %s", &h[i]); input_token.length = slen; } else { if (gssapi_started) errx(1, "Negotiate already started"); gssapi_started = 1; input_token.length = 0; input_token.value = NULL; } if (strstr(req.response, " 200 ") != NULL) sleep(1); maj_stat = gss_init_sec_context(&min_stat, client_cred, &context_hdl, server, mech_oid, flags, 0, GSS_C_NO_CHANNEL_BINDINGS, &input_token, NULL, &output_token, NULL, NULL); if (maj_stat == GSS_S_CONTINUE_NEEDED) { } else if (maj_stat == GSS_S_COMPLETE) { gss_name_t targ_name, src_name; gss_buffer_desc name_buffer; gss_OID mech_type; gssapi_done = 1; maj_stat = gss_inquire_context(&min_stat, context_hdl, &src_name, &targ_name, NULL, &mech_type, NULL, NULL, NULL); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_inquire_context"); printf("Negotiate done: %s\n", mech); maj_stat = gss_display_name(&min_stat, src_name, &name_buffer, NULL); if (GSS_ERROR(maj_stat)) gss_print_errors(min_stat); else printf("Source: %.*s\n", (int)name_buffer.length, (char *)name_buffer.value); gss_release_buffer(&min_stat, &name_buffer); maj_stat = gss_display_name(&min_stat, targ_name, &name_buffer, NULL); if (GSS_ERROR(maj_stat)) gss_print_errors(min_stat); else printf("Target: %.*s\n", (int)name_buffer.length, (char *)name_buffer.value); gss_release_name(&min_stat, &targ_name); gss_release_buffer(&min_stat, &name_buffer); } else { gss_err (1, min_stat, "gss_init_sec_context"); } if (output_token.length) { char *neg_token; base64_encode(output_token.value, (int)output_token.length, &neg_token); asprintf(&headers[0], "Authorization: Negotiate %s", neg_token); num_headers = 1; free(neg_token); gss_release_buffer(&min_stat, &output_token); } if (input_token.length) free(input_token.value); } else done = 1; } else done = 1; if (print_body || verbose_flag) printf("%.*s\n", (int)req.body_size, (char *)req.body); http_req_free(&req); } while (!done); if (gssapi_done == 0) errx(1, "gssapi not done but http dance done"); krb5_storage_free(sp); close(s); return 0; }
struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload) { char url[HTTP_LINE_MAX]; char newurl[HTTP_LINE_MAX]; char line[HTTP_LINE_MAX]; char addr[LINK_ADDRESS_MAX]; struct link *link; int save_errno; int response; char actual_host[HTTP_LINE_MAX]; int actual_port; *size = 0; url_encode(urlin, url, sizeof(url)); if(proxy && !strcmp(proxy, "DIRECT")) proxy = 0; if(proxy) { int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port); if(fields == 2) { /* host and port are good */ } else if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "invalid proxy syntax: %s", proxy); return 0; } } else { int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port); if(fields != 2) { fields = sscanf(url, "http://%[^/]", actual_host); if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "malformed url: %s", url); return 0; } } } debug(D_HTTP, "connect %s port %d", actual_host, actual_port); if(!domain_name_lookup(actual_host, addr)) return 0; link = link_connect(addr, actual_port, stoptime); if(!link) { errno = ECONNRESET; return 0; } if(cache_reload == 0) { debug(D_HTTP, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", action, url, actual_host); link_putfstring(link, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host); } else { // force refresh of cache end-to-end (RFC 2616) debug(D_HTTP, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", action, url, actual_host); link_putfstring(link, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host); } if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) { newurl[0] = 0; while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); sscanf(line, "Location: %s", newurl); sscanf(line, "Content-Length: %lld", size); if(strlen(line) <= 2) { break; } } switch (response) { case 200: return link; break; case 301: case 302: case 303: case 307: link_close(link); if(newurl[0]) { if(!strcmp(url, newurl)) { debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url); errno = EIO; return 0; } else { return http_query(newurl, action, stoptime); } } else { errno = ENOENT; return 0; } break; default: link_close(link); errno = http_response_to_errno(response); return 0; break; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } link_close(link); errno = save_errno; return 0; }
&category, &title, &alt_id); } #if 0 /* get cddb information from a local http server (plain text file) */ /* must change CDDB URL to be http://localhost/testentry.txt */ strcpy (final_cat, "test"); strcpy (final_id, "test"); #else else { if (!strcmp (((char *) config_read (CONF_CDDB_PROXYSRV)), "")) { status = http_query (wwwserver, port, uri, cd_id, tracknum, offset, duration, &matches, &category, &title, &alt_id, NAME, VERSION); } else { char *proxy_server = (char *) config_read (CONF_CDDB_PROXYSRV); gint proxy_port = (gint) config_read (CONF_CDDB_PROXYPRT); status = http_query_proxy (wwwserver, port, proxy_server, proxy_port, uri, cd_id, tracknum, offset, duration, &matches, &category, &title, &alt_id, NAME, VERSION); } }
int main(int argc, char **argv) { struct http_req req; const char *host, *page; int i, done, print_body, gssapi_done, gssapi_started; char *headers[10]; /* XXX */ int num_headers; gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT; gss_name_t server = GSS_C_NO_NAME; int optind = 0; gss_OID mech_oid; OM_uint32 flags; setprogname(argv[0]); if(getarg(http_args, num_http_args, argc, argv, &optind)) usage(1); if (help_flag) usage (0); if(version_flag) { print_version(NULL); exit(0); } argc -= optind; argv += optind; mech_oid = select_mech(mech); if (argc != 1 && argc != 2) errx(1, "usage: %s host [page]", getprogname()); host = argv[0]; if (argc == 2) page = argv[1]; else page = "/"; flags = 0; if (delegate_flag) flags |= GSS_C_DELEG_FLAG; if (mutual_flag) flags |= GSS_C_MUTUAL_FLAG; done = 0; num_headers = 0; gssapi_done = 1; gssapi_started = 0; do { print_body = 0; http_query(host, page, headers, num_headers, &req); for (i = 0 ; i < num_headers; i++) free(headers[i]); num_headers = 0; if (strstr(req.response, " 200 ") != NULL) { print_body = 1; done = 1; } else if (strstr(req.response, " 401 ") != NULL) { if (http_find_header(&req, "WWW-Authenticate:") == NULL) errx(1, "Got %s but missed `WWW-Authenticate'", req.response); gssapi_done = 0; } if (!gssapi_done) { const char *h = http_find_header(&req, "WWW-Authenticate:"); if (h == NULL) errx(1, "Got %s but missed `WWW-Authenticate'", req.response); if (strncasecmp(h, "Negotiate", 9) == 0) { OM_uint32 maj_stat, min_stat; gss_buffer_desc input_token, output_token; if (verbose_flag) printf("Negotiate found\n"); if (server == GSS_C_NO_NAME) { char *name; asprintf(&name, "%s@%s", gss_service, host); input_token.length = strlen(name); input_token.value = name; maj_stat = gss_import_name(&min_stat, &input_token, GSS_C_NT_HOSTBASED_SERVICE, &server); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_inport_name"); free(name); input_token.length = 0; input_token.value = NULL; } i = 9; while(h[i] && isspace((unsigned char)h[i])) i++; if (h[i] != '\0') { int len = strlen(&h[i]); if (len == 0) errx(1, "invalid Negotiate token"); input_token.value = emalloc(len); len = base64_decode(&h[i], input_token.value); if (len < 0) errx(1, "invalid base64 Negotiate token %s", &h[i]); input_token.length = len; } else { if (gssapi_started) errx(1, "Negotiate already started"); gssapi_started = 1; input_token.length = 0; input_token.value = NULL; } maj_stat = gss_init_sec_context(&min_stat, GSS_C_NO_CREDENTIAL, &context_hdl, server, mech_oid, flags, 0, GSS_C_NO_CHANNEL_BINDINGS, &input_token, NULL, &output_token, NULL, NULL); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_init_sec_context"); else if (maj_stat & GSS_S_CONTINUE_NEEDED) gssapi_done = 0; else { gss_name_t targ_name, src_name; gss_buffer_desc name_buffer; gss_OID mech_type; gssapi_done = 1; printf("Negotiate done: %s\n", mech); maj_stat = gss_inquire_context(&min_stat, context_hdl, &src_name, &targ_name, NULL, &mech_type, NULL, NULL, NULL); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_inquire_context"); maj_stat = gss_display_name(&min_stat, src_name, &name_buffer, NULL); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_display_name"); printf("Source: %.*s\n", (int)name_buffer.length, (char *)name_buffer.value); gss_release_buffer(&min_stat, &name_buffer); maj_stat = gss_display_name(&min_stat, targ_name, &name_buffer, NULL); if (GSS_ERROR(maj_stat)) gss_err (1, min_stat, "gss_display_name"); printf("Target: %.*s\n", (int)name_buffer.length, (char *)name_buffer.value); gss_release_name(&min_stat, &targ_name); gss_release_buffer(&min_stat, &name_buffer); } if (output_token.length) { char *neg_token; base64_encode(output_token.value, output_token.length, &neg_token); asprintf(&headers[0], "Authorization: Negotiate %s", neg_token); num_headers = 1; free(neg_token); gss_release_buffer(&min_stat, &output_token); } if (input_token.length) free(input_token.value); } else done = 1; } else done = 1; if (verbose_flag) { printf("%s\n\n", req.response); for (i = 0; i < req.num_headers; i++) printf("%s\n", req.headers[i]); printf("\n"); } if (print_body || verbose_flag) printf("%.*s\n", (int)req.body_size, (char *)req.body); http_req_free(&req); } while (!done); if (gssapi_done == 0) errx(1, "gssapi not done but http dance done"); return 0; }
/*! * Wrapper for HTTP-Query (POST-Variant) */ static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) { return http_query(_m, _url, _result, _post); }
/*! * Wrapper for HTTP-Query (GET) */ static int w_http_query(struct sip_msg* _m, char* _url, char* _result) { return http_query(_m, _url, _result, NULL); }