一个html页面请求的响应处理过程
1 html测试页面
并在html页面引入了 jpg、css、js文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FAW测试</title>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/faw.js"> </script>
</head>
<body>
<h1>FAW测试demo1</h1>
<p>这是一个段落</p>
<!-- 这是一个注释 -->
<div class="col-md-3">
按钮:
<button type="button" class="col-md-offset-4 col-lg-offset-4 col-xl-offset-4 option_btn btn btn-info btn1" id="beg">开始</button>
</div>
<div class="col-md-4">
输入框:
<input type="text" value="189.8.18.8" id="ip" placeholder="服务IP">
</div>
<image src = "images/01.jpg" width="800" height="500">
</body>
</html>
2 后台处理过程
处理处理html页面之外,还要考虑处理jpg、css以及js文件
void process_request(T ctx,G cb)
{
std::string uri = ctx->uri();
ctx->AddResponseHeader("Server", "XboxServer");
std::string path = "H:\\TEST\\2212\\htmltest";
std::string::size_type pos = uri.rfind('.');
std::string url_suffix = uri.substr(pos == std::string::npos ? uri.length() : pos + 1);
std::string file_path = path + uri;
if (url_suffix == "html" || url_suffix == "css" || url_suffix == "js"){
if (url_suffix == "html"){
ctx->AddResponseHeader("Content-Type", "text/html");
}else if (url_suffix == "css"){
ctx->AddResponseHeader("Content-Type", "text/css");
}else if (url_suffix == "js") {
ctx->AddResponseHeader("Content-Type", "text/javascript");
}
QFile file(QString::fromStdString(file_path));
file.open(QFile::ReadOnly);
QString file_content = file.readAll();
std::string content_str = file_content.toStdString();
cb(content_str);
}else if(url_suffix == "jpg" || url_suffix == "png"){
if (url_suffix == "jpg"){
ctx->AddResponseHeader("Content-Type", "image/jpeg");
}else if (url_suffix == "png") {
ctx->AddResponseHeader("Content-Type", "image/png");
}
std::ifstream istream(file_path, std::ios::binary);
std::string
str((std::istreambuf_iterator<char(istream)),std::istreambuf_iterator<char>());
cb(str);
}else {
cb("other");
}
}
3 显示