Statistics
| Branch: | Tag: | Revision:

mongoose / examples / restful_client / restful_client.c @ eaef5bd1

History | View | Annotate | Download (1.34 KB)

1
/*
2
 * Copyright (c) 2014 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "mongoose.h"
7

    
8
/* RESTful server host and request URI */
9
static const char *s_url =
10
    "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta";
11

    
12
static int s_exit_flag = 0;
13

    
14
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
15
  struct http_message *hm = (struct http_message *) ev_data;
16
  int connect_status;
17

    
18
  switch (ev) {
19
    case MG_EV_CONNECT:
20
      connect_status = *(int *) ev_data;
21
      if (connect_status != 0) {
22
        printf("Error connecting to %s: %s\n", s_url, strerror(connect_status));
23
        s_exit_flag = 1;
24
      }
25
      break;
26
    case MG_EV_HTTP_REPLY:
27
      printf("Got reply:\n%.*s\n", (int) hm->body.len, hm->body.p);
28
      nc->flags |= MG_F_SEND_AND_CLOSE;
29
      s_exit_flag = 1;
30
      break;
31
    case MG_EV_CLOSE:
32
      if (s_exit_flag == 0) {
33
        printf("Server closed connection\n");
34
        s_exit_flag = 1;
35
      };
36
      break;
37
    default:
38
      break;
39
  }
40
}
41

    
42
int main(void) {
43
  struct mg_mgr mgr;
44
  struct mg_connection *nc;
45

    
46
  mg_mgr_init(&mgr, NULL);
47
  nc = mg_connect_http(&mgr, ev_handler, s_url, NULL, NULL);
48
  mg_set_protocol_http_websocket(nc);
49

    
50
  printf("Starting RESTful client against %s\n", s_url);
51
  while (s_exit_flag == 0) {
52
    mg_mgr_poll(&mgr, 1000);
53
  }
54
  mg_mgr_free(&mgr);
55

    
56
  return 0;
57
}