Added vscode

This commit is contained in:
Brad Arant 2020-11-07 17:25:37 -08:00
parent 6894968732
commit c00476719c
10 changed files with 183 additions and 158 deletions

26
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}"
],
"options": {
"cwd": "/home/barant/Development/HTTPServer/"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@ -2,40 +2,40 @@
#include "Exception.h" #include "Exception.h"
namespace coreutils { namespace coreutils {
File::File(std::string fileName, int mode, int authority) { File::File(std::string fileName, int mode, int authority) {
this->fileName = fileName; this->fileName = fileName;
struct stat status; struct stat status;
stat(fileName.c_str(), &status); stat(fileName.c_str(), &status);
buffer = NULL; buffer = NULL;
setBufferSize(status.st_size); setBufferSize(status.st_size);
fd = open(fileName.c_str(), mode, authority); fd = open(fileName.c_str(), mode, authority);
if(fd < 0) { if(fd < 0) {
std::stringstream temp; std::stringstream temp;
temp << "Error opening file " << fileName << "."; temp << "Error opening file " << fileName << ".";
throw Exception(temp.str(), __FILE__, __LINE__); throw Exception(temp.str(), __FILE__, __LINE__);
} }
} }
File::~File() { File::~File() {
} }
void File::setBufferSize(size_t size) { void File::setBufferSize(size_t size) {
buffer = (char *)realloc(buffer, size); buffer = (char *)realloc(buffer, size);
this->size = size; this->size = size;
} }
void File::read() { void File::read() {
size = ::read(fd, buffer, size); size = ::read(fd, buffer, size);
setBufferSize(size); setBufferSize(size);
} }
void File::write(std::string data) { void File::write(std::string data) {
::write(fd, data.c_str(), data.length()); ::write(fd, data.c_str(), data.length());
} }
@ -43,5 +43,5 @@ namespace coreutils {
std::string File::asString() { std::string File::asString() {
return std::string(buffer, size); return std::string(buffer, size);
} }
} }

View File

@ -2,9 +2,9 @@
#define __IMFBody_h__ #define __IMFBody_h__
namespace coreutils { namespace coreutils {
class IMFBody {}; class IMFBody {};
} }
#endif #endif

View File

@ -5,51 +5,61 @@
#include "Log.h" #include "Log.h"
namespace coreutils { namespace coreutils {
IMFMessage::IMFMessage() {} IMFMessage::IMFMessage() {
IMFMessage::IMFMessage(PString &in) {
body = NULL; body = NULL;
first = true;
}
IMFMessage::IMFMessage(PString &in) : IMFMessage() {
parse(in); parse(in);
} }
bool IMFMessage::parse(PString &in) { bool IMFMessage::parse(PString &in) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "parse [" << in.str() << "]"; coreutils::Log(coreutils::LOG_DEBUG_4) << "parse [" << in.str() << "]";
if(in.str() != "") { if(first) {
headers.emplace_back(in); if(in.str().find(" ") != std::string::npos) {
parse(in);
first = false;
return true;
}
}
if(in.str() != "") {
headers.emplace_back(in);
return true; return true;
} }
coreutils::Log(coreutils::LOG_DEBUG_2) << "End of header with Content Type = " << getHeader("Content-Type") << " for " << getHeader("Content-Length") << " bytes."; coreutils::Log(coreutils::LOG_DEBUG_2) << "End of header with Content Type = " << getHeader("Content-Type") << " for " << getHeader("Content-Length") << " bytes.";
std::string type = getHeader("Content-Type"); std::string type = getHeader("Content-Type");
if(type.size() == 0) if(type.size() == 0)
type = "text/plain"; type = "text/plain";
if(type == "multipart/form-data") if(type == "multipart/form-data")
body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary")); body = new IMFMultipart(in, getHeaderKeyPairValue("Content-Type", "boundary"));
else if(type == "text/plain") else if(type == "text/plain")
body = new IMFPlainText(in); body = new IMFPlainText(in);
else else
body = new IMFBody(); body = new IMFBody();
return false; return false;
} }
void IMFMessage::output(std::stringstream &out) { void IMFMessage::output(std::stringstream &out) {
for(IMFHeader header: headers) { for(IMFHeader header: headers) {
out << header.getKey() << ": " << header.getValue() << "\r\n"; out << header.getKey() << ": " << header.getValue() << "\r\n";
} }
out << "\r\n"; out << "\r\n";
} }
void IMFMessage::addHeader(std::string key, std::string value) { void IMFMessage::addHeader(std::string key, std::string value) {
headers.emplace_back(key, value); headers.emplace_back(key, value);
} }
std::string IMFMessage::getHeader(std::string key, bool valueOnly) { std::string IMFMessage::getHeader(std::string key, bool valueOnly) {
for(IMFHeader header: headers) { for(IMFHeader header: headers) {
if(header.getKey() == key) { if(header.getKey() == key) {
@ -57,13 +67,13 @@ namespace coreutils {
if(valueOnly) { if(valueOnly) {
std::vector<PString> split = PString(value).split(";"); std::vector<PString> split = PString(value).split(";");
value = split[0].str(); value = split[0].str();
} }
return value; return value;
} }
} }
return std::string(""); return std::string("");
} }
std::string IMFMessage::getHeaderKeyPairValue(std::string headerKey, std::string key) { std::string IMFMessage::getHeaderKeyPairValue(std::string headerKey, std::string key) {
std::string value; std::string value;
coreutils::PString psource(getHeader(headerKey, false)); coreutils::PString psource(getHeader(headerKey, false));
@ -75,30 +85,30 @@ namespace coreutils {
if(token == key) { if(token == key) {
if(work.ifNext("\"")) { if(work.ifNext("\"")) {
value = work.getTokenExclude("\""); value = work.getTokenExclude("\"");
return value; return value;
} }
else { else {
value = work.str(); value = work.str();
return value; return value;
} }
} }
else else
continue; continue;
} else if (work.ifNext(";") || work.ifNext(" ")) { } else if (work.ifNext(";") || work.ifNext(" ")) {
if(token == key) if(token == key)
return std::string("true"); return std::string("true");
else else
continue; continue;
} }
} }
return std::string("false"); return std::string("false");
} }
IMFBody * IMFMessage::getBody() { IMFBody * IMFMessage::getBody() {
return body; return body;
} }
} }

