summaryrefslogtreecommitdiff
path: root/examples/get.c
blob: a03404d993830985aab9c9e7277e9a9e74ea553e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2003, Ximian, Inc.
 * Copyright (C) 2013 Igalia, S.L.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <libsoup/soup.h>

static SoupSession *session;
static GMainLoop *loop;
static gboolean debug, head, quiet;
static const gchar *output_file_path = NULL;

static void
finished (SoupSession *session, SoupMessage *msg, gpointer loop)
{
	g_main_loop_quit (loop);
}

static void
get_url (const char *url)
{
	const char *name;
	SoupMessage *msg;
	const char *header;
	FILE *output_file = NULL;

	msg = soup_message_new (head ? "HEAD" : "GET", url);
	soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);

        g_object_ref (msg);
        soup_session_queue_message (session, msg, finished, loop);
        g_main_loop_run (loop);

	name = soup_message_get_uri (msg)->path;

	if (!debug) {
		if (soup_message_get_status (msg) == SOUP_STATUS_SSL_FAILED) {
			GTlsCertificateFlags flags;

			if (soup_message_get_https_status (msg, NULL, &flags))
				g_print ("%s: %d %s (0x%x)\n", name, soup_message_get_status (msg), soup_message_get_reason_phrase (msg), flags);
			else
				g_print ("%s: %d %s (no handshake status)\n", name, soup_message_get_status (msg), soup_message_get_reason_phrase (msg));
		} else if (!quiet || SOUP_STATUS_IS_TRANSPORT_ERROR (soup_message_get_status (msg)))
			g_print ("%s: %d %s\n", name, soup_message_get_status (msg), soup_message_get_reason_phrase (msg));
	}

	if (SOUP_STATUS_IS_REDIRECTION (soup_message_get_status (msg))) {
		header = soup_message_headers_get_one (soup_message_get_response_headers (msg),
						       "Location");
		if (header) {
			SoupURI *uri;
			char *uri_string;

			if (!debug && !quiet)
				g_print ("  -> %s\n", header);

			uri = soup_uri_new_with_base (soup_message_get_uri (msg), header);
			uri_string = soup_uri_to_string (uri, FALSE);
			get_url (uri_string);
			g_free (uri_string);
			soup_uri_free (uri);
		}
	} else if (!head && SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) {
		if (output_file_path) {
			output_file = fopen (output_file_path, "w");
			if (!output_file)
				g_printerr ("Error trying to create file %s.\n", output_file_path);
		} else if (!quiet)
			output_file = stdout;

		if (output_file) {
			fwrite (msg->response_body->data,
				1,
				msg->response_body->length,
				output_file);

			if (output_file_path)
				fclose (output_file);
		}
	}
	g_object_unref (msg);
}

/* Inline class for providing a pre-configured client certificate */
typedef struct _GetTlsCertInteraction        GetTlsCertInteraction;
typedef struct _GetTlsCertInteractionClass   GetTlsCertInteractionClass;

static GType                    _get_tls_cert_interaction_get_type    (void) G_GNUC_CONST;
static GetTlsCertInteraction *  _get_tls_cert_interaction_new         (GTlsCertificate *cert);

struct _GetTlsCertInteraction
{
	GTlsInteraction parent_instance;
	GTlsCertificate *cert;
};

struct _GetTlsCertInteractionClass
{
	GTlsInteractionClass parent_class;
};

G_DEFINE_TYPE (GetTlsCertInteraction, _get_tls_cert_interaction, G_TYPE_TLS_INTERACTION);

static GTlsInteractionResult
request_certificate (GTlsInteraction              *interaction,
                     GTlsConnection               *connection,
                     GTlsCertificateRequestFlags   flags,
                     GCancellable                 *cancellable,
                     GError                      **error)
{
	GetTlsCertInteraction *self = (GetTlsCertInteraction*)interaction;
	g_tls_connection_set_certificate (connection, self->cert);
	return G_TLS_INTERACTION_HANDLED;
}

static void
_get_tls_cert_interaction_init (GetTlsCertInteraction *interaction)
{
}

static void
_get_tls_cert_interaction_class_init (GetTlsCertInteractionClass *klass)
{
	GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
	interaction_class->request_certificate = request_certificate;
}

GetTlsCertInteraction *
_get_tls_cert_interaction_new (GTlsCertificate *cert)
{
	GetTlsCertInteraction *self = g_object_new (_get_tls_cert_interaction_get_type (), NULL);
	self->cert = g_object_ref (cert);
	return self;
}

static const char *ca_file, *proxy;
static char *client_cert_file, *client_key_file;
static gboolean ntlm;
static gboolean negotiate;

