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
David Reiss28f298d2008-05-01 06:17:36 +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
27/**
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 *
Mark Slee8a98e1b2007-02-27 05:16:23 +000034 */
35class THttpClient : public TTransport {
36 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
43 void open() {
44 transport_->open();
45 }
46
47 bool isOpen() {
48 return transport_->isOpen();
49 }
David Reiss0c90f6f2008-02-06 22:18:40 +000050
51 bool peek() {
Mark Slee8a98e1b2007-02-27 05:16:23 +000052 return transport_->peek();
53 }
54
55 void close() {
56 transport_->close();
57 }
58
59 uint32_t read(uint8_t* buf, uint32_t len);
60
Mark Slee2a22a882007-02-27 19:53:38 +000061 void readEnd();
62
Mark Slee8a98e1b2007-02-27 05:16:23 +000063 void write(const uint8_t* buf, uint32_t len);
David Reiss0c90f6f2008-02-06 22:18:40 +000064
Mark Slee8a98e1b2007-02-27 05:16:23 +000065 void flush();
66
67 private:
68 void init();
69
70 protected:
71
72 boost::shared_ptr<TTransport> transport_;
73
74 TMemoryBuffer writeBuffer_;
75 TMemoryBuffer readBuffer_;
76
77 std::string host_;
78 std::string path_;
79
80 bool readHeaders_;
81 bool chunked_;
Mark Slee2a22a882007-02-27 19:53:38 +000082 bool chunkedDone_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000083 uint32_t chunkSize_;
84 uint32_t contentLength_;
85
86 char* httpBuf_;
Mark Slee2a22a882007-02-27 19:53:38 +000087 uint32_t httpPos_;
88 uint32_t httpBufLen_;
Mark Slee8a98e1b2007-02-27 05:16:23 +000089 uint32_t httpBufSize_;
90
91 uint32_t readMoreData();
Mark Slee2a22a882007-02-27 19:53:38 +000092 char* readLine();
Mark Slee8a98e1b2007-02-27 05:16:23 +000093
94 void readHeaders();
95 void parseHeader(char* header);
96 bool parseStatusLine(char* status);
97
98 uint32_t readChunked();
Mark Slee2a22a882007-02-27 19:53:38 +000099 void readChunkedFooters();
Mark Slee8a98e1b2007-02-27 05:16:23 +0000100 uint32_t parseChunkSize(char* line);
101
Mark Slee2a22a882007-02-27 19:53:38 +0000102 uint32_t readContent(uint32_t size);
Mark Slee8a98e1b2007-02-27 05:16:23 +0000103
Mark Slee2a22a882007-02-27 19:53:38 +0000104 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_