diff --git a/File.h b/File.h index 578028f..1f511a0 100644 --- a/File.h +++ b/File.h @@ -2,6 +2,7 @@ #define __File_h__ #include "includes" +#include "PString.h" /// /// File diff --git a/IMFBody.h b/IMFBody.h index 8bd27d4..add159b 100644 --- a/IMFBody.h +++ b/IMFBody.h @@ -3,7 +3,19 @@ namespace coreutils { - class IMFBody {}; + class IMFBody { + + public: + IMFBody(PString *body) : body(body) {} + + PString *getBody() { + return body; + } + + private: + PString *body; + + }; } diff --git a/IMFFormData.cpp b/IMFFormData.cpp index 0c65494..c252358 100644 --- a/IMFFormData.cpp +++ b/IMFFormData.cpp @@ -4,21 +4,21 @@ namespace coreutils { - IMFFormData::IMFFormData(PString &in, std::string boundary) : IMFMultipart(in, boundary) { - + IMFFormData::IMFFormData(PString *in, std::string boundary) : IMFMultipart(in, boundary) { + } std::string IMFFormData::getByName(std::string name) { - std::string value; + std::string value; for(PString section: sections) { IMFMessage message(section); if(message.getHeaderKeyPairValue("Content-Disposition", "name") == name) { IMFPlainText *text = (IMFPlainText *)message.getBody(); return text->toString(); } - + } return ""; } - + } diff --git a/IMFFormData.h b/IMFFormData.h index a97162b..2eba937 100644 --- a/IMFFormData.h +++ b/IMFFormData.h @@ -4,17 +4,19 @@ #include "IMFMultipart.h" namespace coreutils { - + class IMFFormData: public IMFMultipart { - + public: IMFFormData(); - IMFFormData(PString &in, std::string boundary); + IMFFormData(PString *in, std::string boundary); std::string getByName(std::string name); - + }; - + } #endif + + diff --git a/IMFMessage.cpp b/IMFMessage.cpp index 35033a7..6a25c20 100644 --- a/IMFMessage.cpp +++ b/IMFMessage.cpp @@ -16,7 +16,7 @@ namespace coreutils { bool IMFMessage::parse(PString &in) { - coreutils::Log(coreutils::LOG_DEBUG_1) << "Parsing-------\n" << in.str(); + Log(LOG_DEBUG_1) << "Parsing-------\n" << in.str(); while (!in.ifNext("\r\n")) { headers.emplace_back(in); @@ -31,9 +31,9 @@ namespace coreutils { if(type == "multipart/form-data") body = new IMFMultipart(block, getHeaderKeyPairValue("Content-Type", "boundary")); else if(type == "text/plain") - body = new IMFPlainText(in); + body = new IMFPlainText(block); else - body = new IMFBody(); + body = new IMFBody(&block); } return false; @@ -102,4 +102,8 @@ namespace coreutils { return body; } + void IMFMessage::setBody(PString *body) { + this->body = new IMFBody(body); + } + } diff --git a/IMFMessage.h b/IMFMessage.h index 4b2f6c5..0f8454e 100644 --- a/IMFMessage.h +++ b/IMFMessage.h @@ -8,6 +8,14 @@ namespace coreutils { + /// + /// IMFMessage provides an object representation of a standard Internet + /// Message Format message. + /// + /// Contstructing the object with a reference to a PString will generate + /// an object representation of the data therein. + /// + class IMFMessage { public: @@ -24,6 +32,7 @@ namespace coreutils { std::string getHeaderKeyPairValue(std::string headerKey, std::string key); IMFBody *getBody(); + void setBody(PString *in); protected: std::vector headers; diff --git a/IMFMultipart.cpp b/IMFMultipart.cpp index 1936e6c..8a6699b 100644 --- a/IMFMultipart.cpp +++ b/IMFMultipart.cpp @@ -3,14 +3,10 @@ namespace coreutils { - IMFMultipart::IMFMultipart() { - buffer = ""; - } - - IMFMultipart::IMFMultipart(PString &in, std::string boundary) { - buffer = in.str(); - in.cursor -= 2; - sections = in.split("\r\n--" + boundary); + IMFMultipart::IMFMultipart(PString *in, std::string boundary) : IMFBody(in) { + buffer = in->str(); + in->cursor -= 2; + sections = in->split("\r\n--" + boundary); for(int ix = 0; ix < sections.size(); ++ix) { if(sections[ix].str() == "--\r\n") sections[ix] = PString(""); @@ -20,7 +16,7 @@ namespace coreutils { } std::string IMFMultipart::toString() { - return buffer; + return buffer.str(); } int IMFMultipart::getCount() { @@ -30,5 +26,5 @@ namespace coreutils { PString IMFMultipart::getSectionAt(int index) { return sections[index]; } - + } diff --git a/IMFMultipart.h b/IMFMultipart.h index 2dce763..4b81ed8 100644 --- a/IMFMultipart.h +++ b/IMFMultipart.h @@ -5,26 +5,25 @@ #include "IMFBody.h" namespace coreutils { - + class IMFMultipart: public IMFBody { - + public: - IMFMultipart(); - IMFMultipart(PString &in, std::string boundary); - + IMFMultipart(PString *in, std::string boundary); + std::string toString(); int getCount(); PString getSectionAt(int index); - + protected: std::vector sections; - + private: std::string boundary; - - std::string buffer; + + coreutils::PString buffer; }; - + } #endif diff --git a/IMFPlainText.cpp b/IMFPlainText.cpp index 6e256f8..4f2b3e9 100644 --- a/IMFPlainText.cpp +++ b/IMFPlainText.cpp @@ -2,16 +2,12 @@ namespace coreutils { - IMFPlainText::IMFPlainText() { - text = ""; - } - - IMFPlainText::IMFPlainText(PString &in) : IMFBody() { - text = in.str(); + IMFPlainText::IMFPlainText(PString &in) : IMFBody(in) { + text = in.str(); } std::string IMFPlainText::toString() { return text; } - + } diff --git a/IMFPlainText.h b/IMFPlainText.h index 931ae18..03153d1 100644 --- a/IMFPlainText.h +++ b/IMFPlainText.h @@ -5,20 +5,20 @@ #include "IMFBody.h" namespace coreutils { - + class IMFPlainText: public IMFBody { - + public: IMFPlainText(); IMFPlainText(PString &in); - + std::string toString(); - + protected: std::string text; - + }; - + } #endif diff --git a/IMFResponse.cpp b/IMFResponse.cpp index 51254ad..9965344 100644 --- a/IMFResponse.cpp +++ b/IMFResponse.cpp @@ -2,43 +2,43 @@ #include "Log.h" namespace coreutils { - + IMFResponse::IMFResponse() {} - IMFResponse::~IMFResponse() {} - + IMFResponse::~IMFResponse() {} + std::string IMFResponse::getResponse(Mode mode) { return getResponse("", mode); } - + std::string IMFResponse::getResponse(std::string content, Mode mode) { std::stringstream response; response << protocol << " " << code << " " << text << CRLF; - + if(mode == LENGTH) addHeader("Content-Length", std::to_string(content.size())); else addHeader("Transfer-Encoding", "chunked"); - + addHeader("Server", "JETServer v0.0.1"); - output(response); - response << content; -// core::Log(core::LOG_DEBUG_4) << response.str(); - return response.str(); + output(response); + response << content; + coreutils::Log(coreutils::LOG_DEBUG_4) << response.str(); + return response.str(); } - + void IMFResponse::setProtocol(std::string protocol) { this->protocol = protocol; } - + void IMFResponse::setCode(std::string code) { this->code = code; } - + void IMFResponse::setText(std::string text) { this->text = text; } - + void IMFResponse::setCookie(std::string name, std::string value) { addHeader("Set-Cookie", name + "=" + value + ";"); } diff --git a/IMFResponse.h b/IMFResponse.h index 8ffd5fe..db80234 100644 --- a/IMFResponse.h +++ b/IMFResponse.h @@ -5,32 +5,32 @@ #include "IMFMessage.h" namespace coreutils { - + /// /// IMFResponse /// /// Use this object to build a response output for a protocol that uses headers /// and responses as the main communications protocol. /// - + class IMFResponse : public IMFMessage { - + public: - + enum Mode { LENGTH, STREAMING }; - + /// /// The constructor for the object. /// - + IMFResponse(); - - /// + + /// /// The destructor for the object. /// - - ~IMFResponse(); - + + ~IMFResponse(); + /// /// Returns the response generated from the contained values that /// do not return a content length. Using this constructor ensures @@ -38,9 +38,9 @@ namespace coreutils { /// /// @return the complete response string to send to client. /// - + std::string getResponse(Mode mode = LENGTH); - + /// /// Returns the response plus the content passed as a parameter. /// @@ -52,46 +52,46 @@ namespace coreutils { /// /// @return the complete response string to send to client. /// - + std::string getResponse(std::string content, Mode mode = LENGTH); - + /// - /// Sets the protocol value for the response message. The protocol + /// Sets the protocol value for the response message. The protocol /// should match the header received. /// - /// @param protocol the protocol value to return in response. + /// @param protocol the protocol value to return in response. /// - + void setProtocol(std::string protocol); - + /// /// Sets the return code value for the response. /// /// @param code the response code value to return in the response. /// - + void setCode(std::string code); - + /// /// Sets the return code string value for the response. /// - /// @param text the text value for the response code to return on + /// @param text the text value for the response code to return on /// the response. /// - + void setText(std::string text); - + void setCookie(std::string name, std::string value); - + private: std::string protocol; std::string code; std::string text; - + std::string CRLF = "\r\n"; - + }; - + } #endif diff --git a/PString.cpp b/PString.cpp index ee1f9e1..f8fa349 100644 --- a/PString.cpp +++ b/PString.cpp @@ -98,7 +98,11 @@ namespace coreutils { } std::string PString::readBlock(size_t size) { - printf(">>>>>: %i:%i", size, pstring.size() - cursor); + int cursorSave = cursor; + cursor += size; + if(cursor > pstring.size()) + cursor = pstring.size(); + return pstring.substr(cursorSave, size); } int PString::size() { diff --git a/PString.h b/PString.h index 3cecc87..3a34165 100644 --- a/PString.h +++ b/PString.h @@ -43,7 +43,7 @@ namespace coreutils { std::string str(); /// - /// Return the current value of the PString as a string of + /// Return the current value of the PString as a string of /// specified length. /// @@ -102,11 +102,11 @@ namespace coreutils { /// std::string readBlock(size_t size); - + /// /// /// - + int cursor = 0; /// diff --git a/ZString.h b/ZString.h new file mode 100644 index 0000000..78f9c8b --- /dev/null +++ b/ZString.h @@ -0,0 +1,45 @@ +#ifndef __ZString_h__ +#define __ZString_h__ + +#include "includes"` + +namespace coreutils { + + /// + /// ZString provides a data pointer and a length to basically point to a string + /// from a common backstore. + /// + + class ZString { + + public: + ZString() {} + ZString(char *data) { + this->data = data; + length = strlen(data); + } + + ZString(char *data, size_t length) { + this->data = data; + this->length = length; + } + + std::string asString() { + return std::string(data, length); + } + + void setString(char *data, size_t length) { + this->data = data; + this->length = length; + } + + private: + char *data; + size_t length; + + }; + + +} + +#endif \ No newline at end of file