Skip to content

Commit dcfa5a7

Browse files
author
Sebastiano Merlino
committed
Extracted other classes from webserver.cpp
1 parent 453d744 commit dcfa5a7

File tree

7 files changed

+223
-136
lines changed

7 files changed

+223
-136
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ INCLUDES = -I../ -I$(srcdir)/httpserver/
2020
METASOURCES = AUTO
2121
lib_LTLIBRARIES = libhttpserver.la
2222
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_endpoint.cpp http_request.cpp http_response.cpp http_resource.cpp
23-
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp httpserver/details/http_response_ptr.hpp gettext.h
23+
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp httpserver/details/http_response_ptr.hpp httpserver/details/cache_entry.hpp gettext.h
2424
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/binders.hpp httpserver/event_supplier.hpp httpserver/details/event_tuple.hpp httpserver/details/http_resource_mirror.hpp
2525
AM_CXXFLAGS += -fPIC -Wall
2626
libhttpserver_la_LIBADD = -lmicrohttpd
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#ifndef _CACHE_ENTRY_HPP_
22+
#define _CACHE_ENTRY_HPP_
23+
24+
#include <pthread.h>
25+
#include <set>
26+
#include "details/http_response_ptr.hpp"
27+
28+
namespace httpserver
29+
{
30+
namespace details
31+
{
32+
33+
struct pthread_t_comparator
34+
{
35+
bool operator()(const pthread_t& t1, const pthread_t& t2) const
36+
{
37+
return pthread_equal(t1, t2);
38+
}
39+
};
40+
41+
struct cache_entry
42+
{
43+
long ts;
44+
int validity;
45+
details::http_response_ptr response;
46+
pthread_rwlock_t elem_guard;
47+
pthread_mutex_t lock_guard;
48+
std::set<pthread_t, pthread_t_comparator> lockers;
49+
50+
cache_entry():
51+
ts(-1),
52+
validity(-1)
53+
{
54+
pthread_rwlock_init(&elem_guard, NULL);
55+
pthread_mutex_init(&lock_guard, NULL);
56+
}
57+
58+
~cache_entry()
59+
{
60+
pthread_rwlock_destroy(&elem_guard);
61+
pthread_mutex_destroy(&lock_guard);
62+
}
63+
64+
cache_entry(const cache_entry& b):
65+
ts(b.ts),
66+
validity(b.validity),
67+
response(b.response),
68+
elem_guard(b.elem_guard),
69+
lock_guard(b.lock_guard)
70+
{
71+
}
72+
73+
void operator= (const cache_entry& b)
74+
{
75+
ts = b.ts;
76+
validity = b.validity;
77+
response = b.response;
78+
pthread_rwlock_destroy(&elem_guard);
79+
pthread_mutex_destroy(&lock_guard);
80+
elem_guard = b.elem_guard;
81+
}
82+
83+
cache_entry(
84+
details::http_response_ptr response,
85+
long ts = -1,
86+
int validity = -1
87+
):
88+
ts(ts),
89+
validity(validity),
90+
response(response)
91+
{
92+
pthread_rwlock_init(&elem_guard, NULL);
93+
pthread_mutex_init(&lock_guard, NULL);
94+
}
95+
96+
void lock(bool write = false)
97+
{
98+
pthread_mutex_lock(&lock_guard);
99+
pthread_t tid = pthread_self();
100+
if(!lockers.count(tid))
101+
{
102+
if(write)
103+
{
104+
lockers.insert(tid);
105+
pthread_mutex_unlock(&lock_guard);
106+
pthread_rwlock_wrlock(&elem_guard);
107+
}
108+
else
109+
{
110+
lockers.insert(tid);
111+
pthread_mutex_unlock(&lock_guard);
112+
pthread_rwlock_rdlock(&elem_guard);
113+
}
114+
}
115+
else
116+
pthread_mutex_unlock(&lock_guard);
117+
}
118+
119+
void unlock()
120+
{
121+
pthread_mutex_lock(&lock_guard);
122+
{
123+
pthread_t tid = pthread_self();
124+
if(lockers.count(tid))
125+
{
126+
lockers.erase(tid);
127+
pthread_rwlock_unlock(&elem_guard);
128+
}
129+
}
130+
pthread_mutex_unlock(&lock_guard);
131+
}
132+
};
133+
134+
} //details
135+
} //httpserver
136+
137+
#endif //_CACHE_ENTRY_HPP_

src/httpserver/details/http_response_ptr.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#ifndef _HTTP_RESPONSE_PTR_HPP_
22+
#define _HTTP_RESPONSE_PTR_HPP_
23+
124
namespace httpserver
225
{
326

@@ -89,3 +112,5 @@ struct http_response_ptr
89112

90113
} //details
91114
} //httpserver
115+
116+
#endif //_HTTP_RESPONSE_PTR_HPP_

