From a2cf41ef9c5360f405de5915d4dcb6634f602d9c Mon Sep 17 00:00:00 2001 From: jyelon Date: Tue, 29 Mar 2022 16:50:26 -0400 Subject: [PATCH] Partially finished HTTP stuff --- luprex/core/cpp/http.cpp | 26 +++++++++++++++++++++++++ luprex/core/cpp/http.hpp | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/luprex/core/cpp/http.cpp b/luprex/core/cpp/http.cpp index e69de29b..76890674 100644 --- a/luprex/core/cpp/http.cpp +++ b/luprex/core/cpp/http.cpp @@ -0,0 +1,26 @@ + +#include "wrap-sstream.hpp" +#include "wrap-string.hpp" + +static eng::string url_encode(const eng::string &value) { + eng::ostringstream escaped; + escaped.fill('0'); + escaped << hex; + + for (int i = 0; i < int(value.size()); i++) { + char c = value[i]; + + // Keep alphanumeric and other accepted characters intact + // Any other characters are percent-encoded + if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) << int((unsigned char) c); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + diff --git a/luprex/core/cpp/http.hpp b/luprex/core/cpp/http.hpp index e69de29b..8787c896 100644 --- a/luprex/core/cpp/http.hpp +++ b/luprex/core/cpp/http.hpp @@ -0,0 +1,41 @@ +///////////////////////////////////////////////////////// +// +// HTTP Implementation. +// +// This is a fairly limited implementation of HTTP. +// +// It only supports HTTP 1.1 +// +// It only supports GET, POST, and HEAD. +// +///////////////////////////////////////////////////////// + +#ifndef HTTP_HPP +#define HTTP_HPP + +#include "eng-malloc.hpp" +#include "wrap-string.hpp" +#include "wrap-vector.hpp" +#include "luastack.hpp" + +class HttpRequest : public eng::nevernew { +public: + enum Method { INVALID, GET, POST, HEAD }; + +public: + eng::string host_; + int port_; + Method method_; + eng::string encoded_url_; + eng::string content_type_; +}; + +class HttpReply : public eng::nevernew { +public: + int error_code_; + eng::string error_message_; + eng::string mime_type_; + eng::string content_; +}; + +#endif // HTTP_HPP