2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "WebServer.h"
25 #include "XBDateTime.h"
26 #include "filesystem/File.h"
27 #include "settings/Settings.h"
28 #include "threads/SingleLock.h"
29 #include "utils/Base64.h"
30 #include "utils/log.h"
31 #include "utils/Mime.h"
32 #include "utils/StringUtils.h"
33 #include "utils/URIUtils.h"
34 #include "utils/Variant.h"
35 #include <boost/make_shared.hpp>
37 //#define WEBSERVER_DEBUG
40 #pragma comment(lib, "libmicrohttpd.dll.lib")
43 #define MAX_POST_BUFFER_SIZE 2048
45 #define PAGE_FILE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
46 #define NOT_SUPPORTED "<html><head><title>Not Supported</title></head><body>The method you are trying to use is not supported by this server</body></html>"
48 #define CONTENT_RANGE_FORMAT "bytes %" PRId64 "-%" PRId64 "/%" PRId64
50 using namespace XFILE;
52 using namespace JSONRPC;
55 boost::shared_ptr<CFile> file;
60 string boundaryWithHeader;
63 int64_t writePosition;
64 } HttpFileDownloadContext;
66 vector<IHTTPRequestHandler *> CWebServer::m_requestHandlers;
68 CWebServer::CWebServer()
73 m_needcredentials = true;
74 m_Credentials64Encoded = "eGJtYzp4Ym1j"; // xbmc:xbmc
77 int CWebServer::FillArgumentMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
79 if (cls == NULL || key == NULL)
82 map<string, string> *arguments = (map<string, string> *)cls;
83 arguments->insert(pair<string, string>(key, value != NULL ? value : StringUtils::Empty));
87 int CWebServer::FillArgumentMultiMap(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
89 if (cls == NULL || key == NULL)
92 multimap<string, string> *arguments = (multimap<string, string> *)cls;
93 arguments->insert(pair<string, string>(key, value != NULL ? value : StringUtils::Empty));
97 int CWebServer::AskForAuthentication(struct MHD_Connection *connection)
100 struct MHD_Response *response;
102 response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO);
106 ret = AddHeader(response, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic realm=XBMC");
107 ret |= AddHeader(response, MHD_HTTP_HEADER_CONNECTION, "close");
110 MHD_destroy_response (response);
114 ret = MHD_queue_response (connection, MHD_HTTP_UNAUTHORIZED, response);
116 MHD_destroy_response (response);
121 bool CWebServer::IsAuthenticated(CWebServer *server, struct MHD_Connection *connection)
123 CSingleLock lock (server->m_critSection);
124 if (!server->m_needcredentials)
127 const char *strbase = "Basic ";
128 const char *headervalue = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Authorization");
129 if (NULL == headervalue)
131 if (strncmp (headervalue, strbase, strlen(strbase)))
134 return (server->m_Credentials64Encoded.compare(headervalue + strlen(strbase)) == 0);
137 #if (MHD_VERSION >= 0x00040001)
138 int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection,
139 const char *url, const char *method,
140 const char *version, const char *upload_data,
141 size_t *upload_data_size, void **con_cls)
143 int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection,
144 const char *url, const char *method,
145 const char *version, const char *upload_data,
146 unsigned int *upload_data_size, void **con_cls)
149 CWebServer *server = (CWebServer *)cls;
150 HTTPMethod methodType = GetMethod(method);
151 HTTPRequest request = { connection, url, methodType, version, server };
153 if (!IsAuthenticated(server, connection))
154 return AskForAuthentication(connection);
156 // Check if this is the first call to
157 // AnswerToConnection for this request
158 if (*con_cls == NULL)
160 // Look for a IHTTPRequestHandler which can
161 // take care of the current request
162 for (vector<IHTTPRequestHandler *>::const_iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); it++)
164 IHTTPRequestHandler *requestHandler = *it;
165 if (requestHandler->CheckHTTPRequest(request))
167 // We found a matching IHTTPRequestHandler
168 // so let's get a new instance for this request
169 IHTTPRequestHandler *handler = requestHandler->GetInstance();
171 // If we got a POST request we need to take
172 // care of the POST data
173 if (methodType == POST)
175 ConnectionHandler *conHandler = new ConnectionHandler();
176 conHandler->requestHandler = handler;
178 // Get the content-type of the POST data
179 string contentType = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE);
180 if (!contentType.empty())
182 // If the content-type is application/x-ww-form-urlencoded or multipart/form-data
183 // we can use MHD's POST processor
184 if (stricmp(contentType.c_str(), MHD_HTTP_POST_ENCODING_FORM_URLENCODED) == 0 ||
185 stricmp(contentType.c_str(), MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA) == 0)
187 // Get a new MHD_PostProcessor
188 conHandler->postprocessor = MHD_create_post_processor(connection, MAX_POST_BUFFER_SIZE, &CWebServer::HandlePostField, (void*)conHandler);
190 // MHD doesn't seem to be able to handle
192 if (conHandler->postprocessor == NULL)
194 delete conHandler->requestHandler;
197 return SendErrorResponse(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, methodType);
201 // otherwise we need to handle the POST data ourselves
202 // which is done in the next call to AnswerToConnection
204 *con_cls = (void*)conHandler;
207 // No POST request so nothing special to handle
209 return HandleRequest(handler, request);
213 // This is a subsequent call to
214 // AnswerToConnection for this request
217 // Again we need to take special care
219 if (methodType == POST)
221 ConnectionHandler *conHandler = (ConnectionHandler *)*con_cls;
222 if (conHandler->requestHandler == NULL)
223 return SendErrorResponse(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, methodType);
225 // We only need to handle POST data
226 // if there actually is data left to handle
227 if (*upload_data_size > 0)
229 // Either use MHD's POST processor
230 if (conHandler->postprocessor != NULL)
231 MHD_post_process(conHandler->postprocessor, upload_data, *upload_data_size);
232 // or simply copy the data to the handler
234 conHandler->requestHandler->AddPostData(upload_data, *upload_data_size);
236 // Signal that we have handled the data
237 *upload_data_size = 0;
241 // We have handled all POST data
242 // so it's time to invoke the IHTTPRequestHandler
245 if (conHandler->postprocessor != NULL)
246 MHD_destroy_post_processor(conHandler->postprocessor);
249 int ret = HandleRequest(conHandler->requestHandler, request);
254 // It's unusual to get more than one call
255 // to AnswerToConnection for none-POST
256 // requests, but let's handle it anyway
259 for (vector<IHTTPRequestHandler *>::const_iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); it++)
261 IHTTPRequestHandler *requestHandler = *it;
262 if (requestHandler->CheckHTTPRequest(request))
263 return HandleRequest(requestHandler->GetInstance(), request);
268 return SendErrorResponse(connection, MHD_HTTP_NOT_FOUND, methodType);
271 #if (MHD_VERSION >= 0x00040001)
272 int CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key,
273 const char *filename, const char *content_type,
274 const char *transfer_encoding, const char *data, uint64_t off,
277 int CWebServer::HandlePostField(void *cls, enum MHD_ValueKind kind, const char *key,
278 const char *filename, const char *content_type,
279 const char *transfer_encoding, const char *data, uint64_t off,
283 ConnectionHandler *conHandler = (ConnectionHandler *)cls;
285 if (conHandler == NULL || conHandler->requestHandler == NULL || size == 0)
288 conHandler->requestHandler->AddPostField(key, string(data, size));
292 int CWebServer::HandleRequest(IHTTPRequestHandler *handler, const HTTPRequest &request)
295 return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method);
297 int ret = handler->HandleHTTPRequest(request);
301 return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method);
304 struct MHD_Response *response = NULL;
305 int responseCode = handler->GetHTTPResonseCode();
306 switch (handler->GetHTTPResponseType())
313 ret = CreateRedirect(request.connection, handler->GetHTTPRedirectUrl(), response);
316 case HTTPFileDownload:
317 ret = CreateFileDownloadResponse(request.connection, handler->GetHTTPResponseFile(), request.method, response, responseCode);
320 case HTTPMemoryDownloadNoFreeNoCopy:
321 ret = CreateMemoryDownloadResponse(request.connection, handler->GetHTTPResponseData(), handler->GetHTTPResonseDataLength(), false, false, response);
324 case HTTPMemoryDownloadNoFreeCopy:
325 ret = CreateMemoryDownloadResponse(request.connection, handler->GetHTTPResponseData(), handler->GetHTTPResonseDataLength(), false, true, response);
328 case HTTPMemoryDownloadFreeNoCopy:
329 ret = CreateMemoryDownloadResponse(request.connection, handler->GetHTTPResponseData(), handler->GetHTTPResonseDataLength(), true, false, response);
332 case HTTPMemoryDownloadFreeCopy:
333 ret = CreateMemoryDownloadResponse(request.connection, handler->GetHTTPResponseData(), handler->GetHTTPResonseDataLength(), true, true, response);
337 ret = CreateErrorResponse(request.connection, handler->GetHTTPResonseCode(), request.method, response);
342 return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method);
348 return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method);
351 multimap<string, string> header = handler->GetHTTPResponseHeaderFields();
352 for (multimap<string, string>::const_iterator it = header.begin(); it != header.end(); it++)
353 AddHeader(response, it->first.c_str(), it->second.c_str());
355 MHD_queue_response(request.connection, responseCode, response);
356 MHD_destroy_response(response);
362 HTTPMethod CWebServer::GetMethod(const char *method)
364 if (strcmp(method, "GET") == 0)
366 if (strcmp(method, "POST") == 0)
368 if (strcmp(method, "HEAD") == 0)
374 int CWebServer::CreateRedirect(struct MHD_Connection *connection, const string &strURL, struct MHD_Response *&response)
376 response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO);
379 AddHeader(response, "Location", strURL.c_str());
385 int CWebServer::CreateFileDownloadResponse(struct MHD_Connection *connection, const string &strURL, HTTPMethod methodType, struct MHD_Response *&response, int &responseCode)
387 boost::shared_ptr<CFile> file = boost::make_shared<CFile>();
389 #ifdef WEBSERVER_DEBUG
390 CLog::Log(LOGDEBUG, "webserver [IN] %s", strURL.c_str());
391 multimap<string, string> headers;
392 if (GetRequestHeaderValues(connection, MHD_HEADER_KIND, headers) > 0)
394 for (multimap<string, string>::const_iterator header = headers.begin(); header != headers.end(); header++)
395 CLog::Log(LOGDEBUG, "webserver [IN] %s: %s", header->first.c_str(), header->second.c_str());
399 if (file->Open(strURL, READ_NO_CACHE))
403 int64_t fileLength = file->GetLength();
405 // try to get the file's last modified date
406 CDateTime lastModified;
407 if (!GetLastModifiedDateTime(file.get(), lastModified))
408 lastModified.Reset();
410 // get the MIME type for the Content-Type header
411 CStdString ext = URIUtils::GetExtension(strURL);
412 StringUtils::ToLower(ext);
413 string mimeType = CreateMimeTypeFromExtension(ext.c_str());
415 if (methodType != HEAD)
417 int64_t firstPosition = 0;
418 int64_t lastPosition = fileLength - 1;
419 uint64_t totalLength = 0;
420 std::auto_ptr<HttpFileDownloadContext> context(new HttpFileDownloadContext());
421 context->file = file;
422 context->rangesLength = fileLength;
423 context->contentType = mimeType;
424 context->boundaryWritten = false;
425 context->writePosition = 0;
427 if (methodType == GET)
429 // handle If-Modified-Since
430 string ifModifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, "If-Modified-Since");
431 if (!ifModifiedSince.empty() && lastModified.IsValid())
433 CDateTime ifModifiedSinceDate;
434 ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince);
436 if (lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate)
439 response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
440 if (response == NULL)
443 responseCode = MHD_HTTP_NOT_MODIFIED;
449 // handle Range header
450 context->rangesLength = ParseRangeHeader(GetRequestHeaderValue(connection, MHD_HEADER_KIND, "Range"), fileLength, context->ranges, firstPosition, lastPosition);
452 // handle If-Range header but only if the Range header is present
453 if (!context->ranges.empty())
455 string ifRange = GetRequestHeaderValue(connection, MHD_HEADER_KIND, "If-Range");
456 if (!ifRange.empty() && lastModified.IsValid())
458 CDateTime ifRangeDate;
459 ifRangeDate.SetFromRFC1123DateTime(ifRange);
461 // check if the last modification is newer than the If-Range date
462 // if so we have to server the whole file instead
463 if (lastModified.GetAsUTCDateTime() > ifRangeDate)
464 context->ranges.clear();
472 // if there are no ranges, add the whole range
473 if (context->ranges.empty() || context->rangesLength == fileLength)
475 if (context->rangesLength == fileLength)
476 context->ranges.clear();
478 context->ranges.push_back(HttpRange(0, fileLength - 1));
479 context->rangesLength = fileLength;
481 lastPosition = fileLength - 1;
484 responseCode = MHD_HTTP_PARTIAL_CONTENT;
486 // remember the total number of ranges
487 context->rangeCount = context->ranges.size();
488 // remember the total length
489 totalLength = context->rangesLength;
491 // we need to remember whether we are ranged because the range length
492 // might change and won't be reliable anymore for length comparisons
493 ranged = context->rangeCount > 1 || context->rangesLength < fileLength;
495 // adjust the MIME type and range length in case of multiple ranges
496 // which requires multipart boundaries
497 if (context->rangeCount > 1)
499 context->boundary = GenerateMultipartBoundary();
500 mimeType = "multipart/byteranges; boundary=" + context->boundary;
502 // build part of the boundary with the optional Content-Type header
503 // "--<boundary>\r\nContent-Type: <content-type>\r\n
504 context->boundaryWithHeader = "\r\n--" + context->boundary + "\r\n";
505 if (!context->contentType.empty())
506 context->boundaryWithHeader += "Content-Type: " + context->contentType + "\r\n";
508 // for every range, we need to add a boundary with header
509 for (HttpRanges::const_iterator range = context->ranges.begin(); range != context->ranges.end(); range++)
511 // we need to temporarily add the Content-Range header to the
512 // boundary to be able to determine the length
513 string completeBoundaryWithHeader = context->boundaryWithHeader;
514 completeBoundaryWithHeader += StringUtils::Format("Content-Range: " CONTENT_RANGE_FORMAT,
515 range->first, range->second, range->second - range->first + 1);
516 completeBoundaryWithHeader += "\r\n\r\n";
518 totalLength += completeBoundaryWithHeader.size();
520 // and at the very end a special end-boundary "\r\n--<boundary>--"
521 totalLength += 4 + context->boundary.size() + 2;
524 // set the initial write position
525 context->writePosition = context->ranges.begin()->first;
527 // create the response object
528 response = MHD_create_response_from_callback(totalLength,
530 &CWebServer::ContentReaderCallback, context.get(),
531 &CWebServer::ContentReaderFreeCallback);
532 if (response == NULL)
535 context.release(); // ownership was passed to mhd
538 // add Content-Range header
540 AddHeader(response, "Content-Range", StringUtils::Format(CONTENT_RANGE_FORMAT, firstPosition, lastPosition, fileLength).c_str());
546 CStdString contentLength = StringUtils::Format("%" PRId64, fileLength);
548 response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
549 if (response == NULL)
552 AddHeader(response, "Content-Length", contentLength);
555 // add "Accept-Ranges: bytes" header
556 AddHeader(response, "Accept-Ranges", "bytes");
558 // set the Content-Type header
559 if (!mimeType.empty())
560 AddHeader(response, "Content-Type", mimeType.c_str());
562 // set the Last-Modified header
563 if (lastModified.IsValid())
564 AddHeader(response, "Last-Modified", lastModified.GetAsRFC1123DateTime());
566 // set the Expires header
567 CDateTime expiryTime = CDateTime::GetCurrentDateTime();
568 if (StringUtils::EqualsNoCase(mimeType, "text/html") ||
569 StringUtils::EqualsNoCase(mimeType, "text/css") ||
570 StringUtils::EqualsNoCase(mimeType, "application/javascript"))
571 expiryTime += CDateTimeSpan(1, 0, 0, 0);
573 expiryTime += CDateTimeSpan(365, 0, 0, 0);
574 AddHeader(response, "Expires", expiryTime.GetAsRFC1123DateTime());
578 CLog::Log(LOGERROR, "WebServer: Failed to open %s", strURL.c_str());
579 return SendErrorResponse(connection, MHD_HTTP_NOT_FOUND, methodType);
585 int CWebServer::CreateErrorResponse(struct MHD_Connection *connection, int responseType, HTTPMethod method, struct MHD_Response *&response)
587 size_t payloadSize = 0;
588 void *payload = NULL;
592 switch (responseType)
594 case MHD_HTTP_NOT_FOUND:
595 payloadSize = strlen(PAGE_FILE_NOT_FOUND);
596 payload = (void *)PAGE_FILE_NOT_FOUND;
598 case MHD_HTTP_NOT_IMPLEMENTED:
599 payloadSize = strlen(NOT_SUPPORTED);
600 payload = (void *)NOT_SUPPORTED;
605 response = MHD_create_response_from_data (payloadSize, payload, MHD_NO, MHD_NO);
611 int CWebServer::CreateMemoryDownloadResponse(struct MHD_Connection *connection, void *data, size_t size, bool free, bool copy, struct MHD_Response *&response)
613 response = MHD_create_response_from_data (size, data, free ? MHD_YES : MHD_NO, copy ? MHD_YES : MHD_NO);
619 int CWebServer::SendErrorResponse(struct MHD_Connection *connection, int errorType, HTTPMethod method)
621 struct MHD_Response *response = NULL;
622 int ret = CreateErrorResponse(connection, errorType, method, response);
625 ret = MHD_queue_response (connection, errorType, response);
626 MHD_destroy_response (response);
632 void* CWebServer::UriRequestLogger(void *cls, const char *uri)
634 CLog::Log(LOGDEBUG, "webserver: request received for %s", uri);
638 #if (MHD_VERSION >= 0x00090200)
639 ssize_t CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, size_t max)
640 #elif (MHD_VERSION >= 0x00040001)
641 int CWebServer::ContentReaderCallback(void *cls, uint64_t pos, char *buf, int max)
642 #else //libmicrohttpd < 0.4.0
643 int CWebServer::ContentReaderCallback(void *cls, size_t pos, char *buf, int max)
646 HttpFileDownloadContext *context = (HttpFileDownloadContext *)cls;
647 if (context == NULL || context->file == NULL)
650 #ifdef WEBSERVER_DEBUG
651 CLog::Log(LOGDEBUG, "webserver [OUT] write maximum %d bytes from %" PRIu64 " (%" PRIu64 ")", max, context->writePosition, pos);
654 // check if we need to add the end-boundary
655 if (context->rangeCount > 1 && context->ranges.empty())
657 // put together the end-boundary
658 string endBoundary = "\r\n--" + context->boundary + "--";
659 if ((unsigned int)max != endBoundary.size())
662 // copy the boundary into the buffer
663 memcpy(buf, endBoundary.c_str(), endBoundary.size());
664 return endBoundary.size();
667 if (context->ranges.empty())
670 int64_t start = context->ranges.at(0).first;
671 int64_t end = context->ranges.at(0).second;
672 int64_t maximum = (int64_t)max;
675 if (context->rangeCount > 1 && !context->boundaryWritten)
677 // put together the boundary for the current range
678 string boundary = context->boundaryWithHeader;
679 boundary += StringUtils::Format("Content-Range: " CONTENT_RANGE_FORMAT, start, end, end - start + 1) + "\r\n\r\n";
681 // copy the boundary into the buffer
682 memcpy(buf, boundary.c_str(), boundary.size());
683 // advance the buffer position
684 buf += boundary.size();
685 // update the number of written byte
686 written += boundary.size();
687 // update the maximum number of bytes
688 maximum -= boundary.size();
689 context->boundaryWritten = true;
692 // check if the current position is within this range
693 // if not, set it to the start position
694 if (context->writePosition < start || context->writePosition > end)
695 context->writePosition = start;
696 // adjust the maximum number of read bytes
697 maximum = std::min(maximum, end - context->writePosition + 1);
699 // seek to the position if necessary
700 if(context->writePosition != context->file->GetPosition())
701 context->file->Seek(context->writePosition);
703 // read data from the file
704 unsigned int res = context->file->Read(buf, maximum);
708 // add the number of read bytes to the number of written bytes
710 #ifdef WEBSERVER_DEBUG
711 CLog::Log(LOGDEBUG, "webserver [OUT] wrote %d bytes from %" PRId64 " in range (%" PRId64 " - %" PRId64 ")", written, context->writePosition, start, end);
713 // update the current write position
714 context->writePosition += res;
716 // if we have read all the data from the current range
717 // remove it from the list
718 if (context->writePosition >= end + 1)
720 context->ranges.erase(context->ranges.begin());
721 context->boundaryWritten = false;
727 void CWebServer::ContentReaderFreeCallback(void *cls)
729 HttpFileDownloadContext *context = (HttpFileDownloadContext *)cls;
732 #ifdef WEBSERVER_DEBUG
733 CLog::Log(LOGDEBUG, "webserver [OUT] done");
737 struct MHD_Daemon* CWebServer::StartMHD(unsigned int flags, int port)
739 unsigned int timeout = 60 * 60 * 24;
741 return MHD_start_daemon(flags |
742 #if (MHD_VERSION >= 0x00040002) && (MHD_VERSION < 0x00090B01)
743 // use main thread for each connection, can only handle one request at a
744 // time [unless you set the thread pool size]
745 MHD_USE_SELECT_INTERNALLY
747 // one thread per connection
748 // WARNING: set MHD_OPTION_CONNECTION_TIMEOUT to something higher than 1
749 // otherwise on libmicrohttpd 0.4.4-1 it spins a busy loop
750 MHD_USE_THREAD_PER_CONNECTION
756 &CWebServer::AnswerToConnection,
759 #if (MHD_VERSION >= 0x00040002) && (MHD_VERSION < 0x00090B01)
760 MHD_OPTION_THREAD_POOL_SIZE, 4,
762 MHD_OPTION_CONNECTION_LIMIT, 512,
763 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
764 MHD_OPTION_URI_LOG_CALLBACK, &CWebServer::UriRequestLogger, this,
768 bool CWebServer::Start(int port, const string &username, const string &password)
770 SetCredentials(username, password);
774 if ((v6testSock = socket(AF_INET6, SOCK_STREAM, 0)) >= 0)
776 closesocket(v6testSock);
777 m_daemon_ip6 = StartMHD(MHD_USE_IPv6, port);
780 m_daemon_ip4 = StartMHD(0 , port);
782 m_running = (m_daemon_ip6 != NULL) || (m_daemon_ip4 != NULL);
784 CLog::Log(LOGNOTICE, "WebServer: Started the webserver");
786 CLog::Log(LOGERROR, "WebServer: Failed to start the webserver");
791 bool CWebServer::Stop()
795 if (m_daemon_ip6 != NULL)
796 MHD_stop_daemon(m_daemon_ip6);
798 if (m_daemon_ip4 != NULL)
799 MHD_stop_daemon(m_daemon_ip4);
802 CLog::Log(LOGNOTICE, "WebServer: Stopped the webserver");
805 CLog::Log(LOGNOTICE, "WebServer: Stopped failed because its not running");
810 bool CWebServer::IsStarted()
815 void CWebServer::SetCredentials(const string &username, const string &password)
817 CSingleLock lock (m_critSection);
818 CStdString str = username + ":" + password;
820 Base64::Encode(str.c_str(), m_Credentials64Encoded);
821 m_needcredentials = !password.empty();
824 bool CWebServer::PrepareDownload(const char *path, CVariant &details, std::string &protocol)
826 if (CFile::Exists(path))
830 CStdString strPath = path;
831 if (StringUtils::StartsWith(strPath, "image://") ||
832 (StringUtils::StartsWith(strPath, "special://") && StringUtils::EndsWith(strPath, ".tbn")))
836 url += CURL::Encode(strPath);
837 details["path"] = url;
844 bool CWebServer::Download(const char *path, CVariant &result)
849 int CWebServer::GetCapabilities()
851 return Response | FileDownloadRedirect;
854 void CWebServer::RegisterRequestHandler(IHTTPRequestHandler *handler)
859 for (vector<IHTTPRequestHandler *>::iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); it++)
864 if ((*it)->GetPriority() < handler->GetPriority())
866 m_requestHandlers.insert(it, handler);
871 m_requestHandlers.push_back(handler);
874 void CWebServer::UnregisterRequestHandler(IHTTPRequestHandler *handler)
879 for (vector<IHTTPRequestHandler *>::iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); it++)
883 m_requestHandlers.erase(it);
889 std::string CWebServer::GetRequestHeaderValue(struct MHD_Connection *connection, enum MHD_ValueKind kind, const std::string &key)
891 if (connection == NULL)
894 const char* value = MHD_lookup_connection_value(connection, kind, key.c_str());
898 if (stricmp(key.c_str(), MHD_HTTP_HEADER_CONTENT_TYPE) == 0)
900 // Work around a bug in firefox (see https://bugzilla.mozilla.org/show_bug.cgi?id=416178)
901 // by cutting of anything that follows a ";" in a "Content-Type" header field
902 string strValue(value);
903 size_t pos = strValue.find(';');
904 if (pos != string::npos)
905 strValue = strValue.substr(0, pos);
913 int CWebServer::GetRequestHeaderValues(struct MHD_Connection *connection, enum MHD_ValueKind kind, std::map<std::string, std::string> &headerValues)
915 if (connection == NULL)
918 return MHD_get_connection_values(connection, kind, FillArgumentMap, &headerValues);
921 int CWebServer::GetRequestHeaderValues(struct MHD_Connection *connection, enum MHD_ValueKind kind, std::multimap<std::string, std::string> &headerValues)
923 if (connection == NULL)
926 return MHD_get_connection_values(connection, kind, FillArgumentMultiMap, &headerValues);
929 std::string CWebServer::CreateMimeTypeFromExtension(const char *ext)
931 if (strcmp(ext, ".kar") == 0) return "audio/midi";
932 if (strcmp(ext, ".tbn") == 0) return "image/jpeg";
933 return CMime::GetMimeType(ext);
936 int CWebServer::AddHeader(struct MHD_Response *response, const std::string &name, const std::string &value)
938 if (response == NULL || name.empty())
941 #ifdef WEBSERVER_DEBUG
942 CLog::Log(LOGDEBUG, "webserver [OUT] %s: %s", name.c_str(), value.c_str());
944 return MHD_add_response_header(response, name.c_str(), value.c_str());
947 int64_t CWebServer::ParseRangeHeader(const std::string &rangeHeaderValue, int64_t totalLength, HttpRanges &ranges, int64_t &firstPosition, int64_t &lastPosition)
950 lastPosition = totalLength - 1;
952 if (rangeHeaderValue.empty() || !StringUtils::StartsWithNoCase(rangeHeaderValue, "bytes="))
955 int64_t rangesLength = 0;
957 // remove "bytes=" from the beginning
958 string rangesValue = rangeHeaderValue.substr(6);
959 // split the value of the "Range" header by ","
960 vector<string> rangeValues = StringUtils::Split(rangesValue, ",");
961 for (vector<string>::const_iterator range = rangeValues.begin(); range != rangeValues.end(); range++)
963 // there must be a "-" in the range definition
964 if (range->find("-") == string::npos)
970 vector<string> positions = StringUtils::Split(*range, "-");
971 if (positions.size() > 2)
977 int64_t positionStart = -1;
978 int64_t positionEnd = -1;
979 if (!positions.at(0).empty())
980 positionStart = str2int64(positions.at(0), -1);
981 if (!positions.at(1).empty())
982 positionEnd = str2int64(positions.at(1), -1);
984 if (positionStart < 0 && positionEnd < 0)
990 // if there's no end position, use the file's length
992 positionEnd = totalLength - 1;
993 else if (positionStart < 0)
995 positionStart = totalLength - positionEnd;
996 positionEnd = totalLength - 1;
999 if (positionEnd < positionStart)
1007 firstPosition = positionStart;
1008 lastPosition = positionEnd;
1012 if (positionStart < firstPosition)
1013 firstPosition = positionStart;
1014 if (positionEnd > lastPosition)
1015 lastPosition = positionEnd;
1018 ranges.push_back(HttpRange(positionStart, positionEnd));
1019 rangesLength += positionEnd - positionStart + 1;
1022 if (!ranges.empty() || rangesLength > 0)
1023 return rangesLength;
1028 std::string CWebServer::GenerateMultipartBoundary()
1030 static char chars[] = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1032 // create a string of length 30 to 40 and pre-fill it with "-"
1033 size_t count = (size_t)CUtil::GetRandomNumber() % 11 + 30;
1034 string boundary(count, '-');
1036 for (size_t i = (size_t)CUtil::GetRandomNumber() % 5 + 8; i < count; i++)
1037 boundary.replace(i, 1, 1, chars[(size_t)CUtil::GetRandomNumber() % 64]);
1042 bool CWebServer::GetLastModifiedDateTime(XFILE::CFile *file, CDateTime &lastModified)
1047 struct __stat64 statBuffer;
1048 if (file->Stat(&statBuffer) != 0)
1051 struct tm *time = localtime((time_t *)&statBuffer.st_mtime);
1055 lastModified = *time;