David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 20 | #include <cstdlib> |
David Reiss | e39e937 | 2008-05-01 05:52:48 +0000 | [diff] [blame] | 21 | #include <sstream> |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 22 | |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 23 | #include <transport/THttpClient.h> |
| 24 | #include <transport/TSocket.h> |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 25 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 26 | namespace apache { namespace thrift { namespace transport { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
| 29 | |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 30 | THttpClient::THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path) : |
| 31 | THttpTransport(transport), host_(host), path_(path) { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | THttpClient::THttpClient(string host, int port, string path) : |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 35 | THttpTransport(boost::shared_ptr<TTransport>(new TSocket(host, port))), host_(host), path_(path) { |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 36 | } |
| 37 | |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 38 | THttpClient::~THttpClient() {} |
| 39 | |
| 40 | void THttpClient::parseHeader(char* header) { |
| 41 | char* colon = strchr(header, ':'); |
| 42 | if (colon == NULL) { |
| 43 | return; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 44 | } |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 45 | uint32_t sz = colon - header; |
| 46 | char* value = colon+1; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 47 | |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 48 | if (strncmp(header, "Transfer-Encoding", sz) == 0) { |
| 49 | if (strstr(value, "chunked") != NULL) { |
| 50 | chunked_ = true; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 51 | } |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 52 | } else if (strncmp(header, "Content-Length", sz) == 0) { |
| 53 | chunked_ = false; |
| 54 | contentLength_ = atoi(value); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | } |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool THttpClient::parseStatusLine(char* status) { |
| 59 | char* http = status; |
| 60 | |
| 61 | char* code = strchr(http, ' '); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 62 | if (code == NULL) { |
| 63 | throw TTransportException(string("Bad Status: ") + status); |
| 64 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 65 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 66 | *code = '\0'; |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame^] | 67 | while (*(code++) == ' ') {}; |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 68 | |
| 69 | char* msg = strchr(code, ' '); |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 70 | if (msg == NULL) { |
| 71 | throw TTransportException(string("Bad Status: ") + status); |
| 72 | } |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 73 | *msg = '\0'; |
| 74 | |
| 75 | if (strcmp(code, "200") == 0) { |
| 76 | // HTTP 200 = OK, we got the response |
| 77 | return true; |
| 78 | } else if (strcmp(code, "100") == 0) { |
| 79 | // HTTP 100 = continue, just keep reading |
| 80 | return false; |
| 81 | } else { |
Mark Slee | 2a22a88 | 2007-02-27 19:53:38 +0000 | [diff] [blame] | 82 | throw TTransportException(string("Bad Status: ") + status); |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 86 | void THttpClient::flush() { |
| 87 | // Fetch the contents of the write buffer |
| 88 | uint8_t* buf; |
| 89 | uint32_t len; |
| 90 | writeBuffer_.getBuffer(&buf, &len); |
| 91 | |
| 92 | // Construct the HTTP header |
| 93 | std::ostringstream h; |
| 94 | h << |
| 95 | "POST " << path_ << " HTTP/1.1" << CRLF << |
| 96 | "Host: " << host_ << CRLF << |
| 97 | "Content-Type: application/x-thrift" << CRLF << |
| 98 | "Content-Length: " << len << CRLF << |
| 99 | "Accept: application/x-thrift" << CRLF << |
David Reiss | 112e309 | 2010-08-12 23:03:29 +0000 | [diff] [blame] | 100 | "User-Agent: Thrift/" << VERSION << " (C++/THttpClient)" << CRLF << |
Mark Slee | 8a98e1b | 2007-02-27 05:16:23 +0000 | [diff] [blame] | 101 | CRLF; |
| 102 | string header = h.str(); |
| 103 | |
| 104 | // Write the header, then the data, then flush |
| 105 | transport_->write((const uint8_t*)header.c_str(), header.size()); |
| 106 | transport_->write(buf, len); |
| 107 | transport_->flush(); |
| 108 | |
| 109 | // Reset the buffer and header variables |
| 110 | writeBuffer_.resetBuffer(); |
| 111 | readHeaders_ = true; |
| 112 | } |
| 113 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 114 | }}} // apache::thrift::transport |