~ubuntu-branches/ubuntu/vivid/mediatomb/vivid

« back to all changes in this revision

Viewing changes to src/url.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-03-02 13:09:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080302130916-zlljdze3kt7vuq4b
Tags: 0.11.0-1
* New upstream release.
* Include message about which inotify headers will be used when enabling
  inotify runtime support.
* Fixed error with use of INTERFACE in init script. Also removed use of -m
  option.
* Including new config.xml options.
* Added more build dependencies for new upstream release.
* Removed build dependency of libid3-dev, taglib is now preferred.
* mediatomb.xpm and manpage.xml is now included in orig tarball.
* inotify patch is not needed anymore.
* md5 patch has been committed upstream and is no longer needed. Also removed
  README.Debian.
* TwinHelix PNG fix is now used. Removed from TODO.
* Adding dependency of iceweasel for mediatomb package.
* Updated copyright file.
* Updated watch file.
* Updated rules file for proper configure options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    url.cc - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: url.cc 1714 2008-03-01 17:33:21Z lww $
 
28
*/
 
29
 
 
30
/// \file url.cc
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
    #include "autoconfig.h"
 
34
#endif
 
35
 
 
36
#ifdef HAVE_CURL
 
37
 
 
38
#include <pthread.h>
 
39
#include "common.h"
 
40
#include "rexp.h"
 
41
#include "url.h"
 
42
#include "tools.h"
 
43
 
 
44
using namespace zmm;
 
45
 
 
46
URL::URL(size_t buffer_hint)
 
47
{
 
48
    this->buffer_hint = buffer_hint;
 
49
}
 
50
 
 
51
Ref<StringBuffer> URL::download(String URL, long *HTTP_retcode, 
 
52
                                CURL *curl_handle, bool only_header, 
 
53
                                bool verbose, bool redirect)
 
54
{
 
55
    CURLcode res;
 
56
    bool cleanup = false;
 
57
    char error_buffer[CURL_ERROR_SIZE] = {'\0'};
 
58
 
 
59
    if (curl_handle == NULL)
 
60
    {
 
61
        curl_handle = curl_easy_init();
 
62
        cleanup = true;
 
63
        if (curl_handle == NULL)
 
64
            throw _Exception(_("Invalid curl handle!\n"));
 
65
    }
 
66
 
 
67
    Ref<StringBuffer> buffer(new StringBuffer(buffer_hint));
 
68
 
 
69
    curl_easy_reset(curl_handle);
 
70
    if (verbose)
 
71
        curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
 
72
 
 
73
    curl_easy_setopt(curl_handle, CURLOPT_URL, URL.c_str());
 
74
    curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
 
75
 
 
76
    if (only_header)
 
77
    {
 
78
        curl_easy_setopt(curl_handle, CURLOPT_NOBODY);
 
79
        curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, URL::dl);
 
80
        curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, 
 
81
                         (void *)buffer.getPtr());
 
82
    }
 
83
    else
 
84
    {
 
85
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, URL::dl);
 
86
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, 
 
87
                         (void *)buffer.getPtr());
 
88
    }
 
89
 
 
90
    if (redirect)
 
91
    {
 
92
        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
 
93
        curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, -1);
 
94
    }
 
95
 
 
96
    res = curl_easy_perform(curl_handle);
 
97
    if (res != CURLE_OK)
 
98
    {
 
99
        log_error("%s\n", error_buffer);
 
100
        if (cleanup)
 
101
            curl_easy_cleanup(curl_handle);
 
102
        throw _Exception(String(error_buffer));
 
103
    }
 
104
 
 
105
    res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, HTTP_retcode);
 
106
    if (res != CURLE_OK)
 
107
    {
 
108
        log_error("%s\n", error_buffer);
 
109
        if (cleanup)
 
110
            curl_easy_cleanup(curl_handle);
 
111
        throw _Exception(String(error_buffer));
 
112
    }
 
113
 
 
114
    if (cleanup)
 
115
        curl_easy_cleanup(curl_handle);
 
116
 
 
117
    return buffer;
 
118
}
 
119
 
 
120
Ref<URL::Stat> URL::getInfo(String URL, CURL *curl_handle)
 
