2 * Copyright (C) 1999, 2001, 2002, 2003, 2004, 2006, 2009, 2010,
3 * 2011 Free Software Foundation, Inc.
4 * Copyright (C) 2014 Werner Koch
6 * This file is part of GnuPG.
8 * This file is free software; you can redistribute it and/or modify
9 * it under the terms of either
11 * - the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at
13 * your option) any later version.
17 * - the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * or both in parallel, as here.
23 * This file is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, see <https://www.gnu.org/licenses/>.
36 #include <sys/types.h>
49 # include <gnutls/gnutls.h> /* For init, logging, and deinit. */
50 #endif /*HTTP_USE_GNUTLS*/
59 /* read_dh_params (const char *fname) */
61 /* gpg_error_t err; */
67 /* gnutls_datum_t datum; */
69 /* fp = fopen (fname, "rb"); */
72 /* err = gpg_error_from_syserror (); */
73 /* log_fatal ("can't open '%s': %s\n", fname, gpg_strerror (err)); */
76 /* if (fstat (fileno(fp), &st)) */
78 /* err = gpg_error_from_syserror (); */
79 /* log_fatal ("can't stat '%s': %s\n", fname, gpg_strerror (err)); */
82 /* buflen = st.st_size; */
83 /* buf = xmalloc (buflen+1); */
84 /* if (fread (buf, buflen, 1, fp) != 1) */
86 /* err = gpg_error_from_syserror (); */
87 /* log_fatal ("error reading '%s': %s\n", fname, gpg_strerror (err)); */
91 /* datum.size = buflen; */
92 /* datum.data = buf; */
94 /* rc = gnutls_dh_params_import_pkcs3 (dh_params, &datum, GNUTLS_X509_FMT_PEM); */
96 /* log_fatal ("gnutls_dh_param_import failed: %s\n", gnutls_strerror (rc)); */
105 verify_callback (http_t hd, http_session_t session, int reserved)
109 return no_verify? 0 : http_verify_server_credentials (session);
115 my_gnutls_log (int level, const char *text)
117 fprintf (stderr, "gnutls:L%d: %s", level, text);
121 /* Prepend FNAME with the srcdir environment variable's value and
122 return an allocated filename. */
124 prepend_srcdir (const char *fname)
126 static const char *srcdir;
129 if (!srcdir && !(srcdir = getenv ("srcdir")))
132 result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
133 strcpy (result, srcdir);
134 strcat (result, "/");
135 strcat (result, fname);
141 main (int argc, char **argv)
150 unsigned int my_http_flags = 0;
153 const char *cafile = NULL;
154 http_session_t session = NULL;
157 log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_PID);
160 while (argc && last_argc != argc )
163 if (!strcmp (*argv, "--"))
168 else if (!strcmp (*argv, "--help"))
170 fputs ("usage: " PGM " URL\n"
172 " --verbose print timings etc.\n"
173 " --debug flyswatter\n"
174 " --gnutls-debug N use GNUTLS debug level N\n"
175 " --cacert FNAME expect CA certificate in file FNAME\n"
176 " --no-verify do not verify the certificate\n"
177 " --force-tls use HTTP_FLAG_FORCE_TLS\n"
178 " --force-tor use HTTP_FLAG_FORCE_TOR\n"
179 " --no-out do not print the content\n",
183 else if (!strcmp (*argv, "--verbose"))
188 else if (!strcmp (*argv, "--debug"))
194 else if (!strcmp (*argv, "--gnutls-debug"))
199 tls_dbg = atoi (*argv);
203 else if (!strcmp (*argv, "--cacert"))
212 else if (!strcmp (*argv, "--no-verify"))
217 else if (!strcmp (*argv, "--force-tls"))
219 my_http_flags |= HTTP_FLAG_FORCE_TLS;
222 else if (!strcmp (*argv, "--force-tor"))
224 my_http_flags |= HTTP_FLAG_FORCE_TOR;
227 else if (!strcmp (*argv, "--no-out"))
232 else if (!strncmp (*argv, "--", 2))
234 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
240 fprintf (stderr, PGM ": no or too many URLS given\n");
245 cafile = prepend_srcdir ("tls-ca.pem");
247 /* http.c makes use of the assuan socket wrapper. */
254 ntbtls_set_debug (tls_dbg, NULL, NULL);
256 #elif HTTP_USE_GNUTLS
258 rc = gnutls_global_init ();
260 log_error ("gnutls_global_init failed: %s\n", gnutls_strerror (rc));
262 http_register_tls_callback (verify_callback);
263 http_register_tls_ca (cafile);
265 err = http_session_new (&session, NULL, NULL, HTTP_FLAG_TRUST_DEF);
267 log_error ("http_session_new failed: %s\n", gpg_strerror (err));
269 /* rc = gnutls_dh_params_init(&dh_params); */
271 /* log_error ("gnutls_dh_params_init failed: %s\n", gnutls_strerror (rc)); */
272 /* read_dh_params ("dh_param.pem"); */
274 /* rc = gnutls_certificate_set_x509_trust_file */
275 /* (certcred, "ca.pem", GNUTLS_X509_FMT_PEM); */
277 /* log_error ("gnutls_certificate_set_x509_trust_file failed: %s\n", */
278 /* gnutls_strerror (rc)); */
280 /* gnutls_certificate_set_dh_params (certcred, dh_params); */
282 gnutls_global_set_log_function (my_gnutls_log);
284 gnutls_global_set_log_level (tls_dbg);
286 #endif /*HTTP_USE_GNUTLS*/
288 rc = http_parse_uri (&uri, *argv, 1);
291 log_error ("'%s': %s\n", *argv, gpg_strerror (rc));
295 printf ("Scheme: %s\n", uri->scheme);
297 printf ("Value : %s\n", uri->path);
300 printf ("Auth : %s\n", uri->auth? uri->auth:"[none]");
301 printf ("Host : %s\n", uri->host);
302 printf ("Port : %u\n", uri->port);
303 printf ("Path : %s\n", uri->path);
304 for (r = uri->params; r; r = r->next)
306 printf ("Params: %s", r->name);
309 printf ("=%s", r->value);
310 if (strlen (r->value) != r->valuelen)
311 printf (" [real length=%d]", (int) r->valuelen);
315 for (r = uri->query; r; r = r->next)
317 printf ("Query : %s", r->name);
320 printf ("=%s", r->value);
321 if (strlen (r->value) != r->valuelen)
322 printf (" [real length=%d]", (int) r->valuelen);
326 printf ("Flags :%s%s%s%s\n",
327 uri->is_http? " http":"",
328 uri->opaque? " opaque":"",
329 uri->v6lit? " v6lit":"",
330 uri->onion? " onion":"");
331 printf ("TLS : %s\n",
333 (my_http_flags&HTTP_FLAG_FORCE_TLS)? "forced" : "no");
334 printf ("Tor : %s\n",
335 (my_http_flags&HTTP_FLAG_FORCE_TOR)? "yes" : "no");
339 http_release_parsed_uri (uri);
342 rc = http_open_document (&hd, *argv, NULL, my_http_flags,
343 NULL, session, NULL, NULL);
346 log_error ("can't get '%s': %s\n", *argv, gpg_strerror (rc));
349 log_info ("open_http_document succeeded; status=%u\n",
350 http_get_status_code (hd));
356 names = http_get_header_names (hd);
358 log_fatal ("http_get_header_names failed: %s\n",
359 gpg_strerror (gpg_error_from_syserror ()));
360 for (i = 0; names[i]; i++)
361 printf ("HDR: %s: %s\n", names[i], http_get_header (hd, names[i]));
366 switch (http_get_status_code (hd))
374 unsigned long count = 0;
375 while ((c = es_getc (http_get_read_ptr (hd))) != EOF)
381 log_info ("Received bytes: %lu\n", count);
387 log_info ("Redirected to: %s\n", http_get_header (hd, "Location"));
392 http_session_release (session);
393 #ifdef HTTP_USE_GNUTLS
394 gnutls_global_deinit ();
395 #endif /*HTTP_USE_GNUTLS*/