some ideas of updates delivery
This commit is contained in:
parent
4df8d4319e
commit
2e01fe8d67
6 changed files with 108 additions and 18 deletions
|
@ -11,6 +11,7 @@ constexpr static const char* SERVER_NAME("SERVER_NAME");
|
|||
constexpr static const char* CONTENT_TYPE("CONTENT_TYPE");
|
||||
constexpr static const char* CONTENT_LENGTH("CONTENT_LENGTH");
|
||||
constexpr static const char* AUTHORIZATION("HTTP_AUTHORIZATION");
|
||||
constexpr static const char* QUERY_STRING("QUERY_STRING");
|
||||
|
||||
constexpr static const char* urlEncoded("application/x-www-form-urlencoded");
|
||||
|
||||
|
@ -33,7 +34,8 @@ Request::Request ():
|
|||
state(State::initial),
|
||||
raw(),
|
||||
response(nullptr),
|
||||
path()
|
||||
path(),
|
||||
cachedMethod(Method::unknown)
|
||||
{}
|
||||
|
||||
Request::~Request() {
|
||||
|
@ -68,10 +70,15 @@ std::string_view Request::methodName() const {
|
|||
}
|
||||
|
||||
Request::Method Request::method() const {
|
||||
if (cachedMethod != Method::unknown)
|
||||
return cachedMethod;
|
||||
|
||||
std::string_view method = methodName();
|
||||
for (const auto& pair : methods) {
|
||||
if (pair.first == method)
|
||||
if (pair.first == method) {
|
||||
cachedMethod = pair.second;
|
||||
return pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
return Request::Method::unknown;
|
||||
|
@ -227,20 +234,36 @@ unsigned int Request::contentLength() const {
|
|||
if (!active())
|
||||
throw std::runtime_error("An attempt to read content length of a request in a wrong state");
|
||||
|
||||
return atoi(FCGX_GetParam(CONTENT_LENGTH, raw.envp));
|
||||
char* cl = FCGX_GetParam(CONTENT_LENGTH, raw.envp);
|
||||
if (cl != nullptr)
|
||||
return atoi(cl);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> Request::getForm() const {
|
||||
if (!active())
|
||||
throw std::runtime_error("An attempt to read form of a request in a wrong state");
|
||||
|
||||
switch (Request::method()) {
|
||||
case Method::get:
|
||||
return urlDecodeAndParse(FCGX_GetParam(QUERY_STRING, raw.envp));
|
||||
break;
|
||||
case Method::post:
|
||||
return getFormPOST();
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> Request::getFormPOST () const {
|
||||
std::map<std::string, std::string> result;
|
||||
std::string_view contentType(FCGX_GetParam(CONTENT_TYPE, raw.envp));
|
||||
if (contentType.empty())
|
||||
return result;
|
||||
|
||||
unsigned int length = contentLength();
|
||||
if (contentType.find(urlEncoded) != std::string_view::npos) {
|
||||
unsigned int length = contentLength();
|
||||
std::string postData(length, '\0');
|
||||
FCGX_GetStr(&postData[0], length, raw.in);
|
||||
result = urlDecodeAndParse(postData);
|
||||
|
|
|
@ -69,10 +69,12 @@ private:
|
|||
OStream getErrorStream();
|
||||
void responseIsComplete();
|
||||
void terminate();
|
||||
std::map<std::string, std::string> getFormPOST() const;
|
||||
|
||||
private:
|
||||
State state;
|
||||
FCGX_Request raw;
|
||||
std::unique_ptr<Response> response;
|
||||
std::string path;
|
||||
mutable Method cachedMethod;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue