HttpBridge.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef __HTTP_BRIDGE_H__
  20. #define __HTTP_BRIDGE_H__
  21. #define READING_HEADERS 1
  22. #define SHUFFLING_DATA 2
  23. #define BUFFER_SIZE 4096
  24. #include <boost/asio.hpp>
  25. #include <boost/bind.hpp>
  26. #include <string>
  27. #include "HttpHeaders.hpp"
  28. #include "../Bridge.hpp"
  29. using namespace boost::asio;
  30. class HttpBridgeListener;
  31. class HttpBridge : public Bridge {
  32. public:
  33. static ptr create(boost::shared_ptr<ip::tcp::socket> clientSocket,
  34. io_service& io_service,
  35. HttpBridgeListener *listener)
  36. {
  37. return ptr(new HttpBridge(clientSocket, io_service, listener));
  38. }
  39. virtual ip::tcp::socket& getClientSocket() {
  40. return *clientSocket;
  41. }
  42. virtual ip::tcp::socket& getServerSocket() {
  43. return serverSocket;
  44. }
  45. virtual void shuttle();
  46. virtual void close();
  47. protected:
  48. virtual void processClientInput(char *buffer, int length);
  49. private:
  50. int closed;
  51. io_service& io_service_;
  52. boost::shared_ptr<ip::tcp::socket> clientSocket;
  53. ip::tcp::socket serverSocket;
  54. unsigned int state;
  55. HttpHeaders headers;
  56. HttpBridgeListener *listener;
  57. HttpBridge(boost::shared_ptr<ip::tcp::socket> clientSocket,
  58. io_service& io_service, HttpBridgeListener *listener)
  59. : clientSocket(clientSocket),
  60. serverSocket(io_service),
  61. io_service_(io_service),
  62. state(READING_HEADERS)
  63. {
  64. this->listener = listener;
  65. }
  66. void setFinishedWithHeaders();
  67. };
  68. class HttpBridgeListener {
  69. public:
  70. virtual void setUserAgentFor(ip::address &address, std::string &userAgent) = 0;
  71. };
  72. #endif