forked from cat/WebBase
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 "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
|
||||
@@ -10,8 +20,8 @@ void HTTPrequest::start() {
|
||||
|
||||
void HTTPrequest::processRequest() {
|
||||
try {
|
||||
Webserver::responseMethods[requestType].at(requestPath)(*this);
|
||||
} catch (std::exception e) {
|
||||
Webserver::responseMethods[requestType].at(requestPath)(this);
|
||||
} catch (std::out_of_range &e) {
|
||||
sendResponse("404 Not Found", "text/html",
|
||||
Helpers::ReadFile("www/error.html"));
|
||||
}
|
||||
@@ -21,14 +31,32 @@ void HTTPrequest::sendResponse(std::string status, std::string mime,
|
||||
std::string data) {
|
||||
// Logging here perhaps
|
||||
std::stringstream output;
|
||||
output << "HTTP/1.1 " << status << "\n"
|
||||
<< "Content-Type: " << mime << "\nContent-Length: " << data.size()
|
||||
<< "\nConnection: close\n\n"
|
||||
output << "HTTP/1.1 " << status << "\r\n"
|
||||
<< "Content-Type: " << mime << "\r\nContent-Length: " << data.size()
|
||||
<< "\r\nConnection: close\r\n\r\n"
|
||||
<< 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 =================
|
||||
|
||||
HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {}
|
||||
|
@@ -8,16 +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.sendResponse("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);
|
||||
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
|
||||
@@ -33,7 +36,6 @@ void HTTPrequest::processHTTPHeader() {
|
||||
|
||||
// RESPOND
|
||||
processRequest();
|
||||
sock.close(); // end
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -31,13 +31,14 @@ public:
|
||||
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