commit 604e196fbb6260a85e0d8a468e01b2fefc8f8720 Author: Brad Arant Date: Wed Jul 10 14:19:33 2019 -0700 Initial repository load. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d25cc99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +Debug +Release +*.o +*~ +*.mk +libCoreUtils.a diff --git a/CoreUtils.mk b/CoreUtils.mk new file mode 100644 index 0000000..5a9627d --- /dev/null +++ b/CoreUtils.mk @@ -0,0 +1,152 @@ +## +## Auto Generated makefile by CodeLite IDE +## any manual changes will be erased +## +## Debug +ProjectName :=CoreUtils +ConfigurationName :=Debug +WorkspacePath :=/home/bradarant/barant +ProjectPath :=/home/bradarant/barant/CoreUtils +IntermediateDirectory :=./Debug +OutDir := $(IntermediateDirectory) +CurrentFileName := +CurrentFilePath := +CurrentFileFullPath := +User :=Brad Arant +Date :=10/07/19 +CodeLitePath :=/home/bradarant/.codelite +LinkerName :=/usr/bin/x86_64-linux-gnu-g++ +SharedObjectLinkerName :=/usr/bin/x86_64-linux-gnu-g++ -shared -fPIC +ObjectSuffix :=.o +DependSuffix :=.o.d +PreprocessSuffix :=.i +DebugSwitch :=-g +IncludeSwitch :=-I +LibrarySwitch :=-l +OutputSwitch :=-o +LibraryPathSwitch :=-L +PreprocessorSwitch :=-D +SourceSwitch :=-c +OutputFile :=$(IntermediateDirectory)/lib$(ProjectName).a +Preprocessors := +ObjectSwitch :=-o +ArchiveOutputSwitch := +PreprocessOnlySwitch :=-E +ObjectsFileList :="CoreUtils.txt" +PCHCompileFlags := +MakeDirCommand :=mkdir -p +LinkOptions := +IncludePath := $(IncludeSwitch). $(IncludeSwitch). $(IncludeSwitch)../ServerCore +IncludePCH := +RcIncludePath := +Libs := +ArLibs := +LibPath := $(LibraryPathSwitch). + +## +## Common variables +## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables +## +AR := /usr/bin/x86_64-linux-gnu-ar rcu +CXX := /usr/bin/x86_64-linux-gnu-g++ +CC := /usr/bin/x86_64-linux-gnu-gcc +CXXFLAGS := -g $(Preprocessors) +CFLAGS := -g $(Preprocessors) +ASFLAGS := +AS := /usr/bin/x86_64-linux-gnu-as + + +## +## User defined environment variables +## +CodeLiteDir:=/usr/share/codelite +Objects0=$(IntermediateDirectory)/IMFHeader.cpp$(ObjectSuffix) $(IntermediateDirectory)/PString.cpp$(ObjectSuffix) $(IntermediateDirectory)/IMFMultipart.cpp$(ObjectSuffix) $(IntermediateDirectory)/IMFMessage.cpp$(ObjectSuffix) $(IntermediateDirectory)/IMFRequest.cpp$(ObjectSuffix) $(IntermediateDirectory)/IMFResponse.cpp$(ObjectSuffix) + + + +Objects=$(Objects0) + +## +## Main Build Targets +## +.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs +all: $(IntermediateDirectory) $(OutputFile) + +$(OutputFile): $(Objects) + @$(MakeDirCommand) $(@D) + @echo "" > $(IntermediateDirectory)/.d + @echo $(Objects0) > $(ObjectsFileList) + $(AR) $(ArchiveOutputSwitch)$(OutputFile) @$(ObjectsFileList) $(ArLibs) + @$(MakeDirCommand) "/home/bradarant/barant/.build-debug" + @echo rebuilt > "/home/bradarant/barant/.build-debug/CoreUtils" + +MakeIntermediateDirs: + @test -d ./Debug || $(MakeDirCommand) ./Debug + + +./Debug: + @test -d ./Debug || $(MakeDirCommand) ./Debug + +PreBuild: + + +## +## Objects +## +$(IntermediateDirectory)/IMFHeader.cpp$(ObjectSuffix): IMFHeader.cpp $(IntermediateDirectory)/IMFHeader.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/IMFHeader.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IMFHeader.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IMFHeader.cpp$(DependSuffix): IMFHeader.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IMFHeader.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IMFHeader.cpp$(DependSuffix) -MM IMFHeader.cpp + +$(IntermediateDirectory)/IMFHeader.cpp$(PreprocessSuffix): IMFHeader.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IMFHeader.cpp$(PreprocessSuffix) IMFHeader.cpp + +$(IntermediateDirectory)/PString.cpp$(ObjectSuffix): PString.cpp $(IntermediateDirectory)/PString.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/PString.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/PString.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/PString.cpp$(DependSuffix): PString.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/PString.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/PString.cpp$(DependSuffix) -MM PString.cpp + +$(IntermediateDirectory)/PString.cpp$(PreprocessSuffix): PString.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/PString.cpp$(PreprocessSuffix) PString.cpp + +$(IntermediateDirectory)/IMFMultipart.cpp$(ObjectSuffix): IMFMultipart.cpp $(IntermediateDirectory)/IMFMultipart.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/IMFMultipart.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IMFMultipart.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IMFMultipart.cpp$(DependSuffix): IMFMultipart.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IMFMultipart.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IMFMultipart.cpp$(DependSuffix) -MM IMFMultipart.cpp + +$(IntermediateDirectory)/IMFMultipart.cpp$(PreprocessSuffix): IMFMultipart.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IMFMultipart.cpp$(PreprocessSuffix) IMFMultipart.cpp + +$(IntermediateDirectory)/IMFMessage.cpp$(ObjectSuffix): IMFMessage.cpp $(IntermediateDirectory)/IMFMessage.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/IMFMessage.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IMFMessage.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IMFMessage.cpp$(DependSuffix): IMFMessage.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IMFMessage.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IMFMessage.cpp$(DependSuffix) -MM IMFMessage.cpp + +$(IntermediateDirectory)/IMFMessage.cpp$(PreprocessSuffix): IMFMessage.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IMFMessage.cpp$(PreprocessSuffix) IMFMessage.cpp + +$(IntermediateDirectory)/IMFRequest.cpp$(ObjectSuffix): IMFRequest.cpp $(IntermediateDirectory)/IMFRequest.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/IMFRequest.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IMFRequest.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IMFRequest.cpp$(DependSuffix): IMFRequest.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IMFRequest.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IMFRequest.cpp$(DependSuffix) -MM IMFRequest.cpp + +$(IntermediateDirectory)/IMFRequest.cpp$(PreprocessSuffix): IMFRequest.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IMFRequest.cpp$(PreprocessSuffix) IMFRequest.cpp + +$(IntermediateDirectory)/IMFResponse.cpp$(ObjectSuffix): IMFResponse.cpp $(IntermediateDirectory)/IMFResponse.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/CoreUtils/IMFResponse.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IMFResponse.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IMFResponse.cpp$(DependSuffix): IMFResponse.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IMFResponse.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IMFResponse.cpp$(DependSuffix) -MM IMFResponse.cpp + +$(IntermediateDirectory)/IMFResponse.cpp$(PreprocessSuffix): IMFResponse.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IMFResponse.cpp$(PreprocessSuffix) IMFResponse.cpp + + +-include $(IntermediateDirectory)/*$(DependSuffix) +## +## Clean +## +clean: + $(RM) -r ./Debug/ + + diff --git a/CoreUtils.project b/CoreUtils.project new file mode 100644 index 0000000..8a6855c --- /dev/null +++ b/CoreUtils.project @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CoreUtils.txt b/CoreUtils.txt new file mode 100644 index 0000000..935d9a4 --- /dev/null +++ b/CoreUtils.txt @@ -0,0 +1 @@ +./Debug/IMFHeader.cpp.o ./Debug/PString.cpp.o ./Debug/IMFMultipart.cpp.o ./Debug/IMFMessage.cpp.o ./Debug/IMFRequest.cpp.o ./Debug/IMFResponse.cpp.o diff --git a/Debug/.d b/Debug/.d new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Debug/.d @@ -0,0 +1 @@ + diff --git a/Debug/IMFHeader.cpp.o b/Debug/IMFHeader.cpp.o new file mode 100644 index 0000000..2261bab Binary files /dev/null and b/Debug/IMFHeader.cpp.o differ diff --git a/Debug/IMFHeader.cpp.o.d b/Debug/IMFHeader.cpp.o.d new file mode 100644 index 0000000..8520979 --- /dev/null +++ b/Debug/IMFHeader.cpp.o.d @@ -0,0 +1,12 @@ +Debug/IMFHeader.cpp.o: IMFHeader.cpp IMFHeader.h PString.h includes \ + ../ServerCore/Exception.h ../ServerCore/includes + +IMFHeader.h: + +PString.h: + +includes: + +../ServerCore/Exception.h: + +../ServerCore/includes: diff --git a/Debug/IMFMessage.cpp.o b/Debug/IMFMessage.cpp.o new file mode 100644 index 0000000..7d29cbb Binary files /dev/null and b/Debug/IMFMessage.cpp.o differ diff --git a/Debug/IMFMessage.cpp.o.d b/Debug/IMFMessage.cpp.o.d new file mode 100644 index 0000000..193c8cf --- /dev/null +++ b/Debug/IMFMessage.cpp.o.d @@ -0,0 +1,16 @@ +Debug/IMFMessage.cpp.o: IMFMessage.cpp IMFMessage.h PString.h includes \ + IMFHeader.h IMFRequest.h IMFBody.h IMFMultipart.h + +IMFMessage.h: + +PString.h: + +includes: + +IMFHeader.h: + +IMFRequest.h: + +IMFBody.h: + +IMFMultipart.h: diff --git a/Debug/IMFMultipart.cpp.o b/Debug/IMFMultipart.cpp.o new file mode 100644 index 0000000..4bd58f4 Binary files /dev/null and b/Debug/IMFMultipart.cpp.o differ diff --git a/Debug/IMFMultipart.cpp.o.d b/Debug/IMFMultipart.cpp.o.d new file mode 100644 index 0000000..0c7897a --- /dev/null +++ b/Debug/IMFMultipart.cpp.o.d @@ -0,0 +1,10 @@ +Debug/IMFMultipart.cpp.o: IMFMultipart.cpp IMFMultipart.h PString.h \ + includes IMFBody.h + +IMFMultipart.h: + +PString.h: + +includes: + +IMFBody.h: diff --git a/Debug/IMFRequest.cpp.o b/Debug/IMFRequest.cpp.o new file mode 100644 index 0000000..393c9ba Binary files /dev/null and b/Debug/IMFRequest.cpp.o differ diff --git a/Debug/IMFRequest.cpp.o.d b/Debug/IMFRequest.cpp.o.d new file mode 100644 index 0000000..8e54db4 --- /dev/null +++ b/Debug/IMFRequest.cpp.o.d @@ -0,0 +1,12 @@ +Debug/IMFRequest.cpp.o: IMFRequest.cpp IMFRequest.h PString.h includes \ + ../ServerCore/Exception.h ../ServerCore/includes + +IMFRequest.h: + +PString.h: + +includes: + +../ServerCore/Exception.h: + +../ServerCore/includes: diff --git a/Debug/IMFResponse.cpp.o b/Debug/IMFResponse.cpp.o new file mode 100644 index 0000000..323bc57 Binary files /dev/null and b/Debug/IMFResponse.cpp.o differ diff --git a/Debug/IMFResponse.cpp.o.d b/Debug/IMFResponse.cpp.o.d new file mode 100644 index 0000000..5f1eb8a --- /dev/null +++ b/Debug/IMFResponse.cpp.o.d @@ -0,0 +1,26 @@ +Debug/IMFResponse.cpp.o: IMFResponse.cpp IMFResponse.h includes \ + IMFMessage.h PString.h IMFHeader.h IMFRequest.h IMFBody.h \ + ../ServerCore/Log.h ../ServerCore/includes ../ServerCore/File.h \ + ../ServerCore/Object.h + +IMFResponse.h: + +includes: + +IMFMessage.h: + +PString.h: + +IMFHeader.h: + +IMFRequest.h: + +IMFBody.h: + +../ServerCore/Log.h: + +../ServerCore/includes: + +../ServerCore/File.h: + +../ServerCore/Object.h: diff --git a/Debug/PString.cpp.o b/Debug/PString.cpp.o new file mode 100644 index 0000000..02cbf5a Binary files /dev/null and b/Debug/PString.cpp.o differ diff --git a/Debug/PString.cpp.o.d b/Debug/PString.cpp.o.d new file mode 100644 index 0000000..c6161c8 --- /dev/null +++ b/Debug/PString.cpp.o.d @@ -0,0 +1,14 @@ +Debug/PString.cpp.o: PString.cpp PString.h includes ../ServerCore/Log.h \ + ../ServerCore/includes ../ServerCore/File.h ../ServerCore/Object.h + +PString.h: + +includes: + +../ServerCore/Log.h: + +../ServerCore/includes: + +../ServerCore/File.h: + +../ServerCore/Object.h: diff --git a/Debug/libCoreUtils.a b/Debug/libCoreUtils.a new file mode 100644 index 0000000..45cf6d0 Binary files /dev/null and b/Debug/libCoreUtils.a differ diff --git a/IMFBody.h b/IMFBody.h new file mode 100644 index 0000000..3d7a218 --- /dev/null +++ b/IMFBody.h @@ -0,0 +1,10 @@ +#ifndef __IMFBody_h__ +#define __IMFBody_h__ + +namespace coreutils { + + class IMFBody {}; + +} + +#endif diff --git a/IMFHeader.cpp b/IMFHeader.cpp new file mode 100644 index 0000000..bc2be1e --- /dev/null +++ b/IMFHeader.cpp @@ -0,0 +1,45 @@ +#include "IMFHeader.h" +#include "Exception.h" + +namespace coreutils { + + IMFHeader::IMFHeader() { + + } + + IMFHeader::IMFHeader(PString &in) { + key = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"); + if(!in.ifNext(":")) + throw core::Exception("Invalid character in expected header token."); + in.skipWhitespace(); + value = in.getTokenExclude("\r\n"); + if(!in.ifNext("\r\n")) + throw core::Exception("Expected CRLF."); + } + + IMFHeader::IMFHeader(std::string key, std::string value) { + this->key = key; + this->value = value; + } + + std::string IMFHeader::getKey() { + return key; + } + + void IMFHeader::setKey(std::string key) { + this->key = key; + } + + std::string IMFHeader::getValue() { + return value; + } + + void IMFHeader::setValue(std::string value) { + this->value = value; + } + + void IMFHeader::output(std::stringstream &out) { + out << key << ": " << value << "\r\n"; + } + +} \ No newline at end of file diff --git a/IMFHeader.h b/IMFHeader.h new file mode 100644 index 0000000..b1da8bc --- /dev/null +++ b/IMFHeader.h @@ -0,0 +1,30 @@ +#ifndef __IMFHeader_h__ +#define __IMFHeader_h__ + +#include "PString.h" + +namespace coreutils { + + class IMFHeader { + + public: + IMFHeader(); + IMFHeader(PString &in); + IMFHeader(std::string key, std::string value); + + std::string getKey(); + void setKey(std::string name); + std::string getValue(); + void setValue(std::string value); + + void output(std::stringstream &out); + + private: + std::string key; + std::string value; + + }; + +} + +#endif diff --git a/IMFMessage.cpp b/IMFMessage.cpp new file mode 100644 index 0000000..3f91943 --- /dev/null +++ b/IMFMessage.cpp @@ -0,0 +1,75 @@ +#include "IMFMessage.h" +#include "IMFHeader.h" +#include "IMFMultipart.h" + +namespace coreutils { + + IMFMessage::IMFMessage() {} + + IMFMessage::IMFMessage(PString &in) { + parse(in); + } + + void IMFMessage::parse(PString &in) { + while(!in.ifNext("\r\n")) + headers.emplace_back(in); + + std::string type = getHeader("Content-Type"); + + if(type == "multipart/form-data") + body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary")); + else + body = new IMFBody(); + } + + void IMFMessage::output(std::stringstream &out) { + for(IMFHeader header: headers) { + out << header.getKey() << ": " << header.getValue() << "\r\n"; + } + out << "\r\n"; + } + + void IMFMessage::addHeader(std::string key, std::string value) { + headers.emplace_back(key, value); + + } + + std::string IMFMessage::getHeader(std::string key, bool valueOnly) { + for(IMFHeader header: headers) { + if(header.getKey() == key) { + std::string value = header.getValue(); + if(valueOnly) { + std::vector split = PString(value).split(";"); + value = split[0]; + } + return value; + } + } + return std::string(""); + } + + std::string IMFMessage::getHeaderKeyPairValue(std::string headerKey, std::string key) { + coreutils::PString psource(getHeader(headerKey, false)); + std::vector sourcep = psource.split(";"); + for(std::string keyx: sourcep) { + PString work(keyx); + work.skipWhitespace(); + std::string token = work.getTokenExclude("="); + if(work.ifNext("=")) { + if(token == key) + return work.str(); + else + continue; + + } else if (work.ifNext(";") || work.ifNext(" ")) { + if(token == key) + return std::string("true"); + else + continue; + + } + } + return std::string("false"); + } + +} diff --git a/IMFMessage.h b/IMFMessage.h new file mode 100644 index 0000000..0b52c6d --- /dev/null +++ b/IMFMessage.h @@ -0,0 +1,36 @@ +#ifndef __IMFMessage_h__ +#define __IMFMessage_h__ + +#include "PString.h" +#include "IMFHeader.h" +#include "IMFRequest.h" +#include "IMFBody.h" + +namespace coreutils { + + class IMFMessage { + + public: + IMFMessage(); + IMFMessage(PString &in); + + void parse(PString &in); + + void output(std::stringstream &out); + IMFRequest *request; + + void addHeader(std::string key, std::string value); + + std::string getHeader(std::string key, bool valueOnly = true); + std::string getHeaderKeyPairValue(std::string headerKey, std::string key); + + IMFBody *body; + + protected: + std::vector headers; + + }; + +} + +#endif diff --git a/IMFMultipart.cpp b/IMFMultipart.cpp new file mode 100644 index 0000000..a7253ef --- /dev/null +++ b/IMFMultipart.cpp @@ -0,0 +1,15 @@ +#include "IMFMultipart.h" + +namespace coreutils { + + IMFMultipart::IMFMultipart() { + + } + + IMFMultipart::IMFMultipart(PString &in, std::string boundary) { + in.cursor -= 2; + section = in.split("\r\n--" + boundary); + + } + +} diff --git a/IMFMultipart.h b/IMFMultipart.h new file mode 100644 index 0000000..dfc7a08 --- /dev/null +++ b/IMFMultipart.h @@ -0,0 +1,23 @@ +#ifndef __IMFMultipart_h__ +#define __IMFMultipart_h__ + +#include "PString.h" +#include "IMFBody.h" + +namespace coreutils { + + class IMFMultipart: public IMFBody { + + public: + IMFMultipart(); + IMFMultipart(PString &in, std::string boundary); + + private: + std::string boundary; + std::vector section; + + }; + +} + +#endif diff --git a/IMFRequest.cpp b/IMFRequest.cpp new file mode 100644 index 0000000..7336ec3 --- /dev/null +++ b/IMFRequest.cpp @@ -0,0 +1,48 @@ +#include "IMFRequest.h" +#include "Exception.h" + +namespace coreutils { + + IMFRequest::IMFRequest() {} + + IMFRequest::IMFRequest(PString &in) { + method = in.getTokenExclude(" "); + if(!in.ifNext(" ")) + throw core::Exception("Expecting space after method."); + uri = in.getTokenExclude(" "); + if(!in.ifNext(" ")) + throw core::Exception("Expecting space after URI."); + protocol = in.getTokenExclude("\r\n"); + if(!in.ifNext("\r\n")) + throw core::Exception("Expecting CRLF after protocol."); + } + + std::string IMFRequest::getMethod() { + return method; + } + + void IMFRequest::setMethod(std::string method) { + this->method = method; + } + + std::string IMFRequest::getURI() { + return uri; + } + + void IMFRequest::setURI(std::string uri) { + this->uri = uri; + } + + std::string IMFRequest::getProtocol() { + return protocol; + } + + void IMFRequest::setProtocol(std::string protocol) { + this->protocol = protocol; + } + + void IMFRequest::output(std::stringstream &out) { + out << method << " " << uri << " " << protocol << "\r\n"; + } + +} diff --git a/IMFRequest.h b/IMFRequest.h new file mode 100644 index 0000000..74d724f --- /dev/null +++ b/IMFRequest.h @@ -0,0 +1,32 @@ +#ifndef __IMFRequest_h__ +#define __IMFRequest_h__ + +#include "PString.h" + +namespace coreutils { + + class IMFRequest { + + public: + IMFRequest(); + IMFRequest(PString &in); + + std::string getMethod(); + void setMethod(std::string method); + std::string getURI(); + void setURI(std::string uri); + std::string getProtocol(); + void setProtocol(std::string protocol); + + void output(std::stringstream &out); + + private: + std::string method; + std::string uri; + std::string protocol; + + }; + +} + +#endif diff --git a/IMFResponse.cpp b/IMFResponse.cpp new file mode 100644 index 0000000..0e9d2fd --- /dev/null +++ b/IMFResponse.cpp @@ -0,0 +1,44 @@ +#include "IMFResponse.h" +#include "Log.h" + +namespace coreutils { + + 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"); + + output(response); + response << content; +// core::Log(core::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 new file mode 100644 index 0000000..8ffd5fe --- /dev/null +++ b/IMFResponse.h @@ -0,0 +1,97 @@ +#ifndef __IMFResponse_h__ +#define __IMFResponse_h__ + +#include "includes" +#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(); + + /// + /// Returns the response generated from the contained values that + /// do not return a content length. Using this constructor ensures + /// a zero length Content-Length value. + /// + /// @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. + /// + /// This method will automatically generate the proper Content-Length + /// value for the response. + /// + /// @param content the content that will be provided on the response + /// message to the client. + /// + /// @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 + /// should match the header received. + /// + /// @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 + /// 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 new file mode 100644 index 0000000..a2db255 --- /dev/null +++ b/PString.cpp @@ -0,0 +1,64 @@ +#include "PString.h" +#include "Log.h" + +namespace coreutils { + + PString::PString() { + } + + PString::PString(std::string pstring) { + this->pstring = pstring; + } + + std::string PString::str() { + return pstring.substr(cursor); + } + + std::vector PString::split(std::string delimiter, int maxSize) { + std::vector list; + size_t end, + pos = cursor; + while(pos < pstring.length()) { + end = pstring.find(delimiter, pos); + if(end == std::string::npos) + end = pstring.length(); + list.push_back(pstring.substr(pos, end - pos)); + pos = end + delimiter.size(); + if(pos > pstring.size()) + break; + if(maxSize != 0) { + if(list.size() >= maxSize) { + list.push_back(pstring.substr(pos)); + break; + } + } + } + return list; + } + + std::string PString::getTokenInclude(std::string include) { + int start = cursor; + cursor = pstring.find_first_not_of(include, cursor); + return pstring.substr(start, cursor - start); + } + + std::string PString::getTokenExclude(std::string exclude) { + int start = cursor; + cursor = pstring.find_first_of(exclude, cursor); + if(cursor == -1) + cursor = pstring.size(); + return pstring.substr(start, cursor - start); + } + + bool PString::ifNext(std::string value) { + bool test = (value == pstring.substr(cursor, value.length())); + if(test) + cursor += value.size(); + return test; + } + + void PString::skipWhitespace() { + cursor = pstring.find_first_not_of(" \t", cursor); + } + +} \ No newline at end of file diff --git a/PString.h b/PString.h new file mode 100644 index 0000000..5f685c0 --- /dev/null +++ b/PString.h @@ -0,0 +1,29 @@ +#ifndef __SString_h__ +#define __SString_h__ + +#include "includes" + +namespace coreutils { + + class PString { + + public: + PString(); + PString(std::string pstring); + + std::string str(); + std::vector split(std::string delimiter, int maxSize = 0); + std::string getTokenInclude(std::string include); + std::string getTokenExclude(std::string exclude); + bool ifNext(std::string value); + void skipWhitespace(); + int cursor = 0; + + private: + std::string pstring; + + }; + +} + +#endif diff --git a/includes b/includes new file mode 100644 index 0000000..1ddcaf7 --- /dev/null +++ b/includes @@ -0,0 +1,36 @@ +#define _GLIBCXX_USE_CXX11_ABI 0 + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + \ No newline at end of file