11
11
12
12
// The very first web page in history. You can replace it from command line
13
13
static const char * s_url = "http://info.cern.ch/" ;
14
+ static struct mg_str s_ca_pem ; // CA PEM file
14
15
static const char * s_post_data = NULL ; // POST data
15
16
static const uint64_t s_timeout_ms = 1500 ; // Connect timeout in milliseconds
16
17
@@ -29,8 +30,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
29
30
struct mg_str host = mg_url_host (s_url );
30
31
31
32
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 )};
34
34
mg_tls_init (c , & opts );
35
35
}
36
36
@@ -49,24 +49,45 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
49
49
// Response is received. Print it
50
50
struct mg_http_message * hm = (struct mg_http_message * ) ev_data ;
51
51
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
53
53
* (bool * ) c -> fn_data = true; // Tell event loop to stop
54
54
} else if (ev == MG_EV_ERROR ) {
55
55
* (bool * ) c -> fn_data = true; // Error, tell event loop to stop
56
56
}
57
57
}
58
58
59
59
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 ;
62
63
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
68
88
mg_http_connect (& mgr , s_url , fn , & done ); // Create client connection
69
89
while (!done ) mg_mgr_poll (& mgr , 50 ); // Event manager loops until 'done'
70
90
mg_mgr_free (& mgr ); // Free resources
91
+
71
92
return 0 ;
72
93
}
0 commit comments