summaryrefslogtreecommitdiff
path: root/server/webserver.cpp
blob: 04fb44ae2e60ed1f18cace83ec8565c42df60261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* 
 * File:   upnpwebserver.cpp
 * Author: savop
 * 
 * Created on 30. Mai 2009, 18:13
 */

#include <time.h>
#include <vdr/channels.h>
#include <map>
#include <upnp/upnp.h>
#include "webserver.h"
#include "server.h"
#include "livereceiver.h"
#include "recplayer.h"
#include "search.h"

/* COPIED FROM INTEL UPNP TOOLS */
/*******************************************************************************
 *
 * Copyright (c) 2000-2003 Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither name of Intel Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/
/** @private */
struct File_Info_
{
  /** The length of the file. A length less than 0 indicates the size
   *  is unknown, and data will be sent until 0 bytes are returned from
   *  a read call. */
  off64_t file_length;

  /** The time at which the contents of the file was modified;
   *  The time system is always local (not GMT). */
  time_t last_modified;

  /** If the file is a directory, {\bf is_directory} contains
   * a non-zero value. For a regular file, it should be 0. */
  int is_directory;

  /** If the file or directory is readable, this contains
   * a non-zero value. If unreadable, it should be set to 0. */
  int is_readable;

  /** The content type of the file. This string needs to be allocated
   *  by the caller using {\bf ixmlCloneDOMString}.  When finished
   *  with it, the SDK frees the {\bf DOMString}. */

  DOMString content_type;

};

/** @private */
struct cWebFileHandle {
    cString      Filename;
    off64_t      Size;
    cFileHandle* FileHandle;
};

/****************************************************
 *
 *  The web server
 *
 *  Handles the virtual directories and the
 *  provision of data
 *
 *  Interface between the channels/recordings of the
 *  VDR and the outer world
 *
 ****************************************************/

cUPnPWebServer::cUPnPWebServer(const char* root) : mRootdir(root) {
}

cUPnPWebServer::~cUPnPWebServer(){}

cUPnPWebServer* cUPnPWebServer::mInstance = NULL;

UpnpVirtualDirCallbacks cUPnPWebServer::mVirtualDirCallbacks = {
    cUPnPWebServer::getInfo,
    cUPnPWebServer::open,
    cUPnPWebServer::read,
    cUPnPWebServer::write,
    cUPnPWebServer::seek,
    cUPnPWebServer::close
};

bool cUPnPWebServer::init(){
    MESSAGE(VERBOSE_WEBSERVER, "Initialize callbacks for virtual directories.");

    if(UpnpSetWebServerRootDir(this->mRootdir) == UPNP_E_INVALID_ARGUMENT){
        ERROR("The root directory of the webserver is invalid.");
        return false;
    }
    MESSAGE(VERBOSE_WEBSERVER, "Setting up callbacks");

    if(UpnpSetVirtualDirCallbacks(&cUPnPWebServer::mVirtualDirCallbacks) == UPNP_E_INVALID_ARGUMENT){
        ERROR("The virtual directory callbacks are invalid.");
        return false;
    }

    if(UpnpIsWebserverEnabled() == FALSE){
        WARNING("The webserver has not been started. For whatever reason...");
        return false;
    }

    MESSAGE(VERBOSE_WEBSERVER, "Add virtual directories.");
    if(UpnpAddVirtualDir(UPNP_DIR_SHARES) == UPNP_E_INVALID_ARGUMENT){
        ERROR("The virtual directory %s is invalid.",UPNP_DIR_SHARES);
        return false;
    }
    return true;
}

bool cUPnPWebServer::uninit(){
    MESSAGE(VERBOSE_WEBSERVER, "Disabling the internal webserver");
    UpnpEnableWebserver(FALSE);

    return true;
}

cUPnPWebServer* cUPnPWebServer::getInstance(const char* rootdir){
    if(cUPnPWebServer::mInstance == NULL)
        cUPnPWebServer::mInstance = new cUPnPWebServer(rootdir);

    if(cUPnPWebServer::mInstance){
        return cUPnPWebServer::mInstance;
    }
    else return NULL;
}

