diff --git a/IMFMessage.cpp b/IMFMessage.cpp index 8675800..8059330 100644 --- a/IMFMessage.cpp +++ b/IMFMessage.cpp @@ -57,6 +57,10 @@ namespace coreutils { 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)); diff --git a/IMFMessage.h b/IMFMessage.h index f3cb193..793d8f3 100644 --- a/IMFMessage.h +++ b/IMFMessage.h @@ -19,6 +19,7 @@ namespace coreutils { void addHeader(const char *key, ZString value); ZString getHeader(ZString key, bool valueOnly = true); + ZString getHeaderKeyPairValue(const char *headerKey, const char *key); ZString getHeaderKeyPairValue(ZString headerKey, ZString key); IMFBody * getBody(); diff --git a/IMFResponse.cpp b/IMFResponse.cpp index 7187f9e..6430fe6 100644 --- a/IMFResponse.cpp +++ b/IMFResponse.cpp @@ -2,46 +2,50 @@ #include "Log.h" namespace coreutils { - + IMFResponse::IMFResponse() {} - IMFResponse::~IMFResponse() {} - + IMFResponse::~IMFResponse() {} + std::stringstream IMFResponse::getResponse(Mode mode) { std::stringstream stream(""); return getResponse(stream, mode); } - + std::stringstream IMFResponse::getResponse(std::stringstream &content, Mode mode) { std::stringstream response; response << protocol << " " << code << " " << text << CRLF; - + if(mode == LENGTH) addHeader((char *)"Content-Length", (char *)std::to_string(content.gcount()).c_str()); else addHeader((char *)"Transfer-Encoding", (char *)"chunked"); - + addHeader((char *)"Server", (char *)"JETServer v0.0.1"); - output(response); - response << content.str(); + output(response); + response << content.str(); // core::Log(core::LOG_DEBUG_4) << response.str(); - return response; + return response; } - + void IMFResponse::setProtocol(ZString protocol) { this->protocol = protocol; } - + void IMFResponse::setCode(ZString code) { this->code = code; } - + void IMFResponse::setText(ZString text) { this->text = text; } - + void IMFResponse::setCookie(ZString key, ZString data) { addHeader("Set-Cookie", data); } + void IMFResponse::setCookie(ZString key, std::string data) { + addHeader("Set-Cookie", ZString(data.c_str())); + } + } diff --git a/IMFResponse.h b/IMFResponse.h index 399e232..c5f0b0e 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::stringstream getResponse(Mode mode = LENGTH); - + /// /// Returns the response plus the content passed as a parameter. /// @@ -52,46 +52,47 @@ namespace coreutils { /// /// @return the complete response string to send to client. /// - + std::stringstream getResponse(std::stringstream &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(ZString protocol); - + /// /// Sets the return code value for the response. /// /// @param code the response code value to return in the response. /// - + void setCode(ZString 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(ZString text); - + void setCookie(ZString key, ZString data); - + void setCookie(ZString key, std::string data); + private: ZString protocol; ZString code; ZString text; - + const char *CRLF = "\r\n"; - + }; - + } #endif diff --git a/ZString.cpp b/ZString.cpp index 7095f7b..2354c11 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -36,7 +36,7 @@ namespace coreutils { data = zstring.data; length = zstring.length; cursor = zstring.cursor; -// Log(LOG_DEBUG_2) << "ZSrting Copy Constructor: "; +// Log(LOG_DEBUG_2) << "ZString Copy Constructor: "; } std::vector &ZString::getList() { @@ -49,93 +49,93 @@ namespace coreutils { temp >> tempInt; return tempInt; } - + std::string ZString::str() { return std::string(data, length); } - + std::string ZString::str(int len) { return std::string(data, len); } - + std::vector &ZString::split(std::string delimiter, size_t maxSize) { coreutils::ZString zDelimiter((char *)delimiter.c_str(), delimiter.size()); return split(zDelimiter, maxSize); } - + std::vector &ZString::split(ZString &delimiter, size_t maxSize) { list.clear(); if(length == 0) { - list.push_back(ZString((char *)"")); + list.push_back(ZString("")); return list; } char *end = data + length; char *pos = cursor; - + while(pos < end) { if(strncmp(pos, delimiter.getData(), delimiter.getLength()) == 0) { - list.push_back(ZString(cursor, pos - cursor)); - cursor = pos + delimiter.getLength(); - pos = cursor; - } - else { - ++pos; - } + list.push_back(ZString(cursor, pos - cursor)); + cursor = pos + delimiter.getLength(); + pos = cursor; + } + else { + ++pos; + } } list.push_back(ZString(cursor, pos - cursor)); cursor = pos; return list; } - + ZString ZString::getTokenInclude(const char *include) { char *start = cursor; int len = strcspn(cursor, include); cursor += len; return ZString(start, len); } - + ZString ZString::getTokenExclude(const char *exclude) { char *start = cursor; int len = strspn(cursor, exclude); cursor += len; return ZString(start, len); } - + ZString ZString::getTokenExclude(std::string exclude) { return getTokenExclude(exclude.c_str()); } - + ZString &ZString::operator[](int index) { return list[index]; } - + bool ZString::eod() { return cursor >= data + length; } - + bool ZString::equals(const char *value) { if(strlen(value) != length) return false; return strncmp(data, value, length) == 0; } - + bool ZString::equals(char *value) { if(strlen(value) != length) return false; return strncmp(data, value, length) == 0; } - + bool ZString::equals(ZString &zstring) { if(zstring.getLength() != getLength()) - return false; + return false; return strncmp(data, zstring.getData(), getLength()) == 0; } - + bool ZString::equals(std::string &string) { return string == std::string(data, length); } - + bool ZString::ifNext(const char *value) { bool test = (strncmp(cursor, value, strlen(value)) == 0); if(test) @@ -168,11 +168,11 @@ namespace coreutils { size_t ZString::getLength() { return length; } - + void ZString::setZString(ZString zstring) { data = zstring.getData(); length = zstring.getLength(); cursor = data; } - -} \ No newline at end of file + +} diff --git a/ZString.h b/ZString.h index bb92696..2021adb 100644 --- a/ZString.h +++ b/ZString.h @@ -197,9 +197,11 @@ namespace coreutils { void setZString(ZString zstring); + size_t length; + private: char *data; - size_t length; +// size_t length; char *cursor; std::vector list; diff --git a/testing/zstring_test b/testing/zstring_test new file mode 100755 index 0000000..fdb3c3a Binary files /dev/null and b/testing/zstring_test differ diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp new file mode 100644 index 0000000..1adba88 --- /dev/null +++ b/testing/zstring_test.cpp @@ -0,0 +1,17 @@ +#include +#include "../ZString.h" + +int main(int argc, char **argv) { + + coreutils::ZString test("character1111:22222:33333"); + test.length = 9; + + test.split(":"); + + std::cout << test << std::endl; + std::cout << test.getLength() << std::endl; + + std::cout << test[0] << std::endl; + std::cout << test[1] << std::endl; + +}