This commit is contained in:
Brad Arant 2021-01-09 18:27:48 -08:00
parent c3c9530b4b
commit 7e06591de6
15 changed files with 161 additions and 93 deletions

1
File.h
View File

@ -2,6 +2,7 @@
#define __File_h__ #define __File_h__
#include "includes" #include "includes"
#include "PString.h"
/// ///
/// File /// File

View File

@ -3,7 +3,19 @@
namespace coreutils { namespace coreutils {
class IMFBody {}; class IMFBody {
public:
IMFBody(PString *body) : body(body) {}
PString *getBody() {
return body;
}
private:
PString *body;
};
} }

View File

@ -4,21 +4,21 @@
namespace coreutils { 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 IMFFormData::getByName(std::string name) {
std::string value; std::string value;
for(PString section: sections) { for(PString section: sections) {
IMFMessage message(section); IMFMessage message(section);
if(message.getHeaderKeyPairValue("Content-Disposition", "name") == name) { if(message.getHeaderKeyPairValue("Content-Disposition", "name") == name) {
IMFPlainText *text = (IMFPlainText *)message.getBody(); IMFPlainText *text = (IMFPlainText *)message.getBody();
return text->toString(); return text->toString();
} }
} }
return ""; return "";
} }
} }

View File

@ -4,17 +4,19 @@
#include "IMFMultipart.h" #include "IMFMultipart.h"
namespace coreutils { namespace coreutils {
class IMFFormData: public IMFMultipart { class IMFFormData: public IMFMultipart {
public: public:
IMFFormData(); IMFFormData();
IMFFormData(PString &in, std::string boundary); IMFFormData(PString *in, std::string boundary);
std::string getByName(std::string name); std::string getByName(std::string name);
}; };
} }
#endif #endif

View File

@ -16,7 +16,7 @@ namespace coreutils {
bool IMFMessage::parse(PString &in) { 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")) { while (!in.ifNext("\r\n")) {
headers.emplace_back(in); headers.emplace_back(in);
@ -31,9 +31,9 @@ namespace coreutils {
if(type == "multipart/form-data") if(type == "multipart/form-data")
body = new IMFMultipart(block, getHeaderKeyPairValue("Content-Type", "boundary")); body = new IMFMultipart(block, getHeaderKeyPairValue("Content-Type", "boundary"));
else if(type == "text/plain") else if(type == "text/plain")
body = new IMFPlainText(in); body = new IMFPlainText(block);
else else
body = new IMFBody(); body = new IMFBody(&block);
} }
return false; return false;
@ -102,4 +102,8 @@ namespace coreutils {
return body; return body;
} }
void IMFMessage::setBody(PString *body) {
this->body = new IMFBody(body);
}
} }

View File