int cUPnPWebServer::getInfo(const char* filename, File_Info* info){
    MESSAGE(VERBOSE_WEBSERVER, "Getting information of file '%s'", filename);

    propertyMap Properties;
    int Method;
    int Section;

    if(cPathParser::parse(filename, &Section, &Method, &Properties)){
        switch(Section){
            case 0:
                switch(Method){
                    case UPNP_WEB_METHOD_STREAM:
                        {
                            MESSAGE(VERBOSE_WEBSERVER, "Stream request");
                            propertyMap::iterator It = Properties.find("resId");
                            unsigned int ResourceID = 0;
                            if(It == Properties.end()){
                                ERROR("No resourceID for stream request");
                                return -1;
                            }
                            else {
                                ResourceID = (unsigned)atoi(It->second);
                                cUPnPResource* Resource = cUPnPResources::getInstance()->getResource(ResourceID);
                                if(!Resource){
                                    ERROR("No such resource with ID (%d)", ResourceID);
                                    return -1;
                                }
                                else {
                                    File_Info_ finfo;

                                    finfo.content_type = ixmlCloneDOMString(Resource->getContentType());
                                    finfo.file_length = Resource->getFileSize();
                                    finfo.is_directory = 0;
                                    finfo.is_readable = 1;
                                    finfo.last_modified = Resource->getLastModification();
                                    memcpy(info, &finfo, sizeof(File_Info_));

                                    MESSAGE(VERBOSE_METADATA, "==== File info of Resource #%d ====", Resource->getID());
                                    MESSAGE(VERBOSE_METADATA, "Size: %lld", finfo.file_length);
                                    MESSAGE(VERBOSE_METADATA, "Dir: %s", finfo.is_directory?"yes":"no");
                                    MESSAGE(VERBOSE_METADATA, "Read: %s", finfo.is_readable?"allowed":"not allowed");
                                    MESSAGE(VERBOSE_METADATA, "Last modified: %s", ctime(&(finfo.last_modified)));
                                    MESSAGE(VERBOSE_METADATA, "Content-type: %s", finfo.content_type);

#ifdef UPNP_HAVE_CUSTOMHEADERS
                                    UpnpAddCustomHTTPHeader("transferMode.dlna.org: Streaming");
                                    UpnpAddCustomHTTPHeader(
                                        "contentFeatures.dlna.org: "
                                        "DLNA.ORG_OP=00;"
                                        "DLNA.ORG_CI=0;"
                                        "DLNA.ORG_FLAGS=01700000000000000000000000000000"
                                        );
#endif
                                }
                            }
                        }
                        break;
                    case UPNP_WEB_METHOD_BROWSE:
                    //    break;
                    case UPNP_WEB_METHOD_SHOW:
                    //    break;
                    case UPNP_WEB_METHOD_SEARCH:
                    case UPNP_WEB_METHOD_DOWNLOAD:
                    default:
                        ERROR("Unknown or unsupported method ID (%d)", Method);
                        return -1;
                }
                break;
            default:
                ERROR("Unknown or unsupported section ID (%d).", Section);
                return -1;
        }
    }
    else {
        return -1;
    }

    return 0;
}

