Skip to content

Commit

Permalink
Overload hasParam, getParam (add filters) and overload reply and adde…
Browse files Browse the repository at this point in the history
…d beginReply flavors (aka ESPAsyncWS) (#132)

* Overload hasParam and getParam with filters
* Add overload  reply(response) aka ESPAsyncWS and added beginReply() flavors to start a response from a request
  • Loading branch information
mathieucarbou committed Aug 9, 2024
1 parent c299954 commit 1407bf5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/PsychicRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ bool PsychicRequest::hasParam(const char *key)
return getParam(key) != NULL;
}

bool PsychicRequest::hasParam(const char *key, bool isPost, bool isFile)
{
return getParam(key, isPost, isFile) != NULL;
}

PsychicWebParameter * PsychicRequest::getParam(const char *key)
{
for (auto *param : _params)
Expand All @@ -332,6 +337,14 @@ PsychicWebParameter * PsychicRequest::getParam(const char *key)
return NULL;
}

PsychicWebParameter * PsychicRequest::getParam(const char *key, bool isPost, bool isFile)
{
for (auto *param : _params)
if (param->name().equals(key) && isPost == param->isPost() && isFile == param->isFile())
return param;
return NULL;
}

bool PsychicRequest::hasSessionKey(const String& key)
{
return this->_session->find(key) != this->_session->end();
Expand Down Expand Up @@ -539,4 +552,44 @@ esp_err_t PsychicRequest::reply(int code, const char *contentType, const char *c
response.setContent(content);

return response.send();
}
}

esp_err_t PsychicRequest::reply(PsychicResponse* response)
{
esp_err_t err = response->send();
delete response;
return err;
}

PsychicResponse* PsychicRequest::beginReply(int code)
{
PsychicResponse *response = new PsychicResponse(this);
response->setCode(code);
return response;
}

PsychicResponse* PsychicRequest::beginReply(int code, const char *contentType)
{
PsychicResponse *response = new PsychicResponse(this);
response->setCode(code);
response->setContentType(contentType);
return response;
}

PsychicResponse* PsychicRequest::beginReply(int code, const char *contentType, const char *content)
{
PsychicResponse *response = new PsychicResponse(this);
response->setCode(code);
response->setContentType(contentType);
response->setContent(content);
return response;
}

PsychicResponse* PsychicRequest::beginReply(int code, const char *contentType, const uint8_t *content, size_t len)
{
PsychicResponse *response = new PsychicResponse(this);
response->setCode(code);
response->setContentType(contentType);
response->setContent(content, len);
return response;
}
8 changes: 8 additions & 0 deletions src/PsychicRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class PsychicRequest {
PsychicWebParameter * addParam(PsychicWebParameter *param);
PsychicWebParameter * addParam(const String &name, const String &value, bool decode = true, bool post = false);
bool hasParam(const char *key);
bool hasParam(const char *key, bool isPost, bool isFile = false);
PsychicWebParameter * getParam(const char *name);
PsychicWebParameter * getParam(const char *name, bool isPost, bool isFile = false);

const String getFilename();

Expand All @@ -93,6 +95,12 @@ class PsychicRequest {
esp_err_t reply(int code);
esp_err_t reply(const char *content);
esp_err_t reply(int code, const char *contentType, const char *content);
esp_err_t reply(PsychicResponse* response);

PsychicResponse* beginReply(int code);
PsychicResponse* beginReply(int code, const char* contentType);
PsychicResponse* beginReply(int code, const char* contentType, const char* content);
PsychicResponse* beginReply(int code, const char* contentType, const uint8_t* content, size_t len);
};

#endif // PsychicRequest_h

0 comments on commit 1407bf5

Please sign in to comment.