http_response www_server::on_request(const http_request &req) { http_response resp; // we are support only HTTP GET method if (req.get_method() != http_method::get) { resp.set_status(http_error::method_not_allowed); resp.set_allow(http_method::get); resp.set_content("The POST requests are not supported yet."); return resp; } // obtain the name of the file requested url u(req.get_path_info()); u.normalize(root_dir); // load file content to the server response try { ifstream in(u.path.c_str()); // if file can't be open, throw an error if (!in) throw dbp::exception(string("File not found: ") + u.path); // set file content type resp.set_content_type(mime(u.path)); // load file resp.add_content(in); } catch (dbp::exception &e) { // return error message resp.set_status(http_error::not_found); resp.set_content(e.what()); } return resp; }
int operator() (const http_request& request, http_response* response) { char buf[256] = {0}; std::string resp_body; std::vector<pid_t>::iterator it; std::string path_str; time_t time_now; ///只允许get方法 http_request::request_method method = request.get_method(); if (method != http_request::rGet) goto err; ///只允许请求/ path_str =request.get_path(); if (path_str != "/") return -1; if (request.get_request_head("Connection") == std::string("close")) { response->add_header("Connection", "close"); response->set_close_conn(true); } snprintf(buf, sizeof(buf), "uid: %d \r\n", process_info::uid()); resp_body = buf + process_info::username() + "\r\n" + process_info::hostname() + "\r\n" + "\r\n"; for (it = m_pid_vec_.begin(); it != m_pid_vec_.end(); ++it) { resp_body += process_info::get_proc_status(*it) + "\r\n"; snprintf(buf, sizeof(buf), "opend Files: %d \r\n\r\n", process_info::get_opened_files(*it)); resp_body += buf; } response->set_status_code(http_response::k200Ok); response->set_status_str("OK"); response->add_header("Content-Type", "text/plain"); response->add_header("Server", "async_serv"); response->add_header("Connection", "keep-alive"); ::time(&time_now); ::strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", ::gmtime(&time_now)); response->add_header("Date", buf); response->set_body(resp_body); return 0; err: return default_404_response(request, response); }
// execute event handler http_response process_request(const http_request &req) { // initialize the response http_response resp; // we are only support GET method in this demo if (req.get_method() == http_method::get) { // initialize the response with greeting message resp.set_content_type("text/plain"); resp.set_content("Hello, world!"); } else { // send the response with "405 Method Not Allowed" status code resp.set_status(http_error::method_not_allowed); resp.set_allow(http_method::get); } // send the response to the client return resp; };
void RootResource::render(const http_request &request, http_response **response) { // Execute the appropriate handler function depending on the path or return a 404 std::string path = request.get_path(); std::string method = request.get_method(); if (this->paths.find(path) != this->paths.end()) { // Found path - do we have an entry for the HTTP method being used? if (this->paths[path].find(method) != this->paths[path].end()) { // Found method so execute it and return // Sorry - this is dirty! (this->*(this->paths[path][method]))(request, response); return; } } // If we fall through to here then we didn't find anything - return 404 *response = new http_response(http_response_builder("Not Found", 404).string_response()); }