Compare commits

..

12 Commits

Author SHA1 Message Date
Brad Arant
72c1286053 MFile stuff. 2025-06-20 00:42:24 +00:00
brad Arant
a87ce0bab7 JSON file changes 2025-05-17 20:47:09 -07:00
Brad Arant
9cd8381845 significant work on cookies and cgi formdata variables. 2025-03-18 16:46:20 -07:00
Brad Arant
bd5223ca36 more cgi processing multiform work. 2025-03-17 16:45:34 -07:00
brad Arant
b774e50b59 more session work. 2025-03-17 07:45:02 -07:00
Brad Arant
a3d5a4f93f more smartbuffer work. 2025-03-12 09:30:56 -07:00
Brad Arant
2866056072 more IMF work. 2025-03-11 16:59:16 -07:00
brad Arant
fd7a9ed860 some smart buffer work. 2025-03-11 07:34:17 -07:00
brad Arant
f35fa12902 Merge branch 'develop' of ssh://barant.com/git/CoreUtils into develop 2025-03-07 12:40:56 -08:00
Brad Arant
96f5c02435 Merge branch 'develop' of ssh://barant.com/git/CoreUtils into develop 2025-03-06 14:53:56 -08:00
brad Arant
11f396a749 worked on IMF smartbuffers. 2025-02-21 13:22:57 -08:00
brad Arant
3f0a541e80 added byte count limiter for read on MString. 2025-02-14 11:56:11 -08:00
22 changed files with 300 additions and 285 deletions

View File

