MXS-1220: Rename and reorganize HttpParser

The HttpParser class was renamed to HttpRequest as it parses and processes
only HTTP requests. A second class that creates a HTTP response needs to
be created to handle the response generation.

Moved some of the HTTP constants and helper functions to a separate
http.hh header.
This commit is contained in:
Markus Mäkelä
2017-04-16 19:28:23 +03:00
committed by Markus Mäkelä
parent e34b65658e
commit 4eb121ce35
6 changed files with 282 additions and 82 deletions

102
server/core/httprequest.cc Normal file
View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include "maxscale/httprequest.hh"
#include <ctype.h>
/** TODO: Move this to a C++ string utility header */
namespace maxscale
{
static inline string& trim(string& str)
{
while (isspace(*str.begin()))
{
str.erase(str.begin());
}
while (isspace(*str.rbegin()))
{
str.erase(str.rbegin().base());
}
return str;
}
}
HttpRequest* HttpRequest::parse(string data)
{
size_t pos = data.find("\r\n");
string request_line = data.substr(0, pos);
data.erase(0, pos + 2);
pos = request_line.find(" ");
string verb = request_line.substr(0, pos);
request_line.erase(0, pos + 1);
pos = request_line.find(" ");
string uri = request_line.substr(0, pos);
request_line.erase(0, pos + 1);
pos = request_line.find("\r\n");
string http_version = request_line.substr(0, pos);
request_line.erase(0, pos + 2);
map<string, string> headers;
while ((pos = data.find("\r\n")) != string::npos)
{
string header_line = data.substr(0, pos);
data.erase(0, pos + 2);
if (header_line.length() == 0)
{
/** End of headers */
break;
}
if ((pos = header_line.find(":")) != string::npos)
{
string key = header_line.substr(0, pos);
header_line.erase(0, pos + 1);
headers[key] = mxs::trim(header_line);
}
}
/** The headers are now processed and consumed. The message body is
* the only thing left in the request string. */
HttpRequest* request = NULL;
enum http_verb verb_value = string_to_http_verb(verb);
if (http_version == "HTTP/1.1" && verb_value != HTTP_UNKNOWN)
{
request = new HttpRequest();
request->m_verb = verb_value;
request->m_resource = uri;
request->m_headers = headers;
request->m_body = data;
}
return request;
}
HttpRequest::HttpRequest()
{
}
HttpRequest::~HttpRequest()
{
}