cookie_parser & query_string: use std::unique_ptr instead of raw pointers.

This commit is contained in:
Luca Schlecker 2022-06-08 15:15:41 +02:00 committed by Farook Al-Sammarraie
parent d3e19a53ab
commit 126d79dc38
2 changed files with 15 additions and 27 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <iomanip>
#include <memory>
#include "crow/utility.h"
#include "crow/http_request.h"
#include "crow/http_response.h"
@ -64,7 +65,7 @@ namespace crow
if (expires_at_)
{
ss << DIVIDER << "Expires="
<< std::put_time(expires_at_, HTTP_DATE_FORMAT);
<< std::put_time(expires_at_.get(), HTTP_DATE_FORMAT);
}
if (max_age_)
{
@ -92,16 +93,14 @@ namespace crow
// Expires attribute
Cookie& expires(const std::tm& time)
{
delete expires_at_;
expires_at_ = new std::tm(time);
expires_at_ = std::unique_ptr<std::tm>(new std::tm(time));
return *this;
}
// Max-Age attribute
Cookie& max_age(long long seconds)
{
delete max_age_;
max_age_ = new long long(seconds);
max_age_ = std::unique_ptr<long long>(new long long(seconds));
return *this;
}
@ -136,18 +135,10 @@ namespace crow
// SameSite attribute
Cookie& same_site(SameSitePolicy ssp)
{
delete same_site_;
same_site_ = new SameSitePolicy(ssp);
same_site_ = std::unique_ptr<SameSitePolicy>(new SameSitePolicy(ssp));
return *this;
}
~Cookie()
{
delete max_age_;
delete expires_at_;
delete same_site_;
}
Cookie(const Cookie& c):
key_(c.key_),
value_(c.value_),
@ -157,13 +148,13 @@ namespace crow
httponly_(c.httponly_)
{
if (c.max_age_)
max_age_ = new long long(*c.max_age_);
max_age_ = std::unique_ptr<long long>(new long long(*c.max_age_));
if (c.expires_at_)
expires_at_ = new std::tm(*c.expires_at_);
expires_at_ = std::unique_ptr<std::tm>(new std::tm(*c.expires_at_));
if (c.same_site_)
same_site_ = new SameSitePolicy(*c.same_site_);
same_site_ = std::unique_ptr<SameSitePolicy>(new SameSitePolicy(*c.same_site_));
}
private:
@ -181,13 +172,13 @@ namespace crow
private:
std::string key_;
std::string value_;
long long* max_age_{nullptr};
std::unique_ptr<long long> max_age_{};
std::string domain_ = "";
std::string path_ = "";
bool secure_ = false;
bool httponly_ = false;
std::tm* expires_at_{nullptr};
SameSitePolicy* same_site_{nullptr};
std::unique_ptr<std::tm> expires_at_{};
std::unique_ptr<SameSitePolicy> same_site_{};
static constexpr const char* DIVIDER = "; ";
};

View File

@ -6,6 +6,7 @@
#include <vector>
#include <unordered_map>
#include <iostream>
#include <memory>
namespace crow
{
@ -202,7 +203,7 @@ inline char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int
return nullptr;
}
inline std::pair<std::string, std::string>* qs_dict_name2kv(const char * dict_name, char * const * qs_kv, int qs_kv_size, int nth = 0)
inline std::unique_ptr<std::pair<std::string, std::string>> qs_dict_name2kv(const char * dict_name, char * const * qs_kv, int qs_kv_size, int nth = 0)
{
int i;
size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close;
@ -231,7 +232,7 @@ inline std::pair<std::string, std::string>* qs_dict_name2kv(const char * dict_na
{
auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open);
auto value = std::string(qs_kv[i] + skip_to_eq);
return new std::pair<std::string, std::string>(key, value);
return std::unique_ptr<std::pair<std::string, std::string>>(new std::pair<std::string, std::string>(key, value));
}
else
{
@ -446,14 +447,10 @@ namespace crow
int count = 0;
while (1)
{
auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++);
if (element)
if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++))
ret.insert(*element);
else
break;
delete element;
}
return ret;
}