180 lines
3.8 KiB
C++
180 lines
3.8 KiB
C++
//
|
|
// request_handler.cpp
|
|
// ~~~~~~~~~~~~~~~~~~~
|
|
//
|
|
// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
//
|
|
|
|
#include "request_handler.hpp"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <map>
|
|
#include "mime_types.hpp"
|
|
#include "reply.hpp"
|
|
#include "request.hpp"
|
|
|
|
#include "boost/algorithm/string.hpp"
|
|
#include "boost/property_tree/json_parser.hpp"
|
|
#include "../utf8utf16.h"
|
|
#include "../noun.h"
|
|
|
|
namespace http {
|
|
namespace server {
|
|
|
|
request_handler::request_handler()
|
|
{
|
|
}
|
|
|
|
void request_handler::handle_request(const request& req, reply& rep)
|
|
{
|
|
// Decode url to path.
|
|
std::string request_path;
|
|
if (!url_decode(req.uri, request_path))
|
|
{
|
|
rep = reply::stock_reply(reply::bad_request);
|
|
return;
|
|
}
|
|
|
|
// Request path must be absolute and not contain "..".
|
|
if (request_path.empty() || request_path[0] != '/'
|
|
|| request_path.find("..") != std::wstring::npos)
|
|
{
|
|
rep = reply::stock_reply(reply::bad_request);
|
|
return;
|
|
}
|
|
|
|
// If path ends in slash (i.e. is a directory) then add "index.html".
|
|
if (request_path[request_path.size() - 1] == '/')
|
|
{
|
|
request_path += "index.html";
|
|
}
|
|
|
|
//Prepare request
|
|
if (request_path[0] == '/')
|
|
{
|
|
request_path = std::string(request_path.begin() + 1, request_path.end());
|
|
}
|
|
|
|
boost::to_lower(request_path);
|
|
|
|
|
|
std::wstring requestedStr = UTF8to16(request_path.c_str());
|
|
/*
|
|
requestedStr = L"Âû çàïðîñèëè: " + requestedStr;
|
|
|
|
rep.content = UTF16to8(requestedStr.c_str());
|
|
|
|
rep.content = "<html><body>" + rep.content + "</body></html>";
|
|
*/
|
|
|
|
boost::property_tree::wptree propertyTree = PrepareReport(requestedStr);
|
|
|
|
std::wstringstream output_stream;
|
|
|
|
boost::property_tree::write_json(output_stream, propertyTree);
|
|
|
|
std::string outputJsonCode = UTF16to8(output_stream.str().c_str());
|
|
|
|
rep.status = reply::ok;
|
|
|
|
rep.content = outputJsonCode;
|
|
|
|
rep.headers.resize(2);
|
|
rep.headers[0].name = "Content-Length";
|
|
rep.headers[0].value = std::to_string(rep.content.size());
|
|
rep.headers[1].name = "Content-Type";
|
|
rep.headers[1].value = "application/json; charset=utf-8";
|
|
}
|
|
|
|
bool request_handler::url_decode(const std::string& in, std::string& out)
|
|
{
|
|
|
|
out.clear();
|
|
out.reserve(in.size());
|
|
|
|
for (std::size_t i = 0; i < in.size(); ++i)
|
|
{
|
|
if (in[i] == '%')
|
|
{
|
|
if (i + 3 <= in.size())
|
|
{
|
|
int value = 0;
|
|
std::istringstream is(in.substr(i + 1, 2));
|
|
if (is >> std::hex >> value)
|
|
{
|
|
out += static_cast<char>(value);
|
|
i += 2;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else if (in[i] == '+')
|
|
{
|
|
out += ' ';
|
|
}
|
|
else
|
|
{
|
|
out += in[i];
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
boost::property_tree::wptree PrepareReport(std::wstring request)
|
|
{
|
|
boost::property_tree::wptree result;
|
|
|
|
if (request.size() < 2)
|
|
{
|
|
result.put(L"error", L"String is too short");
|
|
}
|
|
|
|
std::vector<NounStruct> nounStructArr = RecognizeNoun(request);
|
|
|
|
|
|
|
|
int id = 0;
|
|
|
|
boost::property_tree::wptree nounArr;
|
|
|
|
for (auto& nounStruct : nounStructArr)
|
|
{
|
|
boost::property_tree::wptree nounTree;
|
|
|
|
nounTree.put(L"id", id);
|
|
nounTree.put(L"declencion", NounDeclencionToWString(std::get<0>(nounStruct.nounTuple)));
|
|
nounTree.put(L"grammaticalCase", NounGrammaticalCaseToWString(std::get<1>(nounStruct.nounTuple)));
|
|
nounTree.put(L"number", NounNumberToWString(std::get<2>(nounStruct.nounTuple)));
|
|
|
|
|
|
nounTree.put(L"nominative", nounStruct.noun);
|
|
|
|
nounArr.push_back(std::make_pair(L"", nounTree));
|
|
|
|
id++;
|
|
}
|
|
|
|
result.put_child(L"nouns", nounArr);
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // namespace server
|
|
} // namespace http
|