本文整理汇总了C++中HTTPResponse::setRootPath方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPResponse::setRootPath方法的具体用法?C++ HTTPResponse::setRootPath怎么用?C++ HTTPResponse::setRootPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse::setRootPath方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: processRequest
/**
* @brief Process an incoming HTTP request.
*
* We look at the path of the request and see if it has a matching path handler. If it does,
* we invoke the handler function. If it does not, we try and find a file on the file system
* that would resolve to the path.
*
* @param [in] mgConnection The network connection on which the request was received.
* @param [in] message The message representing the request.
*/
void WebServer::processRequest(struct mg_connection *mgConnection, struct http_message* message) {
std::string uri = mgStrToString(message->uri);
ESP_LOGD(tag, "WebServer::processRequest: Matching: %s", uri.c_str());
HTTPResponse httpResponse = HTTPResponse(mgConnection);
httpResponse.setRootPath(getRootPath());
/*
* Iterate through each of the path handlers looking for a match with the method and specified path.
*/
std::vector<PathHandler>::iterator it;
for (it = m_pathHandlers.begin(); it != m_pathHandlers.end(); ++it) {
if ((*it).match(mgStrToString(message->method), uri)) {
HTTPRequest httpRequest(message);
(*it).invoke(&httpRequest, &httpResponse);
ESP_LOGD(tag, "Found a match!!");
return;
}
} // End of examine path handlers.
// Because we reached here, it means that we did NOT match a handler. Now we want to attempt
// to retrieve the corresponding file content.
std::string filePath = httpResponse.getRootPath() + uri;
ESP_LOGD(tag, "Opening file: %s", filePath.c_str());
FILE *file = fopen(filePath.c_str(), "r");
if (file != nullptr) {
fseek(file, 0L, SEEK_END);
size_t length = ftell(file);
fseek(file, 0L, SEEK_SET);
uint8_t *pData = (uint8_t *)malloc(length);
fread(pData, length, 1, file);
fclose(file);
httpResponse.sendData(pData, length);
free(pData);
} else {
// Handle unable to open file
httpResponse.setStatus(404); // Not found
httpResponse.sendData("");
}
} // processRequest