Skip to content

Commit 9b99bac

Browse files
author
Sebastiano Merlino
committed
Finalized work on caching system
1 parent 7068ba6 commit 9b99bac

File tree

5 files changed

+261
-120
lines changed

5 files changed

+261
-120
lines changed

src/http_response.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ namespace httpserver
3131

3232
cache_response::~cache_response()
3333
{
34-
ws->unlock_cache_element(content);
34+
if(locked_element)
35+
ws->unlock_cache_element(content);
3536
}
3637

3738
const std::vector<std::pair<std::string, std::string> > http_response::get_headers()
@@ -88,7 +89,7 @@ shoutCAST_response::shoutCAST_response
8889
{
8990
}
9091

91-
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)
9293
{
9394
size_t size = &(*content.end()) - &(*content.begin());
9495
*response = MHD_create_response_from_buffer(size, (void*) content.c_str(), MHD_RESPMEM_PERSISTENT);
@@ -103,6 +104,10 @@ void http_response::decorate_response(MHD_Response* response)
103104
MHD_add_response_footer(response, (*it).first.c_str(), (*it).second.c_str());
104105
}
105106

107+
void cache_response::decorate_response(MHD_Response* response)
108+
{
109+
}
110+
106111
int http_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
107112
{
108113
return MHD_queue_response(connection, response_code, response);
@@ -118,22 +123,28 @@ int http_digest_auth_fail_response::enqueue_response(MHD_Connection* connection,
118123
return MHD_queue_auth_fail_response(connection, realm.c_str(), opaque.c_str(), response, reload_nonce ? MHD_YES : MHD_NO);
119124
}
120125

121-
void http_file_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
126+
void http_file_response::get_raw_response(MHD_Response** response, webserver* ws)
122127
{
123128
char* page = NULL;
124129
size_t size = http::load_file(filename.c_str(), &page);
125130
if(size)
126131
*response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_FREE);
127-
else
128-
*found = false;
132+
//TODO: At the moment if the file does not exist the system returns empty response
129133
}
130134

131-
void cache_response::get_raw_response(MHD_Response** response, bool* found, webserver* ws)
135+
void cache_response::get_raw_response(MHD_Response** response, webserver* ws)
132136
{
133-
ws->get_from_cache(content, 0x0, true)->get_raw_response(response, found, ws);
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
134145
}
135146

136-
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)
137148
{
138149
#ifdef USE_COMET
139150
this->ws = ws;
@@ -142,7 +153,7 @@ void long_polling_receive_response::get_raw_response(MHD_Response** response, bo
142153
&long_polling_receive_response::data_generator, (void*) this, NULL);
143154
ws->register_to_topics(topics, connection_id, keepalive_secs, keepalive_msg);
144155
#else //USE_COMET
145-
http_response::get_raw_response(response, found, ws);
156+
http_response::get_raw_response(response, ws);
146157
#endif //USE_COMET
147158
}
148159

@@ -164,9 +175,9 @@ ssize_t long_polling_receive_response::data_generator (void* cls, uint64_t pos,
164175
#endif //USE_COMET
165176
}
166177

