2015-09-08 19:49:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Cesanta Software Limited
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* This program fetches HTTP URLs.
|
|
|
|
*/
|
2014-08-06 00:14:41 +08:00
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
static int s_exit_flag = 0;
|
|
|
|
static int s_show_headers = 0;
|
|
|
|
static const char *s_show_headers_opt = "--show-headers";
|
2014-08-06 00:14:41 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
2014-08-06 00:14:41 +08:00
|
|
|
|
|
|
|
switch (ev) {
|
2015-09-19 19:38:46 +08:00
|
|
|
case MG_EV_CONNECT:
|
2015-09-08 19:49:03 +08:00
|
|
|
if (* (int *) ev_data != 0) {
|
|
|
|
fprintf(stderr, "connect() failed: %s\n", strerror(* (int *) ev_data));
|
|
|
|
s_exit_flag = 1;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-19 19:38:46 +08:00
|
|
|
case MG_EV_HTTP_REPLY:
|
|
|
|
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
2015-09-08 19:49:03 +08:00
|
|
|
if (s_show_headers) {
|
|
|
|
fwrite(hm->message.p, 1, hm->message.len, stdout);
|
2014-08-06 00:14:41 +08:00
|
|
|
} else {
|
2015-09-08 19:49:03 +08:00
|
|
|
fwrite(hm->body.p, 1, hm->body.len, stdout);
|
2014-08-06 00:14:41 +08:00
|
|
|
}
|
2015-09-08 19:49:03 +08:00
|
|
|
putchar('\n');
|
|
|
|
s_exit_flag = 1;
|
|
|
|
break;
|
2014-08-06 00:14:41 +08:00
|
|
|
default:
|
2015-09-08 19:49:03 +08:00
|
|
|
break;
|
2014-08-06 00:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
|
|
|
|
|
|
/* Process command line arguments */
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], s_show_headers_opt) == 0) {
|
|
|
|
s_show_headers = 1;
|
|
|
|
} else if (strcmp(argv[i], "--hexdump") == 0 && i + 1 < argc) {
|
|
|
|
mgr.hexdump_file = argv[++i];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 00:14:41 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
if (i + 1 != argc) {
|
|
|
|
fprintf(stderr, "Usage: %s [%s] [--hexdump <file>] <URL>\n",
|
|
|
|
argv[0], s_show_headers_opt);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-08-06 00:14:41 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_connect_http(&mgr, ev_handler, argv[i], NULL, NULL);
|
2014-08-06 00:14:41 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
while (s_exit_flag == 0) {
|
|
|
|
mg_mgr_poll(&mgr, 1000);
|
2014-08-06 00:14:41 +08:00
|
|
|
}
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_mgr_free(&mgr);
|
2014-08-06 00:14:41 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
return 0;
|
2014-08-06 00:14:41 +08:00
|
|
|
}
|