55c58af5a7dc2c4e4b7cb2dcd811f5df3e2d3785
[platform/upstream/connman.git] / tools / web-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 bool web_result(GWebResult *result, gpointer user_data)
47 {
48         const guint8 *chunk;
49         gsize length;
50         guint16 status;
51         gdouble elapsed;
52
53         g_web_result_get_chunk(result, &chunk, &length);
54
55         if (length > 0) {
56                 printf("%s\n", (char *) chunk);
57                 return true;
58         }
59
60         status = g_web_result_get_status(result);
61
62         g_print("status: %03u\n", status);
63
64         elapsed = g_timer_elapsed(timer, NULL);
65
66         g_print("elapse: %f seconds\n", elapsed);
67
68         g_main_loop_quit(main_loop);
69
70         return false;
71 }
72
73 static bool option_debug = false;
74 static gchar *option_proxy = NULL;
75 static gchar *option_nameserver = NULL;
76 static gchar *option_user_agent = NULL;
77 static gchar *option_http_version = NULL;
78
79 static GOptionEntry options[] = {
80         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
81                                         "Enable debug output" },
82         { "proxy", 'p', 0, G_OPTION_ARG_STRING, &option_proxy,
83                                         "Specify proxy", "ADDRESS" },
84         { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
85                                         "Specify nameserver", "ADDRESS" },
86         { "user-agent", 'A', 0, G_OPTION_ARG_STRING, &option_user_agent,
87                                         "Specific user agent", "STRING" },
88         { "http-version", 'H', 0, G_OPTION_ARG_STRING, &option_http_version,
89                                         "Specific HTTP version", "STRING" },
90         { NULL },
91 };
92
93 int main(int argc, char *argv[])
94 {
95         GOptionContext *context;
96         GError *error = NULL;
97         struct sigaction sa;
98         GWeb *web;
99         int index = 0;
100
101         context = g_option_context_new(NULL);
102         g_option_context_add_main_entries(context, options, NULL);
103
104         if (!g_option_context_parse(context, &argc, &argv, &error)) {
105                 if (error) {
106                         g_printerr("%s\n", error->message);
107                         g_error_free(error);
108                 } else
109                         g_printerr("An unknown error occurred\n");
110                 return 1;
111         }
112
113         g_option_context_free(context);
114
115         if (argc < 2) {
116                 fprintf(stderr, "Missing argument\n");
117                 return 1;
118         }
119
120         web = g_web_new(index);
121         if (!web) {
122                 fprintf(stderr, "Failed to create web service\n");
123                 return 1;
124         }
125
126         if (option_debug)
127                 g_web_set_debug(web, web_debug, "WEB");
128
129         main_loop = g_main_loop_new(NULL, FALSE);
130
131         if (option_proxy) {
132                 g_web_set_proxy(web, option_proxy);
133                 g_free(option_proxy);
134         }
135
136         if (option_nameserver) {
137                 g_web_add_nameserver(web, option_nameserver);
138                 g_free(option_nameserver);
139         }
140
141         if (option_user_agent) {
142                 g_web_set_user_agent(web, "%s", option_user_agent);
143                 g_free(option_user_agent);
144         }
145
146         if (option_http_version) {
147                 g_web_set_http_version(web, option_http_version);
148                 g_free(option_http_version);
149         }
150
151         timer = g_timer_new();
152
153         if (g_web_request_get(web, argv[1], web_result, NULL,  NULL) == 0) {
154                 fprintf(stderr, "Failed to start request\n");
155                 return 1;
156         }
157
158         memset(&sa, 0, sizeof(sa));
159         sa.sa_handler = sig_term;
160         sigaction(SIGINT, &sa, NULL);
161         sigaction(SIGTERM, &sa, NULL);
162
163         g_main_loop_run(main_loop);
164
165         g_timer_destroy(timer);
166
167         g_web_unref(web);
168
169         g_main_loop_unref(main_loop);
170
171         return 0;
172 }