~vanvugt/+junk/mediatomb

« back to all changes in this revision

Viewing changes to src/url_request_handler.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_request_handler.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_request_handler.cc 1723 2008-03-01 22:34:27Z jin_eld $
 
28
*/
 
29
 
 
30
/// \file url_request_handler.cc
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
    #include "autoconfig.h"
 
34
#endif
 
35
 
 
36
#ifdef HAVE_CURL
 
37
 
 
38
#include "server.h"
 
39
#include "common.h"
 
40
#include "storage.h"
 
41
#include "ixml.h"
 
42
#include "buffered_io_handler.h"
 
43
#include "dictionary.h"
 
44
#include "url_request_handler.h"
 
45
#include "cds_objects.h"
 
46
#ifdef ONLINE_SERVICES
 
47
    #include "online_service_helper.h"
 
48
#endif
 
49
#include "url.h"
 
50
#include "curl_io_handler.h"
 
51
#ifdef EXTERNAL_TRANSCODING
 
52
    #include "transcoding/transcode_dispatcher.h"
 
53
#endif
 
54
 
 
55
using namespace zmm;
 
56
using namespace mxml;
 
57
 
 
58
URLRequestHandler::URLRequestHandler() : RequestHandler()
 
59
{
 
60
}
 
61
 
 
62
void URLRequestHandler::get_info(IN const char *filename, OUT struct File_Info *info)
 
63
{
 
64
    log_debug("start\n");
 
65
 
 
66
    String mimeType;
 
67
    int objectID;
 
68
#ifdef EXTERNAL_TRANSCODING
 
69
    String tr_profile;
 
70
#endif
 
71
 
 
72
    String url, parameters;
 
73
    split_url(filename, URL_PARAM_SEPARATOR, url, parameters);
 
74
    
 
75
    Ref<Dictionary> dict(new Dictionary());
 
76
    dict->decode(parameters);
 
77
 
 
78
    log_debug("full url (filename): %s, url: %s, parameters: %s\n",
 
79
               filename, url.c_str(), parameters.c_str());
 
80
    
 
81
    String objID = dict->get(_("object_id"));
 
82
    if (objID == nil)
 
83
    {
 
84
        //log_error("object_id not found in url\n");
 
85
        throw _Exception(_("get_info: object_id not found"));
 
86
    }
 
87
    else
 
88
        objectID = objID.toInt();
 
89
 
 
90
    //log_debug("got ObjectID: [%s]\n", object_id.c_str());
 
91
 
 
92
    Ref<Storage> storage = Storage::getInstance();
 
93
 
 
94
    Ref<CdsObject> obj = storage->loadObject(objectID);
 
95
 
 
96
    int objectType = obj->getObjectType();
 
97
 
 
98
    if (!IS_CDS_ITEM_EXTERNAL_URL(objectType))
 
99
    {
 
100
        throw _Exception(_("get_info: object is not an external url item"));
 
101
    }
 
102
 
 
103
#ifdef EXTERNAL_TRANSCODING
 
104
    tr_profile = dict->get(_(URL_PARAM_TRANSCODE_PROFILE_NAME));
 
105
 
 
106
    if (string_ok(tr_profile))
 
107
    {
 
108
        Ref<TranscodingProfile> tp = ConfigManager::getInstance()->getTranscodingProfileListOption(CFG_TRANSCODING_PROFILE_LIST)->getByName(tr_profile);
 
109
 
 
110
        if (tp == nil)
 
111
            throw _Exception(_("Transcoding requested but no profile "
 
112
                               "matching the name ") + tr_profile + " found");
 
113
 
 
114
        mimeType = tp->getTargetMimeType();
 
115
        info->file_length = -1;
 
116
    }
 
117
    else
 
118
#endif
 
119
    {
 
120
        Ref<CdsItemExternalURL> item = RefCast(obj, CdsItemExternalURL);
 
121
 
 
122
#ifdef ONLINE_SERVICES
 
123
        if (item->getFlag(OBJECT_FLAG_ONLINE_SERVICE))
 
124
        {
 
125
            /// \todo write a helper class that will handle various online
 
126
            /// services
 
127
            Ref<OnlineServiceHelper> helper (new OnlineServiceHelper());
 
128
            url = helper->resolveURL(item);
 
129
        }
 
130
        else
 
131
#endif
 
132
        {
 
133
            url = item->getLocation();
 
134
        }
 
135
 
 
136
        log_debug("Online content url: %s\n", url.c_str());
 
137
        Ref<URL> u(new URL(1024));
 
138
        Ref<URL::Stat> st;
 
139
        try
 
140
        { 
 
141
            st = u->getInfo(url);
 
142
            info->file_length = st->getSize();
 
143
        }
 
144
        catch (Exception ex)
 
145
        {
 
146
            log_warning("%s\n", ex.getMessage().c_str());
 
147
            info->file_length = -1;
 
148
        }
 
149
 
 
150
        mimeType = item->getMimeType();
 
151
    }
 
152
 
 
153
    info->is_readable = 1;
 
154
    info->last_modified = 0;
 
155
    info->is_directory = 0;
 
156
    info->http_header = NULL;
 
157
 
 
158
    info->content_type = ixmlCloneDOMString(mimeType.c_str());
 
159
    log_debug("web_get_info(): end\n");
 
160
 
 
161
    /// \todo transcoding for get_info
 
162
}
 
