File: HttpClient.cpp

package info (click to toggle)
vdr-plugin-fritzbox 1.5.3-7.1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 788 kB
  • sloc: cpp: 4,829; makefile: 94; sh: 15
file content (187 lines) | stat: -rw-r--r-- 6,213 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * libnet++
 *
 * Copyright (C) 2007-2013 Joachim Wilke <libnet@joachim-wilke.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "HttpClient.h"

#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>

#include <liblog++/Log.h>
#include <libconv++/CharsetConverter.h>

namespace network {

HttpClient::HttpClient(const std::string &host, int port)
: TcpClient{host, port} {
}

HttpClient::~HttpClient() {
}

HttpClient::response_t HttpClient::parseResponse() {
	header_t header;
	body_t body;

    std::string http_version;
    *stream >> http_version;
    unsigned int status_code;
    *stream >> status_code;
    std::string status_message;
    std::getline(*stream, status_message);
    if (!(*stream) || http_version.substr(0, 5) != "HTTP/")
      throw std::runtime_error("Invalid response");
    DBG("HTTP status code " << status_code);

    // Process the response headers, which are terminated by a blank line
    std::string headerline;
    while (std::getline(*stream, headerline) && headerline != "\r") {
    	size_t separator = headerline.find(':');
    	if (separator == std::string::npos)
    		throw std::runtime_error("Invalid header format detected in HTTP response.");
    	std::string key = headerline.substr(0, separator);
    	std::string value = headerline.substr(separator+2);
    	header.insert(std::pair<std::string, std::string>(key, value));
    	DBG("Found header: " << key << ": " << value);
    }

    // The remaining data is the body
    std::stringstream bodystream;
    bodystream << stream->rdbuf();
    body = bodystream.str();
	DBG("Body size " << body.length() << " Bytes.");

	// check encoding and convert
	const std::string charsetToken = "charset=";
	const std::string& contentType = header["Content-Type"];
	if (contentType.length()) {
		size_t start = contentType.find(charsetToken);
		if (start != std::string::npos) {
			start += charsetToken.length();
			size_t stop = contentType.find('\r', start);
			if (stop == std::string::npos)
				stop = contentType.find('\n', start);
			std::string charset = std::string(contentType, start, stop-start);
			DBG("Converting response from charset " << charset << " to local encoding.");
			body = convert::CharsetConverter::ConvertToLocalEncoding(body, charset);
		}
	}

	return response_t(header, body);
}

std::string HttpClient::sendRequest(const std::string &request, const std::ostream &postdata, const header_t &header) {
	if (!connected)
		connectStream();
	std::stringstream post;
	post << postdata.rdbuf();
	int postContentLength = post.str().length();
	std::string method = postContentLength ? "POST" : "GET";

	DBG("Requesting HTTP " << method << " on " << request);
	*stream << method << " " << request << " HTTP/1.0\r\n";
	for (auto entry : defaultHeader) {
		*stream << entry.first << ": " << entry.second << "\r\n";
	}
	for (auto entry : header) {
		*stream << entry.first << ": " << entry.second << "\r\n";
	}
	if (postContentLength)
		*stream << "Content-Length: " << postContentLength << "\r\n"
		       << "\r\n" << post.str() << "\r\n" << std::flush;
	else
		*stream << "\r\n" << std::flush;

	response_t response = parseResponse();
	disconnectStream();

	// check for redirection
	header_t responseHeader = response.first;
	if (responseHeader["Location"].length() > 0) {
		DBG("Redirect requested to " << responseHeader["Location"]);
		return getURL(responseHeader["Location"]);
	}
	return response.second;
}

std::string HttpClient::get(const std::string& url, const param_t &params, const header_t &header) {
	std::stringstream ss;
	if (url.find('?') == std::string::npos)
		ss << url << "?";
	else
		ss << url << "&";
	for (auto parameter: params)
		ss << parameter.first << "=" << parameter.second << "&";
	return sendRequest(ss.str(), std::ostringstream(), header);
}

std::string HttpClient::post(const std::string &request, const param_t &postdata, const header_t &header) {
	header_t fullheader = {
			{ "Content-Type", "application/x-www-form-urlencoded" }
	};
	fullheader.insert(begin(header), end(header));

	std::stringstream ss;
	for (auto parameter : postdata)
			ss << parameter.first << "=" << parameter.second << "&";

	return sendRequest(request, ss, fullheader);
}

std::string HttpClient::getURL(const std::string &url, const header_t &header) {
	//TODO support other port
	//TODO support HTTPS

	size_t protoMarker = url.find("://");
	size_t hostMarker  = url.find("/", protoMarker+4);
	if (protoMarker == std::string::npos || hostMarker == std::string::npos)
		throw std::runtime_error("Invalid url.");

	std::string proto = url.substr(0, protoMarker);
	std::string host  = url.substr(protoMarker+3, hostMarker-protoMarker-3);
	std::string request = url.substr(hostMarker);
	if (proto.compare("http") != 0)
		throw std::runtime_error("Invalid protocol in url.");

	HttpClient client(host);
	return client.get(request, param_t(), header);
}

std::string HttpClient::postMIME(const std::string &request, const param_t &postdata, const header_t &header) {

	const std::string boundary = "----FormBoundaryZMsGfL5JxTz5LuAW";
	header_t fullheader = {
			{ "Content-Type", "multipart/form-data; boundary=" + boundary }
	};
	fullheader.insert(begin(header), end(header));

	std::stringstream ss;
	for (auto parameter : postdata) {
		ss << "--" << boundary << "\r\n"
		   << "Content-Disposition: form-data; name=\""+parameter.first+"\"\r\n"
		   << "\r\n"
		   << parameter.second
		   << "\r\n";
	}
	ss << "--" << boundary << "--\r\n";
	return sendRequest(request, ss, fullheader);
}

}