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