Fixed write data... again. Fuck my programmer life.
This commit is contained in:
42
src/HTTP.cpp
42
src/HTTP.cpp
@@ -1,7 +1,17 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "Main.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 <string>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
void HTTPrequest::start() {
|
void HTTPrequest::start() {
|
||||||
// Possible Logging here
|
// Possible Logging here
|
||||||
@@ -10,8 +20,8 @@ void HTTPrequest::start() {
|
|||||||
|
|
||||||
void HTTPrequest::processRequest() {
|
void HTTPrequest::processRequest() {
|
||||||
try {
|
try {
|
||||||
Webserver::responseMethods[requestType].at(requestPath)(*this);
|
Webserver::responseMethods[requestType].at(requestPath)(this);
|
||||||
} catch (std::exception e) {
|
} catch (std::out_of_range &e) {
|
||||||
sendResponse("404 Not Found", "text/html",
|
sendResponse("404 Not Found", "text/html",
|
||||||
Helpers::ReadFile("www/error.html"));
|
Helpers::ReadFile("www/error.html"));
|
||||||
}
|
}
|
||||||
@@ -21,14 +31,32 @@ void HTTPrequest::sendResponse(std::string status, std::string mime,
|
|||||||
std::string data) {
|
std::string data) {
|
||||||
// Logging here perhaps
|
// Logging here perhaps
|
||||||
std::stringstream output;
|
std::stringstream output;
|
||||||
output << "HTTP/1.1 " << status << "\n"
|
output << "HTTP/1.1 " << status << "\r\n"
|
||||||
<< "Content-Type: " << mime << "\nContent-Length: " << data.size()
|
<< "Content-Type: " << mime << "\r\nContent-Length: " << data.size()
|
||||||
<< "\nConnection: close\n\n"
|
<< "\r\nConnection: close\r\n\r\n"
|
||||||
<< data;
|
<< data;
|
||||||
|
|
||||||
asio::write(sock, asio::buffer(output.str()));
|
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 =================
|
// ================= CLASS HANDLING SPECIFIC =================
|
||||||
|
|
||||||
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
|
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
|
||||||
|
@@ -8,16 +8,16 @@
|
|||||||
// So just add your logic here and relax ;)
|
// So just add your logic here and relax ;)
|
||||||
std::unordered_map<
|
std::unordered_map<
|
||||||
std::string,
|
std::string,
|
||||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
std::unordered_map<std::string, std::function<void(HTTPrequest *self)>>>
|
||||||
Webserver::responseMethods;
|
Webserver::responseMethods;
|
||||||
|
|
||||||
void Webserver::initResponses() {
|
void Webserver::initResponses() {
|
||||||
responseMethods["GET"]["/"] = [](HTTPrequest &self) {
|
responseMethods["GET"]["/"] = [](HTTPrequest *self) {
|
||||||
self.sendResponse("200 OK", "text/html",
|
self->sendResponse("200 OK", "text/html",
|
||||||
Helpers::ReadFile("www/index.html"));
|
Helpers::ReadFile("www/index.html"));
|
||||||
};
|
};
|
||||||
|
|
||||||
responseMethods["POST"]["/upload"] = [](HTTPrequest &self) {
|
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
|
||||||
self.sendResponse("200 OK", "text/text", self.bodyContent);
|
self->sendResponse("200 OK", "text/text", self->bodyContent);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
|
#include <asio/socket_base.hpp>
|
||||||
|
#include <iostream>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
// TODO: Remove boundary from the body. Do keep other stuff like
|
// TODO: Remove boundary from the body. Do keep other stuff like
|
||||||
// file type and name
|
// file type and name
|
||||||
@@ -33,7 +36,6 @@ void HTTPrequest::processHTTPHeader() {
|
|||||||
|
|
||||||
// RESPOND
|
// RESPOND
|
||||||
processRequest();
|
processRequest();
|
||||||
sock.close(); // end
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -31,13 +31,14 @@ public:
|
|||||||
void sendResponse(std::string status, std::string mime, std::string data);
|
void sendResponse(std::string status, std::string mime, std::string data);
|
||||||
|
|
||||||
// Request itself
|
// Request itself
|
||||||
std::string requestType, requestPath;
|
std::string requestType, requestPath, responseText;
|
||||||
std::unordered_map<std::string, std::string> headers, args;
|
std::unordered_map<std::string, std::string> headers, args;
|
||||||
std::string bodyContent;
|
std::string bodyContent;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HTTPrequest(asio::io_context &context);
|
HTTPrequest(asio::io_context &context);
|
||||||
void processRequest();
|
void processRequest();
|
||||||
|
void waitForClientClose();
|
||||||
|
|
||||||
// Breaking Header to in lines
|
// Breaking Header to in lines
|
||||||
void processHTTPHeader();
|
void processHTTPHeader();
|
||||||
@@ -60,7 +61,7 @@ public:
|
|||||||
// Responses
|
// Responses
|
||||||
static std::unordered_map<
|
static std::unordered_map<
|
||||||
std::string,
|
std::string,
|
||||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
std::unordered_map<std::string, std::function<void(HTTPrequest *self)>>>
|
||||||
responseMethods;
|
responseMethods;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user