]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/transport/THttpClient.cpp
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
23 #include <boost/algorithm/string.hpp>
25 #include <thrift/config.h>
26 #include <thrift/transport/THttpClient.h>
27 #include <thrift/transport/TSocket.h>
35 THttpClient::THttpClient(std::shared_ptr
<TTransport
> transport
,
38 : THttpTransport(transport
), host_(host
), path_(path
) {
41 THttpClient::THttpClient(string host
, int port
, string path
)
42 : THttpTransport(std::shared_ptr
<TTransport
>(new TSocket(host
, port
))),
47 THttpClient::~THttpClient() = default;
49 void THttpClient::parseHeader(char* header
) {
50 char* colon
= strchr(header
, ':');
51 if (colon
== nullptr) {
54 char* value
= colon
+ 1;
56 if (boost::istarts_with(header
, "Transfer-Encoding")) {
57 if (boost::iends_with(value
, "chunked")) {
60 } else if (boost::istarts_with(header
, "Content-Length")) {
62 contentLength_
= atoi(value
);
66 bool THttpClient::parseStatusLine(char* status
) {
69 char* code
= strchr(http
, ' ');
70 if (code
== nullptr) {
71 throw TTransportException(string("Bad Status: ") + status
);
75 while (*(code
++) == ' ') {
78 char* msg
= strchr(code
, ' ');
80 throw TTransportException(string("Bad Status: ") + status
);
84 if (strcmp(code
, "200") == 0) {
85 // HTTP 200 = OK, we got the response
87 } else if (strcmp(code
, "100") == 0) {
88 // HTTP 100 = continue, just keep reading
91 throw TTransportException(string("Bad Status: ") + status
);
95 void THttpClient::flush() {
96 // Fetch the contents of the write buffer
99 writeBuffer_
.getBuffer(&buf
, &len
);
101 // Construct the HTTP header
102 std::ostringstream h
;
103 h
<< "POST " << path_
<< " HTTP/1.1" << CRLF
<< "Host: " << host_
<< CRLF
104 << "Content-Type: application/x-thrift" << CRLF
<< "Content-Length: " << len
<< CRLF
105 << "Accept: application/x-thrift" << CRLF
<< "User-Agent: Thrift/" << PACKAGE_VERSION
106 << " (C++/THttpClient)" << CRLF
<< CRLF
;
107 string header
= h
.str();
109 if (header
.size() > (std::numeric_limits
<uint32_t>::max
)())
110 throw TTransportException("Header too big");
111 // Write the header, then the data, then flush
112 transport_
->write((const uint8_t*)header
.c_str(), static_cast<uint32_t>(header
.size()));
113 transport_
->write(buf
, len
);
116 // Reset the buffer and header variables
117 writeBuffer_
.resetBuffer();
122 } // apache::thrift::transport