1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include "lib_acl.h" #include "lib_protocol.h"
static void get_url(const char *method, const char *url, const char *proxy, const char *dump, int out) { HTTP_UTIL *http = http_util_req_new(url, method); int ret;
if (proxy && *proxy) http_util_set_req_proxy(http, proxy);
if (dump && *dump) http_util_set_dump_file(http, dump);
http_hdr_print(&http->hdr_req->hdr, "---request hdr---");
if (http_util_req_open(http) < 0) { printf("open connection(%s) error\n", http->server_addr); http_util_free(http); return; }
ret = http_util_get_res_hdr(http); if (ret < 0) { printf("get reply http header error\n"); http_util_free(http); return; }
http_hdr_print(&http->hdr_res->hdr, "--- reply http header ---");
while (1) { char buf[4096]; ret = http_util_get_res_body(http, buf, sizeof(buf) - 1); if (ret <= 0) break; buf[ret] = 0; if (out) printf("%s", buf); } http_util_free(http); }
static void usage(const char *procname) { printf("usage: %s -h[help] -t method -r url -f dump_file -o[output] -X proxy_addr\n" "example: %s -t GET -r http://www.sina.com.cn/ -f url_dump.txt\n", procname, procname); }
int main(int argc, char *argv[]) { int ch, out = 0; char url[256], dump[256], proxy[256], method[32];
acl_init();
ACL_SAFE_STRNCPY(method, "GET", sizeof(method)); url[0] = 0; dump[0] = 0; proxy[0] = 0; while ((ch = getopt(argc, argv, "hor:t:f:X:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return (0); case 'o': out = 1; break; case 'r': ACL_SAFE_STRNCPY(url, optarg, sizeof(url)); break; case 't': ACL_SAFE_STRNCPY(method, optarg, sizeof(method)); break; case 'f': ACL_SAFE_STRNCPY(dump, optarg, sizeof(dump)); break; case 'X': ACL_SAFE_STRNCPY(proxy, optarg, sizeof(proxy)); break; default: break; } }
if (url[0] == 0) { usage(argv[0]); return (0); }
get_url(method, url, proxy, dump, out); return (0); }
|