David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |
| 21 | #define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1 |
| 22 | |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 23 | #include <transport/TBufferTransports.h> |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 24 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 25 | namespace apache { namespace thrift { namespace transport { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 26 | |
| 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 Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 34 | */ |
| 35 | class 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 50 | |
| 51 | bool peek() { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 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 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 61 | void readEnd(); |
| 62 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 63 | void write(const uint8_t* buf, uint32_t len); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 64 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 65 | 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 Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 82 | bool chunkedDone_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 83 | uint32_t chunkSize_; |
| 84 | uint32_t contentLength_; |
| 85 | |
| 86 | char* httpBuf_; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 87 | uint32_t httpPos_; |
| 88 | uint32_t httpBufLen_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 89 | uint32_t httpBufSize_; |
| 90 | |
| 91 | uint32_t readMoreData(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 92 | char* readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 93 | |
| 94 | void readHeaders(); |
| 95 | void parseHeader(char* header); |
| 96 | bool parseStatusLine(char* status); |
| 97 | |
| 98 | uint32_t readChunked(); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 99 | void readChunkedFooters(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 100 | uint32_t parseChunkSize(char* line); |
| 101 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 102 | uint32_t readContent(uint32_t size); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 103 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 104 | void refill(); |
| 105 | void shift(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 106 | |
| 107 | }; |
| 108 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 109 | }}} // apache::thrift::transport |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 110 | |
| 111 | #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |