Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 1 | #include "THttpClient.h" |
| 2 | #include "TSocket.h" |
| 3 | |
| 4 | namespace facebook { namespace thrift { namespace transport { |
| 5 | |
| 6 | using namespace std; |
| 7 | |
| 8 | /** |
| 9 | * Http client implementation. |
| 10 | * |
| 11 | * @author Mark Slee <mcslee@facebook.com> |
| 12 | */ |
| 13 | |
| 14 | // Yeah, yeah, hacky to put these here, I know. |
| 15 | static const char* CRLF = "\r\n"; |
| 16 | static const int CRLF_LEN = 2; |
| 17 | |
Mark Slee | a2c760b | 2007-02-27 05:18:07 +0000 | [diff] [blame] | 18 | THttpClient::THttpClient(boost::shared_ptr<TTransport> transport, string host, string path) : |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 19 | transport_(transport), |
| 20 | host_(host), |
| 21 | path_(path), |
| 22 | readHeaders_(true), |
| 23 | chunked_(false), |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 24 | chunkedDone_(false), |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 25 | chunkSize_(0), |
| 26 | contentLength_(0), |
| 27 | httpBuf_(NULL), |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 28 | httpPos_(0), |
| 29 | httpBufLen_(0), |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 30 | httpBufSize_(1024) { |
| 31 | init(); |
| 32 | } |
| 33 | |
| 34 | THttpClient::THttpClient(string host, int port, string path) : |
| 35 | host_(host), |
| 36 | path_(path), |
| 37 | readHeaders_(true), |
| 38 | chunked_(false), |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 39 | chunkedDone_(false), |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 40 | chunkSize_(0), |
| 41 | contentLength_(0), |
| 42 | httpBuf_(NULL), |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 43 | httpPos_(0), |
| 44 | httpBufLen_(0), |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 45 | httpBufSize_(1024) { |
| 46 | transport_ = boost::shared_ptr<TTransport>(new TSocket(host, port)); |
| 47 | init(); |
| 48 | } |
| 49 | |
| 50 | void THttpClient::init() { |
| 51 | httpBuf_ = (char*)malloc(httpBufSize_+1); |
| 52 | if (httpBuf_ == NULL) { |
| 53 | throw TTransportException("Out of memory."); |
| 54 | } |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 55 | httpBuf_[httpBufLen_] = '\0'; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | THttpClient::~THttpClient() { |
| 59 | if (httpBuf_ != NULL) { |
| 60 | free(httpBuf_); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | uint32_t THttpClient::read(uint8_t* buf, uint32_t len) { |
| 65 | if (readBuffer_.available() == 0) { |
| 66 | readBuffer_.resetBuffer(); |
| 67 | uint32_t got = readMoreData(); |
| 68 | if (got == 0) { |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | return readBuffer_.read(buf, len); |
| 73 | } |
| 74 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 75 | void THttpClient::readEnd() { |
| 76 | // Read any pending chunked data (footers etc.) |
| 77 | if (chunked_) { |
| 78 | while (!chunkedDone_) { |
| 79 | readChunked(); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 84 | uint32_t THttpClient::readMoreData() { |
| 85 | // Get more data! |
| 86 | refill(); |
| 87 | |
| 88 | if (readHeaders_) { |
| 89 | readHeaders(); |
| 90 | } |
| 91 | |
| 92 | if (chunked_) { |
| 93 | return readChunked(); |
| 94 | } else { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 95 | return readContent(contentLength_); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | uint32_t THttpClient::readChunked() { |
| 100 | uint32_t length = 0; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 101 | |
| 102 | char* line = readLine(); |
| 103 | uint32_t chunkSize = parseChunkSize(line); |
| 104 | if (chunkSize == 0) { |
| 105 | readChunkedFooters(); |
| 106 | } else { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 107 | // Read data content |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 108 | length += readContent(chunkSize); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 109 | // Read trailing CRLF after content |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 110 | readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 111 | } |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 112 | return length; |
| 113 | } |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 114 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 115 | void THttpClient::readChunkedFooters() { |
| 116 | // End of data, read footer lines until a blank one appears |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 117 | while (true) { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 118 | char* line = readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 119 | if (strlen(line) == 0) { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 120 | chunkedDone_ = true; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uint32_t THttpClient::parseChunkSize(char* line) { |
| 127 | char* semi = strchr(line, ';'); |
| 128 | if (semi != NULL) { |
| 129 | *semi = '\0'; |
| 130 | } |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 131 | int size = 0; |
| 132 | sscanf(line, "%x", &size); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 133 | return (uint32_t)size; |
| 134 | } |
| 135 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 136 | uint32_t THttpClient::readContent(uint32_t size) { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 137 | uint32_t need = size; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 138 | while (need > 0) { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 139 | uint32_t avail = httpBufLen_ - httpPos_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 140 | if (avail == 0) { |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 141 | // We have given all the data, reset position to head of the buffer |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 142 | httpPos_ = 0; |
| 143 | httpBufLen_ = 0; |
| 144 | refill(); |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 145 | |
| 146 | // Now have available however much we read |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 147 | avail = httpBufLen_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 148 | } |
| 149 | uint32_t give = avail; |
| 150 | if (need < give) { |
| 151 | give = need; |
| 152 | } |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 153 | readBuffer_.write((uint8_t*)(httpBuf_+httpPos_), give); |
| 154 | httpPos_ += give; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 155 | need -= give; |
| 156 | } |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 157 | return size; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 158 | } |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 159 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 160 | char* THttpClient::readLine() { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 161 | while (true) { |
| 162 | char* eol = NULL; |
| 163 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 164 | eol = strstr(httpBuf_+httpPos_, CRLF); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 165 | |
| 166 | // No CRLF yet? |
| 167 | if (eol == NULL) { |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 168 | // Shift whatever we have now to front and refill |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 169 | shift(); |
| 170 | refill(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 171 | } else { |
| 172 | // Return pointer to next line |
| 173 | *eol = '\0'; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 174 | char* line = httpBuf_+httpPos_; |
| 175 | httpPos_ = (eol-httpBuf_) + CRLF_LEN; |
| 176 | return line; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 182 | void THttpClient::shift() { |
| 183 | if (httpBufLen_ > httpPos_) { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 184 | // Shift down remaining data and read more |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 185 | uint32_t length = httpBufLen_ - httpPos_; |
| 186 | memmove(httpBuf_, httpBuf_+httpPos_, length); |
| 187 | httpBufLen_ = length; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 188 | } else { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 189 | httpBufLen_ = 0; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 190 | } |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 191 | httpPos_ = 0; |
| 192 | httpBuf_[httpBufLen_] = '\0'; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 195 | void THttpClient::refill() { |
| 196 | uint32_t avail = httpBufSize_ - httpBufLen_; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 197 | if (avail <= (httpBufSize_ / 4)) { |
| 198 | httpBufSize_ *= 2; |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 199 | httpBuf_ = (char*)realloc(httpBuf_, httpBufSize_+1); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 200 | if (httpBuf_ == NULL) { |
| 201 | throw TTransportException("Out of memory."); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Read more data |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 206 | uint32_t got = transport_->read((uint8_t*)(httpBuf_+httpBufLen_), httpBufSize_-httpBufLen_); |
| 207 | httpBufLen_ += got; |
| 208 | httpBuf_[httpBufLen_] = '\0'; |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 209 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 210 | if (got == 0) { |
Mark Slee | 4401814 | 2007-02-27 19:03:01 +0000 | [diff] [blame] | 211 | throw TTransportException("Could not refill buffer"); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | void THttpClient::readHeaders() { |
| 216 | // Initialize headers state variables |
| 217 | contentLength_ = 0; |
| 218 | chunked_ = false; |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 219 | chunkedDone_ = false; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 220 | chunkSize_ = 0; |
| 221 | |
| 222 | // Control state flow |
| 223 | bool statusLine = true; |
| 224 | bool finished = false; |
| 225 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 226 | // Loop until headers are finished |
| 227 | while (true) { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 228 | char* line = readLine(); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 229 | |
| 230 | if (strlen(line) == 0) { |
| 231 | if (finished) { |
| 232 | readHeaders_ = false; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 233 | return; |
| 234 | } else { |
| 235 | // Must have been an HTTP 100, keep going for another status line |
| 236 | statusLine = true; |
| 237 | } |
| 238 | } else { |
| 239 | if (statusLine) { |
| 240 | statusLine = false; |
| 241 | finished = parseStatusLine(line); |
| 242 | } else { |
| 243 | parseHeader(line); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | bool THttpClient::parseStatusLine(char* status) { |
| 250 | char* http = status; |
| 251 | |
| 252 | char* code = strchr(http, ' '); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 253 | if (code == NULL) { |
| 254 | throw TTransportException(string("Bad Status: ") + status); |
| 255 | } |
| 256 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 257 | *code = '\0'; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 258 | while (*(code++) == ' '); |
| 259 | |
| 260 | char* msg = strchr(code, ' '); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 261 | if (msg == NULL) { |
| 262 | throw TTransportException(string("Bad Status: ") + status); |
| 263 | } |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 264 | *msg = '\0'; |
| 265 | |
| 266 | if (strcmp(code, "200") == 0) { |
| 267 | // HTTP 200 = OK, we got the response |
| 268 | return true; |
| 269 | } else if (strcmp(code, "100") == 0) { |
| 270 | // HTTP 100 = continue, just keep reading |
| 271 | return false; |
| 272 | } else { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame^] | 273 | throw TTransportException(string("Bad Status: ") + status); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | void THttpClient::parseHeader(char* header) { |
| 278 | char* colon = strchr(header, ':'); |
| 279 | if (colon == NULL) { |
| 280 | return; |
| 281 | } |
| 282 | uint32_t sz = colon - header; |
| 283 | char* value = colon+1; |
| 284 | |
| 285 | if (strncmp(header, "Transfer-Encoding", sz) == 0) { |
| 286 | if (strstr(value, "chunked") != NULL) { |
| 287 | chunked_ = true; |
| 288 | } |
| 289 | } else if (strncmp(header, "Content-Length", sz) == 0) { |
| 290 | chunked_ = false; |
| 291 | contentLength_ = atoi(value); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void THttpClient::write(const uint8_t* buf, uint32_t len) { |
| 296 | writeBuffer_.write(buf, len); |
| 297 | } |
| 298 | |
| 299 | void THttpClient::flush() { |
| 300 | // Fetch the contents of the write buffer |
| 301 | uint8_t* buf; |
| 302 | uint32_t len; |
| 303 | writeBuffer_.getBuffer(&buf, &len); |
| 304 | |
| 305 | // Construct the HTTP header |
| 306 | std::ostringstream h; |
| 307 | h << |
| 308 | "POST " << path_ << " HTTP/1.1" << CRLF << |
| 309 | "Host: " << host_ << CRLF << |
| 310 | "Content-Type: application/x-thrift" << CRLF << |
| 311 | "Content-Length: " << len << CRLF << |
| 312 | "Accept: application/x-thrift" << CRLF << |
| 313 | "User-Agent: C++/THttpClient" << CRLF << |
| 314 | CRLF; |
| 315 | string header = h.str(); |
| 316 | |
| 317 | // Write the header, then the data, then flush |
| 318 | transport_->write((const uint8_t*)header.c_str(), header.size()); |
| 319 | transport_->write(buf, len); |
| 320 | transport_->flush(); |
| 321 | |
| 322 | // Reset the buffer and header variables |
| 323 | writeBuffer_.resetBuffer(); |
| 324 | readHeaders_ = true; |
| 325 | } |
| 326 | |
| 327 | }}} // facebook::thrift::transport |