r1355@opsdev009 (orig r71477): mcslee | 2007-11-27 00:42:19 -0800
[amiethrift.git] / lib / cpp / src / transport / THttpClient.h
blobacfcf1b1314dd34a81af410d490139904ad25f89
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/
7 #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_
8 #define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1
10 #include <transport/TTransportUtils.h>
12 namespace facebook { namespace thrift { namespace transport {
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.
21 * @author Mark Slee <mcslee@facebook.com>
23 class THttpClient : public TTransport {
24 public:
25 THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path="");
27 THttpClient(std::string host, int port, std::string path="");
29 virtual ~THttpClient();
31 void open() {
32 transport_->open();
35 bool isOpen() {
36 return transport_->isOpen();
39 bool peek() {
40 return transport_->peek();
43 void close() {
44 transport_->close();
47 uint32_t read(uint8_t* buf, uint32_t len);
49 void readEnd();
51 void write(const uint8_t* buf, uint32_t len);
53 void flush();
55 private:
56 void init();
58 protected:
60 boost::shared_ptr<TTransport> transport_;
62 TMemoryBuffer writeBuffer_;
63 TMemoryBuffer readBuffer_;
65 std::string host_;
66 std::string path_;
68 bool readHeaders_;
69 bool chunked_;
70 bool chunkedDone_;
71 uint32_t chunkSize_;
72 uint32_t contentLength_;
74 char* httpBuf_;
75 uint32_t httpPos_;
76 uint32_t httpBufLen_;
77 uint32_t httpBufSize_;
79 uint32_t readMoreData();
80 char* readLine();
82 void readHeaders();
83 void parseHeader(char* header);
84 bool parseStatusLine(char* status);
86 uint32_t readChunked();
87 void readChunkedFooters();
88 uint32_t parseChunkSize(char* line);
90 uint32_t readContent(uint32_t size);
92 void refill();
93 void shift();
97 }}} // facebook::thrift::transport
99 #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_