Skip to content

Commit db3928a

Browse files
author
Sebastiano Merlino
committed
Merge branch 'master' of https://github.com/etr/libhttpserver into test
2 parents dbb14fb + 9b99bac commit db3928a

File tree

5 files changed

+441
-118
lines changed

5 files changed

+441
-118
lines changed

src/http_response.cpp

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ using namespace std;
2929
namespace httpserver
3030
{
3131

32+
cache_response::~cache_response()
33+
{
34+
if(locked_element)
35+
ws->unlock_cache_element(content);
36+
}
37+
3238
const std::vector<std::pair<std::string, std::string> > http_response::get_headers()
3339
{
3440
std::vector<std::pair<std::string, std::string> > to_ret;
@@ -83,23 +89,62 @@ shoutCAST_response::shoutCAST_response
8389
{
8490
}
8591

86-
void http_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
92+
void http_response::get_raw_response(MHD_Response** response, webserver* ws)
8793
{
8894
size_t size = &(*content.end()) - &(*content.begin());
8995
*response = MHD_create_response_from_buffer(size, (void*) content.c_str(), MHD_RESPMEM_PERSISTENT);
9096
}
9197

92-
void http_file_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
98+
void http_response::decorate_response(MHD_Response* response)
99+
{
100+
map<string, string, header_comparator>::iterator it;
101+
for (it=headers.begin() ; it != headers.end(); ++it)
102+
MHD_add_response_header(response, (*it).first.c_str(), (*it).second.c_str());
103+
for (it=footers.begin() ; it != footers.end(); ++it)
104+
MHD_add_response_footer(response, (*it).first.c_str(), (*it).second.c_str());
105+
}
106+
107+
void cache_response::decorate_response(MHD_Response* response)
108+
{
109+
}
110+
111+
int http_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
112+
{
113+
return MHD_queue_response(connection, response_code, response);
114+
}
115+
116+
int http_basic_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
117+
{
118+
return MHD_queue_basic_auth_fail_response(connection, realm.c_str(), response);
119+
}
120+
121+
int http_digest_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
122+
{
123+
return MHD_queue_auth_fail_response(connection, realm.c_str(), opaque.c_str(), response, reload_nonce ? MHD_YES : MHD_NO);
124+
}
125+
126+
void http_file_response::get_raw_response(MHD_Response** response, webserver* ws)
93127
{
94128
char* page = NULL;
95129
size_t size = http::load_file(filename.c_str(), &page);
96130
if(size)
97131
*response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_FREE);
98-
else
99-
*found = false;
132+
//TODO: At the moment if the file does not exist the system returns empty response
133+
}
134+
135+
void cache_response::get_raw_response(MHD_Response** response, webserver* ws)
136+
{
137+
this->ws = ws;
138+
this->locked_element = true;
139+
bool valid;
140+
http_response* r = ws->get_from_cache(content, &valid, true);
141+
r->get_raw_response(response, ws);
142+
r->decorate_response(*response); //It is done here to avoid to search two times for the same element
143+
144+
//TODO: Check if element is not in cache and throw exception
100145
}
101146

102-
void long_polling_receive_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
147+
void long_polling_receive_response::get_raw_response(MHD_Response** response, webserver* ws)
103148
{
104149
#ifdef USE_COMET
105150
this->ws = ws;
@@ -108,7 +153,7 @@ void long_polling_receive_response::get_raw_response(MHD_Response** response, bo
108153
&long_polling_receive_response::data_generator, (void*) this, NULL);
109154
ws->register_to_topics(topics, connection_id, keepalive_secs, keepalive_msg);
110155
#else //USE_COMET
111-
http_response::get_raw_response(response, found, ws);
156+
http_response::get_raw_response(response, ws);
112157
#endif //USE_COMET
113158
}
114159

@@ -130,9 +175,9 @@ ssize_t long_polling_receive_response::data_generator (void* cls, uint64_t pos,
130175
#endif //USE_COMET
131176
}
132177

133-
void long_polling_send_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
178+
void long_polling_send_response::get_raw_response(MHD_Response** response, webserver* ws)
134179
{
135-
http_response::get_raw_response(response, found, ws);
180+
http_response::get_raw_response(response, ws);
136181
#ifdef USE_COMET
137182
ws->send_message_to_topic(send_topic, content);
138183
#endif //USE_COMET
@@ -166,6 +211,9 @@ void clone_response(const http_response& hr, http_response** dhrs)
166211
case(http_response::LONG_POLLING_SEND):
167212
*dhrs = new long_polling_send_response(hr);
168213
return;
214+
case(http_response::CACHED_CONTENT):
215+
*dhrs = new cache_response(hr);
216+
return;
169217
}
170218
}
171219

src/httpserver/http_response.hpp

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ namespace http
4141
class arg_comparator;
4242
};
4343

