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