mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-24 11:54:49 +00:00
Merge dev branch code named Fossa as next stable Mongoose
This commit is contained in:
1 parent
28eb251ccd
commit
8927c9d22b
126 files changed
+162049
-8502
No files matched your search
@@ -1,12 +1,14 @@
|
||||
# Copyright (c) 2014 Cesanta Software
|
||||
# All rights reserved
|
||||
|
||||
PROG = http_client
|
||||
CFLAGS = -W -Wall -I../.. -pthread -g -O0 $(CFLAGS_EXTRA)
|
||||
SOURCES = $(PROG).c ../../mongoose.c
|
||||
CFLAGS = -W -Wall -I../.. -DNS_ENABLE_SSL -lssl -lcrypto -pthread $(CFLAGS_EXTRA)
|
||||
|
||||
unix: $(SOURCES)
|
||||
$(CC) -o $(PROG) $(SOURCES) $(CFLAGS)
|
||||
all: $(PROG)
|
||||
|
||||
$(PROG): $(SOURCES)
|
||||
$(CC) $(SOURCES) -o $@ $(CFLAGS)
|
||||
|
||||
$(PROG).exe: $(SOURCES)
|
||||
cl $(SOURCES) /I../.. /MD /Fe$@
|
||||
|
||||
clean:
|
||||
rm -rf $(PROG) *.exe *.dSYM *.obj *.exp *.o *.lib
|
||||
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
|
||||
@@ -1,82 +1,70 @@
|
||||
// Copyright (c) 2014 Cesanta Software
|
||||
// All rights reserved
|
||||
//
|
||||
// This example demostrates how to connect to the remote Web server,
|
||||
// download data, process it and send back a reply.
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
/*
|
||||
* Copyright (c) 2014 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*
|
||||
* This program fetches HTTP URLs.
|
||||
*/
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static int s_received_signal = 0;
|
||||
static struct mg_server *s_server = NULL;
|
||||
static const char *s_remote_addr = "glosbe.com:80";
|
||||
static int s_exit_flag = 0;
|
||||
static int s_show_headers = 0;
|
||||
static const char *s_show_headers_opt = "--show-headers";
|
||||
|
||||
static void signal_handler(int sig_num) {
|
||||
signal(sig_num, signal_handler);
|
||||
s_received_signal = sig_num;
|
||||
}
|
||||
|
||||
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
||||
struct mg_connection *client, *orig;
|
||||
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_AUTH:
|
||||
return MG_TRUE;
|
||||
|
||||
case MG_CONNECT:
|
||||
// Send request to the remote host.
|
||||
// TODO(lsm): handle connect error here.
|
||||
mg_printf(conn, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n",
|
||||
"/gapi/translate?from=eng&dest=fra&format=json&phrase=cat",
|
||||
s_remote_addr);
|
||||
return MG_TRUE;
|
||||
|
||||
case MG_REPLY:
|
||||
// Send reply to the original connection
|
||||
orig = (struct mg_connection *) conn->connection_param;
|
||||
mg_send_status(orig, conn->status_code);
|
||||
mg_send_header(orig, "Content-Type", "text/plain");
|
||||
mg_send_data(orig, conn->content, conn->content_len);
|
||||
mg_send_data(orig, "", 0); // Last chunk: mark the end of reply
|
||||
|
||||
// Disconnect connections
|
||||
orig->connection_param = NULL;
|
||||
conn->connection_param = NULL;
|
||||
return MG_TRUE;
|
||||
|
||||
case MG_REQUEST:
|
||||
if ((client = mg_connect(s_server, s_remote_addr)) != NULL) {
|
||||
// Interconnect requests
|
||||
client->connection_param = conn;
|
||||
conn->connection_param = client;
|
||||
return MG_MORE;
|
||||
} else {
|
||||
mg_printf_data(conn, "%s", "cannot send API request");
|
||||
return MG_TRUE;
|
||||
case NS_CONNECT:
|
||||
if (* (int *) ev_data != 0) {
|
||||
fprintf(stderr, "connect() failed: %s\n", strerror(* (int *) ev_data));
|
||||
s_exit_flag = 1;
|
||||
}
|
||||
|
||||
break;
|
||||
case NS_HTTP_REPLY:
|
||||
nc->flags |= NSF_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;
|
||||
default:
|
||||
return MG_FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
s_server = mg_create_server(NULL, ev_handler);
|
||||
int main(int argc, char *argv[]) {
|
||||
struct mg_mgr mgr;
|
||||
int i;
|
||||
|
||||
mg_set_option(s_server, "listening_port", "8080");
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
|
||||
// Setup signal handlers
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
printf("Listening on port %s\n", mg_get_option(s_server, "listening_port"));
|
||||
while (s_received_signal == 0) {
|
||||
mg_poll_server(s_server, 1000);
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
mg_destroy_server(&s_server);
|
||||
printf("Existing on signal %d\n", s_received_signal);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user