44+
namespace details
45+
{
46+
struct http_response_ptr;
47+
};
48+
4449
using namespace http;
4550

4651
/**
@@ -61,7 +66,8 @@ class http_response
6166
BASIC_AUTH_FAIL,
6267
SWITCH_PROTOCOL,
6368
LONG_POLLING_RECEIVE,
64-
LONG_POLLING_SEND
69+
LONG_POLLING_SEND,
70+
CACHED_CONTENT
6571
};
6672

6773
/**
@@ -316,10 +322,14 @@ class http_response
316322
std::string send_topic;
317323
struct MHD_Connection* underlying_connection;
318324

319-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
325+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
326+
virtual void decorate_response(MHD_Response* res);
327+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
320328

321329
friend class webserver;
330+
friend struct details::http_response_ptr;
322331
friend void clone_response(const http_response& hr, http_response** dhr);
332+
friend class cache_response;
323333
};
324334

325335
class http_string_response : public http_response
@@ -334,6 +344,8 @@ class http_string_response : public http_response
334344
): http_response(http_response::STRING_CONTENT, content, response_code, content_type, autodelete) { }
335345

336346
http_string_response(const http_response& b) : http_response(b) { }
347+
private:
348+
friend class webserver;
337349
};
338350

339351
class http_byte_response : public http_response
@@ -347,6 +359,8 @@ class http_byte_response : public http_response
347359
const std::string& content_type = "text/plain",
348360
bool autodelete = true
349361
): http_response(http_response::STRING_CONTENT, std::string(content, content_length), response_code, content_type, autodelete) { }
362+
private:
363+
friend class webserver;
350364
};
351365

352366
class http_file_response : public http_response
@@ -364,7 +378,9 @@ class http_file_response : public http_response
364378

365379
http_file_response(const http_response& b) : http_response(b) { }
366380
protected:
367-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
381+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
382+
private:
383+
friend class webserver;
368384
};
369385

370386
class http_basic_auth_fail_response : public http_response
@@ -381,6 +397,10 @@ class http_basic_auth_fail_response : public http_response
381397
) : http_response(http_response::BASIC_AUTH_FAIL, content, response_code, content_type, autodelete, realm) { }
382398

383399
http_basic_auth_fail_response(const http_response& b) : http_response(b) { }
400+
protected:
401+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
402+
private:
403+
friend class webserver;
384404
};
385405

386406
class http_digest_auth_fail_response : public http_response
@@ -400,6 +420,10 @@ class http_digest_auth_fail_response : public http_response
400420
}
401421

402422
http_digest_auth_fail_response(const http_response& b) : http_response(b) { }
423+
protected:
424+
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
425+
private:
426+
friend class webserver;
403427
};
404428

405429
class shoutCAST_response : public http_response
@@ -414,6 +438,8 @@ class shoutCAST_response : public http_response
414438
);
415439

416440
shoutCAST_response(const http_response& b) : http_response(b) { }
441+
private:
442+
friend class webserver;
417443
};
418444

419445
class switch_protocol_response : public http_response
@@ -429,7 +455,9 @@ class switch_protocol_response : public http_response
429455
{
430456
}
431457
protected:
432-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0) {}
458+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0) {}
459+
private:
460+
friend class webserver;
433461
};
434462

435463
class long_polling_receive_response : public http_response
@@ -450,12 +478,12 @@ class long_polling_receive_response : public http_response
450478

451479
long_polling_receive_response(const http_response& b) : http_response(b) { }
452480
protected:
453-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
481+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
454482
private:
455483
static ssize_t data_generator (void* cls, uint64_t pos, char* buf, size_t max);
456-
457484
int connection_id;
458-
webserver* ws;
485+
httpserver::webserver* ws;
486+
friend class webserver;
459487
};
460488

461489
class long_polling_send_response : public http_response
@@ -472,7 +500,34 @@ class long_polling_send_response : public http_response
472500

473501
long_polling_send_response(const http_response& b) : http_response(b) { }
474502
protected:
475-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
503+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
504+
private:
505+
friend class webserver;
506+
};
507+
508+
class cache_response : public http_response
509+
{
510+
public:
511+
cache_response
512+
(
513+
const std::string& key
514+
) : http_response(http_response::CACHED_CONTENT, key),
515+
ws(0x0),
516+
locked_element(false)
517+
{
518+
}
519+
520+
cache_response(const http_response& b) : http_response(b) { }
521+
522+
~cache_response();
523+
524+
protected:
525+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
526+
virtual void decorate_response(MHD_Response* res);
527+
private:
528+
webserver* ws;
529+
bool locked_element;
530+
friend class webserver;
476531
};
477532

478533
void clone_response(http_response* hr, http_response** dhr);

src/httpserver/modded_request.hpp

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)