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 | |
| 10 | #include <transport/TTransportUtils.h> |
| 11 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 12 | namespace facebook { 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 | * |
| 21 | * @author Mark Slee <mcslee@facebook.com> |
| 22 | */ |
| 23 | class THttpClient : public TTransport { |
| 24 | public: |
| 25 | THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path=""); |
| 26 | |
| 27 | THttpClient(std::string host, int port, std::string path=""); |
| 28 | |
| 29 | virtual ~THttpClient(); |
| 30 | |
| 31 | void open() { |
| 32 | transport_->open(); |
| 33 | } |
| 34 | |
| 35 | bool isOpen() { |
| 36 | return transport_->isOpen(); |
| 37 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 38 | |
| 39 | bool peek() { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 40 | return transport_->peek(); |
| 41 | } |
| 42 | |
| 43 | void close() { |
| 44 | transport_->close(); |
| 45 | } |
| 46 | |
| 47 | uint32_t read(uint8_t* buf, uint32_t len); |
| 48 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 49 | void readEnd(); |
| 50 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 51 | void write(const uint8_t* buf, uint32_t len); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 52 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 53 | void flush(); |
| 54 | |
| 55 | private: |
| 56 | void init(); |
| 57 | |
| 58 | protected: |
| 59 | |
| 60 | boost::shared_ptr<TTransport> transport_; |
| 61 | |
| 62 | TMemoryBuffer writeBuffer_; |
| 63 | TMemoryBuffer readBuffer_; |
| 64 | |
| 65 | std::string host_; |
| 66 | std::string path_; |
| 67 | |
| 68 | bool readHeaders_; |
| 69 | bool chunked_; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 70 | bool chunkedDone_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 71 | uint32_t chunkSize_; |
| 72 | uint32_t contentLength_; |
| 73 | |
| 74 | char* httpBuf_; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 75 | uint32_t httpPos_; |
| 76 | uint32_t httpBufLen_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 77 | uint32_t httpBufSize_; |
| 78 | |
| 79 | uint32_t readMoreData(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 80 | char* readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 81 | |
| 82 | void readHeaders(); |
| 83 | void parseHeader(char* header); |
| 84 | bool parseStatusLine(char* status); |
| 85 | |
| 86 | uint32_t readChunked(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 87 | void readChunkedFooters(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 88 | uint32_t parseChunkSize(char* line); |
| 89 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 90 | uint32_t readContent(uint32_t size); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 91 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 92 | void refill(); |
| 93 | void shift(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 94 | |
| 95 | }; |
| 96 | |
| 97 | }}} // facebook::thrift::transport |
| 98 | |
| 99 | #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |