From b6f4bf8a71c60a825b1696b8a54b348fba60034d Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Sun, 29 Nov 2020 13:58:17 -0800 Subject: [PATCH] Got it compiling and somewhat working, except post. --- IMFHeader.cpp | 26 ++++++++++---------------- IMFHeader.h | 4 ++-- IMFMessage.cpp | 28 +++++++++++++--------------- IMFMessage.h | 2 +- IMFRequest.cpp | 9 ++++++++- IMFRequest.h | 2 ++ PString.cpp | 23 ++++++++++++++++++++--- PString.h | 13 +++++++++++++ 8 files changed, 69 insertions(+), 38 deletions(-) diff --git a/IMFHeader.cpp b/IMFHeader.cpp index fe5a3c7..c16c1ae 100644 --- a/IMFHeader.cpp +++ b/IMFHeader.cpp @@ -1,5 +1,6 @@ #include "IMFHeader.h" #include "Exception.h" +#include "Log.h" namespace coreutils { @@ -8,25 +9,18 @@ namespace coreutils { } IMFHeader::IMFHeader(PString &in) { - printf("in='%s'\n", in.str().c_str()); key = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789"); if(key.length() != 0) { - if(!in.ifNext(":")) { - printf("key=%s %02X\n", key.c_str(), in.str()[0]); - throw coreutils::Exception("Invalid character in expected header token."); - } - in.skipWhitespace(); - value = in.str(); + if(!in.ifNext(":")) { + printf("key=%s %02X\n", key.c_str(), in.str()[0]); + throw coreutils::Exception("Invalid character in expected header token."); + } + in.skipWhitespace(); + value = in.goeol(); } - else if(in.skipWhitespace() > 0) { - - - - } - else if(in.str() == "") { - - } - + else if(in.skipWhitespace() > 0) {} + else if(in.str() == "") {} + coreutils::Log(coreutils::LOG_DEBUG_2) << " " << key << "[" << value << "]"; } IMFHeader::IMFHeader(std::string key, std::string value) { diff --git a/IMFHeader.h b/IMFHeader.h index b1da8bc..fd3e721 100644 --- a/IMFHeader.h +++ b/IMFHeader.h @@ -22,9 +22,9 @@ namespace coreutils { private: std::string key; std::string value; - + }; - + } #endif diff --git a/IMFMessage.cpp b/IMFMessage.cpp index 2fa882b..d88cdf2 100644 --- a/IMFMessage.cpp +++ b/IMFMessage.cpp @@ -16,26 +16,24 @@ namespace coreutils { bool IMFMessage::parse(PString &in) { - coreutils::Log(coreutils::LOG_DEBUG_4) << "parse [" << in.str() << "]"; - - if(in.str() != "") { + while (!in.ifNext("\r\n")) { headers.emplace_back(in); - return true; } - coreutils::Log(coreutils::LOG_DEBUG_2) << "End of header with Content Type = " << getHeader("Content-Type") << " for " << getHeader("Content-Length") << " bytes."; - std::string type = getHeader("Content-Type"); + std::stringstream length(getHeader("Content-Length")); + int len = 0; + length >> len; + if(len > 0) { + PString block = in.readBlock(len); + if(type == "multipart/form-data") + body = new IMFMultipart(block, getHeaderKeyPairValue("Content-Type", "boundary")); + else if(type == "text/plain") + body = new IMFPlainText(in); + else + body = new IMFBody(); - if(type.size() == 0) - type = "text/plain"; - - if(type == "multipart/form-data") - body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary")); - else if(type == "text/plain") - body = new IMFPlainText(in); - else - body = new IMFBody(); + } return false; } diff --git a/IMFMessage.h b/IMFMessage.h index 512134d..4b2f6c5 100644 --- a/IMFMessage.h +++ b/IMFMessage.h @@ -25,7 +25,7 @@ namespace coreutils { IMFBody *getBody(); -// protected: + protected: std::vector headers; IMFBody *body; diff --git a/IMFRequest.cpp b/IMFRequest.cpp index 5ee7b2b..f919288 100644 --- a/IMFRequest.cpp +++ b/IMFRequest.cpp @@ -1,3 +1,4 @@ +#include #include "IMFRequest.h" #include "Exception.h" #include "Log.h" @@ -7,7 +8,13 @@ namespace coreutils { IMFRequest::IMFRequest() {} IMFRequest::IMFRequest(PString &in) { - parts = in.split(" "); + coreutils::PString request = in.goeol(); + parts = request.split(" "); + } + + void IMFRequest::parse(PString &in) { + coreutils::PString request = in.goeol(); + parts = request.split(" "); } std::string IMFRequest::getMethod() { diff --git a/IMFRequest.h b/IMFRequest.h index 4a36128..87d613d 100644 --- a/IMFRequest.h +++ b/IMFRequest.h @@ -11,6 +11,8 @@ namespace coreutils { IMFRequest(); IMFRequest(PString &in); + void parse(PString &in); + std::string getMethod(); std::string getURI(); std::string getProtocol(); diff --git a/PString.cpp b/PString.cpp index 6eb34b9..ee1f9e1 100644 --- a/PString.cpp +++ b/PString.cpp @@ -18,12 +18,16 @@ namespace coreutils { return pstring.substr(cursor); } + std::string PString::str(int len) { + return pstring.substr(cursor, len); + } + std::vector PString::split(std::string delimiter, int maxSize) { list.clear(); if(pstring.size() == 0) { - list.push_back(PString("")); - return list; + list.push_back(PString("")); + return list; } size_t end; @@ -80,10 +84,23 @@ namespace coreutils { size_t temp = cursor; cursor = pstring.find_first_not_of(" \t", cursor); cursor = cursor == -1 ? temp: cursor; - coreutils::Log(coreutils::LOG_DEBUG_2) << "Skipping whitespace for " << (cursor - temp) << " spaces."; return cursor - temp; } + std::string PString::goeol() { + size_t temp = cursor; + cursor = pstring.find_first_of("\r\n", cursor); + std::string value = pstring.substr(temp, (cursor - temp)); + if(pstring[cursor] == '\r') + ++cursor; + cursor = cursor == -1 ? temp: ++cursor; + return value; + } + + std::string PString::readBlock(size_t size) { + printf(">>>>>: %i:%i", size, pstring.size() - cursor); + } + int PString::size() { return pstring.size() - cursor; } diff --git a/PString.h b/PString.h index 43495c6..96b0174 100644 --- a/PString.h +++ b/PString.h @@ -42,6 +42,13 @@ namespace coreutils { std::string str(); + /// + /// Return the current value of the PString as a string of + /// specified length. + /// + + std::string str(int len); + /// /// /// @@ -88,6 +95,12 @@ namespace coreutils { /// /// + std::string goeol(); + + /// + /// + /// + int cursor = 0; ///