HTTPSBridge.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "HTTPSBridge.hpp"
  20. #include <string.h>
  21. using namespace boost::asio;
  22. void HTTPSBridge::buildRequestFromHeaders(HttpHeaders &headers, std::string &request) {
  23. std::ostringstream requestStream;
  24. requestStream << headers.getMethod() << " "
  25. << headers.getRequest() << " "
  26. << "HTTP/1.0\r\n";
  27. std::map<std::string,std::string>::iterator iter;
  28. std::map<std::string,std::string,ci_less>& headersMap = headers.getHeaders();
  29. for( iter = headersMap.begin(); iter != headersMap.end(); ++iter ) {
  30. std::string key = iter->first;
  31. std::string value = iter->second;
  32. Util::trimString(key);
  33. Util::trimString(value);
  34. if (key != "Accept-Encoding" && key != "Connection" && key != "Keep-Alive")
  35. requestStream << key << ": " << value << "\r\n";
  36. }
  37. requestStream << "Connection: Close" << "\r\n\r\n";
  38. if (headers.isPost()) requestStream << headers.getPostData();
  39. request = requestStream.str();
  40. }
  41. bool HTTPSBridge::readFromClient() {
  42. char buf[4096];
  43. int bytesRead;
  44. do {
  45. if ((bytesRead = SSL_read(clientSession, buf, sizeof(buf))) <= 0)
  46. return SSL_get_error(clientSession, bytesRead) == SSL_ERROR_WANT_READ ? true : false;
  47. Logger::logFromClient(serverName, buf, bytesRead);
  48. if (headers.process(buf, bytesRead)) {
  49. std::string request;
  50. buildRequestFromHeaders(headers, request);
  51. SSL_write(serverSession, request.c_str(), request.length());
  52. if (headers.isPost()) Logger::logFromClient(serverName, headers);
  53. }
  54. } while (SSL_pending(serverSession));
  55. return true;
  56. }