2015-06-22 11:11:21 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2015-06-22 11:11:21 +02:00
|
|
|
* *
|
|
|
|
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/httprequest.hpp"
|
|
|
|
#include "base/logger.hpp"
|
2015-08-29 01:16:16 +02:00
|
|
|
#include "base/application.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2015-08-29 01:16:16 +02:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
HttpRequest::HttpRequest(const Stream::Ptr& stream)
|
2015-07-09 12:46:04 +02:00
|
|
|
: Complete(false),
|
2015-08-29 01:16:16 +02:00
|
|
|
ProtocolVersion(HttpVersion11),
|
2015-07-09 12:46:04 +02:00
|
|
|
Headers(new Dictionary()),
|
2015-08-29 01:16:16 +02:00
|
|
|
m_Stream(stream),
|
2015-10-27 15:26:19 +01:00
|
|
|
m_State(HttpRequestStart)
|
2015-06-22 11:11:21 +02:00
|
|
|
{ }
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
bool HttpRequest::Parse(StreamReadContext& src, bool may_wait)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
if (m_State != HttpRequestBody) {
|
|
|
|
String line;
|
2015-08-29 01:16:16 +02:00
|
|
|
StreamReadStatus srs = m_Stream->ReadLine(&line, src, may_wait);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
if (srs != StatusNewItem)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_State == HttpRequestStart) {
|
|
|
|
/* ignore trailing new-lines */
|
|
|
|
if (line == "")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, line, boost::is_any_of(" "));
|
2015-08-21 14:44:31 +02:00
|
|
|
Log(LogDebug, "HttpRequest")
|
2015-06-22 11:11:21 +02:00
|
|
|
<< "line: " << line << ", tokens: " << tokens.size();
|
|
|
|
if (tokens.size() != 3)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid HTTP request"));
|
2015-09-22 17:58:12 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
RequestMethod = tokens[0];
|
2015-07-09 17:32:19 +02:00
|
|
|
RequestUrl = new class Url(tokens[1]);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
if (tokens[2] == "HTTP/1.0")
|
|
|
|
ProtocolVersion = HttpVersion10;
|
|
|
|
else if (tokens[2] == "HTTP/1.1") {
|
|
|
|
ProtocolVersion = HttpVersion11;
|
|
|
|
} else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Unsupported HTTP version"));
|
|
|
|
|
|
|
|
m_State = HttpRequestHeaders;
|
|
|
|
} else if (m_State == HttpRequestHeaders) {
|
|
|
|
if (line == "") {
|
|
|
|
m_State = HttpRequestBody;
|
|
|
|
|
|
|
|
/* we're done if the request doesn't contain a message body */
|
|
|
|
if (!Headers->Contains("content-length") && !Headers->Contains("transfer-encoding"))
|
|
|
|
Complete = true;
|
|
|
|
else
|
|
|
|
m_Body = new FIFO();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
String::SizeType pos = line.FindFirstOf(":");
|
|
|
|
if (pos == String::NPos)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid HTTP request"));
|
2015-08-27 18:06:20 +02:00
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
String key = line.SubStr(0, pos).ToLower().Trim();
|
2015-08-27 18:06:20 +02:00
|
|
|
String value = line.SubStr(pos + 1).Trim();
|
2015-06-22 11:11:21 +02:00
|
|
|
Headers->Set(key, value);
|
2015-07-22 08:28:15 +02:00
|
|
|
|
|
|
|
if (key == "x-http-method-override")
|
|
|
|
RequestMethod = value;
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VERIFY(!"Invalid HTTP request state.");
|
|
|
|
}
|
|
|
|
} else if (m_State == HttpRequestBody) {
|
|
|
|
if (Headers->Get("transfer-encoding") == "chunked") {
|
2015-08-29 01:16:16 +02:00
|
|
|
if (!m_ChunkContext)
|
2015-10-20 22:54:58 +02:00
|
|
|
m_ChunkContext = boost::make_shared<ChunkReadContext>(boost::ref(src));
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
char *data;
|
|
|
|
size_t size;
|
2015-08-29 01:16:16 +02:00
|
|
|
StreamReadStatus srs = HttpChunkedEncoding::ReadChunkFromStream(m_Stream, &data, &size, *m_ChunkContext.get(), may_wait);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
if (srs != StatusNewItem)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Body->Write(data, size);
|
|
|
|
|
|
|
|
delete [] data;
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
Complete = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-29 01:16:16 +02:00
|
|
|
if (src.Eof)
|
2015-06-22 11:11:21 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Unexpected EOF in HTTP body"));
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
if (src.MustRead) {
|
|
|
|
if (!src.FillFromStream(m_Stream, false)) {
|
|
|
|
src.Eof = true;
|
2015-06-22 11:11:21 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Unexpected EOF in HTTP body"));
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
src.MustRead = false;
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t length_indicator = Convert::ToLong(Headers->Get("content-length"));
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
if (src.Size < length_indicator) {
|
|
|
|
src.MustRead = true;
|
2015-06-22 11:11:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
m_Body->Write(src.Buffer, length_indicator);
|
|
|
|
src.DropData(length_indicator);
|
2015-06-22 11:11:21 +02:00
|
|
|
Complete = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HttpRequest::ReadBody(char *data, size_t count)
|
|
|
|
{
|
|
|
|
if (!m_Body)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return m_Body->Read(data, count, true);
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpRequest::AddHeader(const String& key, const String& value)
|
|
|
|
{
|
|
|
|
ASSERT(m_State == HttpRequestStart || m_State == HttpRequestHeaders);
|
|
|
|
Headers->Set(key.ToLower(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpRequest::FinishHeaders(void)
|
|
|
|
{
|
|
|
|
if (m_State == HttpRequestStart) {
|
|
|
|
String rqline = RequestMethod + " " + RequestUrl->Format() + " HTTP/1." + (ProtocolVersion == HttpVersion10 ? "0" : "1") + "\n";
|
|
|
|
m_Stream->Write(rqline.CStr(), rqline.GetLength());
|
|
|
|
m_State = HttpRequestHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_State == HttpRequestHeaders) {
|
2015-08-20 16:43:03 +02:00
|
|
|
AddHeader("User-Agent", "Icinga/" + Application::GetAppVersion());
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2016-06-08 12:09:21 +02:00
|
|
|
if (ProtocolVersion == HttpVersion11) {
|
2015-08-29 01:16:16 +02:00
|
|
|
AddHeader("Transfer-Encoding", "chunked");
|
2016-06-08 12:09:21 +02:00
|
|
|
if (!Headers->Contains("Host"))
|
|
|
|
AddHeader("Host", RequestUrl->GetHost() + ":" + RequestUrl->GetPort());
|
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
|
|
|
|
ObjectLock olock(Headers);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : Headers)
|
2015-08-29 01:16:16 +02:00
|
|
|
{
|
|
|
|
String header = kv.first + ": " + kv.second + "\n";
|
|
|
|
m_Stream->Write(header.CStr(), header.GetLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Stream->Write("\n", 1);
|
|
|
|
|
|
|
|
m_State = HttpRequestBody;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpRequest::WriteBody(const char *data, size_t count)
|
|
|
|
{
|
|
|
|
ASSERT(m_State == HttpRequestStart || m_State == HttpRequestHeaders || m_State == HttpRequestBody);
|
|
|
|
|
|
|
|
if (ProtocolVersion == HttpVersion10) {
|
|
|
|
if (!m_Body)
|
|
|
|
m_Body = new FIFO();
|
|
|
|
|
|
|
|
m_Body->Write(data, count);
|
|
|
|
} else {
|
|
|
|
FinishHeaders();
|
|
|
|
|
|
|
|
HttpChunkedEncoding::WriteChunkToStream(m_Stream, data, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpRequest::Finish(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_State != HttpRequestEnd);
|
|
|
|
|
|
|
|
if (ProtocolVersion == HttpVersion10) {
|
|
|
|
if (m_Body)
|
|
|
|
AddHeader("Content-Length", Convert::ToString(m_Body->GetAvailableBytes()));
|
|
|
|
|
|
|
|
FinishHeaders();
|
|
|
|
|
|
|
|
while (m_Body && m_Body->IsDataAvailable()) {
|
|
|
|
char buffer[1024];
|
|
|
|
size_t rc = m_Body->Read(buffer, sizeof(buffer), true);
|
|
|
|
m_Stream->Write(buffer, rc);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_State == HttpRequestStart || m_State == HttpRequestHeaders)
|
|
|
|
FinishHeaders();
|
|
|
|
|
|
|
|
WriteBody(NULL, 0);
|
|
|
|
m_Stream->Write("\r\n", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_State = HttpRequestEnd;
|
|
|
|
}
|
2015-09-22 17:58:12 +02:00
|
|
|
|