@ -7,19 +7,19 @@ namespace coreutils {
CGIFormattedData::CGIFormattedData(ZString cgiData) { CGIFormattedData::CGIFormattedData(ZString cgiData) {
cgiData.split("&"); cgiData.split("&");
for(int ix = 1; ix < cgiData.getList().size(); ++ix) { for(int ix = 0; ix < cgiData.getList().size(); ++ix) {
cgiData[0].split("="); cgiData[ix].split("=");
data[cgiData[0]] = cgiData[1]; data[cgiData[ix][0]] = cgiData[ix][1];
data[cgiData[0]].fromCGI(); data[cgiData[ix][0]].fromCGI();
} }
} }
ZString CGIFormattedData::operator=(ZString cgiData) { ZString CGIFormattedData::operator=(ZString cgiData) {
cgiData.split("&"); cgiData.split("&");
for(int ix = 1; ix < cgiData.getList().size(); ++ix) { for(int ix = 0; ix < cgiData.getList().size(); ++ix) {
cgiData[0].split("="); cgiData[ix].split("=");
data[cgiData[0]] = cgiData[1]; data[cgiData[ix][0]] = cgiData[ix][1];
data[cgiData[0]].fromCGI(); data[cgiData[ix][0]].fromCGI();
} }
return cgiData; return cgiData;
} }

View File

@ -1,21 +1,12 @@
#include "IMFFormData.h" #include "IMFFormData.h"
#include "IMFMessage.h" #include "IMFMessage.h"
#include "IMFPlainText.h" #include "IMFText.h"
namespace coreutils { namespace coreutils {
IMFFormData::IMFFormData() {} IMFFormData::IMFFormData(int fd, ZString boundary) {
IMFHeader header(fd);
IMFFormData::IMFFormData(ZString &in, ZString &boundary) : IMFMultipart(in, boundary) {} IMFText text(fd, boundary);
ZString IMFFormData::getByName(ZString &name) {
for(ZString section: sections) {
IMFMessage message(section);
if(message.getHeaderKeyPairValue("Content-Disposition", ("name")).equals(name)) {
return message.multipart;
}
}
return ZString();
} }
} }

View File

@ -1,17 +1,14 @@
#ifndef __IMFFormData_h__ #ifndef __IMFFormData_h__
# define __IMFFormData_h__ # define __IMFFormData_h__
# include "IMFMultipart.h" #include "ZString.h"
namespace coreutils { namespace coreutils {
class IMFFormData: public IMFMultipart { class IMFFormData {
public: public:
IMFFormData(); IMFFormData(int fd, ZString boundary);
IMFFormData(ZString &in, ZString &boundary);
ZString getByName(ZString &name);
}; };

View File

@ -1,37 +1,18 @@
#include "IMFHeader.h" #include "IMFHeader.h"
#include "Exception.h" #include "Exception.h"
#include "Log.h" #include "Log.h"
#include <iostream> #include <unistd.h>
namespace coreutils { namespace coreutils {
IMFHeader::IMFHeader(ZString &in) : ZString(in) { IMFHeader::IMFHeader(int fd) {
char ch;
key = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789"); int rc = ::read(fd, &ch, 1);
std::cout << "key: [" << key << "]" << std::endl; while(ch != '\r') {
if(key.getLength() != 0) { IMFHeaderField *headerField = new IMFHeaderField(ch, fd);
if(!in.ifNext(":")) headers.push_back(headerField);
throw coreutils::Exception("Invalid character in expected header token.");
in.skipWhitespace();
value = in.goeolwithContinuation();
std::cout << "header: [" << value << "]" << std::endl;
} }
else if(in.skipWhitespace() > 0) {} rc = ::read(fd, &ch, 1);
else if(in.str() == "") {}
}
IMFHeader::IMFHeader(ZString key, ZString value) : ZString(), key(key), value(value) {}
ZString IMFHeader::getKey() {
return key;
}
ZString IMFHeader::getValue() {
return value;
}
void IMFHeader::output(std::stringstream &out) {
out << key << ": " << value << "\r\n";
} }
} }

View File

@ -1,25 +1,18 @@
#ifndef __IMFHeader_h__ #ifndef __IMFHeader_h__
#define __IMFHeader_h__ # define __IMFHeader_h__
#include "ZString.h" # include "IMFHeaderField.h"
#include <sstream> # include <vector>
namespace coreutils { namespace coreutils {
class IMFHeader : public ZString { class IMFHeader {
public: public:
IMFHeader(ZString &in); IMFHeader(int fd);
IMFHeader(ZString key, ZString value);
ZString getKey();
ZString getValue();
void output(std::stringstream &out);
private: private:
ZString key; std::vector<IMFHeaderField*> headers;
ZString value;
}; };

33
IMFHeaderField.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "IMFHeaderField.h"
#include "Exception.h"
#include "Log.h"
#include <unistd.h>
#include <fcntl.h>
namespace coreutils {
IMFHeaderField::IMFHeaderField(char &ch, int fd) {
int rc = 0;
while(((ch >= 'A') && (ch <= 'Z')) ||
((ch >= 'a') && (ch <= 'z')) ||
((ch >= '0') && (ch <= '9')) ||
(ch == '-') || (ch == '_')) {
key.write(ch);
rc = ::read(fd, &ch, 1);
}
if(ch != ':')
throw coreutils::Exception("expecting ':' in data stream.");
rc = ::read(fd, &ch, 1);
while(ch == ' ')
rc = ::read(fd, &ch, 1);
while(ch != '\r') {
value.write(ch);
rc = ::read(fd, &ch, 1);
}
rc = ::read(fd, &ch, 1);
if(ch != '\n')
throw coreutils::Exception("expecting '\n' in data stream.");
rc = ::read(fd, &ch, 1);
}
}

21
IMFHeaderField.h Normal file
View File

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

View File

@ -1,87 +1,11 @@
#include "IMFMessage.h" #include "IMFMessage.h"
#include "IMFHeader.h" #include "IMFHeader.h"
#include "IMFMultipart.h" #include "IMFMultipart.h"
#include "IMFPlainText.h" #include "IMFText.h"
#include "Log.h" #include "Log.h"
namespace coreutils { namespace coreutils {
IMFMessage::IMFMessage() : ZString() {} IMFMessage::IMFMessage(int fd) {}
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<ZString> 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();
}
} }

View File

@ -15,28 +15,21 @@ namespace coreutils {
/// ///
/// ///
class IMFMessage : public ZString { class IMFMessage {
public: public:
IMFMessage(); IMFMessage(int fd);
IMFMessage(ZString &in);
virtual ~IMFMessage(); virtual ~IMFMessage();
void output(std::stringstream &out); // void output(std::stringstream &out);
void addHeader(IMFHeader header); // IMFMultipart multipart;
// ZString block;
ZString getHeader(ZString key, bool valueOnly = true); // char contentLength[8];
ZString getHeaderKeyPairValue(const char *headerKey, const char *key);
ZString getHeaderKeyPairValue(ZString headerKey, ZString key);
IMFMultipart multipart;
ZString block;
char contentLength[8];
protected: protected:
std::vector<IMFHeader> headers; // std::vector<IMFHeader> headers;
std::stringstream newHeaders; // std::stringstream newHeaders;
}; };

View File

@ -1,35 +1,37 @@
#include "IMFMultipart.h" #include "IMFMultipart.h"
#include "Log.h" #include "Log.h"
#include "Exception.h"
#include <unistd.h>
namespace coreutils { namespace coreutils {
IMFMultipart::IMFMultipart() {} IMFMultipart::IMFMultipart(int fd, MString boundary) {
char ch = 0;
int rc = 0;
while((ch != '\r') && (ch != '\n')) {
rc = ::read(fd, &ch, 1);
if((ch != '\r') && (ch != '\n'))
boundary.write(ch);
}
if(ch == '\r') {
rc = ::read(fd, &ch, 1);
}
while(1) {
IMFFormData *formData = new IMFFormData(fd, boundary);
IMFMultipart::IMFMultipart(ZString &in, ZString &boundary) { rc = ::read(fd, &ch, 1);
Log(LOG_DEBUG_1) << in; if(ch == '-') {
std::stringstream temp; rc = ::read(fd, &ch, 1);
temp << "--" << boundary; if(ch == '-') {
sections = in.split(temp.str().c_str()); break;
for(int ix = 0; ix < sections.size(); ++ix) { }
sections[ix].ifNext("\r\n"); } else if(ch == '\r') {
sections[ix].trimCRLF(); rc = ::read(fd, &ch, 1);
Log(LOG_DEBUG_1) << "[" << sections[ix] << "]."; if(ch != '\n')
if(sections[ix].equals("--")) coreutils::Exception("expecting '\n' in data stream.");
sections[ix] = ZString("");
sections[ix].ifNext("\r\n");
} }
} }
std::string IMFMultipart::toString() {
return std::string(getData(), getLength());
}
int IMFMultipart::getCount() {
return sections.size();
}
ZString IMFMultipart::getSectionAt(int index) {
return sections[index];
} }
} }

View File

@ -1,25 +1,20 @@
#ifndef __IMFMultipart_h__ #ifndef __IMFMultipart_h__
#define __IMFMultipart_h__ #define __IMFMultipart_h__
#include "ZString.h" #include "MString.h"
#include "IMFFormData.h"
#include <vector>
namespace coreutils { namespace coreutils {
class IMFMultipart: public ZString { class IMFMultipart {
public: public:
IMFMultipart(); IMFMultipart(int fd, MString boundary);
IMFMultipart(ZString &in, ZString &boundary);
std::string toString();
int getCount();
ZString getSectionAt(int index);
protected:
std::vector<ZString> sections;
private: private:
char *boundary; MString boundary;
std::vector<IMFFormData> sections;
}; };

View File

@ -1,10 +0,0 @@
#include "IMFPlainText.h"
namespace coreutils {
IMFPlainText::IMFPlainText() {
}
IMFPlainText::IMFPlainText(ZString &in) : ZString(in.str().c_str()) {}
}

View File

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

View File

@ -18,14 +18,14 @@ namespace coreutils {
if(mode == LENGTH) { if(mode == LENGTH) {
sprintf(contentLength, "%li", content.str().length()); sprintf(contentLength, "%li", content.str().length());
ZString conlen(contentLength); ZString conlen(contentLength);
addHeader(IMFHeader("Content-Length", conlen)); // addHeader(IMFHeader("Content-Length", conlen));
} }
else else
addHeader(IMFHeader("Transfer-Encoding", "chunked")); // addHeader(IMFHeader("Transfer-Encoding", "chunked"));
addHeader(IMFHeader("Server", "JETServer v0.0.1")); // addHeader(IMFHeader("Server", "JETServer v0.0.1"));
output(response); // output(response);
response << content.str(); response << content.str();
return response; return response;
} }

View File

@ -14,7 +14,7 @@ namespace coreutils {
/// and responses as the main communications protocol. /// and responses as the main communications protocol.
/// ///
class IMFResponse : public IMFMessage { class IMFResponse {
public: public:
@ -87,6 +87,7 @@ namespace coreutils {
ZString code; ZString code;
ZString text; ZString text;
char contentLength[32];
const char *CRLF = "\r\n"; const char *CRLF = "\r\n";
}; };

27
IMFText.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "IMFText.h"
#include <unistd.h>
namespace coreutils {
IMFText::IMFText(int fd, ZString boundary) : MString() {
char ch = 0;
int rc = 0;
boundary.reset();
int blen = boundary.getLength() + 1;
while(1) {
rc = ::read(fd, &ch, 1);
if(boundary.nextChar() == ch) {
if(boundary.eod()) {
length -= blen;
break;
}
} else {
boundary.reset();
}
write(ch);
}
}
}

18
IMFText.h Normal file
View File

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

View File

@ -1,33 +0,0 @@
#ifndef __JSONFile_h__
#define __JSONFile_h__
#include "JString.h"
#include "File.h"
#include <iostream>
#include <sstream>
namespace coreutils {
///
/// Use the JSONFile object where you need a file based persistent backing store
/// for the JString style object.
///
class JSONFile : public JString : public File {
public:
JSONFile(ZString path): JString(), File(path, O_RDWR, 0644) {
}
virtual ~JSONFile() {
write(*this);
}
void changed(ZString key, ZString value) overide;
};
}
#endif

20
MFile.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "MFile.h"
#include "Exception.h"
#include <sys/stat.h>
namespace coreutils {
MFile::MFile(coreutils::MString fileName) : fileName(fileName) {
inFile.open(fileName.c_str());
// *this << inFile;
inFile.close();
}
void MFile::update() {
outFile.open(fileName.c_str());
outFile << *this;
outFile.close();
}
}

35
MFile.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __MFile_h__
#define __MFile_h__
#include "MString.h"
#include <fstream>
#include <iostream>
///
/// MFile
///
/// An MString with a file backing store.
///
namespace coreutils {
class MFile : public MString {
public:
MFile(MString fileName);
virtual ~MFile();
coreutils::MString fileName;
private:
std::ifstream inFile;
std::ofstream outFile;
void onChange() override;
void update();
};
}
#endif

View File

@ -107,6 +107,7 @@ namespace coreutils {
setSize(value.getLength()); setSize(value.getLength());
memcpy(data, value.getData(), value.getLength()); memcpy(data, value.getData(), value.getLength());
length = value.getLength(); length = value.getLength();
onChange();
return *this; return *this;
} }
@ -114,6 +115,7 @@ namespace coreutils {
setSize(value.getLength()); setSize(value.getLength());
memcpy(data, value.getData(), value.getLength()); memcpy(data, value.getData(), value.getLength());
length = value.getLength(); length = value.getLength();
onChange();
return *this; return *this;
} }
@ -121,6 +123,7 @@ namespace coreutils {
setSize(value.length()); setSize(value.length());
memcpy(data, value.c_str(), value.length()); memcpy(data, value.c_str(), value.length());
length = value.length(); length = value.length();
onChange();
return *this; return *this;
} }
@ -128,6 +131,7 @@ namespace coreutils {
int len = strlen(value); int len = strlen(value);
setSize(len); setSize(len);
memcpy(data, value, len); memcpy(data, value, len);
onChange();
return *this; return *this;
} }
@ -135,6 +139,7 @@ namespace coreutils {
int len = 1; int len = 1;
setSize(1); setSize(1);
*data = value; *data = value;
onChange();
return *this; return *this;
} }
@ -146,6 +151,7 @@ namespace coreutils {
setSize(temp.length()); setSize(temp.length());
memcpy(this->data, (char *)temp.data(), temp.length()); memcpy(this->data, (char *)temp.data(), temp.length());
cursor = this->data; cursor = this->data;
onChange();
return *this; return *this;
} }
@ -154,6 +160,7 @@ namespace coreutils {
setSize(temp.length()); setSize(temp.length());
memcpy(this->data, (char *)temp.data(), temp.length()); memcpy(this->data, (char *)temp.data(), temp.length());
cursor = this->data; cursor = this->data;
onChange();
return *this; return *this;
} }
@ -162,6 +169,7 @@ namespace coreutils {
int len = strlen(value); int len = strlen(value);
setSize(len + length); setSize(len + length);
memcpy(data + temp, value, len); memcpy(data + temp, value, len);
onChange();
return *this; return *this;
} }
@ -170,6 +178,7 @@ namespace coreutils {
temp << value; temp << value;
MString temp2 = temp.str(); MString temp2 = temp.str();
write(temp2); write(temp2);
onChange();
return *this; return *this;
} }
@ -186,12 +195,14 @@ namespace coreutils {
int len = length + zstring.getLength(); int len = length + zstring.getLength();
setSize(len); setSize(len);
memcpy(data + temp, zstring.getData(), zstring.getLength()); memcpy(data + temp, zstring.getData(), zstring.getLength());
onChange();
return *this; return *this;
} }
MString &MString::operator<<(char value) { MString &MString::operator<<(char value) {
setSize(length + 1); setSize(length + 1);
data[length - 1] = value; data[length - 1] = value;
onChange();
return *this; return *this;
} }
@ -205,11 +216,16 @@ namespace coreutils {
return *this; return *this;
} }
// MString &operator<<(std::ostream &out) {
// *this << out;
// }
void MString::insert(char ch, int offset) { void MString::insert(char ch, int offset) {
setSize(length + 1); setSize(length + 1);
for (int ix = length; ix >= offset; ix--) for (int ix = length; ix >= offset; ix--)
getData()[ix + 1] = getData()[ix]; getData()[ix + 1] = getData()[ix];
data[offset] = ch; data[offset] = ch;
onChange();
} }
void MString::insert(ZString value, int offset) { void MString::insert(ZString value, int offset) {
@ -217,6 +233,7 @@ namespace coreutils {
for (int ix = length; ix >= offset; ix--) for (int ix = length; ix >= offset; ix--)
getData()[ix] = getData()[ix - value.getLength()]; getData()[ix] = getData()[ix - value.getLength()];
std::memcpy(data + offset, value.getData(), value.getLength()); std::memcpy(data + offset, value.getData(), value.getLength());
onChange();
} }
// void MString::insert(std::string value, int offset) { // void MString::insert(std::string value, int offset) {
@ -240,17 +257,20 @@ namespace coreutils {
for (int ix = 0; ix < len; ix++) for (int ix = 0; ix < len; ix++)
cursor[ix] = cursor[ix + length]; cursor[ix] = cursor[ix + length];
setSize(this->length - length); setSize(this->length - length);
onChange();
} }
void MString::remove(int offset, int length) { void MString::remove(int offset, int length) {
for (int ix = offset; ix < this->length; ix++) for (int ix = offset; ix < this->length; ix++)
getData()[ix] = getData()[ix + length]; getData()[ix] = getData()[ix + length];
setSize(this->length - length); setSize(this->length - length);
onChange();
} }
MString &MString::write(char ch) { MString &MString::write(char ch) {
setSize(length + 1); setSize(length + 1);
*(data + length - 1) = ch; *(data + length - 1) = ch;
onChange();
return *this; return *this;
} }
@ -258,6 +278,7 @@ namespace coreutils {
int len = length; int len = length;
setSize(length + value.getLength()); setSize(length + value.getLength());
memcpy(data + len, value.getData(), value.getLength()); memcpy(data + len, value.getData(), value.getLength());
onChange();
return *this; return *this;
} }
@ -276,6 +297,15 @@ namespace coreutils {
char ch; char ch;
while(::read(fd, &ch, 1)) while(::read(fd, &ch, 1))
write(ch); write(ch);
onChange();
return *this;
}
MString &MString::read(int fd, int count) {
char ch;
while(count-- && ::read(fd, &ch, 1))
write(ch);
onChange();
return *this; return *this;
} }
@ -412,4 +442,6 @@ namespace coreutils {
return c; return c;
} }
void MString::onChange() {}
} }

View File

@ -170,6 +170,12 @@ namespace coreutils {
/// ///
/// ///
MString &operator<<(std::ostream &out);
///
///
///
void insert(char ch, int offset); void insert(char ch, int offset);
/// ///
@ -231,6 +237,7 @@ namespace coreutils {
/// ///
MString &read(int fd); MString &read(int fd);
MString &read(int fd, int len);
/// ///
/// ///
@ -302,6 +309,12 @@ namespace coreutils {
protected: protected:
void setSize(int size); void setSize(int size);
///
/// This method is called whenever a change is made to the MString storage.
///
virtual void onChange();
private: private:
int bufferSize = 0; int bufferSize = 0;
char hexChar(char c); char hexChar(char c);