Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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_THTTPCLIENT_H_
21
#define _THRIFT_TRANSPORT_THTTPCLIENT_H_ 1
22
 
23
#include <transport/TBufferTransports.h>
24
 
25
namespace apache { namespace thrift { namespace transport {
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
 *
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
  }
50
 
51
  bool peek() {
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
 
61
  void readEnd();
62
 
63
  void write(const uint8_t* buf, uint32_t len);
64
 
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_;
82
  bool chunkedDone_;
83
  uint32_t chunkSize_;
84
  uint32_t contentLength_;
85
 
86
  char* httpBuf_;
87
  uint32_t httpPos_;
88
  uint32_t httpBufLen_;
89
  uint32_t httpBufSize_;
90
 
91
  uint32_t readMoreData();
92
  char* readLine();
93
 
94
  void readHeaders();
95
  void parseHeader(char* header);
96
  bool parseStatusLine(char* status);
97
 
98
  uint32_t readChunked();
99
  void readChunkedFooters();
100
  uint32_t parseChunkSize(char* line);
101
 
102
  uint32_t readContent(uint32_t size);
103
 
104
  void refill();
105
  void shift();
106
 
107
};
108
 
109
}}} // apache::thrift::transport
110
 
111
#endif // #ifndef _THRIFT_TRANSPORT_THTTPCLIENT_H_