diff --git a/IMFFormData.cpp b/IMFFormData.cpp index eed1231..6115b3d 100644 --- a/IMFFormData.cpp +++ b/IMFFormData.cpp @@ -1,9 +1,9 @@ #include "IMFFormData.h" #include "IMFMessage.h" -#include "IMFPlainText.h" +#include "IMFText.h" namespace coreutils { - + IMFFormData::IMFFormData(int fd, ZString boundary) { IMFHeader header(fd); IMFText text(fd, boundary); diff --git a/IMFHeader.cpp b/IMFHeader.cpp index 71daaec..4edcb2e 100644 --- a/IMFHeader.cpp +++ b/IMFHeader.cpp @@ -1,15 +1,18 @@ #include "IMFHeader.h" #include "Exception.h" #include "Log.h" -#include +#include namespace coreutils { IMFHeader::IMFHeader(int fd) { - while(1) { - headers.emplace(fd); + char ch; + int rc = ::read(fd, &ch, 1); + while(ch != 'r') { + IMFHeaderField *headerField = new IMFHeaderField(ch, fd); + headers.add(headerField); } - + rc = ::read(fd, &ch, 1); } } \ No newline at end of file diff --git a/IMFHeader.h b/IMFHeader.h index 505ac70..bcfb173 100644 --- a/IMFHeader.h +++ b/IMFHeader.h @@ -1,15 +1,16 @@ #ifndef __IMFHeader_h__ -#define __IMFHeader_h__ +# define __IMFHeader_h__ -#include +# include "IMFHeaderField.h" +# include namespace coreutils { - + class IMFHeader { - + public: IMFHeader(int fd); - + private: std::vector headers; diff --git a/IMFHeaderField.cpp b/IMFHeaderField.cpp new file mode 100644 index 0000000..7b607a8 --- /dev/null +++ b/IMFHeaderField.cpp @@ -0,0 +1,32 @@ +#include "IMFHeaderField.h" +#include "Exception.h" +#include "Log.h" +#include +#include + +namespace coreutils { + + IMFHeaderField::IMFHeaderField(char &ch, int fd) { + int rc = 0; + + if((ch >= 'A') && (ch <= 'Z')) + key.write(); + + while(ch != ':') { + rc = ::read(fd, &ch, 1); + if(ch != ':') + key.write(ch); + } + rc = ::read(fd, &ch, 1); + while(ch == ' ') + rc = ::read(fd, &ch, 1); + + while(ch != '\r') { + rc = ::read(fd, &ch, 1); + if(ch != '\r') + value.write(ch); + } + rc = ::read(fd, &ch, 1); + } + +} \ No newline at end of file diff --git a/IMFHeaderField.h b/IMFHeaderField.h new file mode 100644 index 0000000..7535d6f --- /dev/null +++ b/IMFHeaderField.h @@ -0,0 +1,21 @@ +#ifndef __IMFHeaderField_h__ +# define __IMFHeaderField_h__ + +# include "MString.h" + +namespace coreutils { + + class IMFHeaderField { + + public: + IMFHeaderField(char &ch, int fd); + + private: + MString key; + MString value; + + }; + +} + +#endif diff --git a/IMFMessage.cpp b/IMFMessage.cpp index fd3f0d7..dd4cea1 100644 --- a/IMFMessage.cpp +++ b/IMFMessage.cpp @@ -1,87 +1,11 @@ #include "IMFMessage.h" #include "IMFHeader.h" #include "IMFMultipart.h" -#include "IMFPlainText.h" +#include "IMFText.h" #include "Log.h" namespace coreutils { - IMFMessage::IMFMessage() : ZString() {} - - IMFMessage::IMFMessage(ZString &in) : ZString(in) { - - while (!in.ifNext("\r\n") && in.ifNext("\n")) - headers.emplace_back(in); - - ZString type = getHeader("Content-Type"); - - int len = getHeader("Content-Length").asInteger(); - if(len > 0) { - block = in.readBlock(len); - if(type.equals("multipart/form-data")) { - ZString boundary(getHeaderKeyPairValue("Content-Type", "boundary")); - multipart = IMFMultipart(block, boundary); - } - } - } - - IMFMessage::~IMFMessage() {} - - void IMFMessage::output(std::stringstream &out) { -// for(IMFHeader header: headers) { -// out << header.getKey() << ": " << header.getValue() << "\r\n"; -// } - out << newHeaders.str() << "\r\n"; - } - - void IMFMessage::addHeader(IMFHeader header) { - std::stringstream out; - header.output(out); - newHeaders << out.str() ; - } - - ZString IMFMessage::getHeader(ZString key, bool valueOnly) { - for(IMFHeader header: headers) { - if(header.getKey().equals(key)) { - ZString value = header.getValue(); - if(valueOnly) { - std::vector split = ZString(value).split(";"); - value = split[0]; - } - return value; - } - } - return ZString(); - } - - ZString IMFMessage::getHeaderKeyPairValue(const char *headerKey, const char *key) { - return getHeaderKeyPairValue(ZString(headerKey), ZString(key)); - } - - ZString IMFMessage::getHeaderKeyPairValue(ZString headerKey, ZString key) { - ZString value; - ZString psource(getHeader(headerKey, false)); - for(ZString work: psource.split(";")) { - work.skipWhitespace(); - ZString token = work.getTokenExclude("="); - if(work.ifNext("=")) { - if(token.equals(key)) { - if(work.ifNext("\"")) - value = work.getTokenExclude("\""); - else - value = work; - return value; - } - else - continue; - } else if (work.ifNext(";") || work.ifNext(" ")) { - if(token.equals(key)) - return ZString("true"); - else - continue; - } - } - return ZString(); - } + IMFMessage::IMFMessage(int fd) {} } diff --git a/IMFMessage.h b/IMFMessage.h index e7a7c95..e757337 100644 --- a/IMFMessage.h +++ b/IMFMessage.h @@ -15,28 +15,21 @@ namespace coreutils { /// /// - class IMFMessage : public ZString { + class IMFMessage { public: - IMFMessage(); - IMFMessage(ZString &in); + IMFMessage(int fd); virtual ~IMFMessage(); - void output(std::stringstream &out); +// void output(std::stringstream &out); - void addHeader(IMFHeader header); - - ZString getHeader(ZString key, bool valueOnly = true); - ZString getHeaderKeyPairValue(const char *headerKey, const char *key); - ZString getHeaderKeyPairValue(ZString headerKey, ZString key); - - IMFMultipart multipart; - ZString block; - char contentLength[8]; +// IMFMultipart multipart; +// ZString block; +// char contentLength[8]; protected: - std::vector headers; - std::stringstream newHeaders; +// std::vector headers; +// std::stringstream newHeaders; }; diff --git a/IMFMultipart.cpp b/IMFMultipart.cpp index 61a7ec2..ffa261d 100644 --- a/IMFMultipart.cpp +++ b/IMFMultipart.cpp @@ -3,7 +3,7 @@ namespace coreutils { - IMFMultipart::IMFMultipart(int fd, int length) { + IMFMultipart::IMFMultipart(int fd, ZString boundary) { int ch = 0; int rc = 0; while((ch != '\r') && (ch != '\n')) { diff --git a/IMFMultipart.h b/IMFMultipart.h index 92d642f..27fa2b6 100644 --- a/IMFMultipart.h +++ b/IMFMultipart.h @@ -2,6 +2,7 @@ #define __IMFMultipart_h__ #include "MString.h" +#include "IMFFormData.h" #include namespace coreutils { @@ -9,14 +10,7 @@ namespace coreutils { class IMFMultipart { public: - IMFMultipart(int fd, int length); - -// std::string toString(); -// int getCount(); -// ZString getSectionAt(int index); - -// protected: -// std::vector sections; + IMFMultipart(int fd, ZString boundary); private: MString boundary; diff --git a/IMFPlainText.cpp b/IMFPlainText.cpp deleted file mode 100644 index ef445b2..0000000 --- a/IMFPlainText.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "IMFPlainText.h" - -namespace coreutils { - - IMFPlainText::IMFPlainText() { - } - - IMFPlainText::IMFPlainText(ZString &in) : ZString(in.str().c_str()) {} - -} diff --git a/IMFPlainText.h b/IMFPlainText.h deleted file mode 100644 index 2e504dc..0000000 --- a/IMFPlainText.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __IMFPlainText_h__ -#define __IMFPlainText_h__ - -#include "ZString.h" - -namespace coreutils { - - class IMFPlainText: public ZString { - - public: - IMFPlainText(); - IMFPlainText(ZString &in); - - }; - -} - -#endif diff --git a/IMFResponse.h b/IMFResponse.h index aa05448..000ef56 100644 --- a/IMFResponse.h +++ b/IMFResponse.h @@ -14,7 +14,7 @@ namespace coreutils { /// and responses as the main communications protocol. /// - class IMFResponse : public IMFMessage { + class IMFResponse { public: diff --git a/IMFText.cpp b/IMFText.cpp new file mode 100644 index 0000000..9b0751f --- /dev/null +++ b/IMFText.cpp @@ -0,0 +1,9 @@ +#include "IMFText.h" + +namespace coreutils { + + IMFText::IMFText(int fd, ZString boundary) : MString() { + + } + +} diff --git a/IMFText.h b/IMFText.h new file mode 100644 index 0000000..3d68f3d --- /dev/null +++ b/IMFText.h @@ -0,0 +1,18 @@ + +#ifndef __IMFText_h__ +#define __IMFText_h__ + +#include "MString.h" + +namespace coreutils { + + class IMFText : public MString { + + public: + IMFText(int fd, ZString boundary); + + }; + +} + +#endif