blob: ae28eb75afa4d5e4dde34988ff57d9fc89a2ac48 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Slee8a98e1b2007-02-27 05:16:23 +00007#ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_
8#define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1
9
David Reiss28f298d2008-05-01 06:17:36 +000010#include <transport/TBufferTransports.h>
Mark Slee8a98e1b2007-02-27 05:16:23 +000011
T Jake Lucianib5e62212009-01-31 22:36:20 +000012namespace apache { namespace thrift { namespace transport {
Mark Slee8a98e1b2007-02-27 05:16:23 +000013
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 Slee8a98e1b2007-02-27 05:16:23 +000021 */
22class 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 Reiss0c90f6f2008-02-06 22:18:40 +000037
38 bool peek() {
Mark Slee8a98e1b2007-02-27 05:16:23 +000039 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 Slee2a22a882007-02-27 19:53:38 +000048 void readEnd();
49
Mark Slee8a98e1b2007-02-27 05:16:23 +000050 void write(const uint8_t* buf, uint32_t len);
David Reiss0c90f6f2008-02-06 22:18:40 +000051
Mark Slee8a98e1b2007-02-27 05:16:23 +000052 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 Slee2a22a882007-02-27 19:53:38 +000069 bool chunkedDone_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000070 uint32_t chunkSize_;
71 uint32_t contentLength_;
72
73 char* httpBuf_;
Mark Slee2a22a882007-02-27 19:53:38 +000074 uint32_t httpPos_;
75 uint32_t httpBufLen_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000076 uint32_t httpBufSize_;
77
78 uint32_t readMoreData();
Mark Slee2a22a882007-02-27 19:53:38 +000079 char* readLine();
Mark Slee8a98e1b2007-02-27 05:16:23 +000080
81 void readHeaders();
82 void parseHeader(char* header);
83 bool parseStatusLine(char* status);
84
85 uint32_t readChunked();
Mark Slee2a22a882007-02-27 19:53:38 +000086 void readChunkedFooters();
Mark Slee8a98e1b2007-02-27 05:16:23 +000087 uint32_t parseChunkSize(char* line);
88
Mark Slee2a22a882007-02-27 19:53:38 +000089 uint32_t readContent(uint32_t size);
Mark Slee8a98e1b2007-02-27 05:16:23 +000090
Mark Slee2a22a882007-02-27 19:53:38 +000091 void refill();
92 void shift();
Mark Slee8a98e1b2007-02-27 05:16:23 +000093
94};
95
T Jake Lucianib5e62212009-01-31 22:36:20 +000096}}} // apache::thrift::transport
Mark Slee8a98e1b2007-02-27 05:16:23 +000097
98#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_