Skip to content

Commit 8aeaa9c

Browse files
author
Sebastiano Merlino
committed
Added direct support to Set-Cookie in response
1 parent 0f12cf6 commit 8aeaa9c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/http_response.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "http_response.hpp"
2424

2525
#include <iostream>
26+
#include <sstream>
2627

2728
using namespace std;
2829

@@ -107,6 +108,20 @@ void http_response::decorate_response(MHD_Response* response)
107108
MHD_add_response_header(response, (*it).first.c_str(), (*it).second.c_str());
108109
for (it=footers.begin() ; it != footers.end(); ++it)
109110
MHD_add_response_footer(response, (*it).first.c_str(), (*it).second.c_str());
111+
{
112+
bool first = true;
113+
stringstream ss;
114+
for (it=cookies.begin(); it != cookies.end(); ++it)
115+
{
116+
if(!first)
117+
ss << "; ";
118+
else
119+
first = false;
120+
ss << (*it).first << "=" << (*it).second;
121+
}
122+
if(!first)
123+
MHD_add_response_header(response, "Set-Cookie", ss.str().c_str());
124+
}
110125
}
111126

112127
void cache_response::decorate_response(MHD_Response* response)

src/httpserver/http_response.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class http_response
163163
filename(b.filename),
164164
headers(b.headers),
165165
footers(b.footers),
166+
cookies(b.cookies),
166167
topics(b.topics),
167168
keepalive_secs(b.keepalive_secs),
168169
keepalive_msg(b.keepalive_msg),
@@ -184,6 +185,7 @@ class http_response
184185
filename = b.filename;
185186
headers = b.headers;
186187
footers = b.footers;
188+
cookies = b.cookies;
187189
topics = b.topics;
188190
keepalive_secs = b.keepalive_secs;
189191
keepalive_msg = b.keepalive_msg;
@@ -258,6 +260,14 @@ class http_response
258260
{
259261
result = this->footers[key];
260262
}
263+
const std::string get_cookie(const std::string& key)
264+
{
265+
return this->cookies[key];
266+
}
267+
void get_cookie(const std::string& key, std::string& result)
268+
{
269+
result = this->cookies[key];
270+
}
261271
/**
262272
* Method used to set an header value by key.
263273
* @param key The name identifying the header
@@ -276,6 +286,10 @@ class http_response
276286
{
277287
this->footers[key] = value;
278288
}
289+
void set_cookie(const std::string& key, const std::string& value)
290+
{
291+
this->cookies[key] = value;
292+
}
279293
/**
280294
* Method used to set the content type for the request. This is a shortcut of setting the corresponding header.
281295
* @param content_type the content type to use for the request
@@ -292,6 +306,14 @@ class http_response
292306
{
293307
this->headers.erase(key);
294308
}
309+
void remove_footer(const std::string& key)
310+
{
311+
this->footers.erase(key);
312+
}
313+
void remove_cookie(const std::string& key)
314+
{
315+
this->cookies.erase(key);
316+
}
295317
/**
296318
* Method used to get all headers passed with the request.
297319
* @return a map<string,string> containing all headers.
@@ -309,6 +331,11 @@ class http_response
309331
size_t get_footers(std::vector<std::pair<std::string, std::string> >& result);
310332
#ifndef SWIG
311333
size_t get_footers(std::map<std::string, std::string, header_comparator>& result);
334+
#endif
335+
const std::vector<std::pair<std::string, std::string> > get_cookies();
336+
size_t get_cookies(std::vector<std::pair<std::string, std::string> >& result);
337+
#ifndef SWIG
338+
size_t get_cookies(std::map<std::string, std::string, header_comparator>& result);
312339
#endif
313340
/**
314341
* Method used to set all headers of the response.
@@ -330,6 +357,12 @@ class http_response
330357
for(it = footers.begin(); it != footers.end(); ++it)
331358
this->footers[it->first] = it->second;
332359
}
360+
void set_cookies(const std::map<std::string, std::string>& cookies)
361+
{
362+
std::map<std::string, std::string>::const_iterator it;
363+
for(it = cookies.begin(); it != cookies.end(); ++it)
364+
this->cookies[it->first] = it->second;
365+
}
333366
/**
334367
* Method used to set the response code of the response
335368
* @param response_code the response code to set
@@ -392,6 +425,7 @@ class http_response
392425
std::string filename;
393426
std::map<std::string, std::string, header_comparator> headers;
394427
std::map<std::string, std::string, header_comparator> footers;
428+
std::map<std::string, std::string, header_comparator> cookies;
395429
std::vector<std::string> topics;
396430
int keepalive_secs;
397431
std::string keepalive_msg;

0 commit comments

Comments
 (0)