libconvenience_net: Pass userdata with status callback
[libquvi.git] / src / net / http_metainfo.c
blobbb90d3623df988905c0b696c1ddab52ce0a2b17f
1 /* libquvi
2 * Copyright (C) 2012,2013 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 <lauxlib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_http_metainfo_s.h"
31 #include "_quvi_net_s.h"
32 #include "_quvi_macro.h"
33 /* -- */
34 #include "net/handle.h"
36 extern QuviError l_exec_util_to_file_ext(_quvi_t, const gchar*, GString*);
37 extern QuviError c_http_metainfo(_quvi_t, _quvi_net_t);
39 static const gdouble MIN_LIKELY_MEDIA_LENGTH = 50*1024;
41 static QuviError _http_metainfo(_quvi_http_metainfo_t qmi)
43 _quvi_net_t n;
44 QuviError rc;
45 _quvi_t q;
47 q = qmi->handle.quvi;
48 n = n_new(q, qmi->url.input->str);
50 if (q->cb.http_metainfo != NULL)
51 rc = q->cb.http_metainfo(n);
52 else
53 rc = c_http_metainfo(q, n); /* Query using cURL (default). */
55 if (rc == QUVI_OK)
57 rc = l_exec_util_to_file_ext(q, n->http_metainfo.content_type->str,
58 qmi->file_ext);
59 if (rc == QUVI_OK)
61 g_string_assign(qmi->content_type, n->http_metainfo.content_type->str);
62 qmi->length_bytes = n->http_metainfo.content_length;
65 if (q->cb.status != NULL)
67 const glong p = q_makelong(QUVI_CALLBACK_STATUS_HTTP_QUERY_METAINFO,
68 QUVI_CALLBACK_STATUS_DONE);
70 if (q->cb.status(p, 0, q->cb.userdata.status) != QUVI_OK)
71 rc = QUVI_ERROR_CALLBACK_ABORTED;
74 else
76 /* Save returned error message. */
77 if (n->status.errmsg->len >0)
78 g_string_assign(q->status.errmsg, n->status.errmsg->str);
79 else
81 g_string_assign(q->status.errmsg,
82 "unknown error: http_metainfo: callback returned "
83 "an empty errmsg");
87 q->status.resp_code = n->status.resp_code;
88 n_free(n);
90 return (rc);
93 QuviError n_http_metainfo(_quvi_http_metainfo_t qmi)
95 _quvi_t q;
96 gchar *s;
98 q = qmi->handle.quvi;
99 s = g_uri_parse_scheme(qmi->url.input->str);
101 if (s != NULL) /* HTTP(S) only. */
103 const gboolean r = (gboolean) g_strcmp0(s, "http") == 0
104 || g_strcmp0(s, "https") == 0;
106 g_free(s);
107 s = NULL;
109 if (r == FALSE)
110 return (QUVI_OK); /* Skip process. */
112 else
114 g_string_printf(q->status.errmsg,
115 _("Failed to parse URL: %s"), qmi->url.input->str);
116 return (QUVI_ERROR_INVALID_ARG); /* Skip process. */
119 if (q->cb.status != NULL)
121 const glong p = q_makelong(QUVI_CALLBACK_STATUS_HTTP_QUERY_METAINFO, 0);
123 if (q->cb.status(p, 0, q->cb.userdata.status) != QUVI_OK)
124 return (QUVI_ERROR_CALLBACK_ABORTED);
127 return (_http_metainfo(qmi));
130 /* vim: set ts=2 sw=2 tw=72 expandtab: */