bool client::send_request(request &req, response &resp) { if (!stream_->is_open() || stream_->eof() || stream_->fail() || stream_->bad()) return false; // Make sure there is no pending data in the last response resp.clear(); req.accept_compressed(auto_decompress_); resp.set_auto_decompression(auto_decompress_); if(!req.write(*stream_)) return false; if (!stream_->is_open() || stream_->eof() || stream_->fail() || stream_->bad()) return false; //if (!stream_.is_open()) return false; return resp.read(*stream_) && (resp.status_code!=http_status_code::INVALID_STATUS); }
void response::stock_reply(int status_code, response & res) { res.set_status(status_code); res.set_header("Content-Type", "text/plain"); switch (status_code) { // 2XX case served::status_2XX::OK: res << "Successful"; break; case served::status_2XX::NO_CONTENT: break; // 4XX case served::status_4XX::BAD_REQUEST: res << "Detected a bad request"; break; case served::status_4XX::NOT_FOUND: res << "Resource not found"; break; case served::status_4XX::REQUEST_TIMEOUT: res << "The request timed out"; break; case served::status_4XX::METHOD_NOT_ALLOWED: res << "Method is not supported for this resource"; break; case served::status_4XX::UNAUTHORIZED: res << "Client is unauthorized to access this resource"; break; case served::status_4XX::FORBIDDEN: res << "Access to this resource is forbidden"; break; case served::status_4XX::IM_A_TEAPOT: res << "I'm a teapot"; break; case served::status_4XX::TOO_MANY_REQUESTS: res << "Too many requests have been detected from this client"; break; // 5XX case served::status_5XX::INTERNAL_SERVER_ERROR: res << "Encountered an internal server error"; break; default: // TODO: Maybe throw exception here? break; } }
std::string application::process(request & req, response & res) throw() { unsigned int result_code = 200; view_function_t view = get_route(req.verb(), req.path()); std::string str; // Site response. try { // Check if specified view exists. // If not, throw "404" - view does not exists. if (!view) throw http_error(404); // Run view. view(req, res); // Generated response. str = res.stream().str(); } catch (web::http_error const & e) { // Change HTTP result. result_code = e.error_code(); // Generated response // (before the exception was raised) str = res.stream().str(); } catch (std::exception const & e) { // We know what does this error (could) mean. result_code = 500; // Exception description is our response. str = e.what(); } catch (...) { // We do not have idea what this error means. result_code = 500; } // Construct a valid HTTP response. std::stringstream output; output << "HTTP/1.1 " << result_code << " OK\r\n" "Content-Type:text/html\r\n" "Content-Length: " << str.length() << "\r\n\r\n" << str; return output.str(); }
bool send(response &resp) { bool ret=false; if (bad()) return false; if(write_timeout_>std::chrono::seconds(0)) { // Set write timeout watchdog_timer_->expires_from_now(write_timeout_); } ret=resp.write(stream()); if (!resp.keep_alive) { stream().close(); return false; } return ret; }
inline bool operator!=(response const& l, response const& r) { return !l.equals(r); }
inline void swap(response& l, response& r) { l.swap(r); }
inline void swap(response &lhs, response &rhs) noexcept { lhs.swap(rhs); }