UpnpWebFileHandle cUPnPWebServer::open(const char* filename, UpnpOpenFileMode mode){
    MESSAGE(VERBOSE_WEBSERVER, "File %s was opened for %s.",filename,mode==UPNP_READ ? "reading" : "writing");

    propertyMap Properties;
    int Method;
    int Section;
    cWebFileHandle* WebFileHandle = NULL;

    if(cPathParser::parse(filename, &Section, &Method, &Properties)){
        switch(Section){
            case 0:
                switch(Method){
                    case UPNP_WEB_METHOD_STREAM:
                        {
                            MESSAGE(VERBOSE_WEBSERVER, "Stream request");
                            propertyMap::iterator It = Properties.find("resId");
                            unsigned int ResourceID = 0;
                            if(It == Properties.end()){
                                ERROR("No resourceID for stream request");
                                return NULL;
                            }
                            else {
                                ResourceID = (unsigned)atoi(It->second);
                                cUPnPResource* Resource = cUPnPResources::getInstance()->getResource(ResourceID);
                                if(!Resource){
                                    ERROR("No such resource with ID (%d)", ResourceID);
                                    return NULL;
                                }
                                else {
                                    WebFileHandle = new cWebFileHandle;
                                    WebFileHandle->Filename = Resource->getResource();
                                    WebFileHandle->Size = Resource->getFileSize();
                                    switch(Resource->getResourceType()){
                                        case UPNP_RESOURCE_CHANNEL:
                                            {
                                                char* ChannelID = strtok(strdup(Resource->getResource()),":");
                                                int    StreamID = atoi(strtok(NULL,":"));
                                                MESSAGE(VERBOSE_LIVE_TV, "Try to create Receiver for Channel %s with Stream ID %d", ChannelID, StreamID);
                                                cChannel* Channel = Channels.GetByChannelID(tChannelID::FromString(ChannelID));
                                                if(!Channel){
                                                    ERROR("No such channel with ID %s", ChannelID);
                                                    return NULL;
                                                }
                                                cLiveReceiver* Receiver = cLiveReceiver::newInstance(Channel,0);
                                                if(!Receiver){
                                                    ERROR("Unable to tune channel. No available tuners?");
                                                    return NULL;
                                                }
                                                WebFileHandle->FileHandle = Receiver;
                                            }
                                            break;
                                        case UPNP_RESOURCE_RECORDING:
                                            {
                                                const char* RecordFile = Resource->getResource();
                                                MESSAGE(VERBOSE_RECORDS, "Try to create Player for Record %s", RecordFile);
                                                cRecording* Recording = Recordings.GetByName(RecordFile);
                                                if(!Recording){
                                                    ERROR("No such recording with file name %s", RecordFile);
                                                    return NULL;
                                                }
                                                cRecordingPlayer* RecPlayer = cRecordingPlayer::newInstance(Recording);
                                                if(!RecPlayer){
                                                    ERROR("Unable to start record player. No access?!");
                                                    return NULL;
                                                }
                                                WebFileHandle->FileHandle = RecPlayer;
                                            }
                                            break;
                                        case UPNP_RESOURCE_FILE:
                                        //    break;
                                        case UPNP_RESOURCE_URL:
                                        default:
                                            return NULL;
                                    }
                                }
                            }
                        }
                        break;
                    case UPNP_WEB_METHOD_BROWSE:
                    //    break;
                    case UPNP_WEB_METHOD_SHOW:
                    //    break;
                    case UPNP_WEB_METHOD_SEARCH:
                    case UPNP_WEB_METHOD_DOWNLOAD:
                    default:
                        ERROR("Unknown or unsupported method ID (%d)", Method);
                        return NULL;
                }
                break;
            default:
                ERROR("Unknown or unsupported section ID (%d).", Section);
                return NULL;
        }
    }
    else {
        return NULL;
    }
    MESSAGE(VERBOSE_WEBSERVER, "Open the file handle");
    WebFileHandle->FileHandle->open(mode);
    return (UpnpWebFileHandle)WebFileHandle;
}

int cUPnPWebServer::write(UpnpWebFileHandle fh, char* buf, size_t buflen){
    cWebFileHandle* FileHandle = (cWebFileHandle*)fh;
    MESSAGE(VERBOSE_BUFFERS, "Writing to %s", *FileHandle->Filename);
    return FileHandle->FileHandle->write(buf, buflen);
}

int cUPnPWebServer::read(UpnpWebFileHandle fh, char* buf, size_t buflen){
    cWebFileHandle* FileHandle = (cWebFileHandle*)fh;
    MESSAGE(VERBOSE_BUFFERS, "Reading from %s", *FileHandle->Filename);
    return FileHandle->FileHandle->read(buf, buflen);
}

int cUPnPWebServer::seek(UpnpWebFileHandle fh, off_t offset, int origin){
    cWebFileHandle* FileHandle = (cWebFileHandle*)fh;
    MESSAGE(VERBOSE_BUFFERS, "Seeking on %s", *FileHandle->Filename);
    return FileHandle->FileHandle->seek(offset, origin);
}

int cUPnPWebServer::close(UpnpWebFileHandle fh){
    cWebFileHandle *FileHandle = (cWebFileHandle *)fh;
    MESSAGE(VERBOSE_WEBSERVER, "Closing file %s", *FileHandle->Filename);
    FileHandle->FileHandle->close();
    delete FileHandle->FileHandle;
    delete FileHandle;
    return 0;
}