167-
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)
168179
{
169-
http_response::get_raw_response(response, found, ws);
180+
http_response::get_raw_response(response, ws);
170181
#ifdef USE_COMET
171182
ws->send_message_to_topic(send_topic, content);
172183
#endif //USE_COMET

src/httpserver/http_response.hpp

Lines changed: 35 additions & 7 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
/**
@@ -317,11 +322,12 @@ class http_response
317322
std::string send_topic;
318323
struct MHD_Connection* underlying_connection;
319324

320-
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);
321326
virtual void decorate_response(MHD_Response* res);
322327
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
323328

324329
friend class webserver;
330+
friend struct details::http_response_ptr;
325331
friend void clone_response(const http_response& hr, http_response** dhr);
326332
friend class cache_response;
327333
};
@@ -338,6 +344,8 @@ class http_string_response : public http_response
338344
): http_response(http_response::STRING_CONTENT, content, response_code, content_type, autodelete) { }
339345

340346
http_string_response(const http_response& b) : http_response(b) { }
347+
private:
348+
friend class webserver;
341349
};
342350

343351
class http_byte_response : public http_response
@@ -351,6 +359,8 @@ class http_byte_response : public http_response
351359
const std::string& content_type = "text/plain",
352360
bool autodelete = true
353361
): http_response(http_response::STRING_CONTENT, std::string(content, content_length), response_code, content_type, autodelete) { }
362+
private:
363+
friend class webserver;
354364
};
355365

356366
class http_file_response : public http_response
@@ -368,7 +378,9 @@ class http_file_response : public http_response
368378

369379
http_file_response(const http_response& b) : http_response(b) { }
370380
protected:
371-
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;
372384
};
373385

374386
class http_basic_auth_fail_response : public http_response
@@ -387,6 +399,8 @@ class http_basic_auth_fail_response : public http_response
387399
http_basic_auth_fail_response(const http_response& b) : http_response(b) { }
388400
protected:
389401
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
402+
private:
403+
friend class webserver;
390404
};
391405

392406
class http_digest_auth_fail_response : public http_response
@@ -408,6 +422,8 @@ class http_digest_auth_fail_response : public http_response
408422
http_digest_auth_fail_response(const http_response& b) : http_response(b) { }
409423
protected:
410424
virtual int enqueue_response(MHD_Connection* connection, MHD_Response* res);
425+
private:
426+
friend class webserver;
411427
};
412428

413429
class shoutCAST_response : public http_response
@@ -422,6 +438,8 @@ class shoutCAST_response : public http_response
422438
);
423439

424440
shoutCAST_response(const http_response& b) : http_response(b) { }
441+
private:
442+
friend class webserver;
425443
};
426444

427445
class switch_protocol_response : public http_response
@@ -437,7 +455,9 @@ class switch_protocol_response : public http_response
437455
{
438456
}
439457
protected:
440-
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;
441461
};
442462

443463
class long_polling_receive_response : public http_response
@@ -458,11 +478,12 @@ class long_polling_receive_response : public http_response
458478

459479
long_polling_receive_response(const http_response& b) : http_response(b) { }
460480
protected:
461-
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);
462482
private:
463483
static ssize_t data_generator (void* cls, uint64_t pos, char* buf, size_t max);
464484
int connection_id;
465485
httpserver::webserver* ws;
486+
friend class webserver;
466487
};
467488

468489
class long_polling_send_response : public http_response
@@ -479,7 +500,9 @@ class long_polling_send_response : public http_response
479500

480501
long_polling_send_response(const http_response& b) : http_response(b) { }
481502
protected:
482-
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;
483506
};
484507

485508
class cache_response : public http_response
@@ -488,7 +511,9 @@ class cache_response : public http_response
488511
cache_response
489512
(
490513
const std::string& key
491-
) : http_response(http_response::CACHED_CONTENT, key)
514+
) : http_response(http_response::CACHED_CONTENT, key),
515+
ws(0x0),
516+
locked_element(false)
492517
{
493518
}
494519

@@ -497,9 +522,12 @@ class cache_response : public http_response
497522
~cache_response();
498523

499524
protected:
500-
virtual void get_raw_response(MHD_Response** res, bool* found, webserver* ws = 0x0);
525+
virtual void get_raw_response(MHD_Response** res, webserver* ws = 0x0);
526+
virtual void decorate_response(MHD_Response* res);
501527
private:
502528
webserver* ws;
529+
bool locked_element;
530+
friend class webserver;
503531
};
504532

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

src/httpserver/modded_request.hpp

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

src/httpserver/webserver.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ namespace httpserver {
5151

5252
class http_resource;
5353
class http_response;
54+
class cache_response;
5455
class http_request;
55-
struct modded_request;
5656
class long_polling_receive_response;
5757
class long_polling_send_response;
5858

5959
namespace details
6060
{
6161
class http_endpoint;
6262
struct cache_entry;
63+
struct modded_request;
64+
struct cache_manager;
6365
}
6466

6567
using namespace http;
@@ -264,6 +266,7 @@ class webserver
264266
void unlock_cache_element(const std::string& key);
265267
void put_in_cache(const std::string& key, http_response* value, int validity = -1);
266268
void remove_from_cache(const std::string& key);
269+
bool is_valid(const std::string& key);
267270
void clean_cache();
268271

269272
const logging_delegate* get_logging_delegate() const;
@@ -327,7 +330,7 @@ class webserver
327330

328331
std::map<details::http_endpoint, http_resource* > registered_resources;
329332

330-
std::map<std::string, details::cache_entry> response_cache;
333+
details::cache_manager* cache_m;
331334
pthread_rwlock_t cache_guard;
332335
#ifdef USE_CPP_ZEROX
333336
std::unordered_set<ip_representation> bans;
@@ -355,9 +358,9 @@ class webserver
355358
static void* cleaner(void* self);
356359
void clean_connections();
357360

358-
void method_not_allowed_page(http_response** dhrs, modded_request* mr);
359-
void internal_error_page(http_response** dhrs, modded_request* mr);
360-
void not_found_page(http_response** dhrs, modded_request* mr);
361+
void method_not_allowed_page(http_response** dhrs, details::modded_request* mr);
362+
void internal_error_page(http_response** dhrs, details::modded_request* mr);
363+
void not_found_page(http_response** dhrs, details::modded_request* mr);
361364

362365
static int method_not_acceptable_page
363366
(
@@ -395,30 +398,30 @@ class webserver
395398

396399
int bodyless_requests_answer(MHD_Connection* connection,
397400
const char* url, const char* method,
398-
const char* version, struct modded_request* mr
401+
const char* version, struct details::modded_request* mr
399402
);
400403

401-
int bodyfull_requests_answer_first_step(MHD_Connection* connection, struct modded_request* mr);
404+
int bodyfull_requests_answer_first_step(MHD_Connection* connection, struct details::modded_request* mr);
402405

403406
int bodyfull_requests_answer_second_step(MHD_Connection* connection,
404407
const char* url, const char* method,
405408
const char* version, const char* upload_data,
406-
size_t* upload_data_size, struct modded_request* mr
409+
size_t* upload_data_size, struct details::modded_request* mr
407410
);
408411

409412
void end_request_construction(MHD_Connection* connection,
410-
struct modded_request* mr, const char* version,
413+
struct details::modded_request* mr, const char* version,
411414
const char* st_url, const char* method,
412415
char* user, char* pass, char* digested_user
413416
);
414417

415418
int finalize_answer(MHD_Connection* connection,
416-
struct modded_request* mr, const char* st_url,
419+
struct details::modded_request* mr, const char* st_url,
417420
const char* method
418421
);
419422

420423
int complete_request(MHD_Connection* connection,
421-
struct modded_request* mr, const char* version,
424+
struct details::modded_request* mr, const char* version,
422425
const char* st_url, const char* method
423426
);
424427

@@ -433,6 +436,7 @@ class webserver
433436
friend void* uri_log(void* cls, const char* uri);
434437
friend size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s);
435438
friend size_t internal_unescaper(void * cls, char *s);
439+
friend class cache_response;
436440
};
437441

438442
class create_webserver

0 commit comments

Comments
 (0)