View File

@ -9,29 +9,32 @@
namespace coreutils { namespace coreutils {
class IMFMessage { class IMFMessage {
public: public:
IMFMessage(); IMFMessage();
IMFMessage(PString &in); IMFMessage(PString &in);
bool parse(PString &in); bool parse(PString &in);
void output(std::stringstream &out); void output(std::stringstream &out);
IMFRequest *request;
void addHeader(std::string key, std::string value); void addHeader(std::string key, std::string value);
std::string getHeader(std::string key, bool valueOnly = true); std::string getHeader(std::string key, bool valueOnly = true);
std::string getHeaderKeyPairValue(std::string headerKey, std::string key); std::string getHeaderKeyPairValue(std::string headerKey, std::string key);
IMFBody *getBody(); IMFBody *getBody();
protected: protected:
IMFRequest request;
std::vector<IMFHeader> headers; std::vector<IMFHeader> headers;
IMFBody *body; IMFBody *body;
private:
bool first = true;
}; };
} }
#endif #endif

View File

@ -3,46 +3,37 @@
#include "Log.h" #include "Log.h"
namespace coreutils { namespace coreutils {
IMFRequest::IMFRequest() {} IMFRequest::IMFRequest() {}
IMFRequest::IMFRequest(PString &in) { IMFRequest::IMFRequest(PString &in) {
method = in.getTokenExclude(" "); parse(in);
if(!in.ifNext(" "))
throw coreutils::Exception("Expecting space after method.");
uri = in.getTokenExclude(" ");
if(!in.ifNext(" "))
throw coreutils::Exception("Expecting space after URI.");
protocol = in.str();
}
std::string IMFRequest::getMethod() {
return method;
}
void IMFRequest::setMethod(std::string method) {
this->method = method;
}
std::string IMFRequest::getURI() {
return uri;
}
void IMFRequest::setURI(std::string uri) {
this->uri = uri;
}
std::string IMFRequest::getProtocol() {
return protocol;
}
void IMFRequest::setProtocol(std::string protocol) {
this->protocol = protocol;
} }
void IMFRequest::output(std::stringstream &out) { bool IMFRequest::parse(PString &in) {
out << method << " " << uri << " " << protocol << "\r\n"; parts = in.split(" ");
return parts.size() == 3;
}
std::string IMFRequest::getMethod() {
if(parts.size() == 3)
return parts[0].str();
else
return NULL;
}
std::string IMFRequest::getURI() {
if(parts.size() == 3)
return parts[1].str();
else
return NULL;
}
std::string IMFRequest::getProtocol() {
if(parts.size() == 3)
return parts[2].str();
else
return NULL;
} }
} }