static GOptionEntry entries[] = {
	{ "ca-file", 'c', 0,
	  G_OPTION_ARG_STRING, &ca_file,
	  "Use FILE as the TLS CA file", "FILE" },
	{ "cert", 0, 0,
	  G_OPTION_ARG_STRING, &client_cert_file,
	  "Use FILE as the TLS client certificate file", "FILE" },
	{ "key", 0, 0,
	  G_OPTION_ARG_STRING, &client_key_file,
	  "Use FILE as the TLS client key file", "FILE" },
	{ "debug", 'd', 0,
	  G_OPTION_ARG_NONE, &debug,
	  "Show HTTP headers", NULL },
	{ "head", 'h', 0,
	  G_OPTION_ARG_NONE, &head,
	  "Do HEAD rather than GET", NULL },
	{ "ntlm", 'n', 0,
	  G_OPTION_ARG_NONE, &ntlm,
	  "Use NTLM authentication", NULL },
	{ "output", 'o', 0,
	  G_OPTION_ARG_STRING, &output_file_path,
	  "Write the received data to FILE instead of stdout", "FILE" },
	{ "proxy", 'p', 0,
	  G_OPTION_ARG_STRING, &proxy,
	  "Use URL as an HTTP proxy", "URL" },
	{ "quiet", 'q', 0,
	  G_OPTION_ARG_NONE, &quiet,
	  "Don't show HTTP status code", NULL },
	{ NULL }
};

static GOptionEntry negotiate_entries[] = {
	{ "negotiate", 'N', 0,
	  G_OPTION_ARG_NONE, &negotiate,
	  "Use Negotiate authentication", NULL },
	{ NULL }
};

int
main (int argc, char **argv)
{
	GOptionContext *opts;
	const char *url;
	SoupURI *proxy_uri, *parsed;
	GError *error = NULL;
	SoupLogger *logger = NULL;
	char *help;

	opts = g_option_context_new (NULL);
	g_option_context_add_main_entries (opts, entries, NULL);
	if (soup_auth_negotiate_supported())
		g_option_context_add_main_entries (opts, negotiate_entries, NULL);
	if (!g_option_context_parse (opts, &argc, &argv, &error)) {
		g_printerr ("Could not parse arguments: %s\n",
			    error->message);
		g_printerr ("%s",
			    g_option_context_get_help (opts, TRUE, NULL));
		exit (1);
	}

	if (argc != 2) {
		help = g_option_context_get_help (opts, TRUE, NULL);
		g_printerr ("%s", help);
		g_free (help);
		exit (1);
	}
	g_option_context_free (opts);

	url = argv[1];
	parsed = soup_uri_new (url);
	if (!parsed) {
		g_printerr ("Could not parse '%s' as a URL\n", url);
		exit (1);
	}
	soup_uri_free (parsed);

	session = g_object_new (SOUP_TYPE_SESSION,
				"user-agent", "get ",
				"accept-language-auto", TRUE,
				NULL);
        soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_DECODER);
        soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
	if (ntlm)
		soup_session_add_feature_by_type (session, SOUP_TYPE_AUTH_NTLM);
	if (ca_file)
		g_object_set (session, "ssl-ca-file", ca_file, NULL);

	if (client_cert_file) {
		GTlsCertificate *client_cert;
		GetTlsCertInteraction *interaction;
		if (!client_key_file) {
			g_printerr ("--key is required with --cert\n");
			exit (1);
		}
		client_cert = g_tls_certificate_new_from_files (client_cert_file, client_key_file, &error);
		if (!client_cert) {
			g_printerr ("%s\n", error->message);
			exit (1);
		}
		interaction = _get_tls_cert_interaction_new (client_cert);
		g_object_set (session, "tls-interaction", interaction, NULL);
	}

	if (debug) {
		logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
		soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
		g_object_unref (logger);
	}

	if (proxy) {
		GProxyResolver *resolver;
		proxy_uri = soup_uri_new (proxy);
		if (!proxy_uri) {
			g_printerr ("Could not parse '%s' as URI\n",
				    proxy);
			exit (1);
		}

		resolver = g_simple_proxy_resolver_new (proxy, NULL);
		g_object_set (G_OBJECT (session),
			      "proxy-resolver", resolver,
			      NULL);
		soup_uri_free (proxy_uri);
		g_object_unref (resolver);
	}

#ifdef LIBSOUP_HAVE_GSSAPI
	if (negotiate) {
		soup_session_add_feature_by_type (session,
						  SOUP_TYPE_AUTH_NEGOTIATE);
	}
#endif /* LIBSOUP_HAVE_GSSAPI */

	loop = g_main_loop_new (NULL, TRUE);

	get_url (url);

	g_main_loop_unref (loop);

	g_object_unref (session);

	return 0;
}