Browse Source

new interface add_header

master
Artyom Beilis 8 years ago
parent
commit
61e1e4aa37
2 changed files with 24 additions and 5 deletions
  1. +6
    -0
      cppcms/http_response.h
  2. +18
    -5
      src/http_response.cpp

+ 6
- 0
cppcms/http_response.h View File

@@ -226,6 +226,12 @@ namespace http {
void erase_header(std::string const &h);

///
/// add header - independently of set_header/get_header/erase_header - allows
/// to specify multiple headers of same type like Set-Cookie or WWW-Authenticate
///
void add_header(std::string const &name,std::string const &value);

///
/// This function set HTTP Content-Type header, but unlike contet_type function it also adds
/// current locale charset encoding (unless localization.disable_charset_in_content_type set to true in configuration)
///


+ 18
- 5
src/http_response.cpp View File

@@ -28,6 +28,7 @@
#include <streambuf>
#include <iterator>
#include <map>
#include <list>
#include <stdio.h>

#ifndef CPPCMS_NO_GZIP
@@ -318,7 +319,7 @@ struct response::_data {
typedef bool (*compare_type)(std::string const &left,std::string const &right);
typedef std::map<std::string,std::string,compare_type> headers_type;
headers_type headers;
std::vector<cookie> cookies;
std::list<std::string> added_headers;

details::copy_buf buffered;
details::copy_buf cached;
@@ -388,7 +389,9 @@ void response::set_redirect_header(std::string const &loc,int s)
}
void response::set_cookie(cookie const &cookie)
{
d->cookies.push_back(cookie);
std::ostringstream ss;
ss << cookie;
d->added_headers.push_back(ss.str());
}

void response::set_header(std::string const &name,std::string const &value)
@@ -480,14 +483,24 @@ void response::write_http_headers(std::ostream &out)
continue;
out<<h->first<<": "<<h->second<<"\r\n";
}
for(unsigned i=0;i<d->cookies.size();i++) {
out<<d->cookies[i]<<"\r\n";
for(std::list<std::string>::const_iterator p=d->added_headers.begin();p!=d->added_headers.end();++p) {
out << *p << "\r\n";
}

out<<"\r\n";
out<<std::flush;
}
void response::add_header(std::string const &name,std::string const &value)
{
std::string h;
h.reserve(name.size() + value.size() + 3);
h+=name;
h+=": ";
h+=value;
d->added_headers.push_back(std::string());
d->added_headers.back().swap(h);
}


void response::copy_to_cache()


Loading…
Cancel
Save