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 */ 19 20 #ifndef _THRIFT_TRANSPORT_THTTPTRANSPORT_H_ 21 #define _THRIFT_TRANSPORT_THTTPTRANSPORT_H_ 1 22 23 #include <thrift/transport/TBufferTransports.h> 24 #include <thrift/transport/TVirtualTransport.h> 25 26 namespace apache { 27 namespace thrift { 28 namespace transport { 29 30 /** 31 * HTTP implementation of the thrift transport. This was irritating 32 * to write, but the alternatives in C++ land are daunting. Linking CURL 33 * requires 23 dynamic libraries last time I checked (WTF?!?). All we have 34 * here is a VERY basic HTTP/1.1 client which supports HTTP 100 Continue, 35 * chunked transfer encoding, keepalive, etc. Tested against Apache. 36 */ 37 class THttpTransport : public TVirtualTransport<THttpTransport> { 38 public: 39 THttpTransport(std::shared_ptr<TTransport> transport, std::shared_ptr<TConfiguration> config = nullptr); 40 41 ~THttpTransport() override; 42 open()43 void open() override { transport_->open(); } 44 isOpen()45 bool isOpen() const override { return transport_->isOpen(); } 46 peek()47 bool peek() override { return transport_->peek(); } 48 close()49 void close() override { transport_->close(); } 50 51 uint32_t read(uint8_t* buf, uint32_t len); 52 53 uint32_t readEnd() override; 54 55 void write(const uint8_t* buf, uint32_t len); 56 flush()57 void flush() override { 58 resetConsumedMessageSize(); 59 }; 60 61 const std::string getOrigin() const override; 62 63 protected: 64 std::shared_ptr<TTransport> transport_; 65 std::string origin_; 66 67 TMemoryBuffer writeBuffer_; 68 TMemoryBuffer readBuffer_; 69 70 bool readHeaders_; 71 bool chunked_; 72 bool chunkedDone_; 73 uint32_t chunkSize_; 74 uint32_t contentLength_; 75 76 char* httpBuf_; 77 uint32_t httpPos_; 78 uint32_t httpBufLen_; 79 uint32_t httpBufSize_; 80 81 virtual void init(); 82 83 uint32_t readMoreData(); 84 char* readLine(); 85 86 void readHeaders(); 87 virtual void parseHeader(char* header) = 0; 88 virtual bool parseStatusLine(char* status) = 0; 89 90 uint32_t readChunked(); 91 void readChunkedFooters(); 92 uint32_t parseChunkSize(char* line); 93 94 uint32_t readContent(uint32_t size); 95 96 void refill(); 97 void shift(); 98 99 static const char* CRLF; 100 static const int CRLF_LEN; 101 }; 102 } 103 } 104 } // apache::thrift::transport 105 106 #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ 107