src/httpserver/details/modded_request.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#ifndef _MODDED_REQUEST_HPP_
22+
#define _MODDED_REQUEST_HPP_
23+
124
#include "binders.hpp"
225
#include "details/http_response_ptr.hpp"
326

@@ -49,3 +72,5 @@ struct modded_request
4972
} //details
5073

5174
} //httpserver
75+
76+
#endif //_MODDED_REQUEST_HPP_

src/httpserver/http_response.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ namespace httpserver
3636
{
3737

3838
class webserver;
39-
struct cache_entry;
4039

4140
namespace http
4241
{
@@ -48,6 +47,7 @@ namespace details
4847
{
4948
struct http_response_ptr;
5049
ssize_t cb(void*, uint64_t, char*, size_t);
50+
struct cache_entry;
5151
};
5252

5353
using namespace http;
@@ -84,7 +84,7 @@ http_response \
8484
int keepalive_secs,\
8585
const std::string keepalive_msg,\
8686
const std::string send_topic,\
87-
cache_entry* ce\
87+
details::cache_entry* ce\
8888
):\
8989
content(content),\
9090
response_code(response_code),\
@@ -139,7 +139,7 @@ class http_response
139139
int keepalive_secs = -1,
140140
const std::string keepalive_msg = "",
141141
const std::string send_topic = "",
142-
cache_entry* ce = 0x0
142+
details::cache_entry* ce = 0x0
143143
):
144144
content(content),
145145
response_code(response_code),
@@ -441,7 +441,7 @@ class http_response
441441
struct MHD_Connection* underlying_connection;
442442
void(*ca)(void*);
443443
void* closure_data;
444-
cache_entry* ce;
444+
details::cache_entry* ce;
445445

446446
const get_raw_response_t get_raw_response;
447447
const decorate_response_t decorate_response;
@@ -680,7 +680,7 @@ class cache_response : public http_response
680680
}
681681
cache_response
682682
(
683-
cache_entry* ce
683+
details::cache_entry* ce
684684
) : http_response(this, "", 200, "", true, "", "", false,
685685
std::vector<std::string>(), -1, "", "", ce
686686
)

src/httpserver/webserver.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ namespace httpserver {
5151

5252
template<typename CHILD> class http_resource;
5353
class http_response;
54-
struct cache_entry;
5554
template<typename CHILD> class event_supplier;
5655
class create_webserver;
5756

@@ -66,6 +65,7 @@ namespace details {
6665
class http_endpoint;
6766
class daemon_item;
6867
class modded_request;
68+
struct cache_entry;
6969
}
7070

7171
/**
@@ -140,11 +140,11 @@ class webserver
140140
bool lock = false, bool write = false
141141
);
142142
http_response* get_from_cache(const std::string& key, bool* valid,
143-
cache_entry** ce, bool lock = false, bool write = false
143+
details::cache_entry** ce, bool lock = false, bool write = false
144144
);
145-
void lock_cache_element(cache_entry* ce, bool write = false);
146-
void unlock_cache_element(cache_entry* ce);
147-
cache_entry* put_in_cache(const std::string& key, http_response* value,
145+
void lock_cache_element(details::cache_entry* ce, bool write = false);
146+
void unlock_cache_element(details::cache_entry* ce);
147+
details::cache_entry* put_in_cache(const std::string& key, http_response* value,
148148
bool* new_elem, bool lock = false,
149149
bool write = false, int validity = -1
150150
);
@@ -252,7 +252,7 @@ class webserver
252252
std::map<details::http_endpoint, details::http_resource_mirror> registered_resources;
253253
std::map<std::string, details::http_resource_mirror*> registered_resources_str;
254254

255-
std::map<std::string, cache_entry*> response_cache;
255+
std::map<std::string, details::cache_entry*> response_cache;
256256
int next_to_choose;
257257
pthread_rwlock_t cache_guard;
258258
#ifdef USE_CPP_ZEROX
@@ -338,9 +338,9 @@ class webserver
338338
void **con_cls, int upgrade_socket
339339
);
340340

341-
static void unlock_cache_entry(cache_entry*);
342-
static void lock_cache_entry(cache_entry*);
343-
static void get_response(cache_entry*, http_response** res);
341+
static void unlock_cache_entry(details::cache_entry*);
342+
static void lock_cache_entry(details::cache_entry*);
343+
static void get_response(details::cache_entry*, http_response** res);
344344

345345
int bodyless_requests_answer(MHD_Connection* connection,
346346
const char* method, const char* version,

0 commit comments

Comments
 (0)