@ -8,6 +8,14 @@
namespace coreutils { 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 { class IMFMessage {
public: public:
@ -24,6 +32,7 @@ namespace coreutils {
std::string getHeaderKeyPairValue(std::string headerKey, std::string key); std::string getHeaderKeyPairValue(std::string headerKey, std::string key);
IMFBody *getBody(); IMFBody *getBody();
void setBody(PString *in);
protected: protected:
std::vector<IMFHeader> headers; std::vector<IMFHeader> headers;

View File

@ -3,14 +3,10 @@
namespace coreutils { namespace coreutils {
IMFMultipart::IMFMultipart() { IMFMultipart::IMFMultipart(PString *in, std::string boundary) : IMFBody(in) {
buffer = ""; buffer = in->str();
} in->cursor -= 2;
sections = in->split("\r\n--" + boundary);
IMFMultipart::IMFMultipart(PString &in, std::string boundary) {
buffer = in.str();
in.cursor -= 2;
sections = in.split("\r\n--" + boundary);
for(int ix = 0; ix < sections.size(); ++ix) { for(int ix = 0; ix < sections.size(); ++ix) {
if(sections[ix].str() == "--\r\n") if(sections[ix].str() == "--\r\n")
sections[ix] = PString(""); sections[ix] = PString("");
@ -20,7 +16,7 @@ namespace coreutils {
} }
std::string IMFMultipart::toString() { std::string IMFMultipart::toString() {
return buffer; return buffer.str();
} }
int IMFMultipart::getCount() { int IMFMultipart::getCount() {
@ -30,5 +26,5 @@ namespace coreutils {
PString IMFMultipart::getSectionAt(int index) { PString IMFMultipart::getSectionAt(int index) {
return sections[index]; return sections[index];
} }
} }

View File

@ -5,26 +5,25 @@
#include "IMFBody.h" #include "IMFBody.h"
namespace coreutils { namespace coreutils {
class IMFMultipart: public IMFBody { class IMFMultipart: public IMFBody {
public: public:
IMFMultipart(); IMFMultipart(PString *in, std::string boundary);
IMFMultipart(PString &in, std::string boundary);
std::string toString(); std::string toString();
int getCount(); int getCount();
PString getSectionAt(int index); PString getSectionAt(int index);
protected: protected:
std::vector<PString> sections; std::vector<PString> sections;
private: private:
std::string boundary; std::string boundary;
std::string buffer; coreutils::PString buffer;
}; };
} }
#endif #endif

View File

@ -2,16 +2,12 @@
namespace coreutils { namespace coreutils {
IMFPlainText::IMFPlainText() { IMFPlainText::IMFPlainText(PString &in) : IMFBody(in) {
text = ""; text = in.str();
}
IMFPlainText::IMFPlainText(PString &in) : IMFBody() {
text = in.str();
} }
std::string IMFPlainText::toString() { std::string IMFPlainText::toString() {
return text; return text;
} }
} }

View File

@ -5,20 +5,20 @@
#include "IMFBody.h" #include "IMFBody.h"
namespace coreutils { namespace coreutils {
class IMFPlainText: public IMFBody { class IMFPlainText: public IMFBody {
public: public:
IMFPlainText(); IMFPlainText();
IMFPlainText(PString &in); IMFPlainText(PString &in);
std::string toString(); std::string toString();
protected: protected:
std::string text; std::string text;
}; };
} }
#endif #endif

View File

@ -2,43 +2,43 @@
#include "Log.h" #include "Log.h"
namespace coreutils { namespace coreutils {
IMFResponse::IMFResponse() {} IMFResponse::IMFResponse() {}
IMFResponse::~IMFResponse() {} IMFResponse::~IMFResponse() {}
std::string IMFResponse::getResponse(Mode mode) { std::string IMFResponse::getResponse(Mode mode) {
return getResponse("", mode); return getResponse("", mode);
} }
std::string IMFResponse::getResponse(std::string content, Mode mode) { std::string IMFResponse::getResponse(std::string content, Mode mode) {
std::stringstream response; std::stringstream response;
response << protocol << " " << code << " " << text << CRLF; response << protocol << " " << code << " " << text << CRLF;
if(mode == LENGTH) if(mode == LENGTH)
addHeader("Content-Length", std::to_string(content.size())); addHeader("Content-Length", std::to_string(content.size()));
else else
addHeader("Transfer-Encoding", "chunked"); addHeader("Transfer-Encoding", "chunked");
addHeader("Server", "JETServer v0.0.1"); addHeader("Server", "JETServer v0.0.1");
output(response); output(response);
response << content; response << content;
// core::Log(core::LOG_DEBUG_4) << response.str(); coreutils::Log(coreutils::LOG_DEBUG_4) << response.str();
return response.str(); return response.str();
} }
void IMFResponse::setProtocol(std::string protocol) { void IMFResponse::setProtocol(std::string protocol) {
this->protocol = protocol; this->protocol = protocol;
} }
void IMFResponse::setCode(std::string code) { void IMFResponse::setCode(std::string code) {
this->code = code; this->code = code;
} }
void IMFResponse::setText(std::string text) { void IMFResponse::setText(std::string text) {
this->text = text; this->text = text;
} }
void IMFResponse::setCookie(std::string name, std::string value) { void IMFResponse::setCookie(std::string name, std::string value) {
addHeader("Set-Cookie", name + "=" + value + ";"); addHeader("Set-Cookie", name + "=" + value + ";");
} }

View File

@ -5,32 +5,32 @@
#include "IMFMessage.h" #include "IMFMessage.h"
namespace coreutils { namespace coreutils {
/// ///
/// IMFResponse /// IMFResponse
/// ///
/// Use this object to build a response output for a protocol that uses headers /// Use this object to build a response output for a protocol that uses headers
/// and responses as the main communications protocol. /// and responses as the main communications protocol.
/// ///
class IMFResponse : public IMFMessage { class IMFResponse : public IMFMessage {
public: public:
enum Mode { LENGTH, STREAMING }; enum Mode { LENGTH, STREAMING };
/// ///
/// The constructor for the object. /// The constructor for the object.
/// ///
IMFResponse(); IMFResponse();
/// ///
/// The destructor for the object. /// The destructor for the object.
/// ///
~IMFResponse(); ~IMFResponse();
/// ///
/// Returns the response generated from the contained values that /// Returns the response generated from the contained values that
/// do not return a content length. Using this constructor ensures /// 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. /// @return the complete response string to send to client.
/// ///
std::string getResponse(Mode mode = LENGTH); std::string getResponse(Mode mode = LENGTH);
/// ///
/// Returns the response plus the content passed as a parameter. /// 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. /// @return the complete response string to send to client.
/// ///
std::string getResponse(std::string content, Mode mode = LENGTH); 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. /// 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); void setProtocol(std::string protocol);
/// ///
/// Sets the return code value for the response. /// Sets the return code value for the response.
/// ///
/// @param code the response code value to return in the response. /// @param code the response code value to return in the response.
/// ///
void setCode(std::string code); void setCode(std::string code);
/// ///
/// Sets the return code string value for the response. /// 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. /// the response.
/// ///
void setText(std::string text); void setText(std::string text);
void setCookie(std::string name, std::string value); void setCookie(std::string name, std::string value);
private: private:
std::string protocol; std::string protocol;
std::string code; std::string code;
std::string text; std::string text;
std::string CRLF = "\r\n"; std::string CRLF = "\r\n";
}; };
} }
#endif #endif

View File

@ -98,7 +98,11 @@ namespace coreutils {
} }
std::string PString::readBlock(size_t size) { 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() { int PString::size() {

View File

@ -43,7 +43,7 @@ namespace coreutils {
std::string str(); 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. /// specified length.
/// ///
@ -102,11 +102,11 @@ namespace coreutils {
/// ///
std::string readBlock(size_t size); std::string readBlock(size_t size);
/// ///
/// ///
/// ///
int cursor = 0; int cursor = 0;
/// ///

45
ZString.h Normal file
View File

@ -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