some smart buffer work.

This commit is contained in:
brad Arant 2025-03-11 07:34:17 -07:00
parent f35fa12902
commit fd7a9ed860
14 changed files with 109 additions and 142 deletions

View File

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

View File

@ -1,15 +1,18 @@
#include "IMFHeader.h"
#include "Exception.h"
#include "Log.h"
#include <iostream>
#include <unistd.h>
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);
}
}

View File

@ -1,15 +1,16 @@
#ifndef __IMFHeader_h__
#define __IMFHeader_h__
# define __IMFHeader_h__
#include <vector>
# include "IMFHeaderField.h"
# include <vector>
namespace coreutils {
class IMFHeader {
public:
IMFHeader(int fd);
private:
std::vector<IMFHeaderField> headers;

32
IMFHeaderField.cpp Normal file
View File

@ -0,0 +1,32 @@
#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;
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);
}
}

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 "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) {}
}

View File

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

View File

@ -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')) {

View File

@ -2,6 +2,7 @@
#define __IMFMultipart_h__
#include "MString.h"
#include "IMFFormData.h"
#include <vector>
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<ZString> sections;
IMFMultipart(int fd, ZString boundary);
private:
MString boundary;

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

@ -14,7 +14,7 @@ namespace coreutils {
/// and responses as the main communications protocol.
///
class IMFResponse : public IMFMessage {
class IMFResponse {
public:

9
IMFText.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "IMFText.h"
namespace coreutils {
IMFText::IMFText(int fd, ZString boundary) : MString() {
}
}

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