View File

@ -11,22 +11,17 @@ namespace coreutils {
IMFRequest(); IMFRequest();
IMFRequest(PString &in); IMFRequest(PString &in);
bool parse(PString &in);
std::string getMethod(); std::string getMethod();
void setMethod(std::string method);
std::string getURI(); std::string getURI();
void setURI(std::string uri);
std::string getProtocol(); std::string getProtocol();
void setProtocol(std::string protocol);
void output(std::stringstream &out);
private: private:
std::string method; std::vector<PString> parts;
std::string uri;
std::string protocol;
}; };
} }
#endif #endif

View File

@ -5,7 +5,7 @@ namespace coreutils {
PString::PString() { PString::PString() {
} }
PString::PString(std::string pstring) { PString::PString(std::string pstring) {
this->pstring = pstring; this->pstring = pstring;
} }
@ -13,29 +13,29 @@ namespace coreutils {
std::vector<PString> & PString::getList() { std::vector<PString> & PString::getList() {
return list; return list;
} }
std::string PString::str() { std::string PString::str() {
return pstring.substr(cursor); return pstring.substr(cursor);
} }
std::vector<PString> PString::split(std::string delimiter, int maxSize) { std::vector<PString> PString::split(std::string delimiter, int maxSize) {
list.clear(); list.clear();
if(pstring.size() == 0) { if(pstring.size() == 0) {
list.push_back(PString("")); list.push_back(PString(""));
return list; return list;
} }
size_t end; size_t end;
size_t pos = cursor; size_t pos = cursor;
while(pos < pstring.length()) { while(pos < pstring.length()) {
end = pstring.find(delimiter, pos); end = pstring.find(delimiter, pos);
if(end == -1) if(end == -1)
end = pstring.length(); end = pstring.length();
list.push_back(PString(pstring.substr(pos, end - pos))); list.push_back(PString(pstring.substr(pos, end - pos)));
pos = end + delimiter.size(); pos = end + delimiter.size();
if(pos > pstring.size()) if(pos > pstring.size())
break; break;
if(maxSize != 0) { if(maxSize != 0) {
if(list.size() >= maxSize) { if(list.size() >= maxSize) {
@ -43,42 +43,42 @@ namespace coreutils {
break; break;
} }
} }
} }
return list; return list;
} }
std::string PString::getTokenInclude(std::string include) { std::string PString::getTokenInclude(std::string include) {
int start = cursor; int start = cursor;
cursor = pstring.find_first_not_of(include, cursor); cursor = pstring.find_first_not_of(include, cursor);
return pstring.substr(start, cursor - start); return pstring.substr(start, cursor - start);
} }
std::string PString::getTokenExclude(std::string exclude) { std::string PString::getTokenExclude(std::string exclude) {
int start = cursor; int start = cursor;
cursor = pstring.find_first_of(exclude, cursor); cursor = pstring.find_first_of(exclude, cursor);
if(cursor == -1) if(cursor == -1)
cursor = pstring.size(); cursor = pstring.size();
return pstring.substr(start, cursor - start); return pstring.substr(start, cursor - start);
} }
coreutils::PString PString::operator[](int index) { coreutils::PString PString::operator[](int index) {
return list[index]; return list[index];
} }
bool PString::eod() { bool PString::eod() {
return cursor >= pstring.size(); return cursor >= pstring.size();
} }
bool PString::ifNext(std::string value) { bool PString::ifNext(std::string value) {
bool test = (value == pstring.substr(cursor, value.length())); bool test = (value == pstring.substr(cursor, value.length()));
if(test) if(test)
cursor += value.size(); cursor += value.size();
return test; return test;
} }
int PString::skipWhitespace() { int PString::skipWhitespace() {
size_t temp = cursor; size_t temp = cursor;
cursor = pstring.find_first_not_of(" \t", cursor); cursor = pstring.find_first_not_of(" \t", cursor);
cursor = cursor == -1 ? temp: cursor; cursor = cursor == -1 ? temp: cursor;
coreutils::Log(coreutils::LOG_DEBUG_2) << "Skipping whitespace for " << (cursor - temp) << " spaces."; coreutils::Log(coreutils::LOG_DEBUG_2) << "Skipping whitespace for " << (cursor - temp) << " spaces.";
return cursor - temp; return cursor - temp;

View File

@ -7,101 +7,101 @@ namespace coreutils {
/// ///
/// Use the PString object when advanced parsing algorithms are required to simplify /// Use the PString object when advanced parsing algorithms are required to simplify
/// parsing. Two methods of parsing are available within the PString object. One /// parsing. Two methods of parsing are available within the PString object. One
/// method involves using the split method to divide the string into several PString /// method involves using the split method to divide the string into several PString
/// objects. Split separates the string based upon a delimiter. Another method employs /// objects. Split separates the string based upon a delimiter. Another method employs
/// the use of the getToken methods provided to move to through the string and assign /// the use of the getToken methods provided to move to through the string and assign
/// values based upon the followed path. /// values based upon the followed path.
/// ///
class PString { class PString {
public: public:
/// ///
/// ///
/// ///
PString(); PString();
/// ///
/// ///
/// ///
PString(std::string pstring); PString(std::string pstring);
/// ///
/// ///
/// ///
std::vector<PString> & getList(); std::vector<PString> & getList();
/// ///
/// ///
/// ///
std::string str(); std::string str();
///
///
///
std::vector<PString> split(std::string delimiter, int maxSize = 0);
/// ///
/// ///
/// ///
std::vector<PString> split(std::string delimiter, int maxSize = 0);
///
///
///
std::string getTokenInclude(std::string include); std::string getTokenInclude(std::string include);
/// ///
/// ///
/// ///
std::string getTokenExclude(std::string exclude); std::string getTokenExclude(std::string exclude);
/// ///
/// ///
/// ///
coreutils::PString operator[](int index); coreutils::PString operator[](int index);
/// ///
/// ///
/// ///
bool eod(); bool eod();
/// ///
/// ///
/// ///
bool ifNext(std::string value); bool ifNext(std::string value);
/// ///
/// ///
/// ///
int skipWhitespace(); int skipWhitespace();
/// ///
/// ///
/// ///
int cursor = 0; int cursor = 0;
/// ///
/// ///
/// ///
int size(); int size();
private: private:
std::string pstring; std::string pstring;
std::vector<PString> list; std::vector<PString> list;
}; };
} }
#endif #endif

10
compile
View File

@ -3,17 +3,17 @@
for file in *.cpp for file in *.cpp
do do
filename="${file%.*}" filename="${file%.*}"
list="$list $filename.o" list="$list $filename.o"
echo -n "Compiling $filename..." echo -n "Compiling $filename..."
g++ -g -c $file g++ -g -c $file
if [ $? = '0' ] if [ $? = '0' ]
then then
echo "OK" echo "OK"
else else
echo "ERROR" echo "ERROR"
exit -1 exit -1
fi fi
done done
wait wait
@ -25,6 +25,6 @@ then
else else
echo "ERROR" echo "ERROR"
exit -1 exit -1
fi fi