<commit_before>#ifndef BEEEON_ROUTED_REQUEST_HANDLER_H
#define BEEEON_ROUTED_REQUEST_HANDLER_H

#include <Poco/Exception.h>
#include <Poco/Net/NetException.h>
#include <Poco/Logger.h>
#include <vector>
#include "server/Session.h"
#include "server/SessionVerifier.h"
#include "server/Route.h"
#include "Debug.h"

namespace BeeeOn {

template <typename Request, typename Response>
class TRestRequestHandlerFactory;

/**
 * Template class RequestHandler. It handles requests from clients
 * and generates appropriate responses. The TRestRequestHandler
 * uses Route class to define actions for URI patterns.
 */
template <typename Request, typename Response>
class TRestRequestHandler {
	friend TRestRequestHandlerFactory<Request, Response>;
public:
	typedef TRestRequestHandlerFactory<Request, Response> Factory;

	using Route = TRoute<Request, Response, ExpirableSession::Ptr>;
	using Params = typename Route::Params;
	using RouteContext = TRouteContext<Request, Response, ExpirableSession::Ptr>;

	TRestRequestHandler(const Route &route,
			const Params &params,
			const std::string &name,
			SessionVerifier *sessionVerifier = NULL):
		m_route(route),
		m_params(params),
		m_name(name),
		m_sessionVerifier(sessionVerifier),
		m_logger(LOGGER_CLASS(this))
	{
	}

	~TRestRequestHandler()
	{
	}

	void handleNoContentLength(Request &req)
	{
		const std::string &method = req.getMethod();

		if (!method.compare("POST") || !method.compare("PUT")) {
			if (!req.hasContentLength())
				throw Poco::InvalidArgumentException(
						"no Content-Length header");
		}
	}

	void handleRequest(Request &req, Response &res)
	{
		_TRACE_METHOD(m_logger);

		try {
			res.setVersion(Response::HTTP_1_1);
			res.setStatusAndReason(Response::HTTP_OK);
			m_route.debug(m_logger, m_params, __FILE__, __LINE__);

			handleNoContentLength(req);

			ExpirableSession::Ptr session =
				sessionVerify(req, m_route);

			m_route.execute(req, res, m_params, session);
		}
		catch (Poco::InvalidAccessException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(Response::HTTP_FORBIDDEN);
		}
		catch (Poco::Net::NotAuthenticatedException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.requireAuthentication(m_name);
		}
		catch (Poco::SyntaxException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_BAD_REQUEST);
		}
		catch (Poco::InvalidArgumentException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_BAD_REQUEST);
		}
		catch (Poco::Exception &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_INTERNAL_SERVER_ERROR);
		}
		catch (std::exception &e) {
			m_logger.critical(e.what(), __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_INTERNAL_SERVER_ERROR);
			res.setReason("Server internal error");<commit_msg>RestRequestHandler: convert NotFoundException to 404<commit_after>#ifndef BEEEON_ROUTED_REQUEST_HANDLER_H
#define BEEEON_ROUTED_REQUEST_HANDLER_H

#include <Poco/Exception.h>
#include <Poco/Net/NetException.h>
#include <Poco/Logger.h>
#include <vector>
#include "server/Session.h"
#include "server/SessionVerifier.h"
#include "server/Route.h"
#include "Debug.h"

namespace BeeeOn {

template <typename Request, typename Response>
class TRestRequestHandlerFactory;

/**
 * Template class RequestHandler. It handles requests from clients
 * and generates appropriate responses. The TRestRequestHandler
 * uses Route class to define actions for URI patterns.
 */
template <typename Request, typename Response>
class TRestRequestHandler {
	friend TRestRequestHandlerFactory<Request, Response>;
public:
	typedef TRestRequestHandlerFactory<Request, Response> Factory;

	using Route = TRoute<Request, Response, ExpirableSession::Ptr>;
	using Params = typename Route::Params;
	using RouteContext = TRouteContext<Request, Response, ExpirableSession::Ptr>;

	TRestRequestHandler(const Route &route,
			const Params &params,
			const std::string &name,
			SessionVerifier *sessionVerifier = NULL):
		m_route(route),
		m_params(params),
		m_name(name),
		m_sessionVerifier(sessionVerifier),
		m_logger(LOGGER_CLASS(this))
	{
	}

	~TRestRequestHandler()
	{
	}

	void handleNoContentLength(Request &req)
	{
		const std::string &method = req.getMethod();

		if (!method.compare("POST") || !method.compare("PUT")) {
			if (!req.hasContentLength())
				throw Poco::InvalidArgumentException(
						"no Content-Length header");
		}
	}

	void handleRequest(Request &req, Response &res)
	{
		_TRACE_METHOD(m_logger);

		try {
			res.setVersion(Response::HTTP_1_1);
			res.setStatusAndReason(Response::HTTP_OK);
			m_route.debug(m_logger, m_params, __FILE__, __LINE__);

			handleNoContentLength(req);

			ExpirableSession::Ptr session =
				sessionVerify(req, m_route);

			m_route.execute(req, res, m_params, session);
		}
		catch (Poco::NotFoundException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(Response::HTTP_NOT_FOUND);
		}
		catch (Poco::InvalidAccessException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(Response::HTTP_FORBIDDEN);
		}
		catch (Poco::Net::NotAuthenticatedException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.requireAuthentication(m_name);
		}
		catch (Poco::SyntaxException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_BAD_REQUEST);
		}
		catch (Poco::InvalidArgumentException &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_BAD_REQUEST);
		}
		catch (Poco::Exception &e) {
			m_logger.log(e, __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_INTERNAL_SERVER_ERROR);
		}
		catch (std::exception &e) {
			m_logger.critical(e.what(), __FILE__, __LINE__);
			res.setStatusAndReason(
				Response::HTTP_INTERNAL_SERVER_ERROR);
			res.setReason("Server internal error");
{'fraction_non_alphanumeric': 0.11905969896837476, 'fraction_numerical': 0.0011838322340605445, 'mean_word_length': 5.513215859030837, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2750378', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1944, 'n_words': 384}