]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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); | |
40 | ||
41 | ~THttpTransport() override; | |
42 | ||
43 | void open() override { transport_->open(); } | |
44 | ||
45 | bool isOpen() const override { return transport_->isOpen(); } | |
46 | ||
47 | bool peek() override { return transport_->peek(); } | |
48 | ||
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 | ||
57 | void flush() override = 0; | |
58 | ||
59 | const std::string getOrigin() const override; | |
60 | ||
61 | protected: | |
62 | std::shared_ptr<TTransport> transport_; | |
63 | std::string origin_; | |
64 | ||
65 | TMemoryBuffer writeBuffer_; | |
66 | TMemoryBuffer readBuffer_; | |
67 | ||
68 | bool readHeaders_; | |
69 | bool chunked_; | |
70 | bool chunkedDone_; | |
71 | uint32_t chunkSize_; | |
72 | uint32_t contentLength_; | |
73 | ||
74 | char* httpBuf_; | |
75 | uint32_t httpPos_; | |
76 | uint32_t httpBufLen_; | |
77 | uint32_t httpBufSize_; | |
78 | ||
79 | virtual void init(); | |
80 | ||
81 | uint32_t readMoreData(); | |
82 | char* readLine(); | |
83 | ||
84 | void readHeaders(); | |
85 | virtual void parseHeader(char* header) = 0; | |
86 | virtual bool parseStatusLine(char* status) = 0; | |
87 | ||
88 | uint32_t readChunked(); | |
89 | void readChunkedFooters(); | |
90 | uint32_t parseChunkSize(char* line); | |
91 | ||
92 | uint32_t readContent(uint32_t size); | |
93 | ||
94 | void refill(); | |
95 | void shift(); | |
96 | ||
97 | static const char* CRLF; | |
98 | static const int CRLF_LEN; | |
99 | }; | |
100 | } | |
101 | } | |
102 | } // apache::thrift::transport | |
103 | ||
104 | #endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_ |