本文整理汇总了C++中HTTPResponse::sendData方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPResponse::sendData方法的具体用法?C++ HTTPResponse::sendData怎么用?C++ HTTPResponse::sendData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse::sendData方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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) {
ESP_LOGD(LOG_TAG, "WebServer::processRequest: Matching: %.*s", (int) message->uri.len, message->uri.p);
HTTPResponse httpResponse = HTTPResponse(mgConnection);
/*
* 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(message->method.p, message->method.len, message->uri.p)) {
HTTPRequest httpRequest(message);
(*it).invoke(&httpRequest, &httpResponse);
ESP_LOGD(LOG_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;
filePath.reserve(httpResponse.getRootPath().length() + message->uri.len + 1);
filePath += httpResponse.getRootPath();
filePath.append(message->uri.p, message->uri.len);
ESP_LOGD(LOG_TAG, "Opening file: %s", filePath.c_str());
FILE* file = nullptr;
if (strcmp(filePath.c_str(), "/") != 0) {
file = fopen(filePath.c_str(), "rb");
}
if (file != nullptr) {
auto pData = (uint8_t*)malloc(MAX_CHUNK_LENGTH);
size_t read = fread(pData, 1, MAX_CHUNK_LENGTH, file);
if (read >= MAX_CHUNK_LENGTH) {
httpResponse.sendChunkHead();
httpResponse.sendChunk((char*) pData, read);
fclose(unfinishedConnection[mgConnection->sock]);
unfinishedConnection[mgConnection->sock] = file;
} else {
fclose(file);
httpResponse.sendData(pData, read);
}
free(pData);
} else {
// Handle unable to open file
httpResponse.setStatus(404); // Not found
httpResponse.sendData("");
}
} // processRequest
示例2: 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