Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |
| 2 | #define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1 |
| 3 | |
| 4 | #include <transport/TTransportUtils.h> |
| 5 | |
| 6 | namespace facebook { namespace thrift { namespace transport { |
| 7 | |
| 8 | /** |
| 9 | * HTTP client implementation of the thrift transport. This was irritating |
| 10 | * to write, but the alternatives in C++ land are daunting. Linking CURL |
| 11 | * requires 23 dynamic libraries last time I checked (WTF?!?). All we have |
| 12 | * here is a VERY basic HTTP/1.1 client which supports HTTP 100 Continue, |
| 13 | * chunked transfer encoding, keepalive, etc. Tested against Apache. |
| 14 | * |
| 15 | * @author Mark Slee <mcslee@facebook.com> |
| 16 | */ |
| 17 | class THttpClient : public TTransport { |
| 18 | public: |
| 19 | THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path=""); |
| 20 | |
| 21 | THttpClient(std::string host, int port, std::string path=""); |
| 22 | |
| 23 | virtual ~THttpClient(); |
| 24 | |
| 25 | void open() { |
| 26 | transport_->open(); |
| 27 | } |
| 28 | |
| 29 | bool isOpen() { |
| 30 | return transport_->isOpen(); |
| 31 | } |
| 32 | |
| 33 | bool peek() { |
| 34 | return transport_->peek(); |
| 35 | } |
| 36 | |
| 37 | void close() { |
| 38 | transport_->close(); |
| 39 | } |
| 40 | |
| 41 | uint32_t read(uint8_t* buf, uint32_t len); |
| 42 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 43 | void readEnd(); |
| 44 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 45 | void write(const uint8_t* buf, uint32_t len); |
| 46 | |
| 47 | void flush(); |
| 48 | |
| 49 | private: |
| 50 | void init(); |
| 51 | |
| 52 | protected: |
| 53 | |
| 54 | boost::shared_ptr<TTransport> transport_; |
| 55 | |
| 56 | TMemoryBuffer writeBuffer_; |
| 57 | TMemoryBuffer readBuffer_; |
| 58 | |
| 59 | std::string host_; |
| 60 | std::string path_; |
| 61 | |
| 62 | bool readHeaders_; |
| 63 | bool chunked_; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 64 | bool chunkedDone_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 65 | uint32_t chunkSize_; |
| 66 | uint32_t contentLength_; |
| 67 | |
| 68 | char* httpBuf_; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 69 | uint32_t httpPos_; |
| 70 | uint32_t httpBufLen_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 71 | uint32_t httpBufSize_; |
| 72 | |
| 73 | uint32_t readMoreData(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 74 | char* readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 75 | |
| 76 | void readHeaders(); |
| 77 | void parseHeader(char* header); |
| 78 | bool parseStatusLine(char* status); |
| 79 | |
| 80 | uint32_t readChunked(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 81 | void readChunkedFooters(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 82 | uint32_t parseChunkSize(char* line); |
| 83 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 84 | uint32_t readContent(uint32_t size); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 85 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 86 | void refill(); |
| 87 | void shift(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 88 | |
| 89 | }; |
| 90 | |
| 91 | }}} // facebook::thrift::transport |
| 92 | |
| 93 | #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |