Skip to content

Commit 7068ba6

Browse files
author
Sebastiano Merlino
committed
Used strategy pattern also for header setting and response enqueuing
1 parent 00523b3 commit 7068ba6

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/http_response.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ void http_response::get_raw_response(MHD_Response** response, bool* found, webse
9494
*response = MHD_create_response_from_buffer(size, (void*) content.c_str(), MHD_RESPMEM_PERSISTENT);
9595
}
9696

97+
void http_response::decorate_response(MHD_Response* response)
98+
{
99+
map<string, string, header_comparator>::iterator it;
100+
for (it=headers.begin() ; it != headers.end(); ++it)
101+
MHD_add_response_header(response, (*it).first.c_str(), (*it).second.c_str());
102+
for (it=footers.begin() ; it != footers.end(); ++it)
103+
MHD_add_response_footer(response, (*it).first.c_str(), (*it).second.c_str());
104+
}
105+
106+
int http_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
107+
{
108+
return MHD_queue_response(connection, response_code, response);
109+
}
110+
111+
int http_basic_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
112+
{
113+
return MHD_queue_basic_auth_fail_response(connection, realm.c_str(), response);
114+
}
115+
116+
int http_digest_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
117+
{
118+
return MHD_queue_auth_fail_response(connection, realm.c_str(), opaque.c_str(), response, reload_nonce ? MHD_YES : MHD_NO);
119+
}
120+
97121
void http_file_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
98122
{
99123
char* page = NULL;

src/httpserver/http_response.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class http_response
318318
struct MHD_Connection* underlying_connection;
319319

320320
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
321+
virtual void decorate_response(MHD_Response* res);
322+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
321323

322324
friend class webserver;
323325
friend void clone_response(const http_response& hr, http_response** dhr);
@@ -383,6 +385,8 @@ class http_basic_auth_fail_response : public http_response
383385
) : http_response(http_response::BASIC_AUTH_FAIL, content, response_code, content_type, autodelete, realm) { }
384386

385387
http_basic_auth_fail_response(const http_response& b) : http_response(b) { }
388+
protected:
389+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
386390
};
387391

388392
class http_digest_auth_fail_response : public http_response
@@ -402,6 +406,8 @@ class http_digest_auth_fail_response : public http_response
402406
}
403407

404408
http_digest_auth_fail_response(const http_response& b) : http_response(b) { }
409+
protected:
410+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
405411
};
406412

407413
class shoutCAST_response : public http_response

src/webserver.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,21 +1050,8 @@ int webserver::finalize_answer(MHD_Connection* connection, struct modded_request
10501050
mr->dhrs = dhrs;
10511051
mr->dhrs->underlying_connection = connection;
10521052
dhrs->get_raw_response(&response, &found, this);
1053-
vector<pair<string,string> > response_headers;
1054-
dhrs->get_headers(response_headers);
1055-
vector<pair<string,string> > response_footers;
1056-
dhrs->get_footers(response_footers);
1057-
vector<pair<string,string> >::iterator it;
1058-
for (it=response_headers.begin() ; it != response_headers.end(); ++it)
1059-
MHD_add_response_header(response, (*it).first.c_str(), (*it).second.c_str());
1060-
for (it=response_footers.begin() ; it != response_footers.end(); ++it)
1061-
MHD_add_response_footer(response, (*it).first.c_str(), (*it).second.c_str());
1062-
if(dhrs->response_type == http_response::DIGEST_AUTH_FAIL)
1063-
to_ret = MHD_queue_auth_fail_response(connection, dhrs->get_realm().c_str(), dhrs->get_opaque().c_str(), response, dhrs->need_nonce_reload() ? MHD_YES : MHD_NO);
1064-
else if(dhrs->response_type == http_response::BASIC_AUTH_FAIL)
1065-
to_ret = MHD_queue_basic_auth_fail_response(connection, dhrs->get_realm().c_str(), response);
1066-
else
1067-
to_ret = MHD_queue_response(connection, dhrs->get_response_code(), response);
1053+
dhrs->decorate_response(response);
1054+
to_ret = dhrs->enqueue_response(connection, response);
10681055
MHD_destroy_response (response);
10691056
return to_ret;
10701057
}

0 commit comments

Comments
 (0)