PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/cgi/example/fcgi/server1/main.cpp

http://github.com/darrengarvey/cgi
C++ | 93 lines | 47 code | 11 blank | 35 comment | 3 complexity | faed2554f159adf86a30c4d24b5a5dcd MD5 | raw file
  1. // -- server1/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. //[fcgi_server1
  11. //
  12. // This example simply echoes all variables back to the user. ie.
  13. // the environment and the parsed GET, POST and cookie variables.
  14. // Note that GET and cookie variables come from the environment
  15. // variables QUERY_STRING and HTTP_COOKIE respectively.
  16. //
  17. // It is a demonstration of how a 'server' can be used to abstract
  18. // away the differences between FastCGI and CGI requests.
  19. //
  20. // This is very similar to the fcgi_echo example.
  21. //
  22. #include <iostream>
  23. #include "boost/cgi/fcgi.hpp"
  24. #include "./server.hpp"
  25. using namespace std;
  26. using namespace boost::fcgi;
  27. // This function writes the title and map contents to the ostream in an
  28. // HTML-encoded format (to make them easier on the eye).
  29. template<typename OStream, typename Map>
  30. void format_map(OStream& os, Map& m, const std::string& title)
  31. {
  32. os<< "<h2>" << title << "</h2>";
  33. if (m.empty()) os<< "NONE<br />";
  34. for (typename Map::const_iterator i = m.begin(), end = m.end()
  35. ; i != end; ++i)
  36. {
  37. os<< "<b>" << i->first << "</b> = <i>" << i->second << "</i><br />";
  38. }
  39. }
  40. /// The handle_request function handles a single request.
  41. int handle_request(request& req, boost::system::error_code& ec)
  42. {
  43. // Construct a `response` object (makes writing/sending responses easier).
  44. response resp;
  45. // Responses in CGI programs require at least a 'Content-type' header. The
  46. // library provides helpers for several common headers:
  47. resp<< content_type("text/html")
  48. // You can also stream text to a response object.
  49. << "Hello there, universe!<p />";
  50. // Use the function defined above to show some of the request data.
  51. format_map(resp, req.env, "Environment Variables");
  52. format_map(resp, req.get, "GET Variables");
  53. format_map(resp, req.cookies, "Cookie Variables");
  54. // Response headers can be added at any time before send/flushing it:
  55. resp<< "<h3>Response Length</h3>" << resp.content_length()
  56. // response::content_length() returns the length of the *body*
  57. // of the response (ie. not including the headers).
  58. << content_length(resp);
  59. return commit(req, resp);
  60. }
  61. ///////////////////////////////////////////////////////////
  62. int main()
  63. ///////////////////////////////////////////////////////////
  64. try
  65. {
  66. server4 s(&handle_request);
  67. return s.run();
  68. }
  69. catch(boost::system::system_error& se){
  70. cerr<< "[fcgi] System error (" << se.code() << "): "
  71. << se.what() << endl;
  72. return 1313;
  73. }
  74. catch(exception& e){
  75. cerr<< "[fcgi] Exception: " << e.what() << endl;
  76. return 666;
  77. }
  78. catch(...){
  79. cerr<< "[fcgi] Unknown exception!" << endl;
  80. return 667;
  81. }
  82. //]