ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

190 lines
5.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_IMPL_CGI_API_H
  9. #define CPPCMS_IMPL_CGI_API_H
  10. #include <booster/noncopyable.h>
  11. #include <booster/shared_ptr.h>
  12. #include <booster/enable_shared_from_this.h>
  13. #include <booster/aio/buffer.h>
  14. #include <vector>
  15. #include <map>
  16. #include <booster/callback.h>
  17. #include <booster/system_error.h>
  18. #include <cppcms/http_context.h>
  19. #include <cppcms/defs.h>
  20. #include <cppcms/config.h>
  21. #include "string_map.h"
  22. namespace booster {
  23. namespace aio {
  24. class io_service;
  25. class acceptor;
  26. class stream_socket;
  27. }
  28. }
  29. namespace cppcms {
  30. class service;
  31. class application;
  32. namespace http {
  33. class context;
  34. class request;
  35. class response;
  36. }
  37. namespace impl {
  38. class multipart_parser;
  39. namespace cgi {
  40. typedef booster::callback<void(booster::system::error_code const &e)> handler;
  41. typedef booster::callback<void(booster::system::error_code const &e,size_t)> io_handler;
  42. typedef booster::callback<void()> callback;
  43. typedef cppcms::http::context::handler ehandler;
  44. class connection;
  45. class acceptor : public booster::noncopyable {
  46. public:
  47. virtual void async_accept() = 0;
  48. virtual booster::aio::acceptor &socket() = 0;
  49. #ifndef CPPCMS_WIN32
  50. virtual booster::shared_ptr<cppcms::http::context> accept(int fd) = 0;
  51. #endif
  52. virtual void sndbuf(int v) = 0;
  53. virtual void rcvbuf(int v) = 0;
  54. virtual void stop() = 0;
  55. virtual ~acceptor(){}
  56. };
  57. class CPPCMS_API connection :
  58. public booster::noncopyable,
  59. public booster::enable_shared_from_this<connection>
  60. {
  61. public:
  62. connection(cppcms::service &srv);
  63. virtual ~connection();
  64. cppcms::service &service();
  65. void async_prepare_request( http::context *context,
  66. ehandler const &on_post_data_ready);
  67. void async_write_response( http::response &response,
  68. bool complete_response,
  69. ehandler const &on_response_written);
  70. void aync_wait_for_close_by_peer(callback const &on_eof);
  71. std::string getenv(std::string const &key)
  72. {
  73. return env_.get_safe(key.c_str());
  74. }
  75. char const *cgetenv(char const *key)
  76. {
  77. return env_.get_safe(key);
  78. }
  79. std::string getenv(char const *key)
  80. {
  81. return env_.get_safe(key);
  82. }
  83. virtual std::map<std::string,std::string> const &getenv()
  84. {
  85. if(map_env_.empty() && env_.begin()!=env_.end()) {
  86. for(string_map::iterator p=env_.begin();p!=env_.end();++p) {
  87. map_env_[p->key]=p->value;
  88. }
  89. }
  90. return map_env_;
  91. }
  92. bool is_reuseable();
  93. std::string last_error();
  94. /****************************************************************************/
  95. // These are abstract member function that should be implemented by
  96. // actual protocol like FCGI, SCGI, HTTP or CGI
  97. public:
  98. bool has_pending();
  99. virtual bool nonblocking_write(booster::aio::const_buffer const &buf,bool eof,booster::system::error_code &e);
  100. virtual void async_write(booster::aio::const_buffer const &buf,bool eof,handler const &h);
  101. virtual bool write(booster::aio::const_buffer const &buf,bool eof,booster::system::error_code &e);
  102. virtual void on_async_write_start() = 0;
  103. virtual void on_async_write_progress(bool completed) = 0;
  104. virtual void do_eof() = 0;
  105. virtual booster::aio::const_buffer format_output(booster::aio::const_buffer const &in,bool completed,booster::system::error_code &e) = 0;
  106. virtual bool write_to_socket(booster::aio::const_buffer const &in,booster::system::error_code &e);
  107. virtual booster::aio::io_service &get_io_service() = 0;
  108. protected:
  109. void append_pending(booster::aio::const_buffer const &new_data);
  110. virtual booster::aio::stream_socket &socket() = 0;
  111. virtual void async_read_headers(handler const &h) = 0;
  112. virtual bool keep_alive() = 0;
  113. // Concept implementation headers
  114. virtual void async_read_some(void *,size_t,io_handler const &h) = 0;
  115. virtual void on_async_read_complete() {}
  116. virtual void async_read_eof(callback const &h) = 0;
  117. /****************************************************************************/
  118. protected:
  119. string_pool pool_;
  120. string_map env_;
  121. std::vector<char> pending_output_;
  122. booster::shared_ptr<connection> self();
  123. void async_read(void *,size_t,io_handler const &h);
  124. private:
  125. struct reader;
  126. struct cgi_forwarder;
  127. struct async_write_binder;
  128. struct async_write_handler;
  129. friend struct reader;
  130. friend struct writer;
  131. friend struct async_write_binder;
  132. friend struct async_write_handler;
  133. friend struct cgi_forwarder;
  134. void set_error(ehandler const &h,std::string s);
  135. void on_headers_read(booster::system::error_code const &e,http::context *,ehandler const &h);
  136. void load_content(http::context *,ehandler const &h);
  137. void on_some_content_read(booster::system::error_code const &e,size_t n,http::context *,ehandler const &h);
  138. void handle_eof(callback const &on_eof);
  139. void handle_http_error(int code,http::context *context,ehandler const &h);
  140. void handle_http_error_eof(booster::system::error_code const &e,int code,ehandler const &h);
  141. cppcms::service *service_;
  142. std::string async_chunk_;
  143. std::string error_;
  144. bool request_in_progress_;
  145. std::map<std::string,std::string> map_env_;
  146. booster::intrusive_ptr<async_write_binder> cached_async_write_binder_;
  147. };
  148. } // cgi
  149. } // impl
  150. } // cppcms
  151. #endif