PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/cgi/example/cgi/DebugServer/hello.cpp

http://github.com/darrengarvey/cgi
C++ | 61 lines | 51 code | 6 blank | 4 comment | 4 complexity | e1d78eef6fb59fee65ccbbf8cd1ec989 MD5 | raw file
  1. // hello.cpp : Defines the entry point for the application.
  2. //
  3. #include <iostream>
  4. // Include the CGI headers.
  5. #include <boost/cgi/cgi.hpp>
  6. // Uses a server class that catches and reports errors in your handler.
  7. #include "TracingServer.hpp"
  8. using boost::cgi::request;
  9. using boost::cgi::response;
  10. using boost::cgi::header;
  11. using boost::cgi::content_type;
  12. int cgi_handler(request& req, response& resp)
  13. {
  14. resp<< header("X-Custom-Header", "some value")
  15. << content_type("text/html")
  16. << "<html>"
  17. "<head>"
  18. "<title>Debug Server</title>"
  19. "</head>"
  20. "<body>"
  21. "<h1>Debugging Server Example</h1>"
  22. "<p>"
  23. "The server used in this example will catch exceptions thrown by "
  24. "your request handler, or a non-zero return value. It will print "
  25. "some presumably helpful info about what might have caused the "
  26. "problem - obviously this is just an example, which you'd "
  27. "probably want to ammend."
  28. "</p>"
  29. "<p>"
  30. "The handler in this example will throw a std::runtime_error if "
  31. "you pass 'badger=bait!' to the script, for example by following "
  32. "<a href=\"?badger=bait%21\">this link</a>."
  33. "</p>"
  34. "<p>"
  35. "The handler in this example will also return false if you pass "
  36. "pass 'spam' to the script, for example by clicking "
  37. "<a href=\"?spam\">here</a>."
  38. "</p>"
  39. "<p>"
  40. "Finally, you can simulate an error and get traceback details "
  41. "regardless of whether the script completed by passing 'debug=1',"
  42. " or by following <a href=\"?debug=1\">this link</a>."
  43. "</p>"
  44. "</body>"
  45. "</html>";
  46. if (req.get["badger"] == "bait!") throw std::runtime_error("You asked for an error, you got one.");
  47. else if (req.get.count("spam")) return 33;
  48. return 0;
  49. }
  50. int main()
  51. {
  52. TracingServer server;;
  53. server.run(&cgi_handler);
  54. return 0;
  55. }