Add misc/url.* with m_url_(un)escaped_form
[libquvi.git] / src / curl / http_metainfo.c
blob7497f03f2b0e76885b89f79bd704b020ac13d99e
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library 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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib/gi18n-lib.h>
24 #include <glib.h>
25 #include <curl/curl.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_media_s.h"
31 #include "_quvi_net_s.h"
32 /* -- */
33 #include "curl/autoproxy.h"
34 #include "curl/temp.h"
36 static void _set_opts(_quvi_net_t n, _c_temp_t t, CURL *c)
38 typedef curl_write_callback cwc;
40 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) c_temp_wrcb);
41 curl_easy_setopt(c, CURLOPT_URL, n->url.addr->str);
42 curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
43 curl_easy_setopt(c, CURLOPT_NOBODY, 1L); /* GET -> HEAD */
45 c_autoproxy(n->handle.quvi, n->url.addr->str);
48 static const gchar *_EOK = N_("The server responded with the code %03ld");
50 static QuviError _http_metainfo(_quvi_net_t n, CURL *c)
52 CURLcode curlcode;
53 QuviError rc;
55 curlcode = curl_easy_perform(c);
57 curl_easy_setopt(c, CURLOPT_HTTPGET, 1L); /* HEAD -> GET */
58 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &n->status.resp_code);
60 rc = QUVI_OK;
62 if (curlcode == CURLE_OK)
64 if (n->status.resp_code == 200 || n->status.resp_code == 206)
66 gdouble *l = &n->http_metainfo.content_length;
67 gchar *s = NULL;
69 curl_easy_getinfo(c, CURLINFO_CONTENT_TYPE, &s);
70 curl_easy_getinfo(c, CURLINFO_CONTENT_LENGTH_DOWNLOAD, l);
72 g_string_assign(n->http_metainfo.content_type, s);
74 else
76 g_string_printf(n->status.errmsg,
77 g_dgettext(GETTEXT_PACKAGE, _EOK),
78 n->status.resp_code);
79 rc = QUVI_ERROR_CALLBACK;
82 else
84 const gchar *s = curl_easy_strerror(curlcode);
85 const gint r = n->status.resp_code;
86 const gint c = curlcode;
87 #define _ENO "%s (HTTP/%03d, cURL=0x%03x)"
88 g_string_printf(n->status.errmsg, _ENO, s, r, c);
89 #undef _ENO
90 rc = QUVI_ERROR_CALLBACK;
92 return (rc);
95 QuviError c_http_metainfo(_quvi_t q, _quvi_net_t n)
97 QuviError rc;
98 _c_temp_t t;
99 CURL *c;
101 c = q->handle.curl;
102 t = c_temp_new();
104 _set_opts(n, t, c);
105 rc = _http_metainfo(n, c);
107 c_temp_free(t);
108 t = NULL;
110 return (rc);
113 /* vim: set ts=2 sw=2 tw=72 expandtab: */