mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-19 03:59:45 +08:00
eaef5bd133
This reverts commit 1a17e17c462bdd4e1d26d8742f8b7087273e04c2. PUBLISHED_FROM=80028de308c9a021955d1425d2bfee8feb85f193
77 lines
1.7 KiB
C
77 lines
1.7 KiB
C
/*
|
|
* Copyright (c) 2014 Cesanta Software Limited
|
|
* All rights reserved
|
|
*
|
|
* This program fetches HTTP URLs.
|
|
*/
|
|
|
|
#include "mongoose.h"
|
|
|
|
static int s_exit_flag = 0;
|
|
static int s_show_headers = 0;
|
|
static const char *s_show_headers_opt = "--show-headers";
|
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
|
|
|
switch (ev) {
|
|
case MG_EV_CONNECT:
|
|
if (*(int *) ev_data != 0) {
|
|
fprintf(stderr, "connect() failed: %s\n", strerror(*(int *) ev_data));
|
|
s_exit_flag = 1;
|
|
}
|
|
break;
|
|
case MG_EV_HTTP_REPLY:
|
|
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
|
if (s_show_headers) {
|
|
fwrite(hm->message.p, 1, hm->message.len, stdout);
|
|
} else {
|
|
fwrite(hm->body.p, 1, hm->body.len, stdout);
|
|
}
|
|
putchar('\n');
|
|
s_exit_flag = 1;
|
|
break;
|
|
case MG_EV_CLOSE:
|
|
if (s_exit_flag == 0) {
|
|
printf("Server closed connection\n");
|
|
s_exit_flag = 1;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (i + 1 != argc) {
|
|
fprintf(stderr, "Usage: %s [%s] [--hexdump <file>] <URL>\n", argv[0],
|
|
s_show_headers_opt);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
mg_connect_http(&mgr, ev_handler, argv[i], NULL, NULL);
|
|
|
|
while (s_exit_flag == 0) {
|
|
mg_mgr_poll(&mgr, 1000);
|
|
}
|
|
mg_mgr_free(&mgr);
|
|
|
|
return 0;
|
|
}
|