PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/cgi/example/xcgi/basic/main.cpp

http://github.com/darrengarvey/cgi
C++ | 88 lines | 52 code | 11 blank | 25 comment | 1 complexity | 1ac801c2822186e1ff582ef689bcfdaf MD5 | raw file
  1. // -- main.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. //[xcgi_basic
  11. //
  12. // A completely stripped-down, protocol-independent program, outputs only
  13. // "Hello there, universe."
  14. //
  15. // example/xcgi/basic$ `bjam install`
  16. //
  17. // will install the example to your cgi-bin and fcgi-bin. Look
  18. // [link ../../doc.qbk here] for more information, including how to set these.
  19. //
  20. #include <boost/cgi.hpp>
  21. #include <boost/cgi/fcgi.hpp>
  22. using namespace std;
  23. using namespace boost::fcgi;
  24. template<typename Request, typename Response>
  25. int handle_request(Request& req, Response& resp)
  26. {
  27. // This is a minimal response. The content_type(...) may go before or after
  28. // the response text.
  29. resp<< content_type("text/plain")
  30. << "Hello there, universe.";
  31. return commit(req, resp, 0);
  32. }
  33. /// Handle a vanilla CGI request
  34. int handle_request()
  35. {
  36. boost::cgi::request req;
  37. response resp;
  38. return handle_request(req,resp);
  39. }
  40. /// Handle a FastCGI request
  41. template<typename Acceptor>
  42. int handle_request(Acceptor& a)
  43. {
  44. request req(a.protocol_service()); // Our request.
  45. int ret = 0;
  46. for (;;) // Handle requests until something goes wrong
  47. // (an exception will be thrown).
  48. {
  49. a.accept(req);
  50. response resp; // A response object to make our lives easier.
  51. ret = handle_request(req, resp);
  52. }
  53. return ret;
  54. }
  55. int main()
  56. {
  57. try
  58. {
  59. service s; // This becomes useful with async operations.
  60. acceptor a(s);
  61. return a.is_cgi() ?
  62. handle_request() // Start it as a CGI request.
  63. : handle_request(a); // Start it as a FastCGI request.
  64. }
  65. catch(boost::system::system_error& err)
  66. {
  67. std::cerr<< "System error " << err.code() << ": "
  68. << err.what() << std::endl;
  69. return 1;
  70. }
  71. catch(...)
  72. {
  73. std::cerr<< "Unknown error!" << std::endl;
  74. return 2;
  75. }
  76. }
  77. //]