SafeHttpServer.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file SafeHttpServer.cpp
  15. * @authors:
  16. * Marek
  17. * @date 2015
  18. */
  19. #include <microhttpd.h>
  20. #include <sstream>
  21. #include "SafeHttpServer.h"
  22. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  23. using namespace std;
  24. using namespace dev;
  25. /// structure copied from libjson-rpc-cpp httpserver version 0.6.0
  26. struct mhd_coninfo
  27. {
  28. struct MHD_PostProcessor *postprocessor;
  29. MHD_Connection* connection;
  30. stringstream request;
  31. jsonrpc::HttpServer* server;
  32. int code;
  33. };
  34. bool SafeHttpServer::SendResponse(string const& _response, void* _addInfo)
  35. {
  36. struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
  37. struct MHD_Response *result = MHD_create_response_from_data(_response.size(),(void *) _response.c_str(), 0, 1);
  38. /* struct MHD_Response *result = MHD_create_response_from_buffer(
  39. _response.size(),
  40. static_cast<void *>(const_cast<char *>(_response.c_str())),
  41. MHD_RESPMEM_MUST_COPY
  42. ); */ /* @bug probably */
  43. MHD_add_response_header(result, "Content-Type", "application/json");
  44. MHD_add_response_header(result, "Access-Control-Allow-Origin", m_allowedOrigin.c_str());
  45. int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
  46. MHD_destroy_response(result);
  47. return ret == MHD_YES;
  48. }
  49. bool SafeHttpServer::SendOptionsResponse(void* _addInfo)
  50. {
  51. struct mhd_coninfo* client_connection = static_cast<struct mhd_coninfo*>(_addInfo);
  52. // struct MHD_Response *result = MHD_create_response_from_data(0, NULL, 0, 1);
  53. struct MHD_Response *result = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_MUST_COPY);
  54. // probably correct but doesn't compile https://github.com/ethereum/webthree/pull/95/commits/b073219360ec0cfa23d44cf5a3f4ef01a15f3d05
  55. MHD_add_response_header(result, "Allow", "POST, OPTIONS");
  56. MHD_add_response_header(result, "Access-Control-Allow-Origin", m_allowedOrigin.c_str());
  57. MHD_add_response_header(result, "Access-Control-Allow-Headers", "origin, content-type, accept");
  58. MHD_add_response_header(result, "DAV", "1");
  59. int ret = MHD_queue_response(client_connection->connection, client_connection->code, result);
  60. MHD_destroy_response(result);
  61. return ret == MHD_YES;
  62. }