blob: dbffe99d43ab2bc41f12c84510cd4ad4def7867d [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
David Reiss0c90f6f2008-02-06 22:18:40 +000012namespace facebook { 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 *
21 * @author Mark Slee <mcslee@facebook.com>
22 */
23class 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 Reiss0c90f6f2008-02-06 22:18:40 +000038
39 bool peek() {
Mark Slee8a98e1b2007-02-27 05:16:23 +000040 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 Slee2a22a882007-02-27 19:53:38 +000049 void readEnd();
50
Mark Slee8a98e1b2007-02-27 05:16:23 +000051 void write(const uint8_t* buf, uint32_t len);
David Reiss0c90f6f2008-02-06 22:18:40 +000052
Mark Slee8a98e1b2007-02-27 05:16:23 +000053 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 Slee2a22a882007-02-27 19:53:38 +000070 bool chunkedDone_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000071 uint32_t chunkSize_;
72 uint32_t contentLength_;
73
74 char* httpBuf_;
Mark Slee2a22a882007-02-27 19:53:38 +000075 uint32_t httpPos_;
76 uint32_t httpBufLen_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000077 uint32_t httpBufSize_;
78
79 uint32_t readMoreData();
Mark Slee2a22a882007-02-27 19:53:38 +000080 char* readLine();
Mark Slee8a98e1b2007-02-27 05:16:23 +000081
82 void readHeaders();
83 void parseHeader(char* header);
84 bool parseStatusLine(char* status);
85
86 uint32_t readChunked();
Mark Slee2a22a882007-02-27 19:53:38 +000087 void readChunkedFooters();
Mark Slee8a98e1b2007-02-27 05:16:23 +000088 uint32_t parseChunkSize(char* line);
89
Mark Slee2a22a882007-02-27 19:53:38 +000090 uint32_t readContent(uint32_t size);
Mark Slee8a98e1b2007-02-27 05:16:23 +000091
Mark Slee2a22a882007-02-27 19:53:38 +000092 void refill();
93 void shift();
Mark Slee8a98e1b2007-02-27 05:16:23 +000094
95};
96
97}}} // facebook::thrift::transport
98
99#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_