bool CWebServer::GetRequestedRanges(struct MHD_Connection *connection, uint64_t totalLength, CHttpRanges &ranges) { ranges.Clear(); if (connection == NULL) return false; return ranges.Parse(GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE), totalLength); }
bool HTTPRequestHandlerUtils::GetRequestedRanges(struct MHD_Connection *connection, uint64_t totalLength, CHttpRanges &ranges) { ranges.Clear(); if (connection == nullptr) return false; return ranges.Parse(GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE), totalLength); }
bool CWebServer::IsAuthenticated(CWebServer *server, struct MHD_Connection *connection) { CSingleLock lock(server->m_critSection); if (!server->m_needcredentials) return true; const char *base = "Basic "; std::string authorization = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_AUTHORIZATION); if (authorization.empty() || !StringUtils::StartsWith(authorization, base)) return false; return server->m_Credentials64Encoded.compare(StringUtils::Mid(authorization.c_str(), strlen(base))) == 0; }
int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, unsigned int *upload_data_size, void **con_cls) #endif { if (cls == NULL || con_cls == NULL || *con_cls == NULL) { CLog::Log(LOGERROR, "CWebServer: invalid request received"); return MHD_NO; } CWebServer *server = reinterpret_cast<CWebServer*>(cls); std::auto_ptr<ConnectionHandler> conHandler(reinterpret_cast<ConnectionHandler*>(*con_cls)); HTTPMethod methodType = GetMethod(method); HTTPRequest request = { server, connection, conHandler->fullUri, url, methodType, version }; // remember if the request was new bool isNewRequest = conHandler->isNew; // because now it isn't anymore conHandler->isNew = false; // reset con_cls and set it if still necessary *con_cls = NULL; #ifdef WEBSERVER_DEBUG if (isNewRequest) { std::multimap<std::string, std::string> headerValues; GetRequestHeaderValues(connection, MHD_HEADER_KIND, headerValues); std::multimap<std::string, std::string> getValues; GetRequestHeaderValues(connection, MHD_GET_ARGUMENT_KIND, getValues); CLog::Log(LOGDEBUG, "webserver [IN] %s %s %s", version, method, request.pathUrlFull.c_str()); if (!getValues.empty()) { std::string tmp; for (std::multimap<std::string, std::string>::const_iterator get = getValues.begin(); get != getValues.end(); ++get) { if (get != getValues.begin()) tmp += "; "; tmp += get->first + " = " + get->second; } CLog::Log(LOGDEBUG, "webserver [IN] Query arguments: %s", tmp.c_str()); } for (std::multimap<std::string, std::string>::const_iterator header = headerValues.begin(); header != headerValues.end(); ++header) CLog::Log(LOGDEBUG, "webserver [IN] %s: %s", header->first.c_str(), header->second.c_str()); } #endif if (!IsAuthenticated(server, connection)) return AskForAuthentication(connection); // check if this is the first call to AnswerToConnection for this request if (isNewRequest) { // parse the Range header and store it in the request object CHttpRanges ranges; bool ranged = ranges.Parse(GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE)); // look for a IHTTPRequestHandler which can take care of the current request for (std::vector<IHTTPRequestHandler *>::const_iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); ++it) { IHTTPRequestHandler *requestHandler = *it; if (requestHandler->CanHandleRequest(request)) { // we found a matching IHTTPRequestHandler so let's get a new instance for this request IHTTPRequestHandler *handler = requestHandler->Create(request); // if we got a GET request we need to check if it should be cached if (methodType == GET) { if (handler->CanBeCached()) { bool cacheable = true; // handle Cache-Control std::string cacheControl = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CACHE_CONTROL); if (!cacheControl.empty()) { std::vector<std::string> cacheControls = StringUtils::Split(cacheControl, ","); for (std::vector<std::string>::const_iterator it = cacheControls.begin(); it != cacheControls.end(); ++it) { std::string control = *it; control = StringUtils::Trim(control); // handle no-cache if (control.compare(HEADER_VALUE_NO_CACHE) == 0) cacheable = false; } } if (cacheable) { // handle Pragma (but only if "Cache-Control: no-cache" hasn't been set) std::string pragma = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_PRAGMA); if (pragma.compare(HEADER_VALUE_NO_CACHE) == 0) cacheable = false; } CDateTime lastModified; if (handler->GetLastModifiedDate(lastModified) && lastModified.IsValid()) { // handle If-Modified-Since or If-Unmodified-Since std::string ifModifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_MODIFIED_SINCE); std::string ifUnmodifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE); CDateTime ifModifiedSinceDate; CDateTime ifUnmodifiedSinceDate; // handle If-Modified-Since (but only if the response is cacheable) if (cacheable && ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince) && lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate) { struct MHD_Response *response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO); if (response == NULL) { CLog::Log(LOGERROR, "CWebServer: failed to create a HTTP 304 response"); return MHD_NO; } return FinalizeRequest(handler, MHD_HTTP_NOT_MODIFIED, response); } // handle If-Unmodified-Since else if (ifUnmodifiedSinceDate.SetFromRFC1123DateTime(ifUnmodifiedSince) && lastModified.GetAsUTCDateTime() > ifUnmodifiedSinceDate) return SendErrorResponse(connection, MHD_HTTP_PRECONDITION_FAILED, methodType); } // handle If-Range header but only if the Range header is present if (ranged && lastModified.IsValid()) { std::string ifRange = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_RANGE); if (!ifRange.empty() && lastModified.IsValid()) { CDateTime ifRangeDate; ifRangeDate.SetFromRFC1123DateTime(ifRange); // check if the last modification is newer than the If-Range date // if so we have to server the whole file instead if (lastModified.GetAsUTCDateTime() > ifRangeDate) ranges.Clear(); } } // pass the requested ranges on to the request handler handler->SetRequestRanged(!ranges.IsEmpty()); } } // if we got a POST request we need to take care of the POST data else if (methodType == POST) { conHandler->requestHandler = handler; // get the content-type of the POST data std::string contentType = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE); if (!contentType.empty()) { // if the content-type is application/x-ww-form-urlencoded or multipart/form-data we can use MHD's POST processor if (StringUtils::EqualsNoCase(contentType, MHD_HTTP_POST_ENCODING_FORM_URLENCODED) || StringUtils::EqualsNoCase(contentType, MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)) { // Get a new MHD_PostProcessor conHandler->postprocessor = MHD_create_post_processor(connection, MAX_POST_BUFFER_SIZE, &CWebServer::HandlePostField, (void*)conHandler.get()); // MHD doesn't seem to be able to handle this post request if (conHandler->postprocessor == NULL) { CLog::Log(LOGERROR, "CWebServer: unable to create HTTP POST processor for %s", url); delete conHandler->requestHandler; return SendErrorResponse(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, methodType); } } } // otherwise we need to handle the POST data ourselves which is done in the next call to AnswerToConnection // as ownership of the connection handler is passed to libmicrohttpd we must not destroy it *con_cls = conHandler.release(); return MHD_YES; } return HandleRequest(handler); } } } // this is a subsequent call to AnswerToConnection for this request else { // again we need to take special care of the POST data if (methodType == POST) { if (conHandler->requestHandler == NULL) { CLog::Log(LOGERROR, "CWebServer: cannot handle partial HTTP POST for %s request because there is no valid request handler available", url); return SendErrorResponse(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, methodType); } // we only need to handle POST data if there actually is data left to handle if (*upload_data_size > 0) { // either use MHD's POST processor if (conHandler->postprocessor != NULL) MHD_post_process(conHandler->postprocessor, upload_data, *upload_data_size); // or simply copy the data to the handler else conHandler->requestHandler->AddPostData(upload_data, *upload_data_size); // signal that we have handled the data *upload_data_size = 0; // we may need to handle more POST data which is done in the next call to AnswerToConnection // as ownership of the connection handler is passed to libmicrohttpd we must not destroy it *con_cls = conHandler.release(); return MHD_YES; } // we have handled all POST data so it's time to invoke the IHTTPRequestHandler else { if (conHandler->postprocessor != NULL) MHD_destroy_post_processor(conHandler->postprocessor); return HandleRequest(conHandler->requestHandler); } } // it's unusual to get more than one call to AnswerToConnection for none-POST requests, but let's handle it anyway else { for (std::vector<IHTTPRequestHandler *>::const_iterator it = m_requestHandlers.begin(); it != m_requestHandlers.end(); ++it) { IHTTPRequestHandler *requestHandler = *it; if (requestHandler->CanHandleRequest(request)) return HandleRequest(requestHandler->Create(request)); } } } CLog::Log(LOGERROR, "CWebServer: couldn't find any request handler for %s", url); return SendErrorResponse(connection, MHD_HTTP_NOT_FOUND, methodType); }