PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/cgi/example/fcgi/server1/server.hpp

http://github.com/darrengarvey/cgi
C++ Header | 81 lines | 43 code | 11 blank | 27 comment | 3 complexity | 02e48e41d2475e14dca370010a46f6f6 MD5 | raw file
  1. // -- server1/server.hpp --
  2. //
  3. // Copyright (c) Darren Garvey 2007.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. ////////////////////////////////////////////////////////////////
  9. //
  10. // standard includes
  11. #include <set>
  12. // external includes
  13. #include <boost/function.hpp>
  14. // internal includes
  15. #include <boost/cgi/fcgi.hpp>
  16. /// The server is used to abstract away protocol-specific setup of requests.
  17. /**
  18. * This server only works with FastCGI. Later examples will show you how to
  19. * make a protocol-independent server.
  20. *
  21. * Later examples will demonstrate making protocol-independent servers.
  22. * (**FIXME**)
  23. */
  24. class server4
  25. {
  26. public:
  27. typedef boost::fcgi::request request_type;
  28. typedef boost::function<
  29. int ( request_type&
  30. , boost::system::error_code&)
  31. > function_type;
  32. server4(const function_type& handler)
  33. : handler_(handler)
  34. , service_()
  35. , acceptor_(service_)
  36. {}
  37. int run()
  38. {
  39. // Create a new request (on the heap - uses boost::shared_ptr<>).
  40. request_type::pointer new_request = request_type::create(service_);
  41. // Add the request to the set of existing requests.
  42. requests_.insert(new_request);
  43. int ret(0);
  44. for (;;)
  45. {
  46. boost::system::error_code ec;
  47. acceptor_.accept(*new_request, ec);
  48. if (ec)
  49. {
  50. std::cerr<< "Error accepting: " << ec.message() << std::endl;
  51. return 5;
  52. }
  53. // Load in the request data so we can access it easily.
  54. // Read and parse POST data.
  55. new_request->load(boost::fcgi::parse_post, ec);
  56. // Call the request handler and capture the result of handling
  57. // the request.
  58. ret = handler_(*new_request, ec);
  59. // A non-zero return value indicates an error.
  60. if (ret)
  61. break;
  62. }
  63. return ret;
  64. }
  65. private:
  66. function_type handler_;
  67. boost::fcgi::service service_;
  68. boost::fcgi::acceptor acceptor_;
  69. std::set<request_type::pointer> requests_;
  70. };