2015-09-08 19:49:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Cesanta Software Limited
|
|
|
|
* All rights reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
/* RESTful server host and request URI */
|
2016-03-05 01:05:47 +08:00
|
|
|
static const char *s_url =
|
|
|
|
"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta";
|
2015-09-08 19:49:03 +08:00
|
|
|
|
|
|
|
static int s_exit_flag = 0;
|
|
|
|
|
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
|
|
|
int connect_status;
|
|
|
|
|
|
|
|
switch (ev) {
|
2015-09-19 19:38:46 +08:00
|
|
|
case MG_EV_CONNECT:
|
2016-03-05 01:05:47 +08:00
|
|
|
connect_status = *(int *) ev_data;
|
|
|
|
if (connect_status != 0) {
|
|
|
|
printf("Error connecting to %s: %s\n", s_url, strerror(connect_status));
|
2015-09-08 19:49:03 +08:00
|
|
|
s_exit_flag = 1;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-19 19:38:46 +08:00
|
|
|
case MG_EV_HTTP_REPLY:
|
2015-09-08 19:49:03 +08:00
|
|
|
printf("Got reply:\n%.*s\n", (int) hm->body.len, hm->body.p);
|
2015-09-19 19:38:46 +08:00
|
|
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
2015-09-08 19:49:03 +08:00
|
|
|
s_exit_flag = 1;
|
|
|
|
break;
|
2016-11-01 16:46:09 +08:00
|
|
|
case MG_EV_CLOSE:
|
|
|
|
if (s_exit_flag == 0) {
|
|
|
|
printf("Server closed connection\n");
|
|
|
|
s_exit_flag = 1;
|
|
|
|
};
|
|
|
|
break;
|
2015-09-08 19:49:03 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
struct mg_connection *nc;
|
|
|
|
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
2016-03-05 01:05:47 +08:00
|
|
|
nc = mg_connect_http(&mgr, ev_handler, s_url, NULL, NULL);
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_set_protocol_http_websocket(nc);
|
|
|
|
|
2016-03-05 01:05:47 +08:00
|
|
|
printf("Starting RESTful client against %s\n", s_url);
|
2015-09-08 19:49:03 +08:00
|
|
|
while (s_exit_flag == 0) {
|
|
|
|
mg_mgr_poll(&mgr, 1000);
|
|
|
|
}
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|