本文整理汇总了C++中CDateTime::SetFromRFC1123DateTime方法的典型用法代码示例。如果您正苦于以下问题:C++ CDateTime::SetFromRFC1123DateTime方法的具体用法?C++ CDateTime::SetFromRFC1123DateTime怎么用?C++ CDateTime::SetFromRFC1123DateTime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CDateTime
的用法示例。
在下文中一共展示了CDateTime::SetFromRFC1123DateTime方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: IsRequestRanged
bool CWebServer::IsRequestRanged(const HTTPRequest& request, const CDateTime &lastModified) const
{
// parse the Range header and store it in the request object
CHttpRanges ranges;
bool ranged = ranges.Parse(HTTPRequestHandlerUtils::GetRequestHeaderValue(request.connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE));
// handle If-Range header but only if the Range header is present
if (ranged && lastModified.IsValid())
{
std::string ifRange = HTTPRequestHandlerUtils::GetRequestHeaderValue(request.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();
}
}
return !ranges.IsEmpty();
}
示例2: AnswerToConnection
//.........这里部分代码省略.........
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();
}
}
示例3: CreateFileDownloadResponse
int CWebServer::CreateFileDownloadResponse(struct MHD_Connection *connection, const string &strURL, HTTPMethod methodType, struct MHD_Response *&response, int &responseCode)
{
CFile *file = new CFile();
#ifdef WEBSERVER_DEBUG
CLog::Log(LOGDEBUG, "webserver [IN] %s", strURL.c_str());
multimap<string, string> headers;
if (GetRequestHeaderValues(connection, MHD_HEADER_KIND, headers) > 0)
{
for (multimap<string, string>::const_iterator header = headers.begin(); header != headers.end(); header++)
CLog::Log(LOGDEBUG, "webserver [IN] %s: %s", header->first.c_str(), header->second.c_str());
}
#endif
if (file->Open(strURL, READ_NO_CACHE))
{
bool getData = true;
bool ranged = false;
int64_t fileLength = file->GetLength();
// try to get the file's last modified date
CDateTime lastModified;
if (!GetLastModifiedDateTime(file, lastModified))
lastModified.Reset();
// get the MIME type for the Content-Type header
CStdString ext = URIUtils::GetExtension(strURL);
ext = ext.ToLower();
string mimeType = CreateMimeTypeFromExtension(ext.c_str());
if (methodType != HEAD)
{
int64_t firstPosition = 0;
int64_t lastPosition = fileLength - 1;
uint64_t totalLength = 0;
HttpFileDownloadContext *context = new HttpFileDownloadContext();
context->file = file;
context->rangesLength = fileLength;
context->contentType = mimeType;
context->boundaryWritten = false;
context->writePosition = 0;
if (methodType == GET)
{
// handle If-Modified-Since
string ifModifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, "If-Modified-Since");
if (!ifModifiedSince.empty() && lastModified.IsValid())
{
CDateTime ifModifiedSinceDate;
ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince);
if (lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate)
{
getData = false;
response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
responseCode = MHD_HTTP_NOT_MODIFIED;
}
}
if (getData)
{
// handle Range header
context->rangesLength = ParseRangeHeader(GetRequestHeaderValue(connection, MHD_HEADER_KIND, "Range"), fileLength, context->ranges, firstPosition, lastPosition);
// handle If-Range header but only if the Range header is present
if (!context->ranges.empty())
{
string ifRange = GetRequestHeaderValue(connection, MHD_HEADER_KIND, "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)
context->ranges.clear();
}
}
}
}
if (getData)
{
// if there are no ranges, add the whole range
if (context->ranges.empty() || context->rangesLength == fileLength)
{
if (context->rangesLength == fileLength)
context->ranges.clear();
context->ranges.push_back(HttpRange(0, fileLength - 1));
context->rangesLength = fileLength;
firstPosition = 0;
lastPosition = fileLength - 1;
}
else
responseCode = MHD_HTTP_PARTIAL_CONTENT;
// remember the total number of ranges
context->rangeCount = context->ranges.size();
//.........这里部分代码省略.........
示例4: CreateFileDownloadResponse
int CWebServer::CreateFileDownloadResponse(struct MHD_Connection *connection, const string &strURL, HTTPMethod methodType, struct MHD_Response *&response, int &responseCode)
{
CFile *file = new CFile();
if (file->Open(strURL, READ_NO_CACHE))
{
bool getData = true;
if (methodType != HEAD)
{
if (methodType == GET)
{
string ifModifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, "If-Modified-Since");
if (!ifModifiedSince.empty())
{
CDateTime ifModifiedSinceDate;
ifModifiedSinceDate.SetFromRFC1123DateTime(ifModifiedSince);
struct __stat64 statBuffer;
if (file->Stat(&statBuffer) == 0)
{
struct tm *time = localtime((time_t *)&statBuffer.st_mtime);
if (time != NULL)
{
CDateTime lastModified = *time;
if (lastModified.GetAsUTCDateTime() <= ifModifiedSinceDate)
{
getData = false;
response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO);
responseCode = MHD_HTTP_NOT_MODIFIED;
}
}
}
}
}
if (getData)
response = MHD_create_response_from_callback(file->GetLength(),
2048,
&CWebServer::ContentReaderCallback, file,
&CWebServer::ContentReaderFreeCallback);
if (response == NULL)
{
file->Close();
delete file;
return MHD_NO;
}
}
else
{
getData = false;
CStdString contentLength;
contentLength.Format("%I64d", file->GetLength());
response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO);
if (response == NULL)
{
file->Close();
delete file;
return MHD_NO;
}
MHD_add_response_header(response, "Content-Length", contentLength);
}
// set the Content-Type header
CStdString ext = URIUtils::GetExtension(strURL);
ext = ext.ToLower();
const char *mime = CreateMimeTypeFromExtension(ext.c_str());
if (mime)
MHD_add_response_header(response, "Content-Type", mime);
// set the Last-Modified header
struct __stat64 statBuffer;
if (file->Stat(&statBuffer) == 0)
{
struct tm *time = localtime((time_t *)&statBuffer.st_mtime);
if (time != NULL)
{
CDateTime lastModified = *time;
MHD_add_response_header(response, "Last-Modified", lastModified.GetAsRFC1123DateTime());
}
}
// set the Expires header
CDateTime expiryTime = CDateTime::GetCurrentDateTime();
if (mime && strncmp(mime, "text/html", 9) == 0)
expiryTime += CDateTimeSpan(1, 0, 0, 0);
else
expiryTime += CDateTimeSpan(365, 0, 0, 0);
MHD_add_response_header(response, "Expires", expiryTime.GetAsRFC1123DateTime());
// only close the CFile instance if libmicrohttpd doesn't have to grab the data of the file
if (!getData)
{
file->Close();
delete file;
}
}
else
{
//.........这里部分代码省略.........
示例5: HandlePartialRequest
int CWebServer::HandlePartialRequest(struct MHD_Connection *connection, ConnectionHandler* connectionHandler, const HTTPRequest& request, const char *upload_data, size_t *upload_data_size, void **con_cls)
{
std::unique_ptr<ConnectionHandler> conHandler(connectionHandler);
// 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 = nullptr;
if (!IsAuthenticated(request))
return AskForAuthentication(request);
// check if this is the first call to AnswerToConnection for this request
if (isNewRequest)
{
// look for a IHTTPRequestHandler which can take care of the current request
auto handler = FindRequestHandler(request);
if (handler != nullptr)
{
// if we got a GET request we need to check if it should be cached
if (request.method == GET)
{
if (handler->CanBeCached())
{
bool cacheable = IsRequestCacheable(request);
CDateTime lastModified;
if (handler->GetLastModifiedDate(lastModified) && lastModified.IsValid())
{
// handle If-Modified-Since or If-Unmodified-Since
std::string ifModifiedSince = HTTPRequestHandlerUtils::GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_MODIFIED_SINCE);
std::string ifUnmodifiedSince = HTTPRequestHandlerUtils::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 = create_response(0, nullptr, MHD_NO, MHD_NO);
if (response == nullptr)
{
CLog::Log(LOGERROR, "CWebServer[%hu]: failed to create a HTTP 304 response", m_port);
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(request, MHD_HTTP_PRECONDITION_FAILED, request.method);
}
// pass the requested ranges on to the request handler
handler->SetRequestRanged(IsRequestRanged(request, lastModified));
}
}
// if we got a POST request we need to take care of the POST data
else if (request.method == POST)
{
// as ownership of the connection handler is passed to libmicrohttpd we must not destroy it
SetupPostDataProcessing(request, conHandler.get(), handler, con_cls);
// as ownership of the connection handler has been passed to libmicrohttpd we must not destroy it
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 (request.method == POST)
{
// process additional / remaining POST data
if (ProcessPostData(request, conHandler.get(), upload_data, upload_data_size, con_cls))
{
// as ownership of the connection handler has been passed to libmicrohttpd we must not destroy it
conHandler.release();
return MHD_YES;
}
// finalize POST data processing
FinalizePostDataProcessing(conHandler.get());
// check if something went wrong while handling the POST data
if (conHandler->errorStatus != MHD_HTTP_OK)
return SendErrorResponse(request, conHandler->errorStatus, request.method);
// we have handled all POST data so it's time to invoke the IHTTPRequestHandler
//.........这里部分代码省略.........
示例6: CreateFileDownloadResponse
int CWebServer::CreateFileDownloadResponse(struct MHD_Connection *connection, const string &strURL, HTTPMethod methodType, struct MHD_Response *&response, int &responseCode)
{
boost::shared_ptr<CFile> file = boost::make_shared<CFile>();
#ifdef WEBSERVER_DEBUG
CLog::Log(LOGDEBUG, "webserver [IN] %s", strURL.c_str());
multimap<string, string> headers;
if (GetRequestHeaderValues(connection, MHD_HEADER_KIND, headers) > 0)
{
for (multimap<string, string>::const_iterator header = headers.begin(); header != headers.end(); header++)
CLog::Log(LOGDEBUG, "webserver [IN] %s: %s", header->first.c_str(), header->second.c_str());
}
#endif
if (!file->Open(strURL, READ_NO_CACHE))
{
CLog::Log(LOGERROR, "WebServer: Failed to open %s", strURL.c_str());
return SendErrorResponse(connection, MHD_HTTP_NOT_FOUND, methodType);
}
bool getData = true;
bool ranged = false;
int64_t fileLength = file->GetLength();
// try to get the file's last modified date
CDateTime lastModified;
if (!GetLastModifiedDateTime(file.get(), lastModified))
lastModified.Reset();
// get the MIME type for the Content-Type header
std::string ext = URIUtils::GetExtension(strURL);
StringUtils::ToLower(ext);
string mimeType = CreateMimeTypeFromExtension(ext.c_str());
if (methodType != HEAD)
{
int64_t firstPosition = 0;
int64_t lastPosition = fileLength - 1;
uint64_t totalLength = 0;
std::auto_ptr<HttpFileDownloadContext> context(new HttpFileDownloadContext());
context->file = file;
context->rangesLength = fileLength;
context->contentType = mimeType;
context->boundaryWritten = false;
context->writePosition = 0;
if (methodType == GET)
{
bool cacheable = true;
// handle Cache-Control
string cacheControl = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CACHE_CONTROL);
if (!cacheControl.empty())
{
vector<string> cacheControls = StringUtils::Split(cacheControl, ",");
for (vector<string>::const_iterator it = cacheControls.begin(); it != cacheControls.end(); ++it)
{
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)
string pragma = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_PRAGMA);
if (pragma.compare(HEADER_VALUE_NO_CACHE) == 0)
cacheable = false;
}
if (lastModified.IsValid())
{
// handle If-Modified-Since or If-Unmodified-Since
string ifModifiedSince = GetRequestHeaderValue(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_MODIFIED_SINCE);
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)
{
getData = false;
response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
if (response == NULL)
return MHD_NO;
responseCode = MHD_HTTP_NOT_MODIFIED;
}
// handle If-Unmodified-Since
else if (ifUnmodifiedSinceDate.SetFromRFC1123DateTime(ifUnmodifiedSince) &&
lastModified.GetAsUTCDateTime() > ifUnmodifiedSinceDate)
return SendErrorResponse(connection, MHD_HTTP_PRECONDITION_FAILED, methodType);
}
//.........这里部分代码省略.........