Skip to content

Commit aae1a68

Browse files
authored
Merge pull request #3235 from cesanta/client
Refactor HTTP client
2 parents a00b3f1 + 61285ec commit aae1a68

File tree

3 files changed

+33
-3191
lines changed

3 files changed

+33
-3191
lines changed

tutorials/http/http-client/Makefile

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ PROG ?= example # Program we are building
22
PACK ?= ./pack # Packing executable
33
DELETE = rm -rf # Command to remove files
44
OUT ?= -o $(PROG) # Compiler argument for output file
5-
SOURCES = main.c mongoose.c packed_fs.c # Source code files, packed_fs.c contains ca.pem, which contains CA certs for TLS
5+
SOURCES = main.c mongoose.c # Source code files
66
CFLAGS = -W -Wall -Wextra -g -I. # Build options
77

88
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
9-
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_PACKED_FS=1
9+
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1
1010
CFLAGS_EXTRA ?= -DMG_TLS=MG_TLS_BUILTIN
1111

1212
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
@@ -27,14 +27,3 @@ $(PROG): $(SOURCES) # Build program from sources
2727

2828
clean: # Cleanup. Delete built program and all build artifacts
2929
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM mbedtls $(PACK)
30-
31-
# Generate packed filesystem for serving cert
32-
packed_fs.c: $(wildcard certs/*) Makefile
33-
$(CC) ../../../test/pack.c -o $(PACK)
34-
$(PACK) $(wildcard certs/*) > $@
35-
36-
# see https://mongoose.ws/tutorials/tls/#how-to-build for TLS build options
37-
38-
mbedtls: # Pull and build mbedTLS library
39-
git clone --depth 1 -b v2.28.2 https://github.com/mbed-tls/mbedtls $@
40-
$(MAKE) -C mbedtls/library

tutorials/http/http-client/main.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// The very first web page in history. You can replace it from command line
1313
static const char *s_url = "http://info.cern.ch/";
14+
static struct mg_str s_ca_pem; // CA PEM file
1415
static const char *s_post_data = NULL; // POST data
1516
static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds
1617

@@ -29,8 +30,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
2930
struct mg_str host = mg_url_host(s_url);
3031

3132
if (c->is_tls) {
32-
struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
33-
.name = mg_url_host(s_url)};
33+
struct mg_tls_opts opts = {.ca = s_ca_pem, .name = mg_url_host(s_url)};
3434
mg_tls_init(c, &opts);
3535
}
3636

@@ -49,24 +49,45 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
4949
// Response is received. Print it
5050
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
5151
printf("%.*s", (int) hm->message.len, hm->message.buf);
52-
c->is_draining = 1; // Tell mongoose to close this connection
52+
c->is_draining = 1; // Tell mongoose to close this connection
5353
*(bool *) c->fn_data = true; // Tell event loop to stop
5454
} else if (ev == MG_EV_ERROR) {
5555
*(bool *) c->fn_data = true; // Error, tell event loop to stop
5656
}
5757
}
5858

5959
int main(int argc, char *argv[]) {
60-
const char *log_level = getenv("LOG_LEVEL"); // Allow user to set log level
61-
if (log_level == NULL) log_level = "4"; // Default is verbose
60+
struct mg_mgr mgr; // Event manager
61+
bool done = false; // Event handler flips it to true
62+
int i, log_level = MG_LL_DEBUG;
6263

63-
struct mg_mgr mgr; // Event manager
64-
bool done = false; // Event handler flips it to true
65-
if (argc > 1) s_url = argv[1]; // Use URL provided in the command line
66-
mg_log_set(atoi(log_level)); // Set to 0 to disable debug
67-
mg_mgr_init(&mgr); // Initialise event manager
64+
// Parse command-line flags
65+
for (i = 1; i + 1 < argc; i++) {
66+
if (strcmp(argv[i], "-ca") == 0) {
67+
s_ca_pem = mg_file_read(&mg_fs_posix, argv[++i]);
68+
} else if (strcmp(argv[i], "-post") == 0) {
69+
s_post_data = argv[++i];
70+
} else if (strcmp(argv[i], "-url") == 0) {
71+
s_url = argv[++i];
72+
} else if (strcmp(argv[i], "-v") == 0) {
73+
log_level = atoi(argv[++i]);
74+
} else {
75+
fprintf(stderr,
76+
"Usage: %s OPTIONS\n"
77+
" -ca PEM - TLS CA PEM file path, default: not set\n"
78+
" -post DATA - data to POST, default: not set\n"
79+
" -url URL - URL to fetch, default: %s\n"
80+
" -v LEVEL - debug level, from 0 to 4, default: %d\n",
81+
argv[0], s_url, log_level);
82+
exit(EXIT_FAILURE);
83+
}
84+
}
85+
86+
mg_mgr_init(&mgr); // Initialise event manager
87+
mg_log_set(log_level); // Set log level
6888
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
6989
while (!done) mg_mgr_poll(&mgr, 50); // Event manager loops until 'done'
7090
mg_mgr_free(&mgr); // Free resources
91+
7192
return 0;
7293
}

0 commit comments

Comments
 (0)