Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content type and length are headers
Browse files Browse the repository at this point in the history
mathieucarbou committed Oct 15, 2024
1 parent 8311339 commit 7c4b380
Showing 2 changed files with 31 additions and 26 deletions.
32 changes: 18 additions & 14 deletions src/PsychicResponse.cpp
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@
PsychicResponse::PsychicResponse(PsychicRequest* request) : _request(request),
_code(200),
_status(""),
_contentType(emptyString),
_contentLength(0),
_body("")
{
// get our global headers out of the way
@@ -33,6 +31,22 @@ void PsychicResponse::addHeader(const char* field, const char* value)
_headers.push_back({field, value});
}

const String& PsychicResponse::getHeader(const char* field) {
for (auto& header : _headers) {
if (header.field.equalsIgnoreCase(field))
return header.value;
}
return emptyString;
}

bool PsychicResponse::hasHeader(const char* field) {
for (auto& header : _headers) {
if (header.field.equalsIgnoreCase(field))
return true;
}
return false;
}

void PsychicResponse::setCookie(const char* name, const char* value, unsigned long secondsFromNow, const char* extras)
{
time_t now = time(nullptr);
@@ -67,11 +81,6 @@ void PsychicResponse::setCode(int code)
_code = code;
}

void PsychicResponse::setContentType(const char* contentType)
{
_contentType = contentType;
}

void PsychicResponse::setContent(const char* content)
{
_body = content;
@@ -89,19 +98,14 @@ const char* PsychicResponse::getContent()
return _body;
}

size_t PsychicResponse::getContentLength()
{
return _contentLength;
}

esp_err_t PsychicResponse::send()
{
// esp-idf makes you set the whole status.
sprintf(_status, "%u %s", _code, http_status_reason(_code));
httpd_resp_set_status(_request->request(), _status);

// set the content type
httpd_resp_set_type(_request->request(), _contentType.c_str());
httpd_resp_set_type(_request->request(), getContentType().c_str());

// our headers too
this->sendHeaders();
@@ -162,7 +166,7 @@ esp_err_t PsychicResponse::send(const char* content)
{
if (!_code)
setCode(200);
if (_contentType.isEmpty())
if (!hasHeader("Content-Type"))
setContentType("text/html");
setContent(content);
return send();
25 changes: 13 additions & 12 deletions src/PsychicResponse.h
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@ class PsychicResponse
int _code;
char _status[60];
std::list<HTTPHeader> _headers;
String _contentType;
int64_t _contentLength;
const char* _body;

public:
@@ -26,23 +24,24 @@ class PsychicResponse

void setCode(int code);
int getCode() { return _code; }

void setContentType(const char* contentType);
String& getContentType() { return _contentType; }

void setContentLength(int64_t contentLength) { _contentLength = contentLength; }
int64_t getContentLength(int64_t contentLength) { return _contentLength; }
void setContentType(const char* contentType) { addHeader("Content-Type", contentType); }
const String& getContentType() { return getHeader("Content-Type"); }

void setContentLength(size_t contentLength) { addHeader("Content-Length", String(contentLength).c_str()); }
size_t getContentLength() { return strtoul(getHeader("Content-Length").c_str(), nullptr, 10); }

void addHeader(const char* field, const char* value);
std::list<HTTPHeader>& headers() { return _headers; }
bool hasHeader(const char* field);
const String& getHeader(const char* field);

void setCookie(const char* key, const char* value, unsigned long max_age = 60 * 60 * 24 * 30, const char* extras = "");

void setContent(const char* content);
void setContent(const uint8_t* content, size_t len);

const char* getContent();
size_t getContentLength();

virtual esp_err_t send();
void sendHeaders();
@@ -74,20 +73,22 @@ class PsychicResponseDelegate
void setCode(int code) { _response->setCode(code); }

void setContentType(const char* contentType) { _response->setContentType(contentType); }
String& getContentType() { return _response->getContentType(); }
const String& getContentType() { return _response->getContentType(); }

void setContentLength(int64_t contentLength) { _response->setContentLength(contentLength); }
int64_t getContentLength(int64_t contentLength) { return _response->getContentLength(); }
void setContentLength(size_t contentLength) { _response->setContentLength(contentLength); }
size_t getContentLength() { return _response->getContentLength(); }

void addHeader(const char* field, const char* value) { _response->addHeader(field, value); }
std::list<HTTPHeader>& headers() { return _response->headers(); }
bool hasHeader(const char* field) { return _response->hasHeader(field); }
const String& getHeader(const char* field) { return _response->getHeader(field); }

void setCookie(const char* key, const char* value, unsigned long max_age = 60 * 60 * 24 * 30, const char* extras = "") { _response->setCookie(key, value, max_age, extras); }

void setContent(const char* content) { _response->setContent(content); }
void setContent(const uint8_t* content, size_t len) { _response->setContent(content, len); }

const char* getContent() { return _response->getContent(); }
size_t getContentLength() { return _response->getContentLength(); }

esp_err_t send() { return _response->send(); }
void sendHeaders() { _response->sendHeaders(); }

0 comments on commit 7c4b380

Please sign in to comment.