void setShouldConnectionBeClosed(const Request& request, Response& response) { auto headers = request.getSortedHeaders(); auto const& connection = headers["Connection"]; if (boost::iequals(connection, CLOSE)) { response.connectionShouldBeClosed(true); return; } if (boost::iequals(connection, KEEPALIVE)) { response.connectionShouldBeClosed(false); response.addHeader("Connection", KEEPALIVE); return; } if (request.major == 1 && request.minor == 1) { response.connectionShouldBeClosed(false); return; } response.connectionShouldBeClosed(true); return; }
static void handleRequest(Response& response) { response.addHeader("Content-Type", "text/html"); response << "<html><body>POST example." "<form method=\"POST\" action=\"" << wf::router().uri("post") << "\">" "Input 1: <input type=\"text\" name=\"input_1\" /> <br/>" "Input 2: <input type=\"text\" name=\"input_2\" /> <br/>" "<input type=\"submit\" />" "</form></body></html>"; }
static void handleRequest(Response& response) { const std::string& postData = wf::request().data; if (postData.length() == 17) { throw HttpAbortException(Response::FORBIDDEN); } response.addHeader("Content-Type", "text/plain"); response << "Submitted data: [" << postData << "]\n"; response << "Submitted data length: " << postData.length() << " bytes\n"; }
void HTTPServer::handleConnections (HTTPServer *server) { int r; #ifdef WIN32 int addrSz; #else socklen_t addrSz; #endif SOCKADDR_IN addr; SOCKET cli; while (server->isRunning) { r = listen (server->srv, 50); if (r == SOCKET_ERROR) { #ifdef WIN32 Sleep (500); #else sleep (1); #endif continue; } addrSz = sizeof addr; cli = accept (server->srv, (SOCKADDR*)&addr, &addrSz); if (cli == INVALID_SOCKET) continue; Response resp (cli); // doesn't throw exceptions. try { Request req (cli, &addr); // may throw exceptions. reqCallback *cb = getRequestHandler (&server->rootNode, req.getPath()); if (cb == NULL) { // error 404 resp.setStatus (404, "Not Found"); resp.addHeader ("Content-Type", "text/html; charset=US-ASCII"); stringstream stream; stream << "<html>"; stream << "<head><title>Not Found</title></head>"; stream << "<body><h1>Not Found</h1><div>The server couldn't find the request resource.</div><br /><hr /><div style=\"font-size:small;text-align:center;\">© 2013 Naim A. | <a href=\"http://udpt.googlecode.com/\">The UDPT Project</a></div></body>"; stream << "</html>"; string str = stream.str(); resp.write (str.c_str(), str.length()); } else { try { cb (server, &req, &resp); } catch (...) { resp.setStatus(500, "Internal Server Error"); resp.addHeader ("Content-Type", "text/html; charset=US-ASCII"); stringstream stream; stream << "<html>"; stream << "<head><title>Internal Server Error</title></head>"; stream << "<body><h1>Internal Server Error</h1><div>An Error Occurred while trying to process your request.</div><br /><hr /><div style=\"font-size:small;text-align:center;\">© 2013 Naim A. | <a href=\"http://udpt.googlecode.com/\">The UDPT Project</a></div></body>"; stream << "</html>"; string str = stream.str(); resp.write (str.c_str(), str.length()); } } resp.finalize(); } catch (ServerException &e) { // Error 400 Bad Request! } closesocket (cli); } }