blob: 604df0d39c81ac766cd8bc9c5a3b8be50e30a5ff [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>
David Reissd7a16f42008-02-19 22:47:29 +000022
David Reiss112e3092010-08-12 23:03:29 +000023#include <transport/THttpClient.h>
24#include <transport/TSocket.h>
Mark Slee8a98e1b2007-02-27 05:16:23 +000025
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift { namespace transport {
Mark Slee8a98e1b2007-02-27 05:16:23 +000027
28using namespace std;
29
David Reiss112e3092010-08-12 23:03:29 +000030THttpClient::THttpClient(boost::shared_ptr<TTransport> transport, std::string host, std::string path) :
31 THttpTransport(transport), host_(host), path_(path) {
Mark Slee8a98e1b2007-02-27 05:16:23 +000032}
33
34THttpClient::THttpClient(string host, int port, string path) :
David Reiss112e3092010-08-12 23:03:29 +000035 THttpTransport(boost::shared_ptr<TTransport>(new TSocket(host, port))), host_(host), path_(path) {
Mark Slee8a98e1b2007-02-27 05:16:23 +000036}
37
David Reiss112e3092010-08-12 23:03:29 +000038THttpClient::~THttpClient() {}
39
40void THttpClient::parseHeader(char* header) {
41 char* colon = strchr(header, ':');
42 if (colon == NULL) {
43 return;
Mark Slee8a98e1b2007-02-27 05:16:23 +000044 }
David Reiss112e3092010-08-12 23:03:29 +000045 uint32_t sz = colon - header;
46 char* value = colon+1;
Mark Slee8a98e1b2007-02-27 05:16:23 +000047
David Reiss112e3092010-08-12 23:03:29 +000048 if (strncmp(header, "Transfer-Encoding", sz) == 0) {
49 if (strstr(value, "chunked") != NULL) {
50 chunked_ = true;
Mark Slee8a98e1b2007-02-27 05:16:23 +000051 }
David Reiss112e3092010-08-12 23:03:29 +000052 } else if (strncmp(header, "Content-Length", sz) == 0) {
53 chunked_ = false;
54 contentLength_ = atoi(value);
David Reiss0c90f6f2008-02-06 22:18:40 +000055 }
Mark Slee8a98e1b2007-02-27 05:16:23 +000056}
57
58bool THttpClient::parseStatusLine(char* status) {
59 char* http = status;
60
61 char* code = strchr(http, ' ');
Mark Slee2a22a882007-02-27 19:53:38 +000062 if (code == NULL) {
63 throw TTransportException(string("Bad Status: ") + status);
64 }
David Reiss0c90f6f2008-02-06 22:18:40 +000065
Mark Slee8a98e1b2007-02-27 05:16:23 +000066 *code = '\0';
Roger Meier3b771a12010-11-17 22:11:26 +000067 while (*(code++) == ' ') {};
Mark Slee8a98e1b2007-02-27 05:16:23 +000068
69 char* msg = strchr(code, ' ');
Mark Slee2a22a882007-02-27 19:53:38 +000070 if (msg == NULL) {
71 throw TTransportException(string("Bad Status: ") + status);
72 }
Mark Slee8a98e1b2007-02-27 05:16:23 +000073 *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 Slee2a22a882007-02-27 19:53:38 +000082 throw TTransportException(string("Bad Status: ") + status);
Mark Slee8a98e1b2007-02-27 05:16:23 +000083 }
84}
85
Mark Slee8a98e1b2007-02-27 05:16:23 +000086void 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 Reiss112e3092010-08-12 23:03:29 +0000100 "User-Agent: Thrift/" << VERSION << " (C++/THttpClient)" << CRLF <<
Mark Slee8a98e1b2007-02-27 05:16:23 +0000101 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 Lucianib5e62212009-01-31 22:36:20 +0000114}}} // apache::thrift::transport