The HTTP status code can be retrieved from result object
[platform/upstream/connman.git] / tools / web-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <signal.h>
29
30 #include <gweb/gweb.h>
31
32 static GTimer *timer;
33
34 static GMainLoop *main_loop;
35
36 static void web_debug(const char *str, void *data)
37 {
38         g_print("%s: %s\n", (const char *) data, str);
39 }
40
41 static void sig_term(int sig)
42 {
43         g_main_loop_quit(main_loop);
44 }
45
46 static void web_result(GWebResult *result, gpointer user_data)
47 {
48         guint16 status;
49         gdouble elapsed;
50
51         status = g_web_result_get_status(result);
52
53         elapsed = g_timer_elapsed(timer, NULL);
54
55         g_print("elapse: %f seconds\n", elapsed);
56
57         g_print("status: %03u\n", status);
58
59         g_main_loop_quit(main_loop);
60 }
61
62 static gboolean option_debug = FALSE;
63 static gchar *option_nameserver = NULL;
64
65 static GOptionEntry options[] = {
66         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
67                                         "Enable debug output" },
68         { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
69                                         "Specify nameserver", "ADDRESS" },
70         { NULL },
71 };
72
73 int main(int argc, char *argv[])
74 {
75         GOptionContext *context;
76         GError *error = NULL;
77         struct sigaction sa;
78         GWeb *web;
79         int index = 0;
80
81         context = g_option_context_new(NULL);
82         g_option_context_add_main_entries(context, options, NULL);
83
84         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
85                 if (error != NULL) {
86                         g_printerr("%s\n", error->message);
87                         g_error_free(error);
88                 } else
89                         g_printerr("An unknown error occurred\n");
90                 return 1;
91         }
92
93         g_option_context_free(context);
94
95         if (argc < 2) {
96                 fprintf(stderr, "Missing argument\n");
97                 return 1;
98         }
99
100         web = g_web_new(index);
101         if (web == NULL) {
102                 fprintf(stderr, "Failed to create web service\n");
103                 return 1;
104         }
105
106         if (option_debug == TRUE)
107                 g_web_set_debug(web, web_debug, "WEB");
108
109         main_loop = g_main_loop_new(NULL, FALSE);
110
111         if (option_nameserver != NULL) {
112                 g_web_add_nameserver(web, option_nameserver);
113                 g_free(option_nameserver);
114         }
115
116         g_web_set_user_agent(web, "ConnMan/%s", VERSION);
117
118         timer = g_timer_new();
119
120         if (g_web_request(web, G_WEB_METHOD_GET, argv[1],
121                                         web_result, NULL) == 0) {
122                 fprintf(stderr, "Failed to start request\n");
123                 return 1;
124         }
125
126         memset(&sa, 0, sizeof(sa));
127         sa.sa_handler = sig_term;
128         sigaction(SIGINT, &sa, NULL);
129         sigaction(SIGTERM, &sa, NULL);
130
131         g_main_loop_run(main_loop);
132
133         g_timer_destroy(timer);
134
135         g_web_unref(web);
136
137         g_main_loop_unref(main_loop);
138
139         return 0;
140 }