Compare commits
3 Commits
0d87542196
...
809c833259
Author | SHA1 | Date | |
---|---|---|---|
809c833259 | |||
fb3f1e7c37 | |||
cc3eb54b3b |
51
src/HTTP.cpp
51
src/HTTP.cpp
@@ -1,6 +1,17 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
#include <exception>
|
||||
#include <asio/buffer.hpp>
|
||||
#include <asio/completion_condition.hpp>
|
||||
#include <asio/impl/write.hpp>
|
||||
#include <asio/registered_buffer.hpp>
|
||||
#include <asio/socket_base.hpp>
|
||||
#include <asio/streambuf.hpp>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
void HTTPrequest::start() {
|
||||
// Possible Logging here
|
||||
@@ -9,19 +20,43 @@ void HTTPrequest::start() {
|
||||
|
||||
void HTTPrequest::processRequest() {
|
||||
try {
|
||||
Webserver::responseMethods[requestType].at(requestPath)(*this);
|
||||
} catch (std::exception e) {
|
||||
writeData(Helpers::GenerateResponse("404 Not Found", "text/html",
|
||||
Helpers::ReadFile("www/error.html")));
|
||||
Webserver::responseMethods[requestType].at(requestPath)(this);
|
||||
} catch (std::out_of_range &e) {
|
||||
sendResponse("404 Not Found", "text/html",
|
||||
Helpers::ReadFile("www/error.html"));
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPrequest::writeData(std::string data) {
|
||||
void HTTPrequest::sendResponse(std::string status, std::string mime,
|
||||
std::string data) {
|
||||
// Logging here perhaps
|
||||
asio::async_write(sock, asio::buffer(data),
|
||||
[](std::error_code, std::size_t) {});
|
||||
std::stringstream output;
|
||||
output << "HTTP/1.1 " << status << "\r\n"
|
||||
<< "Content-Type: " << mime << "\r\nContent-Length: " << data.size()
|
||||
<< "\r\nConnection: close\r\n\r\n"
|
||||
<< data;
|
||||
|
||||
responseText = output.str();
|
||||
asio::async_write(
|
||||
sock, asio::buffer(responseText),
|
||||
[this, self = shared_from_this()](std::error_code, std::size_t) {
|
||||
sock.shutdown(asio::ip::tcp::socket::shutdown_send);
|
||||
waitForClientClose();
|
||||
});
|
||||
}
|
||||
|
||||
// I'm not happy with this
|
||||
void HTTPrequest::waitForClientClose() {
|
||||
auto buf = std::array<char, 1>();
|
||||
sock.async_read_some(asio::buffer(buf),
|
||||
[this, self = shared_from_this()](
|
||||
const std::error_code &error, std::size_t) {
|
||||
if (error) {
|
||||
sock.close();
|
||||
}
|
||||
waitForClientClose();
|
||||
});
|
||||
}
|
||||
// ================= CLASS HANDLING SPECIFIC =================
|
||||
|
||||
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
|
||||
|
@@ -8,12 +8,16 @@
|
||||
// So just add your logic here and relax ;)
|
||||
std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest *self)>>>
|
||||
Webserver::responseMethods;
|
||||
|
||||
void Webserver::initResponses() {
|
||||
responseMethods["GET"]["/"] = [](HTTPrequest &self) {
|
||||
self.writeData(Helpers::GenerateResponse(
|
||||
"200 OK", "text/html", Helpers::ReadFile("www/index.html")));
|
||||
responseMethods["GET"]["/"] = [](HTTPrequest *self) {
|
||||
self->sendResponse("200 OK", "text/html",
|
||||
Helpers::ReadFile("www/index.html"));
|
||||
};
|
||||
|
||||
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
|
||||
self->sendResponse("200 OK", "text/text", self->bodyContent);
|
||||
};
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
#include <asio/socket_base.hpp>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
// TODO: Remove boundary from the body. Do keep other stuff like
|
||||
// file type and name
|
||||
@@ -31,8 +34,8 @@ void HTTPrequest::processHTTPHeader() {
|
||||
processHeaderValues(stream, octetCount, packageSize);
|
||||
processBody();
|
||||
|
||||
processRequest(); // This writes data too
|
||||
sock.close();
|
||||
// RESPOND
|
||||
processRequest();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -22,13 +22,3 @@ void Helpers::getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
|
||||
count += string.size() + 1; // Delimiter
|
||||
return;
|
||||
}
|
||||
std::string Helpers::GenerateResponse(std::string statusCode,
|
||||
std::string contentType,
|
||||
std::string content) {
|
||||
std::stringstream output;
|
||||
output << "HTTP/1.1 " << statusCode << "\n"
|
||||
<< "Content-Type: " << contentType
|
||||
<< "\nContent-Length: " << content.size() << "\nConnection: close\n\n"
|
||||
<< content;
|
||||
return output.str();
|
||||
}
|
||||
|
@@ -5,6 +5,4 @@ namespace Helpers {
|
||||
std::string ReadFile(std::string Path);
|
||||
void getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
|
||||
std::string &string, char delimit = '\0');
|
||||
std::string GenerateResponse(std::string statusCode, std::string contentType,
|
||||
std::string content);
|
||||
} // namespace Helpers
|
||||
|
@@ -28,16 +28,17 @@ public:
|
||||
static HTTPrequest_ptr create(asio::io_context &context);
|
||||
|
||||
void start();
|
||||
void writeData(std::string data);
|
||||
void sendResponse(std::string status, std::string mime, std::string data);
|
||||
|
||||
// Request itself
|
||||
std::string requestType, requestPath;
|
||||
std::string requestType, requestPath, responseText;
|
||||
std::unordered_map<std::string, std::string> headers, args;
|
||||
std::string bodyContent;
|
||||
|
||||
private:
|
||||
HTTPrequest(asio::io_context &context);
|
||||
void processRequest();
|
||||
void waitForClientClose();
|
||||
|
||||
// Breaking Header to in lines
|
||||
void processHTTPHeader();
|
||||
@@ -60,7 +61,7 @@ public:
|
||||
// Responses
|
||||
static std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest *self)>>>
|
||||
responseMethods;
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user