blob: e7f88c9dd17427fd63ae3db3d53bd8fd460507f5 [file] [log] [blame]
Mark Slee8a98e1b2007-02-27 05:16:23 +00001#ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_
2#define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1
3
4#include <transport/TTransportUtils.h>
5
6namespace 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 */
17class 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
43 void write(const uint8_t* buf, uint32_t len);
44
45 void flush();
46
47 private:
48 void init();
49
50 protected:
51
52 boost::shared_ptr<TTransport> transport_;
53
54 TMemoryBuffer writeBuffer_;
55 TMemoryBuffer readBuffer_;
56
57 std::string host_;
58 std::string path_;
59
60 bool readHeaders_;
61 bool chunked_;
62 uint32_t chunkSize_;
63 uint32_t contentLength_;
64
65 char* httpBuf_;
66 uint32_t httpBufPos_;
67 uint32_t httpBufSize_;
68
69 uint32_t readMoreData();
70 char* readLine(char* line);
71
72 void readHeaders();
73 void parseHeader(char* header);
74 bool parseStatusLine(char* status);
75
76 uint32_t readChunked();
77 uint32_t parseChunkSize(char* line);
78
79 char* readContent(char* pos, uint32_t size);
80
81 void refill();
82 char* shift(char* pos);
83
84};
85
86}}} // facebook::thrift::transport
87
88#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_