blob: f4be4c1a6ce95e96039afac04bcd5cc5115fad51 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Slee8a98e1b2007-02-27 05:16:23 +000020#ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_
21#define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1
22
Bryan Duxbury7ba364f2010-07-28 20:45:37 +000023#include <transport/TBufferTransports.h>
Mark Slee8a98e1b2007-02-27 05:16:23 +000024
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { namespace thrift { namespace transport {
Mark Slee8a98e1b2007-02-27 05:16:23 +000026
Bryan Duxbury7ba364f2010-07-28 20:45:37 +000027/**
28 * HTTP client implementation of the thrift transport. This was irritating
29 * to write, but the alternatives in C++ land are daunting. Linking CURL
30 * requires 23 dynamic libraries last time I checked (WTF?!?). All we have
31 * here is a VERY basic HTTP/1.1 client which supports HTTP 100 Continue,
32 * chunked transfer encoding, keepalive, etc. Tested against Apache.
33 *
34 */
35class THttpClient : public TTransport {
Mark Slee8a98e1b2007-02-27 05:16:23 +000036 public:
37 THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path="");
38
39 THttpClient(std::string host, int port, std::string path="");
40
41 virtual ~THttpClient();
42
Bryan Duxbury7ba364f2010-07-28 20:45:37 +000043 void open() {
44 transport_->open();
45 }
46
47 bool isOpen() {
48 return transport_->isOpen();
49 }
50
51 bool peek() {
52 return transport_->peek();
53 }
54
55 void close() {
56 transport_->close();
57 }
58
59 uint32_t read(uint8_t* buf, uint32_t len);
60
61 void readEnd();
62
63 void write(const uint8_t* buf, uint32_t len);
64
65 void flush();
66
67 private:
68 void init();
Mark Slee8a98e1b2007-02-27 05:16:23 +000069
70 protected:
71
Bryan Duxbury7ba364f2010-07-28 20:45:37 +000072 boost::shared_ptr<TTransport> transport_;
73
74 TMemoryBuffer writeBuffer_;
75 TMemoryBuffer readBuffer_;
76
Mark Slee8a98e1b2007-02-27 05:16:23 +000077 std::string host_;
78 std::string path_;
79
Bryan Duxbury7ba364f2010-07-28 20:45:37 +000080 bool readHeaders_;
81 bool chunked_;
82 bool chunkedDone_;
83 uint32_t chunkSize_;
84 uint32_t contentLength_;
85
86 char* httpBuf_;
87 uint32_t httpPos_;
88 uint32_t httpBufLen_;
89 uint32_t httpBufSize_;
90
91 uint32_t readMoreData();
92 char* readLine();
93
94 void readHeaders();
95 void parseHeader(char* header);
96 bool parseStatusLine(char* status);
97
98 uint32_t readChunked();
99 void readChunkedFooters();
100 uint32_t parseChunkSize(char* line);
101
102 uint32_t readContent(uint32_t size);
103
104 void refill();
105 void shift();
Mark Slee8a98e1b2007-02-27 05:16:23 +0000106
107};
108
T Jake Lucianib5e62212009-01-31 22:36:20 +0000109}}} // apache::thrift::transport
Mark Slee8a98e1b2007-02-27 05:16:23 +0000110
111#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_