1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| #include "httpresponse.h" #include <fcntl.h> #include <string> #include <sys/mman.h> #include <sys/stat.h>
const std::unordered_map<std::string, std::string> HttpResponse::SUFFIX_TYPE = { {".html", "text/html"}, {".xml", "text/xml"}, {".xhtml", "application/xhtml+xml"}, {".txt", "text/plain"}, {".rtf", "application/rtf"}, {".pdf", "application/pdf"}, {".word", "application/nsword"}, {".png", "image/png"}, {".gif", "image/gif"}, {".jpg", "image/jpeg"}, {".jpeg", "image/jpeg"}, {".au", "audio/basic"}, {".mpeg", "video/mpeg"}, {".mpg", "video/mpeg"}, {".avi", "video/x-msvideo"}, {".gz", "application/x-gzip"}, {".tar", "application/x-tar"}, {".css", "text/css "}, {".js", "text/javascript "}, };
const std::unordered_map<int, std::string> HttpResponse::CODE_STATUS = { {200, "OK"}, {400, "Bad Request"}, {403, "Forbidden"}, {404, "Not Found"}, };
const std::unordered_map<int, std::string> HttpResponse::CODE_PATH = { {400, "/400.html"}, {403, "/403.html"}, {404, "/404.html"}, };
HttpResponse::HttpResponse() { code_ = -1; path_ = srcDir_ = ""; isKeepAlive_ = false; mmFile_ = nullptr; mmFileStat_ = {0}; }
HttpResponse::~HttpResponse() { UnmapFile(); }
void HttpResponse::Init(const std::string &srcDir, std::string &path, bool isKeepAlive, int code) { assert(srcDir != ""); if (mmFile_) UnmapFile(); code_ = code; isKeepAlive_ = isKeepAlive; path_ = path; srcDir_ = srcDir; mmFile_ = nullptr; mmFileStat_ = { 0 }; }
void HttpResponse::MakeResponse(Buffer &buff) { if (stat((srcDir_ + path_).data(), &mmFileStat_) < 0 || S_ISDIR(mmFileStat_.st_mode)) code_ = 404; else if (!(mmFileStat_.st_mode & S_IROTH)) { code_ = 403; } else if (code_ == -1) { code_ = 200; } ErrorHtml_(); AddStateLine_(buff); AddHeader_(buff); AddContent_(buff); }
void HttpResponse::UnmapFile() { if (mmFile_) { munmap(mmFile_, mmFileStat_.st_size); mmFile_ = nullptr; } }
char *HttpResponse::File() { return mmFile_; } size_t HttpResponse::FileLen() const { return mmFileStat_.st_size; }
void HttpResponse::ErrorContent(Buffer &buff, std::string message) { std::string body; std::string status; body += "<html><title>Error</title>"; body += "<body bgcolor=\"ffffff\">"; if (CODE_STATUS.count(code_) == 1) { status = CODE_STATUS.find(code_)->second; } else { status = "Bad Request"; } body += std::to_string(code_) + " : " + status + "\n"; body += "<p>" + message + "</p>"; body += "<hr><em>TinyWebServer</em></body></html>";
buff.Append("Content-length: " + std::to_string(body.size()) + "\r\n\r\n"); buff.Append(body); }
int HttpResponse::Code() const { return code_; }
void HttpResponse::AddStateLine_(Buffer &buff) { std::string status; if (CODE_STATUS.count(code_) == 1) { status = CODE_STATUS.find(code_)->second; } else { code_ = 400; status = CODE_STATUS.find(code_)->second; } buff.Append("HTTP/1.1 " + std::to_string(code_) + " " + status + "\r\n"); }
void HttpResponse::AddHeader_(Buffer &buff) { buff.Append("Connection: "); if (isKeepAlive_) { buff.Append("keep-alive\r\n"); buff.Append("keep-alive: max=6, timeout=120\r\n"); } else { buff.Append("close\r\n"); } buff.Append("Content-type: " + GetFileType_() + "\r\n"); }
void HttpResponse::AddContent_(Buffer &buff) { int srcFd = open((srcDir_ + path_).data(), O_RDONLY); if (srcFd < 0) { ErrorContent(buff, "File NotFound!"); return; } LOG_DEBUG("file path %s", (srcDir_ + path_).data()); int *mmRet = (int *)mmap(nullptr, mmFileStat_.st_size, PROT_READ, MAP_PRIVATE, srcFd, 0); if (*mmRet == -1) { ErrorContent(buff, "File NotFound!"); return; } mmFile_ = (char *)mmRet; close(srcFd); buff.Append("Content-length: " + std::to_string(mmFileStat_.st_size) + "\r\n\r\n"); }
void HttpResponse::ErrorHtml_() { if (CODE_PATH.count(code_) == 1) { path_ = CODE_PATH.find(code_)->second; stat((srcDir_ + path_).data(), &mmFileStat_); } }
std::string HttpResponse::GetFileType_() { std::string::size_type idx = path_.find_last_of('.'); if (idx == std::string::npos) return "text/plain"; std::string suffix = path_.substr(idx, path_.length() - idx);
if (SUFFIX_TYPE.count(suffix) == 1) return SUFFIX_TYPE.find(suffix)->second;
return "text/plain"; }
|