代码拉取完成,页面将自动刷新
同步操作将从 xiaozhuai/efserv 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
* @author : xiaozhuai
* @date : 17/3/19
*/
#include "Response.h"
string Response::dir_indexs_html;
string Response::err_html;
void Response::loadTpl() {
FileHandler dirIndexsTpl(SERV_ENV.getConfig(KEY_DIR_INDEXS_TPL, DEFAULT_DIR_INDEXS_TPL));
FileHandler errTpl(SERV_ENV.getConfig(KEY_ERR_TPL, DEFAULT_ERR_TPL));
if(dirIndexsTpl.exist()){
dir_indexs_html = dirIndexsTpl.readAsText();
}else{
dir_indexs_html = "{{file_list}}";
}
if(errTpl.exist()){
err_html = errTpl.readAsText();
}else{
err_html = "{{status_code}} {{msg}}";
}
}
void Response::respondHeader(int fd, string mimetype, off_t content_length) {
header(fd, "HTTP/1.1 200 OK");
header(fd, "Content-Type", mimetype);
header(fd, "Content-Length", to_string(content_length));
header_end(fd);
}
void Response::respondContent(int fd, const char *content, size_t length) {
send(fd, content, length, 0);
}
string assign(string format, string key, string value){
key = "{{" + key + "}}";
format = StringUtils::replaceAll(format, key, value);
return format;
}
void Response::respondErr(int fd, int status_code) {
string msg;
switch (status_code){
case 403:
msg = "Forbidden";
break;
case 404:
msg = "Not Found";
break;
default:
status_code = 500;
case 500:
msg = "Internal Server Error";
break;
}
header(fd, "HTTP/1.1 "+to_string(status_code)+" "+msg);
string content;
content = assign(err_html, "status_code", to_string(status_code));
content = assign(content, "msg", msg);
size_t len = content.length();
header(fd, "Content-Type", "text/html");
header(fd, "Content-Length", to_string(len));
header_end(fd);
respondContent(fd, content.c_str(), len);
}
void Response::respondIndexs(int fd, vector<FileHandler> files, string url) {
header(fd, "HTTP/1.1 200 OK");
header(fd, "Content-Type", "text/html");
size_t fileCount = files.size();
string fileListJson = "[\n";
for(int i=0; i<fileCount; i++){
FileHandler file = files[i];
/**
* ignore the url that are denied
*/
string tmp = url;
rtrim(tmp, '/');
tmp += "/" + file.getName();
if(!ACCESS_RULE.permissible(tmp)){
continue;
}
string obj = tfm::format(
" {\n"
" \"is_file\":%s,\n"
" \"is_link\":%s,\n"
" \"name\":\"%s\",\n"
" \"ext\":\"%s\",\n"
" \"mime_type\":\"%s\",\n"
" \"ctime\":%ld,\n"
" \"mtime\":%ld,\n"
" \"atime\":%ld\n"
" }",
file.isFile() ? "true" : "false",
file.isLink() ? "true" : "false",
file.getName(),
file.getExt(),
file.getMimeType(),
file.getCreateTime(),
file.getModifyTime(),
file.getAccessTime()
);
if(i==fileCount-1) obj += "\n";
else obj += ",\n";
fileListJson += obj;
}
fileListJson += "]";
string content;
content = assign(dir_indexs_html, "file_list", fileListJson);
content = assign(content, "url", url);
size_t len = content.length();
header(fd, "Content-Length", to_string(len));
header_end(fd);
respondContent(fd, content.c_str(), len);
}
void Response::respondRedirection(int fd, int status_code, string location) {
string msg;
switch(status_code){
case 300:
msg = "Multiple Choices";
break;
case 301:
msg = "Moved Permanently";
break;
default:
status_code = 302;
case 302:
msg = "Found (Moved Temporarily)";
break;
case 303:
msg = "See Other";
break;
case 304:
msg = "Not Modified";
break;
case 305:
msg = "Use Proxy";
break;
case 306:
msg = "Switch Proxy (Unused)";
break;
case 307:
msg = "Temporary Redirect";
break;
}
header(fd, "HTTP/1.1 "+to_string(status_code)+" "+msg);
header(fd, "Location", location);
header_end(fd);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。