Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
72c1286053 | ||
![]() |
a87ce0bab7 | ||
![]() |
9cd8381845 | ||
![]() |
bd5223ca36 | ||
![]() |
b774e50b59 | ||
![]() |
a3d5a4f93f | ||
![]() |
2866056072 | ||
![]() |
fd7a9ed860 | ||
![]() |
f35fa12902 | ||
![]() |
96f5c02435 | ||
![]() |
25916051e6 | ||
![]() |
11f396a749 | ||
![]() |
3f0a541e80 | ||
![]() |
5e6d162a05 | ||
![]() |
d1c450fe48 |
46
Base64.cpp
46
Base64.cpp
@ -12,9 +12,12 @@ namespace coreutils {
|
||||
return (isalnum(c) || (c == '+') || (c == '/'));
|
||||
}
|
||||
|
||||
std::string Base64::encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
|
||||
|
||||
std::string ret;
|
||||
MString Base64::encode(ZString data) {
|
||||
|
||||
data.push();
|
||||
data.reset();
|
||||
MString ret;
|
||||
int in_len = data.getLength();
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
unsigned char char_array_3[3];
|
||||
@ -22,7 +25,7 @@ namespace coreutils {
|
||||
|
||||
while (in_len--) {
|
||||
|
||||
char_array_3[i++] = *(bytes_to_encode++);
|
||||
char_array_3[i++] = data.nextChar();
|
||||
if (i == 3) {
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
@ -31,10 +34,9 @@ namespace coreutils {
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for(i = 0; (i <4) ; i++)
|
||||
ret += base64_chars[char_array_4[i]];
|
||||
ret.write(base64_chars[char_array_4[i]]);
|
||||
i = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
@ -47,29 +49,28 @@ namespace coreutils {
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
|
||||
for (j = 0; (j < i + 1); j++)
|
||||
ret += base64_chars[char_array_4[j]];
|
||||
ret.write(base64_chars[char_array_4[j]]);
|
||||
|
||||
while((i++ < 3))
|
||||
ret += '=';
|
||||
|
||||
ret.write('=');
|
||||
}
|
||||
|
||||
|
||||
data.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string Base64::decode(std::string const& encoded_string) {
|
||||
|
||||
int in_len = encoded_string.size();
|
||||
MString Base64::decode(ZString b64data) {
|
||||
|
||||
b64data.push();
|
||||
b64data.reset();
|
||||
int in_len = b64data.getLength();
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int in_ = 0;
|
||||
unsigned char char_array_4[4], char_array_3[3];
|
||||
std::string ret;
|
||||
MString ret;
|
||||
|
||||
while (in_len-- && ( encoded_string[in_] != '=') && isBase64(encoded_string[in_])) {
|
||||
while (in_len-- && (b64data.getChar() != '=') && isBase64(b64data.getChar())) {
|
||||
|
||||
char_array_4[i++] = encoded_string[in_]; in_++;
|
||||
char_array_4[i++] = b64data.nextChar();
|
||||
if (i ==4) {
|
||||
|
||||
for (i = 0; i <4; i++)
|
||||
@ -80,7 +81,7 @@ namespace coreutils {
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for (i = 0; (i < 3); i++)
|
||||
ret += char_array_3[i];
|
||||
ret.write(char_array_3[i]);
|
||||
i = 0;
|
||||
}
|
||||
|
||||
@ -94,9 +95,10 @@ namespace coreutils {
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
|
||||
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
|
||||
for (j = 0; (j < i - 1); j++)
|
||||
ret.write(char_array_3[j]);
|
||||
}
|
||||
|
||||
b64data.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
10
Base64.h
10
Base64.h
@ -1,20 +1,22 @@
|
||||
#ifndef __Base64_h__
|
||||
#define __Base64_h__
|
||||
|
||||
#include <string>
|
||||
#include "ZString.h"
|
||||
#include "MString.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
///
|
||||
///
|
||||
/// Use the Base64 object when you need to translate character string
|
||||
/// to/from base64 encoded strings.
|
||||
///
|
||||
|
||||
class Base64 {
|
||||
|
||||
public:
|
||||
bool isBase64(unsigned char c);
|
||||
std::string encode(unsigned char const* bytes_to_encode, unsigned int in_len);
|
||||
std::string decode(std::string const& encoded_string);
|
||||
MString encode(ZString data);
|
||||
MString decode(ZString b64data);
|
||||
|
||||
};
|
||||
|
||||
|
27
CGIFormattedData.cpp
Normal file
27
CGIFormattedData.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "CGIFormattedData.h"
|
||||
#include "ZString.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
CGIFormattedData::CGIFormattedData() {}
|
||||
|
||||
CGIFormattedData::CGIFormattedData(ZString cgiData) {
|
||||
cgiData.split("&");
|
||||
for(int ix = 0; ix < cgiData.getList().size(); ++ix) {
|
||||
cgiData[ix].split("=");
|
||||
data[cgiData[ix][0]] = cgiData[ix][1];
|
||||
data[cgiData[ix][0]].fromCGI();
|
||||
}
|
||||
}
|
||||
|
||||
ZString CGIFormattedData::operator=(ZString cgiData) {
|
||||
cgiData.split("&");
|
||||
for(int ix = 0; ix < cgiData.getList().size(); ++ix) {
|
||||
cgiData[ix].split("=");
|
||||
data[cgiData[ix][0]] = cgiData[ix][1];
|
||||
data[cgiData[ix][0]].fromCGI();
|
||||
}
|
||||
return cgiData;
|
||||
}
|
||||
|
||||
}
|
29
CGIFormattedData.h
Normal file
29
CGIFormattedData.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef __CGIFormattedData_h__
|
||||
# define __CGIFormattedData_h__
|
||||
|
||||
# include "MString.h"
|
||||
# include <map>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
///
|
||||
/// Use the CGIFormattedData object to store and process data that
|
||||
/// interfaces to the CGI format. Value pairs are accessible using
|
||||
/// a common map interface.
|
||||
///
|
||||
|
||||
class CGIFormattedData {
|
||||
|
||||
public:
|
||||
CGIFormattedData();
|
||||
CGIFormattedData(ZString cgiData);
|
||||
|
||||
ZString operator=(ZString cgiData);
|
||||
|
||||
std::map<MString, MString> data;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,21 +1,12 @@
|
||||
#include "IMFFormData.h"
|
||||
#include "IMFMessage.h"
|
||||
#include "IMFPlainText.h"
|
||||
#include "IMFText.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
IMFFormData::IMFFormData() {}
|
||||
|
||||
IMFFormData::IMFFormData(ZString &in, ZString &boundary) : IMFMultipart(in, 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();
|
||||
|
||||
IMFFormData::IMFFormData(int fd, ZString boundary) {
|
||||
IMFHeader header(fd);
|
||||
IMFText text(fd, boundary);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,14 @@
|
||||
#ifndef __IMFFormData_h__
|
||||
# define __IMFFormData_h__
|
||||
|
||||
# include "IMFMultipart.h"
|
||||
#include "ZString.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
class IMFFormData: public IMFMultipart {
|
||||
class IMFFormData {
|
||||
|
||||
public:
|
||||
IMFFormData();
|
||||
IMFFormData(ZString &in, ZString &boundary);
|
||||
|
||||
ZString getByName(ZString &name);
|
||||
IMFFormData(int fd, ZString boundary);
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,37 +1,18 @@
|
||||
#include "IMFHeader.h"
|
||||
#include "Exception.h"
|
||||
#include "Log.h"
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
IMFHeader::IMFHeader(ZString &in) : ZString(in) {
|
||||
|
||||
key = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789");
|
||||
std::cout << "key: [" << key << "]" << std::endl;
|
||||
if(key.getLength() != 0) {
|
||||
if(!in.ifNext(":"))
|
||||
throw coreutils::Exception("Invalid character in expected header token.");
|
||||
in.skipWhitespace();
|
||||
value = in.goeolwithContinuation();
|
||||
std::cout << "header: [" << value << "]" << std::endl;
|
||||
IMFHeader::IMFHeader(int fd) {
|
||||
char ch;
|
||||
int rc = ::read(fd, &ch, 1);
|
||||
while(ch != '\r') {
|
||||
IMFHeaderField *headerField = new IMFHeaderField(ch, fd);
|
||||
headers.push_back(headerField);
|
||||
}
|
||||
else if(in.skipWhitespace() > 0) {}
|
||||
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";
|
||||
rc = ::read(fd, &ch, 1);
|
||||
}
|
||||
|
||||
}
|
27
IMFHeader.h
27
IMFHeader.h
@ -1,26 +1,19 @@
|
||||
#ifndef __IMFHeader_h__
|
||||
#define __IMFHeader_h__
|
||||
# define __IMFHeader_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include <sstream>
|
||||
# include "IMFHeaderField.h"
|
||||
# include <vector>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
class IMFHeader : public ZString {
|
||||
|
||||
|
||||
class IMFHeader {
|
||||
|
||||
public:
|
||||
IMFHeader(ZString &in);
|
||||
IMFHeader(ZString key, ZString value);
|
||||
|
||||
ZString getKey();
|
||||
ZString getValue();
|
||||
|
||||
void output(std::stringstream &out);
|
||||
|
||||
IMFHeader(int fd);
|
||||
|
||||
private:
|
||||
ZString key;
|
||||
ZString value;
|
||||
|
||||
std::vector<IMFHeaderField*> headers;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
33
IMFHeaderField.cpp
Normal file
33
IMFHeaderField.cpp
Normal 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
21
IMFHeaderField.h
Normal 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
|
@ -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<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();
|
||||
}
|
||||
IMFMessage::IMFMessage(int fd) {}
|
||||
|
||||
}
|
||||
|
23
IMFMessage.h
23
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<IMFHeader> headers;
|
||||
std::stringstream newHeaders;
|
||||
// std::vector<IMFHeader> headers;
|
||||
// std::stringstream newHeaders;
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,35 +1,37 @@
|
||||
#include "IMFMultipart.h"
|
||||
#include "Log.h"
|
||||
#include "Exception.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
IMFMultipart::IMFMultipart() {}
|
||||
|
||||
IMFMultipart::IMFMultipart(ZString &in, ZString &boundary) {
|
||||
Log(LOG_DEBUG_1) << in;
|
||||
std::stringstream temp;
|
||||
temp << "--" << boundary;
|
||||
sections = in.split(temp.str().c_str());
|
||||
for(int ix = 0; ix < sections.size(); ++ix) {
|
||||
sections[ix].ifNext("\r\n");
|
||||
sections[ix].trimCRLF();
|
||||
Log(LOG_DEBUG_1) << "[" << sections[ix] << "].";
|
||||
if(sections[ix].equals("--"))
|
||||
sections[ix] = ZString("");
|
||||
sections[ix].ifNext("\r\n");
|
||||
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);
|
||||
|
||||
rc = ::read(fd, &ch, 1);
|
||||
if(ch == '-') {
|
||||
rc = ::read(fd, &ch, 1);
|
||||
if(ch == '-') {
|
||||
break;
|
||||
}
|
||||
} else if(ch == '\r') {
|
||||
rc = ::read(fd, &ch, 1);
|
||||
if(ch != '\n')
|
||||
coreutils::Exception("expecting '\n' in data stream.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string IMFMultipart::toString() {
|
||||
return std::string(getData(), getLength());
|
||||
}
|
||||
|
||||
int IMFMultipart::getCount() {
|
||||
return sections.size();
|
||||
}
|
||||
|
||||
ZString IMFMultipart::getSectionAt(int index) {
|
||||
return sections[index];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,26 +1,21 @@
|
||||
#ifndef __IMFMultipart_h__
|
||||
#define __IMFMultipart_h__
|
||||
|
||||
#include "ZString.h"
|
||||
#include "MString.h"
|
||||
#include "IMFFormData.h"
|
||||
#include <vector>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
class IMFMultipart: public ZString {
|
||||
class IMFMultipart {
|
||||
|
||||
public:
|
||||
IMFMultipart();
|
||||
IMFMultipart(ZString &in, ZString &boundary);
|
||||
|
||||
std::string toString();
|
||||
int getCount();
|
||||
ZString getSectionAt(int index);
|
||||
|
||||
protected:
|
||||
std::vector<ZString> sections;
|
||||
IMFMultipart(int fd, MString boundary);
|
||||
|
||||
private:
|
||||
char *boundary;
|
||||
|
||||
MString boundary;
|
||||
std::vector<IMFFormData> sections;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
#include "IMFPlainText.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
IMFPlainText::IMFPlainText() {
|
||||
}
|
||||
|
||||
IMFPlainText::IMFPlainText(ZString &in) : ZString(in.str().c_str()) {}
|
||||
|
||||
}
|
@ -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
|
@ -18,14 +18,14 @@ namespace coreutils {
|
||||
if(mode == LENGTH) {
|
||||
sprintf(contentLength, "%li", content.str().length());
|
||||
ZString conlen(contentLength);
|
||||
addHeader(IMFHeader("Content-Length", conlen));
|
||||
// addHeader(IMFHeader("Content-Length", conlen));
|
||||
}
|
||||
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();
|
||||
return response;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace coreutils {
|
||||
/// and responses as the main communications protocol.
|
||||
///
|
||||
|
||||
class IMFResponse : public IMFMessage {
|
||||
class IMFResponse {
|
||||
|
||||
public:
|
||||
|
||||
@ -87,6 +87,7 @@ namespace coreutils {
|
||||
ZString code;
|
||||
ZString text;
|
||||
|
||||
char contentLength[32];
|
||||
const char *CRLF = "\r\n";
|
||||
|
||||
};
|
||||
|
27
IMFText.cpp
Normal file
27
IMFText.cpp
Normal 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
18
IMFText.h
Normal 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
|
33
JSONFile.h
33
JSONFile.h
@ -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
20
MFile.cpp
Normal 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
35
MFile.h
Normal 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
|
72
MString.cpp
72
MString.cpp
@ -2,6 +2,7 @@
|
||||
#include "Log.h"
|
||||
#include "uuid/uuid.h"
|
||||
#include "Exception.h"
|
||||
#include "Base64.h"
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <format>
|
||||
@ -9,6 +10,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cctype>
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
@ -105,27 +107,31 @@ namespace coreutils {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(coreutils::ZString value) {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(std::string value) {
|
||||
setSize(value.length());
|
||||
memcpy(data, value.c_str(), value.length());
|
||||
length = value.length();
|
||||
return *this;
|
||||
MString &MString::operator=(coreutils::ZString value) {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(std::string value) {
|
||||
setSize(value.length());
|
||||
memcpy(data, value.c_str(), value.length());
|
||||
length = value.length();
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(const char *value) {
|
||||
int len = strlen(value);
|
||||
setSize(len);
|
||||
memcpy(data, value, len);
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -133,6 +139,7 @@ namespace coreutils {
|
||||
int len = 1;
|
||||
setSize(1);
|
||||
*data = value;
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -144,6 +151,7 @@ namespace coreutils {
|
||||
setSize(temp.length());
|
||||
memcpy(this->data, (char *)temp.data(), temp.length());
|
||||
cursor = this->data;
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -152,6 +160,7 @@ namespace coreutils {
|
||||
setSize(temp.length());
|
||||
memcpy(this->data, (char *)temp.data(), temp.length());
|
||||
cursor = this->data;
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -160,6 +169,7 @@ namespace coreutils {
|
||||
int len = strlen(value);
|
||||
setSize(len + length);
|
||||
memcpy(data + temp, value, len);
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -168,6 +178,7 @@ namespace coreutils {
|
||||
temp << value;
|
||||
MString temp2 = temp.str();
|
||||
write(temp2);
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -184,12 +195,14 @@ namespace coreutils {
|
||||
int len = length + zstring.getLength();
|
||||
setSize(len);
|
||||
memcpy(data + temp, zstring.getData(), zstring.getLength());
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator<<(char value) {
|
||||
setSize(length + 1);
|
||||
data[length - 1] = value;
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -202,12 +215,17 @@ namespace coreutils {
|
||||
std::cout << "xxx" << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// MString &operator<<(std::ostream &out) {
|
||||
// *this << out;
|
||||
// }
|
||||
|
||||
void MString::insert(char ch, int offset) {
|
||||
setSize(length + 1);
|
||||
for (int ix = length; ix >= offset; ix--)
|
||||
getData()[ix + 1] = getData()[ix];
|
||||
data[offset] = ch;
|
||||
onChange();
|
||||
}
|
||||
|
||||
void MString::insert(ZString value, int offset) {
|
||||
@ -215,6 +233,7 @@ namespace coreutils {
|
||||
for (int ix = length; ix >= offset; ix--)
|
||||
getData()[ix] = getData()[ix - value.getLength()];
|
||||
std::memcpy(data + offset, value.getData(), value.getLength());
|
||||
onChange();
|
||||
}
|
||||
|
||||
// void MString::insert(std::string value, int offset) {
|
||||
@ -238,17 +257,20 @@ namespace coreutils {
|
||||
for (int ix = 0; ix < len; ix++)
|
||||
cursor[ix] = cursor[ix + length];
|
||||
setSize(this->length - length);
|
||||
onChange();
|
||||
}
|
||||
|
||||
void MString::remove(int offset, int length) {
|
||||
for (int ix = offset; ix < this->length; ix++)
|
||||
getData()[ix] = getData()[ix + length];
|
||||
setSize(this->length - length);
|
||||
onChange();
|
||||
}
|
||||
|
||||
MString &MString::write(char ch) {
|
||||
setSize(length + 1);
|
||||
*(data + length - 1) = ch;
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -256,6 +278,7 @@ namespace coreutils {
|
||||
int len = length;
|
||||
setSize(length + value.getLength());
|
||||
memcpy(data + len, value.getData(), value.getLength());
|
||||
onChange();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -274,6 +297,15 @@ namespace coreutils {
|
||||
char ch;
|
||||
while(::read(fd, &ch, 1))
|
||||
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;
|
||||
}
|
||||
|
||||
@ -364,25 +396,29 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
MString MString::toBase64() {
|
||||
MString target;
|
||||
return target;
|
||||
Base64 b64;
|
||||
return b64.encode(*this);
|
||||
}
|
||||
|
||||
|
||||
MString MString::fromBase64() {
|
||||
MString target;
|
||||
return target;
|
||||
Base64 b64;
|
||||
return b64.decode(*this);
|
||||
}
|
||||
|
||||
|
||||
MString MString::toUpper() {
|
||||
MString target;
|
||||
for(int ix = 0; ix < length; ++ix)
|
||||
target.write(std::toupper(data[ix]));
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
MString MString::toLower() {
|
||||
MString target;
|
||||
for(int ix = 0; ix < length; ++ix)
|
||||
target.write(std::tolower(data[ix]));
|
||||
return target;
|
||||
}
|
||||
|
||||
@ -405,5 +441,7 @@ namespace coreutils {
|
||||
c += 7;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void MString::onChange() {}
|
||||
|
||||
}
|
||||
|
15
MString.h
15
MString.h
@ -165,6 +165,12 @@ namespace coreutils {
|
||||
|
||||
// MString &operator<<(std::ostream (*f)(std::ostream&) );
|
||||
MString &operator<<(std::basic_ostream<char> (*pf)(std::basic_ostream<char>&));
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
MString &operator<<(std::ostream &out);
|
||||
|
||||
///
|
||||
///
|
||||
@ -231,7 +237,8 @@ namespace coreutils {
|
||||
///
|
||||
|
||||
MString &read(int fd);
|
||||
|
||||
MString &read(int fd, int len);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
@ -301,6 +308,12 @@ namespace coreutils {
|
||||
|
||||
protected:
|
||||
void setSize(int size);
|
||||
|
||||
///
|
||||
/// This method is called whenever a change is made to the MString storage.
|
||||
///
|
||||
|
||||
virtual void onChange();
|
||||
|
||||
private:
|
||||
int bufferSize = 0;
|
||||
|
@ -614,7 +614,11 @@ namespace coreutils {
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
char ZString::getChar() {
|
||||
return *cursor;
|
||||
}
|
||||
|
||||
char ZString::nextChar() {
|
||||
char temp = 0;
|
||||
if(!eod())
|
||||
|
@ -434,6 +434,12 @@ namespace coreutils {
|
||||
|
||||
bool trimCRLF();
|
||||
|
||||
///
|
||||
/// Returns the character at the cursor and returns. Cursor is not changed.
|
||||
///
|
||||
|
||||
char getChar();
|
||||
|
||||
///
|
||||
/// Returns the character at the cursor and advances the cursor one
|
||||
/// position until end of data.
|
||||
|
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils -lb64
|
||||
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
|
Binary file not shown.
@ -95,8 +95,19 @@ int main(int argc, char **argv) {
|
||||
test28 = test29;
|
||||
std::cout << "int: " << test28 << std::endl;
|
||||
|
||||
coreutils::MString test30 = "ABCDEF";
|
||||
coreutils::MString test30 = "ABCDEF0123";
|
||||
std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl;
|
||||
|
||||
std::cout << test30 << " to lower " << test30.toLower() << std::endl;
|
||||
|
||||
coreutils::MString test31 = "abcdef0123";
|
||||
std::cout << test31 << " to upper " << test31.toUpper() << std::endl;
|
||||
|
||||
coreutils::MString test32 = "This is a test of converting this string to base64";
|
||||
std::cout << test32 << " to base64 " << test32.toBase64() << std::endl;
|
||||
|
||||
coreutils::MString test33 = test32.toBase64();
|
||||
std::cout << test33 << " from base64 " << test33.fromBase64() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user