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__
#include "includes"
#include "PString.h"
///
/// File

View File

@ -3,7 +3,19 @@
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 {
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 "";
}
}

View File

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

View File

@ -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);
}
}

View File

@ -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<IMFHeader> headers;

View File

@ -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];
}
}

View File

@ -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<PString> sections;
private:
std::string boundary;
std::string buffer;
coreutils::PString buffer;
};
}
#endif

View File

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

View File

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

View File

@ -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 + ";");
}

View File

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

View File

@ -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() {

View File

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

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