#include #include #include #include "../main/MiscSupport.h" class HttpInput { public: enum RequestType { hrtUnknown, hrtGet, hrtPost, hrtPut, hrtHead } requestType; std::string uri; std::string versionString; std::map< std::string, std::string > headers; std::string body; HttpInput() : requestType(hrtUnknown) { } }; class HttpInputHelper { public: enum HttpState { hsAwaitingInitialRequestLine, hsAwaitingHeaders, hsAwaitingBodyWhole, hsAwaitingBodyChunkedSize, hsAwaitingBodyChuckedData, hsAwaitingFooters, hsReadComplete, hsError } state; std::string lastHeader; HttpInputHelper() : state(hsAwaitingInitialRequestLine) { } }; static bool getLine(std::string &newLine, std::string &buffer) { int split = buffer.find('\n'); if (split == buffer.npos) { newLine.clear(); return false; } newLine = buffer.substr(0, split); buffer.erase(0, split+1); int length = newLine.length(); if (length && (newLine[length - 1] == '\r')) { newLine.erase(length - 1); } return true; } static void readHttp(HttpInput &httpInput, HttpInputHelper &httpInputHelper, std::string &buffer) { HttpInputHelper::HttpState &state = httpInputHelper.state; while (true) { switch (state) { case HttpInputHelper::hsAwaitingInitialRequestLine: { std::string initialRequestLine; if (!getLine(initialRequestLine, buffer)) { return; } int break1 = initialRequestLine.find(' '); if (break1 == initialRequestLine.npos) { state = HttpInputHelper::hsError; return; } std::string requestType = initialRequestLine.substr(0, break1); initialRequestLine.erase(0, break1+1); if (requestType == "GET") { httpInput.requestType = HttpInput::hrtGet; } else if (requestType == "POST") { httpInput.requestType = HttpInput::hrtPost; } else if (requestType == "PUT") { httpInput.requestType = HttpInput::hrtPut; } else if (requestType == "HEAD") { httpInput.requestType = HttpInput::hrtHead; } else { httpInput.requestType = HttpInput::hrtUnknown; } std::cout<<"requestType = '" <