5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <gweb/gweb.h>
34 static GMainLoop *main_loop;
36 static void web_debug(const char *str, void *data)
38 g_print("%s: %s\n", (const char *) data, str);
41 static void sig_term(int sig)
43 g_main_loop_quit(main_loop);
46 static gboolean web_result(GWebResult *result, gpointer user_data)
53 g_web_result_get_chunk(result, &chunk, &length);
56 printf("%s\n", (char *) chunk);
60 status = g_web_result_get_status(result);
62 g_print("status: %03u\n", status);
64 elapsed = g_timer_elapsed(timer, NULL);
66 g_print("elapse: %f seconds\n", elapsed);
68 g_main_loop_quit(main_loop);
73 static gboolean option_debug = FALSE;
74 static gchar *option_nameserver = NULL;
75 static gchar *option_user_agent = NULL;
76 static gchar *option_http_version = NULL;
78 static GOptionEntry options[] = {
79 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
80 "Enable debug output" },
81 { "nameserver", 'n', 0, G_OPTION_ARG_STRING, &option_nameserver,
82 "Specify nameserver", "ADDRESS" },
83 { "user-agent", 'A', 0, G_OPTION_ARG_STRING, &option_user_agent,
84 "Specific user agent", "STRING" },
85 { "http-version", 'H', 0, G_OPTION_ARG_STRING, &option_http_version,
86 "Specific HTTP version", "STRING" },
90 int main(int argc, char *argv[])
92 GOptionContext *context;
98 context = g_option_context_new(NULL);
99 g_option_context_add_main_entries(context, options, NULL);
101 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
103 g_printerr("%s\n", error->message);
106 g_printerr("An unknown error occurred\n");
110 g_option_context_free(context);
113 fprintf(stderr, "Missing argument\n");
117 web = g_web_new(index);
119 fprintf(stderr, "Failed to create web service\n");
123 if (option_debug == TRUE)
124 g_web_set_debug(web, web_debug, "WEB");
126 main_loop = g_main_loop_new(NULL, FALSE);
128 if (option_nameserver != NULL) {
129 g_web_add_nameserver(web, option_nameserver);
130 g_free(option_nameserver);
133 if (option_user_agent != NULL) {
134 g_web_set_user_agent(web, "%s", option_user_agent);
135 g_free(option_user_agent);
138 if (option_http_version != NULL) {
139 g_web_set_http_version(web, option_http_version);
140 g_free(option_http_version);
143 timer = g_timer_new();
145 if (g_web_request_get(web, argv[1], web_result, NULL) == 0) {
146 fprintf(stderr, "Failed to start request\n");
150 memset(&sa, 0, sizeof(sa));
151 sa.sa_handler = sig_term;
152 sigaction(SIGINT, &sa, NULL);
153 sigaction(SIGTERM, &sa, NULL);
155 g_main_loop_run(main_loop);
157 g_timer_destroy(timer);
161 g_main_loop_unref(main_loop);