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

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

http://github.com/darrengarvey/cgi
C++ | 82 lines | 50 code | 15 blank | 17 comment | 1 complexity | c594a2f58b4f3ca4ab18402ccab0bde4 MD5 | raw file
  1. // -- main.hpp --
  2. //
  3. // Copyright (c) Darren Garvey 2009.
  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_upload
  11. //
  12. #include <boost/cgi/fcgi.hpp>
  13. #include <boost/cgi/utility/stencil.hpp>
  14. namespace cgi = boost::fcgi;
  15. namespace http = boost::fcgi::http;
  16. int main()
  17. {
  18. try {
  19. cgi::service service;
  20. // Construct a request. Parses all GET, POST and environment data,
  21. // as well as cookies.
  22. cgi::request req(service);
  23. cgi::acceptor acceptor(service);
  24. int ret(0);
  25. for(;;)
  26. {
  27. acceptor.accept(req);
  28. try {
  29. req.load(cgi::parse_all);
  30. // Using a response is the simplest way to write data back to the client.
  31. cgi::stencil resp;
  32. resp<< cgi::content_type("text/html");
  33. resp.set("filename", req.post["file"]);
  34. resp.set("path", req.uploads["file"].path.parent_path().string());
  35. resp.set("filename_check", req.uploads["file"].filename);
  36. resp.set("content_type", req.uploads["file"].content_type);
  37. resp.set("content_disposition", req.uploads["file"].content_disposition);
  38. resp.expand("upload.html");
  39. ret = cgi::commit(req, resp);
  40. } catch(boost::system::system_error& e) {
  41. using namespace std;
  42. cerr<< "Error " << e.code() << ": " << e.what() << endl;
  43. boost::system::error_code ec;
  44. ret = req.close(http::internal_server_error, 1, ec);
  45. } catch(std::exception& e) {
  46. using namespace std;
  47. cerr<< "Error: " << e.what() << endl;
  48. boost::system::error_code ec;
  49. ret = req.close(http::internal_server_error, 1, ec);
  50. }
  51. }
  52. // Leave this function, after sending the response and closing the request.
  53. // Returns 0 on success.
  54. return ret;
  55. } catch(std::exception& e) {
  56. using namespace std;
  57. cerr<< "Error: " << e.what() << endl;
  58. cin.get();
  59. return 1;
  60. } catch(...) {
  61. using namespace std;
  62. cerr<< "Unexpected exception." << endl;
  63. cin.get();
  64. return 1;
  65. }
  66. }
  67. //]