163
 
 
164
Ref<IOHandler> URLRequestHandler::open(IN const char *filename, OUT struct File_Info *info, IN enum UpnpOpenFileMode mode)
 
165
{
 
166
    int objectID;
 
167
    String mimeType;
 
168
#ifdef EXTERNAL_TRANSCODING
 
169
    String tr_profile;
 
170
#endif
 
171
 
 
172
    log_debug("start\n");
 
173
 
 
174
    // Currently we explicitly do not support UPNP_WRITE
 
175
    // due to security reasons.
 
176
    if (mode != UPNP_READ)
 
177
        throw _Exception(_("UPNP_WRITE unsupported"));
 
178
 
 
179
    String url, parameters;
 
180
    split_url(filename, URL_PARAM_SEPARATOR, url, parameters);
 
181
 
 
182
    Ref<Dictionary> dict(new Dictionary());
 
183
    dict->decode(parameters);
 
184
    log_debug("full url (filename): %s, url: %s, parameters: %s\n",
 
185
               filename, url.c_str(), parameters.c_str());
 
186
 
 
187
    String objID = dict->get(_("object_id"));
 
188
    if (objID == nil)
 
189
    {
 
190
        throw _Exception(_("object_id not found"));
 
191
    }
 
192
    else
 
193
        objectID = objID.toInt();
 
194
 
 
195
    Ref<Storage> storage = Storage::getInstance();
 
196
 
 
197
    Ref<CdsObject> obj = storage->loadObject(objectID);
 
198
 
 
199
    int objectType = obj->getObjectType();
 
200
 
 
201
    if (!IS_CDS_ITEM_EXTERNAL_URL(objectType))
 
202
    {
 
203
        throw _Exception(_("object is not an external url item"));
 
204
    }
 
205
 
 
206
    Ref<CdsItemExternalURL> item = RefCast(obj, CdsItemExternalURL);
 
207
 
 
208
#ifdef ONLINE_SERVICES
 
209
    if (item->getFlag(OBJECT_FLAG_ONLINE_SERVICE))
 
210
    {
 
211
        Ref<OnlineServiceHelper> helper (new OnlineServiceHelper());
 
212
        url = helper->resolveURL(item);
 
213
    }
 
214
    else 
 
215
#endif
 
216
    {
 
217
        url = item->getLocation();
 
218
    }
 
219
 
 
220
    log_debug("Online content url: %s\n", url.c_str());
 
221
 
 
222
    info->is_readable = 1;
 
223
    info->last_modified = 0;
 
224
    info->is_directory = 0;
 
225
    info->http_header = NULL;
 
226
 
 
227
#ifdef EXTERNAL_TRANSCODING
 
228
    tr_profile = dict->get(_(URL_PARAM_TRANSCODE_PROFILE_NAME));
 
229
 
 
230
    if (string_ok(tr_profile))
 
231
    {
 
232
        Ref<TranscodingProfile> tp = ConfigManager::getInstance()->getTranscodingProfileListOption(CFG_TRANSCODING_PROFILE_LIST)->getByName(tr_profile);
 
233
 
 
234
        if (tp == nil)
 
235
            throw _Exception(_("Transcoding of file ") + url +
 
236
                    " but no profile matching the name " +
 
237
                    tr_profile + " found");
 
238
 
 
239
        Ref<TranscodeDispatcher> tr_d(new TranscodeDispatcher());
 
240
        return tr_d->open(tp, url, item->getObjectType(), info);
 
241
    }
 
242
    else
 
243
#endif
 
244
    {
 
245
        Ref<URL> u(new URL(1024));
 
246
        Ref<URL::Stat> st;
 
247
        try
 
248
        {
 
249
            st = u->getInfo(url);
 
250
            info->file_length = st->getSize();
 
251
        }
 
252
        catch (Exception ex)
 
253
        {
 
254
            log_warning("%s\n", ex.getMessage().c_str());
 
255
            info->file_length = -1;
 
256
        }
 
257
        mimeType = item->getMimeType();
 
258
        info->content_type = ixmlCloneDOMString(mimeType.c_str());
 
259
    }
 
260
 
 
261
    ///\todo make curl io handler configurable for url request handler
 
262
    Ref<IOHandler> io_handler(new CurlIOHandler(url, NULL, 1024*1024, 0));
 
263
 
 
264
    io_handler->open(mode);
 
265
    return io_handler;
 
266
}
 
267
 
 
268
#endif // HAVE_CURL
 
269