Let's make (git) history!

This commit is contained in:
2018-11-03 17:12:20 +01:00
کامیت 3bfebfe8a8
212فایلهای تغییر یافته به همراه11970 افزوده شده و 0 حذف شده

109
gateway/cgi.cpp Normal file
مشاهده پرونده

@ -0,0 +1,109 @@
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "cgi.h"
#include "../utils.h"
#include <cstdlib>
#include <string>
#include <memory>
#include <iostream>
Cgi::Cgi(const Config &c)
{
this->config = &c;
}
bool Cgi::keepReading()
{
return !this->responseSent;
}
Request Cgi::readRequest()
{
std::string request_uri = utils::getenv("REQUEST_URI");
if(request_uri == "")
{
throw std::runtime_error("REQUEST_URI is empty");
}
Request result{request_uri};
std::string method = utils::getenv("REQUEST_METHOD");
if(method == "POST")
{
std::string content_type = utils::getenv("CONTENT_TYPE");
if(content_type != "application/x-www-form-urlencoded")
{
throw "invalid content_type";
}
std::string content_length = utils::getenv("CONTENT_LENGTH");
int cl = std::stoi(content_length);
std::unique_ptr<char[]> ptr(new char[cl + 1]);
std::cin.get(ptr.get(), cl + 1);
std::string post_data{ptr.get()};
}
result.initCookies(utils::getenv("HTTP_COOKIE"));
result.setIp(utils::getenv("REMOTE_ADDR"));
result.setUseragent(utils::getenv("HTTP_USER_AGENT"));
return result;
}
void Cgi::work(RequestWorker &worker)
{
while(this->keepReading())
{
Request req = readRequest();
sendResponse(worker.processRequest(req));
}
}
void Cgi::sendResponse(const Response &r)
{
std::cout << "Status: " << r.getStatus() << "\r\n";
std::cout << "Content-Type: " << r.getContentType() << "\r\n";
for(auto header : r.getResponseHeaders())
{
std::string key = header.first;
std::string second = header.second;
if(key.back() != ':')
{
std::cout << key << ":" << second << "\r\n";
}
else
{
std::cout << key << second << "\r\n";
}
}
for(const Cookie &c : r.getCookies())
{
std::cout << "Set-Cookie: " << c.createHeaderValue() << "\r\n";
}
std::cout << "\r\n";
std::cout << r.getBody();
std::cout.flush();
this->responseSent = true;
}
Cgi::~Cgi()
{
}

22
gateway/cgi.h Normal file
مشاهده پرونده

@ -0,0 +1,22 @@
#ifndef CGI_H
#define CGI_H
#include "gatewayinterface.h"
#include "../requestworker.h"
class Cgi : public GatewayInterface
{
private:
bool responseSent = false;
const Config *config;
Request readRequest();
void sendResponse(const Response &r);
public:
Cgi(const Config &c);
bool keepReading() override;
void work(RequestWorker &worker) override;
~Cgi();
};
#endif // CGI_H

مشاهده پرونده

@ -0,0 +1,28 @@
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gatewayfactory.h"
#include "cgi.h"
#include "httpgateway.h"
std::unique_ptr<GatewayInterface> createGateway(const Config &c)
{
return std::make_unique<HttpGateway>(c);
}

8
gateway/gatewayfactory.h Normal file
مشاهده پرونده

@ -0,0 +1,8 @@
#ifndef GATEWAYFACTORY_H
#define GATEWAYFACTORY_H
#include <memory>
#include "../config.h"
#include "gatewayinterface.h"
std::unique_ptr<GatewayInterface> createGateway(const Config &c);
#endif // GATEWAYFACTORY_H

مشاهده پرونده

@ -0,0 +1,25 @@
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gatewayinterface.h"
GatewayInterface::GatewayInterface()
{
}

مشاهده پرونده

@ -0,0 +1,18 @@
#ifndef GATEWAYINTERFACE_H
#define GATEWAYINTERFACE_H
#include "../request.h"
#include "../response.h"
#include "../config.h"
#include "../requestworker.h"
class GatewayInterface
{
public:
GatewayInterface();
virtual bool keepReading() = 0;
virtual void work(RequestWorker &worker) = 0;
virtual ~GatewayInterface()
{
}
};
#endif // GATEWAYINTERFACE_H

93
gateway/httpgateway.cpp Normal file
مشاهده پرونده

@ -0,0 +1,93 @@
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "httpgateway.h"
#include "../logger.h"
HttpGateway::HttpGateway(const Config &config)
{
}
bool HttpGateway::keepReading()
{
return true;
}
Request HttpGateway::convertRequest(httplib::Request request)
{
Request result;
result.setRequestMethod(request.method);
result.setUrl(request.target);
// TODO: this eats resources, where perhaps it does not need to. move it to request?
for(auto &it : request.params)
{
it.second = utils::html_xss(std::string{it.second});
}
if(request.method == "GET")
{
result.setGetVars(request.params);
}
else
{
result.initGetMap(request.target);
result.setPostVars(request.params);
}
if(request.has_header("COOKIE"))
{
result.initCookies(request.get_header_value("COOKIE"));
}
result.setIp(request.get_header_value("REMOTE_ADDR"));
return result;
}
httplib::Response HttpGateway::convertResponse(Response response)
{
httplib::Response result;
result.set_content(response.getBody(), response.getContentType().c_str());
result.status = response.getStatus();
for(auto &header : response.getResponseHeaders())
{
result.set_header(header.first.c_str(), header.second.c_str());
}
for(const Cookie &cookie : response.getCookies())
{
result.set_header("Set-Cookie", cookie.createHeaderValue().c_str());
}
return result;
}
void HttpGateway::work(RequestWorker &worker)
{
httplib::Server server;
auto handler = [&](const httplib::Request &req, httplib::Response &res) {
Request wikiRequest = convertRequest(req);
Logger::debug() << "httpgateway: received request " << wikiRequest;
Response wikiresponse = worker.processRequest(wikiRequest);
res = convertResponse(wikiresponse);
};
server.Get("/(.*)", handler);
server.Post("/(.*)", handler);
server.listen("localhost", 1234);
}

22
gateway/httpgateway.h Normal file
مشاهده پرونده

@ -0,0 +1,22 @@
#ifndef HTTPGATEWAY_H
#define HTTPGATEWAY_H
#include "httplib.h"
#include "gatewayinterface.h"
#include "../requestworker.h"
#include "../request.h"
#include "../response.h"
#include "../utils.h"
class HttpGateway : public GatewayInterface
{
private:
Response convertResponse(httplib::Response response);
httplib::Response convertResponse(Response response);
Request convertRequest(httplib::Request request);
// void worker(const httplib::Request& req, httplib::Response& res);
public:
HttpGateway(const Config &config);
bool keepReading() override;
void work(RequestWorker &worker) override;
};
#endif // HTTPGATEWAY_H

2665
gateway/httplib.h Normal file

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است Diff را بارگزاری کن