blob: d7833512dd1b6defa64d3662b4e644a3ffe1a46c [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
David Reissd7a16f42008-02-19 22:47:29 +000020#include <cstdlib>
David Reisse39e9372008-05-01 05:52:48 +000021#include <sstream>
Roger Meier46d32b42011-02-28 16:04:51 +000022#include <boost/algorithm/string.hpp>
David Reissd7a16f42008-02-19 22:47:29 +000023
David Reiss112e3092010-08-12 23:03:29 +000024#include <transport/THttpClient.h>
25#include <transport/TSocket.h>
Mark Slee8a98e1b2007-02-27 05:16:23 +000026
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace transport {
Mark Slee8a98e1b2007-02-27 05:16:23 +000028
29using namespace std;
30
David Reiss112e3092010-08-12 23:03:29 +000031THttpClient::THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path) :
32 THttpTransport(transport), host_(host), path_(path) {
Mark Slee8a98e1b2007-02-27 05:16:23 +000033}
34
35THttpClient::THttpClient(string host, int port, string path) :
David Reiss112e3092010-08-12 23:03:29 +000036 THttpTransport(boost::shared_ptr<TTransport>(new TSocket(host, port))), host_(host), path_(path) {
Mark Slee8a98e1b2007-02-27 05:16:23 +000037}
38
David Reiss112e3092010-08-12 23:03:29 +000039THttpClient::~THttpClient() {}
40
41void THttpClient::parseHeader(char* header) {
42 char* colon = strchr(header, ':');
43 if (colon == NULL) {
44 return;
Mark Slee8a98e1b2007-02-27 05:16:23 +000045 }
David Reiss112e3092010-08-12 23:03:29 +000046 uint32_t sz = colon - header;
47 char* value = colon+1;
Mark Slee8a98e1b2007-02-27 05:16:23 +000048
Roger Meier46d32b42011-02-28 16:04:51 +000049 if (boost::istarts_with(header, "Transfer-Encoding")) {
50 if (boost::iends_with(value, "chunked")) {
David Reiss112e3092010-08-12 23:03:29 +000051 chunked_ = true;
Mark Slee8a98e1b2007-02-27 05:16:23 +000052 }
Roger Meier46d32b42011-02-28 16:04:51 +000053 } else if (boost::istarts_with(header, "Content-Length")) {
David Reiss112e3092010-08-12 23:03:29 +000054 chunked_ = false;
55 contentLength_ = atoi(value);
David Reiss0c90f6f2008-02-06 22:18:40 +000056 }
Mark Slee8a98e1b2007-02-27 05:16:23 +000057}
58
59bool THttpClient::parseStatusLine(char* status) {
60 char* http = status;
61
62 char* code = strchr(http, ' ');
Mark Slee2a22a882007-02-27 19:53:38 +000063 if (code == NULL) {
64 throw TTransportException(string("Bad Status: ") + status);
65 }
David Reiss0c90f6f2008-02-06 22:18:40 +000066
Mark Slee8a98e1b2007-02-27 05:16:23 +000067 *code = '\0';
Roger Meier3b771a12010-11-17 22:11:26 +000068 while (*(code++) == ' ') {};
Mark Slee8a98e1b2007-02-27 05:16:23 +000069
70 char* msg = strchr(code, ' ');
Mark Slee2a22a882007-02-27 19:53:38 +000071 if (msg == NULL) {
72 throw TTransportException(string("Bad Status: ") + status);
73 }
Mark Slee8a98e1b2007-02-27 05:16:23 +000074 *msg = '\0';
75
76 if (strcmp(code, "200") == 0) {
77 // HTTP 200 = OK, we got the response
78 return true;
79 } else if (strcmp(code, "100") == 0) {
80 // HTTP 100 = continue, just keep reading
81 return false;
82 } else {
Mark Slee2a22a882007-02-27 19:53:38 +000083 throw TTransportException(string("Bad Status: ") + status);
Mark Slee8a98e1b2007-02-27 05:16:23 +000084 }
85}
86
Mark Slee8a98e1b2007-02-27 05:16:23 +000087void THttpClient::flush() {
88 // Fetch the contents of the write buffer
89 uint8_t* buf;
90 uint32_t len;
91 writeBuffer_.getBuffer(&buf, &len);
92
93 // Construct the HTTP header
94 std::ostringstream h;
95 h <<
96 "POST " << path_ << " HTTP/1.1" << CRLF <<
97 "Host: " << host_ << CRLF <<
98 "Content-Type: application/x-thrift" << CRLF <<
99 "Content-Length: " << len << CRLF <<
100 "Accept: application/x-thrift" << CRLF <<
David Reiss112e3092010-08-12 23:03:29 +0000101 "User-Agent: Thrift/" << VERSION << " (C++/THttpClient)" << CRLF <<
Mark Slee8a98e1b2007-02-27 05:16:23 +0000102 CRLF;
103 string header = h.str();
104
105 // Write the header, then the data, then flush
106 transport_->write((const uint8_t*)header.c_str(), header.size());
107 transport_->write(buf, len);
108 transport_->flush();
109
110 // Reset the buffer and header variables
111 writeBuffer_.resetBuffer();
112 readHeaders_ = true;
113}
114
T Jake Lucianib5e62212009-01-31 22:36:20 +0000115}}} // apache::thrift::transport