1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef THRIFT_TRANSPORT_THTTPCLIENT_H_ 18 #define THRIFT_TRANSPORT_THTTPCLIENT_H_ 1 19 20 #include <thrift/lib/cpp/transport/THttpTransport.h> 21 22 namespace apache { 23 namespace thrift { 24 namespace transport { 25 26 class THttpClient : public THttpTransport { 27 typedef std::map<std::string, std::string> StringToStringMap; 28 29 public: 30 /* 31 * Create for a given host and port. The version that doesn't take 32 * a transport constructs its own TSocket as a transport. 33 * 34 * The path must be non-empty and start with "/". 35 */ 36 THttpClient( 37 const std::shared_ptr<TTransport>& transport, 38 const std::string& host, 39 const std::string& path); 40 THttpClient(const std::string& host, int port, const std::string& path); 41 42 ~THttpClient() override; 43 44 void setUserAgent(const std::string&); 45 46 void setHeader(const std::string& name, const std::string& value); 47 getRequestHeaders()48 StringToStringMap& getRequestHeaders() { return requestHeaders_; } 49 getResponseHeaders()50 const StringToStringMap& getResponseHeaders() const { 51 return responseHeaders_; 52 } 53 54 void flush() override; 55 close()56 void close() override { 57 connectionClosedByServer_ = false; 58 THttpTransport::close(); 59 } 60 init()61 void init() override { 62 /* 63 * HTTP requires that the `abs_path' component of a POST message start 64 * with '/' (see rfc2616 and rfc2396). 65 */ 66 assert(!path_.empty() && path_[0] == '/'); 67 68 THttpTransport::init(); 69 } 70 getHost()71 const std::string& getHost() const { return host_; } 72 setHost(const std::string & host)73 void setHost(const std::string& host) { host_ = host; } 74 getPath()75 const std::string& getPath() const { return path_; } 76 77 const static std::string kAcceptHeader; 78 const static std::string kConnectionHeader; 79 const static std::string kContentLengthHeader; 80 const static std::string kContentTypeHeader; 81 const static std::string kHostHeader; 82 const static std::string kTransferEncodingHeader; 83 const static std::string kUserAgentHeader; 84 85 protected: 86 std::string host_; 87 const std::string path_; 88 bool connectionClosedByServer_; 89 uint16_t statusCode_; 90 StringToStringMap requestHeaders_; 91 StringToStringMap responseHeaders_; 92 93 void beginParsingHeaders() override; 94 void parseHeader(char* header) override; 95 bool parseStatusLine(char* status) override; 96 void endParsingHeaders() override; 97 }; 98 99 } // namespace transport 100 } // namespace thrift 101 } // namespace apache 102 103 #endif // #ifndef THRIFT_TRANSPORT_THTTPCLIENT_H_ 104