76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
#include "IMFMessage.h"
|
|
#include "IMFHeader.h"
|
|
#include "IMFMultipart.h"
|
|
|
|
namespace coreutils {
|
|
|
|
IMFMessage::IMFMessage() {}
|
|
|
|
IMFMessage::IMFMessage(PString &in) {
|
|
parse(in);
|
|
}
|
|
|
|
void IMFMessage::parse(PString &in) {
|
|
while(!in.ifNext("\r\n"))
|
|
headers.emplace_back(in);
|
|
|
|
std::string type = getHeader("Content-Type");
|
|
|
|
if(type == "multipart/form-data")
|
|
body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary"));
|
|
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<std::string> split = PString(value).split(";");
|
|
value = split[0];
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
return std::string("");
|
|
}
|
|
|
|
std::string IMFMessage::getHeaderKeyPairValue(std::string headerKey, std::string key) {
|
|
coreutils::PString psource(getHeader(headerKey, false));
|
|
std::vector<std::string> sourcep = psource.split(";");
|
|
for(std::string keyx: sourcep) {
|
|
PString work(keyx);
|
|
work.skipWhitespace();
|
|
std::string token = work.getTokenExclude("=");
|
|
if(work.ifNext("=")) {
|
|
if(token == key)
|
|
return work.str();
|
|
else
|
|
continue;
|
|
|
|
} else if (work.ifNext(";") || work.ifNext(" ")) {
|
|
if(token == key)
|
|
return std::string("true");
|
|
else
|
|
continue;
|
|
|
|
}
|
|
}
|
|
return std::string("false");
|
|
}
|
|
|
|
}
|