diff --git a/src/PsychicResponse.cpp b/src/PsychicResponse.cpp index 8379592..ed24414 100644 --- a/src/PsychicResponse.cpp +++ b/src/PsychicResponse.cpp @@ -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,11 +98,6 @@ 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. @@ -101,7 +105,7 @@ esp_err_t PsychicResponse::send() 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(); diff --git a/src/PsychicResponse.h b/src/PsychicResponse.h index 0b8728f..ac82611 100644 --- a/src/PsychicResponse.h +++ b/src/PsychicResponse.h @@ -14,8 +14,6 @@ class PsychicResponse int _code; char _status[60]; std::list _headers; - String _contentType; - int64_t _contentLength; const char* _body; public: @@ -26,15 +24,17 @@ 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& 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 = ""); @@ -42,7 +42,6 @@ class PsychicResponse void setContent(const uint8_t* content, size_t len); const char* getContent(); - size_t getContentLength(); virtual esp_err_t send(); void sendHeaders(); @@ -74,12 +73,15 @@ 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& 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); } @@ -87,7 +89,6 @@ class PsychicResponseDelegate 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(); }