Get header value in c++ - Smart pointer (http)
Posted: Wed Jul 20, 2022 8:23 am
I'm working in c++ and I have a small question of how to write the `get_header` method in the http protocol.
I want to create a small helper function
I want to just use it as
But preferably as a smart pointer such that it will free up the resources automatically instead of calling delete manually every time, which could be prone to human errors.
Or as a sd::string. Whatever is more performant.
Thanks for your help.
I want to create a small helper function
Code: Select all
/**
* @param [in] req
* @param [in] key The header key on which we want the value back
* @param [out] buf The header value written to.
**/
esp_err_t get_header(httpd_req_t *req, const char *key, char *buf) {
size_t buf_len = httpd_req_get_hdr_value_len(req, key) + 1;
if (buf_len > 1) {
buf = new char[buf_len];
if (!buf) {
ESP_LOGE(TAG, "No enough memory for basic authorization");
return ESP_ERR_NO_MEM;
}
return httpd_req_get_hdr_value_str(req, key, buf, buf_len);
}
return ESP_ERR_NOT_FOUND ;
}
Code: Select all
char *header_value{nullptr};
esp_err_t status = get_header(req, "Authorization", header_value);
// use it outside
if (header_value ..... // makes a condition on the header value
Or as a sd::string. Whatever is more performant.
Thanks for your help.