121
{
 
122
    long retcode;
 
123
    bool cleanup = false;
 
124
    CURLcode res;
 
125
    double cl;
 
126
    char *ct;
 
127
    char error_buffer[CURL_ERROR_SIZE] = {'\0'};
 
128
    String mt;
 
129
 
 
130
    if (curl_handle == NULL)
 
131
    {
 
132
        curl_handle = curl_easy_init();
 
133
        cleanup = true;
 
134
        if (curl_handle == NULL)
 
135
            throw _Exception(_("Invalid curl handle!\n"));
 
136
    }
 
137
 
 
138
    Ref<StringBuffer> buffer = download(URL, &retcode, curl_handle, true);
 
139
    if (retcode != 200)
 
140
    {
 
141
        if (cleanup)
 
142
            curl_easy_cleanup(curl_handle);
 
143
        throw _Exception(_("Error retrieving information from ") +
 
144
                          URL + _(" HTTP return code: ") + 
 
145
                          String::from(retcode));
 
146
    }
 
147
/*    
 
148
    Ref<RExp> getMT(new RExp());
 
149
    
 
150
    try
 
151
    {
 
152
        getMT->compile(_("\nContent-Type: ([^\n]+)\n"), REG_ICASE);
 
153
    }
 
154
    catch (Exception ex)
 
155
    {
 
156
        if (cleanup)
 
157
            curl_easy_cleanup(curl_handle);
 
158
 
 
159
        throw ex;
 
160
    }
 
161
 
 
162
    Ref<Matcher> matcher = getMT->matcher(buffer->toString());
 
163
    String mt;
 
164
    if (matcher->next())
 
165
        mt = trim_string(matcher->group(1));
 
166
    else
 
167
        mt = _(MIMETYPE_DEFAULT);
 
168
 
 
169
    log_debug("Extracted content type: %s\n", mt.c_str());
 
170
 
 
171
    Ref<RExp> getCL(new RExp());
 
172
 
 
173
    try
 
174
    {
 
175
        getCL->compile(_("\nContent-Length: ([^\n]+)\n"), REG_ICASE);
 
176
    }
 
177
    catch (Exception ex)
 
178
    {
 
179
        if (cleanup)
 
180
            curl_easy_cleanup(curl_handle);
 
181
 
 
182
        throw ex;
 
183
    }
 
184
 
 
185
    matcher = getCL->matcher(buffer->toString());
 
186
    off_t cl;
 
187
    if (matcher->next())
 
188
        cl = trim_string(matcher->group(1)).toOFF_T();
 
189
    else
 
190
        cl = -1;
 
191
*/
 
192
    res = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
 
193
    if (res != CURLE_OK)
 
194
    {
 
195
        log_error("%s\n", error_buffer);
 
196
        if (cleanup)
 
197
            curl_easy_cleanup(curl_handle);
 
198
        throw _Exception(String(error_buffer));
 
199
    }
 
200
 
 
201
    res = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &ct);
 
202
    if (res != CURLE_OK)
 
203
    {
 
204
        log_error("%s\n", error_buffer);
 
205
        if (cleanup)
 
206
            curl_easy_cleanup(curl_handle);
 
207
        throw _Exception(String(error_buffer));
 
208
    }
 
209
 
 
210
    if (ct == NULL)
 
211
        mt = _(MIMETYPE_DEFAULT);
 
212
    else
 
213
        mt = String(ct);
 
214
    
 
215
    log_debug("Extracted content length: %ld\n", cl);
 
216
 
 
217
    Ref<Stat> st(new Stat((off_t)cl, mt));
 
218
 
 
219
    if (cleanup)
 
220
        curl_easy_cleanup(curl_handle);
 
221
 
 
222
    return st;
 
223
}
 
224
 
 
225
 
 
226
size_t URL::dl(void *buf, size_t size, size_t nmemb, void *data)
 
227
{
 
228
    StringBuffer *buffer = (StringBuffer *)data;
 
229
    if (buffer == NULL)
 
230
        return 0;
 
231
 
 
232
    size_t s = size * nmemb;
 
233
    *buffer << String((char *)buf, s);
 
234
 
 
235
    return s;
 
236
}
 
237
 
 
238
#endif//HAVE_CURL
 
239