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/>.
23 #include <glib/gi18n-lib.h>
30 #include "_quvi_http_metainfo_s.h"
31 #include "_quvi_net_s.h"
32 #include "_quvi_macro.h"
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
)
48 n
= n_new(q
, qmi
->url
.input
->str
);
50 if (q
->cb
.http_metainfo
!= NULL
)
51 rc
= q
->cb
.http_metainfo(n
);
53 rc
= c_http_metainfo(q
, n
); /* Query using cURL (default). */
57 rc
= l_exec_util_to_file_ext(q
, n
->http_metainfo
.content_type
->str
,
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
;
76 /* Save returned error message. */
77 if (n
->status
.errmsg
->len
>0)
78 g_string_assign(q
->status
.errmsg
, n
->status
.errmsg
->str
);
81 g_string_assign(q
->status
.errmsg
,
82 "unknown error: http_metainfo: callback returned "
87 q
->status
.resp_code
= n
->status
.resp_code
;
93 QuviError
n_http_metainfo(_quvi_http_metainfo_t qmi
)
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;
110 return (QUVI_OK
); /* Skip process. */
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: */