99 lines
2.5 KiB
C++
99 lines
2.5 KiB
C++
#include "IMFMessage.h"
|
|
#include "IMFHeader.h"
|
|
#include "IMFMultipart.h"
|
|
#include "IMFPlainText.h"
|
|
|
|
namespace coreutils {
|
|
|
|
IMFMessage::IMFMessage() {}
|
|
|
|
IMFMessage::IMFMessage(PString &in) {
|
|
parse(in);
|
|
}
|
|
|
|
void IMFMessage::parse(PString &in) {
|
|
if(in.size() == 0)
|
|
return;
|
|
|
|
while(!in.ifNext("\r\n"))
|
|
headers.emplace_back(in);
|
|
|
|
std::string type = getHeader("Content-Type");
|
|
|
|
if(type.size() == 0)
|
|
type = "text/plain";
|
|
|
|
if(type == "multipart/form-data")
|
|
body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary"));
|
|
else if(type == "text/plain")
|
|
body = new IMFPlainText(in);
|
|
else
|
|
body = new IMFBody();
|
|
}
|
|
|
|
void IMFMessage::output(std::stringstream &out) {
|
|
for(IMFHeader header: headers) {
|
|
out << header.getKey() << ": " << header.getValue() << "\r\n";
|
|
}
|
|
out << "\r\n";
|
|
}
|
|
|
|
void IMFMessage::addHeader(std::string key, std::string value) {
|
|
headers.emplace_back(key, value);
|
|
|
|
}
|
|
|
|
std::string IMFMessage::getHeader(std::string key, bool valueOnly) {
|
|
for(IMFHeader header: headers) {
|
|
if(header.getKey() == key) {
|
|
std::string value = header.getValue();
|
|
if(valueOnly) {
|
|
std::vector<PString> split = PString(value).split(";");
|
|
value = split[0].str();
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
return std::string("");
|
|
}
|
|
|
|
std::string IMFMessage::getHeaderKeyPairValue(std::string headerKey, std::string key) {
|
|
std::string value;
|
|
coreutils::PString psource(getHeader(headerKey, false));
|
|
std::vector<PString> sourcep = psource.split(";");
|
|
for(PString work: sourcep) {
|
|
work.skipWhitespace();
|
|
std::string token = work.getTokenExclude("=");
|
|
if(work.ifNext("=")) {
|
|
if(token == key) {
|
|
if(work.ifNext("\"")) {
|
|
value = work.getTokenExclude("\"");
|
|
return value;
|
|
}
|
|
else {
|
|
value = work.str();
|
|
return value;
|
|
}
|
|
}
|
|
else
|
|
continue;
|
|
|
|
|
|
} else if (work.ifNext(";") || work.ifNext(" ")) {
|
|
if(token == key)
|
|
return std::string("true");
|
|
else
|
|
continue;
|
|
|
|
}
|
|
}
|
|
return std::string("false");
|
|
}
|
|
|
|
IMFBody * IMFMessage::getBody() {
|
|
return body;
|
|
}
|
|
|
|
|
|
}
|