blob: c34494e9ff943b54553afc3b9a47280c17147e6f [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
Mark Slee2a22a882007-02-27 19:53:38 +000043 void readEnd();
44
Mark Slee8a98e1b2007-02-27 05:16:23 +000045 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 Slee2a22a882007-02-27 19:53:38 +000064 bool chunkedDone_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000065 uint32_t chunkSize_;
66 uint32_t contentLength_;
67
68 char* httpBuf_;
Mark Slee2a22a882007-02-27 19:53:38 +000069 uint32_t httpPos_;
70 uint32_t httpBufLen_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000071 uint32_t httpBufSize_;
72
73 uint32_t readMoreData();
Mark Slee2a22a882007-02-27 19:53:38 +000074 char* readLine();
Mark Slee8a98e1b2007-02-27 05:16:23 +000075
76 void readHeaders();
77 void parseHeader(char* header);
78 bool parseStatusLine(char* status);
79
80 uint32_t readChunked();
Mark Slee2a22a882007-02-27 19:53:38 +000081 void readChunkedFooters();
Mark Slee8a98e1b2007-02-27 05:16:23 +000082 uint32_t parseChunkSize(char* line);
83
Mark Slee2a22a882007-02-27 19:53:38 +000084 uint32_t readContent(uint32_t size);
Mark Slee8a98e1b2007-02-27 05:16:23 +000085
Mark Slee2a22a882007-02-27 19:53:38 +000086 void refill();
87 void shift();
Mark Slee8a98e1b2007-02-27 05:16:23 +000088
89};
90
91}}} // facebook::thrift::transport
92
93#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_