Skip to content

Commit fdbaa11

Browse files
author
Sebastiano Merlino
committed
Adjusted constructors in order to allow startMethod initialization
Cleaned some code Added configurable connection validation Added configurable unescaper Added -Wall option during compilation
1 parent 3e325a0 commit fdbaa11

File tree

3 files changed

+54
-68
lines changed

3 files changed

+54
-68
lines changed

Makefile.am

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
# not a GNU package. You can remove this line, if
2-
# have all needed files, that a GNU package needs
3-
AUTOMAKE_OPTIONS = foreign 1.4
4-
5-
SUBDIRS = src $(PYTHON_DIR) $(JAVA_DIR) $(PHP_DIR)
6-
DIST_SUBDIRS = src $(PYTHON_DIR) $(JAVA_DIR) $(PHP_DIR)
7-
8-
if PYTHON
9-
PYTHON_DIR = src/python
10-
endif
11-
12-
if JAVA
13-
JAVA_DIR = src/java
14-
endif
15-
16-
if PHP
17-
PHP_DIR = src/php
18-
endif
1+
INCLUDES = -I../
2+
METASOURCES = AUTO
3+
lib_LTLIBRARIES = libhttpserver.la
4+
libhttpserver_la_SOURCES = string_utilities.cpp Webserver.cpp HttpUtils.cpp
5+
noinst_HEADERS = string_utilities.hpp
6+
include_HEADERS = Webserver.hpp HttpUtils.hpp
7+
AM_CXXFLAGS = -fPIC -Wall
8+
libhttpserver_la_LIBADD = -lmicrohttpd
9+
libhttpserver_la_LDFLAGS =

src/Webserver.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void error_log(void*, const char*, va_list);
3939
void* uri_log(void*, const char*);
4040
void access_log(Webserver*, string);
4141
size_t unescaper_func(void*, struct MHD_Connection*, char*);
42-
size_t internal_unescaper(void*, struct MHD_Connection*, char*);
42+
size_t internal_unescaper(void*, char*);
4343

4444
static void catcher (int sig)
4545
{
@@ -81,7 +81,7 @@ HttpEndpoint::HttpEndpoint(const string& url, bool family, bool registration):
8181
bool first = true;
8282
if(registration)
8383
{
84-
for(int i = 0; i< parts.size(); i++)
84+
for(unsigned int i = 0; i< parts.size(); i++)
8585
{
8686
if((parts[i] != "") && (parts[i][0] != '{'))
8787
{
@@ -107,7 +107,7 @@ HttpEndpoint::HttpEndpoint(const string& url, bool family, bool registration):
107107
if(( parts[i].size() >= 3) && (parts[i][0] == '{') && (parts[i][parts[i].size() - 1] == '}') )
108108
{
109109
int bar = parts[i].find_first_of('|');
110-
if(bar != string::npos)
110+
if(bar != (int)string::npos)
111111
{
112112
this->url_pars.push_back(parts[i].substr(1, bar - 1));
113113
if(first)
@@ -147,7 +147,7 @@ HttpEndpoint::HttpEndpoint(const string& url, bool family, bool registration):
147147
}
148148
else
149149
{
150-
for(int i = 0; i< parts.size(); i++)
150+
for(unsigned int i = 0; i< parts.size(); i++)
151151
{
152152
if(first)
153153
{
@@ -212,7 +212,7 @@ bool HttpEndpoint::match(const HttpEndpoint& url) const
212212
{
213213
string nn = "/";
214214
bool first = true;
215-
for(int i = 0; i < this->url_pieces.size(); i++)
215+
for(unsigned int i = 0; i < this->url_pieces.size(); i++)
216216
{
217217
if(first)
218218
{
@@ -481,7 +481,7 @@ void HttpRequest::setPath(const string& path)
481481
//this->path = boost::to_lower_copy(path);
482482
this->path = path;
483483
vector<string> complete_path = HttpUtils::tokenizeUrl(this->path);
484-
for(int i = 0; i < complete_path.size(); i++)
484+
for(unsigned int i = 0; i < complete_path.size(); i++)
485485
{
486486
this->post_path.push_back(complete_path[i]);
487487
}
@@ -821,7 +821,7 @@ RequestValidator::RequestValidator() {}
821821

822822
RequestValidator::~RequestValidator() {}
823823

824-
bool RequestValidator::validate(const string& address) const {}
824+
bool RequestValidator::validate(const string& address) const { return true; }
825825

826826
//UNESCAPER
827827
Unescaper::Unescaper() {}
@@ -859,6 +859,7 @@ Webserver::Webserver
859859
int nonceNcSize
860860
) :
861861
port(port),
862+
startMethod(startMethod),
862863
maxThreads(maxThreads),
863864
maxConnections(maxConnections),
864865
memoryLimit(memoryLimit),
@@ -888,6 +889,7 @@ Webserver::Webserver
888889

889890
Webserver::Webserver(const CreateWebserver& params):
890891
port(params._port),
892+
startMethod(params._startMethod),
891893
maxThreads(params._maxThreads),
892894
maxConnections(params._maxConnections),
893895
memoryLimit(params._memoryLimit),
@@ -936,7 +938,8 @@ void Webserver::requestCompleted (void *cls, struct MHD_Connection *connection,
936938
{
937939
MHD_destroy_post_processor (mr->pp);
938940
}
939-
delete mr->dhr;
941+
if(mr->second)
942+
delete mr->dhr; //TODO: verify. It could be an error
940943
delete mr->completeUri;
941944
free(mr);
942945
}
@@ -988,7 +991,7 @@ bool Webserver::start(bool blocking)
988991
iov.push_back(gen(MHD_OPTION_END, 0, NULL ));
989992

990993
struct MHD_OptionItem ops[iov.size()];
991-
for(int i = 0; i < iov.size(); i++)
994+
for(unsigned int i = 0; i < iov.size(); i++)
992995
{
993996
ops[i] = iov[i];
994997
}
@@ -1015,13 +1018,14 @@ bool Webserver::start(bool blocking)
10151018
return false;
10161019
}
10171020
this->running = true;
1021+
bool value_onclose = false;
10181022
if(blocking)
10191023
{
10201024
while(blocking && running)
10211025
sleep(1);
1022-
this->stop();
1026+
value_onclose = this->stop();
10231027
}
1024-
return true;
1028+
return value_onclose;
10251029
}
10261030

10271031
bool Webserver::isRunning()
@@ -1036,6 +1040,7 @@ bool Webserver::stop()
10361040
MHD_stop_daemon (this->daemon);
10371041
this->running = false;
10381042
}
1043+
return true;
10391044
}
10401045

10411046
void Webserver::registerResource(const string& resource, HttpResource* http_resource, bool family)
@@ -1066,9 +1071,9 @@ int Webserver::buildRequestFooter (void *cls, enum MHD_ValueKind kind, const cha
10661071

10671072
int Webserver::buildRequestArgs (void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
10681073
{
1069-
HttpRequest* dhr = (HttpRequest*)(cls);
1070-
int size = http_unescape((char*) value);
1071-
dhr->setArg(key, string(value, size));
1074+
ModdedRequest* mr = (ModdedRequest*)(cls);
1075+
int size = internal_unescaper((void*)mr->ws, (char*) value);
1076+
mr->dhr->setArg(key, string(value, size));
10721077
return MHD_YES;
10731078
}
10741079

@@ -1120,7 +1125,7 @@ size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s)
11201125
return strlen(s);
11211126
}
11221127

1123-
size_t internal_unescaper(void* cls, struct MHD_Connection *c, char* s)
1128+
size_t internal_unescaper(void* cls, char* s)
11241129
{
11251130
Webserver* dws = (Webserver*) cls;
11261131
if(dws->unescaper != 0x0)
@@ -1134,7 +1139,6 @@ size_t internal_unescaper(void* cls, struct MHD_Connection *c, char* s)
11341139
}
11351140
}
11361141

1137-
11381142
int Webserver::post_iterator (void *cls, enum MHD_ValueKind kind,
11391143
const char *key,
11401144
const char *filename,
@@ -1199,23 +1203,22 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
11991203
int ret;
12001204
HttpRequest supportReq;
12011205
Webserver* dws = (Webserver*)(cls);
1202-
http_unescape((char*) url);
1206+
internal_unescaper(cls, (char*) url);
12031207
string st_url = HttpUtils::standardizeUrl(url);
12041208

12051209
mr = (struct ModdedRequest*) *con_cls;
12061210
access_log(dws, *(mr->completeUri) + " METHOD: " + method);
1211+
mr->ws = dws;
12071212
if (0 == strcmp (method, MHD_HTTP_METHOD_POST) || 0 == strcmp(method, MHD_HTTP_METHOD_PUT))
12081213
{
12091214
if (mr->second == false)
12101215
{
12111216
mr->second = true;
12121217
mr->dhr = new HttpRequest();
1213-
mr->dhr->setPath(st_url);
1214-
mr->dhr->setMethod(method);
12151218
const char *encoding = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, HttpUtils::http_header_content_type.c_str());
1219+
//mr->dhr->setHeader(HttpUtils::http_header_content_type, string(encoding));
12161220
if ( 0x0 != encoding && 0 == strcmp(method, MHD_HTTP_METHOD_POST) && ((0 == strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding, strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))))
12171221
{
1218-
mr->dhr->setHeader(HttpUtils::http_header_content_type, string(encoding));
12191222
mr->pp = MHD_create_post_processor (connection, 1024, &post_iterator, mr);
12201223
}
12211224
else
@@ -1228,25 +1231,16 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
12281231
else
12291232
{
12301233
supportReq = HttpRequest();
1231-
supportReq.setMethod(string(method));
1232-
supportReq.setPath(string(st_url));
1233-
const char *encoding = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, HttpUtils::http_header_content_type.c_str());
1234-
if(encoding != 0x0)
1235-
supportReq.setHeader(HttpUtils::http_header_content_type, string(encoding));
1234+
mr->dhr = &supportReq;
12361235
}
12371236

1238-
if(0 == strcmp (method, MHD_HTTP_METHOD_POST) || 0 == strcmp(method, MHD_HTTP_METHOD_PUT))
1239-
{
1240-
MHD_get_connection_values (connection, MHD_HEADER_KIND, &buildRequestHeader, (void*) mr->dhr);
1241-
MHD_get_connection_values (connection, MHD_FOOTER_KIND, &buildRequestFooter, (void*) mr->dhr);
1242-
MHD_get_connection_values (connection, MHD_COOKIE_KIND, &buildRequestCookie, (void*) mr->dhr);
1243-
}
1244-
else
1245-
{
1246-
MHD_get_connection_values (connection, MHD_HEADER_KIND, &buildRequestHeader, (void*) &supportReq);
1247-
MHD_get_connection_values (connection, MHD_FOOTER_KIND, &buildRequestFooter, (void*) &supportReq);
1248-
MHD_get_connection_values (connection, MHD_COOKIE_KIND, &buildRequestCookie, (void*) &supportReq);
1249-
}
1237+
mr->dhr->setPath(string(st_url));
1238+
mr->dhr->setMethod(string(method));
1239+
1240+
MHD_get_connection_values (connection, MHD_HEADER_KIND, &buildRequestHeader, (void*) mr->dhr);
1241+
MHD_get_connection_values (connection, MHD_FOOTER_KIND, &buildRequestFooter, (void*) mr->dhr);
1242+
MHD_get_connection_values (connection, MHD_COOKIE_KIND, &buildRequestCookie, (void*) mr->dhr);
1243+
12501244
if ( 0 == strcmp(method, MHD_HTTP_METHOD_DELETE) ||
12511245
0 == strcmp(method, MHD_HTTP_METHOD_GET) ||
12521246
0 == strcmp(method, MHD_HTTP_METHOD_HEAD) ||
@@ -1255,7 +1249,7 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
12551249
0 == strcmp(method, MHD_HTTP_METHOD_TRACE)
12561250
)
12571251
{
1258-
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, &buildRequestArgs, (void*) &supportReq);
1252+
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, &buildRequestArgs, (void*) mr);
12591253
}
12601254
else if (0 == strcmp (method, MHD_HTTP_METHOD_POST) || 0 == strcmp(method, MHD_HTTP_METHOD_PUT))
12611255
{
@@ -1335,7 +1329,7 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
13351329
HttpEndpoint matchingEndpoint;
13361330
for(it=dws->registeredResources.begin(); it!=dws->registeredResources.end(); it++)
13371331
{
1338-
if(len == -1 || (*it).first.get_url_pieces().size() > len)
1332+
if(len == -1 || ((int)((*it).first.get_url_pieces().size())) > len)
13391333
{
13401334
if((*it).first.match(endpoint))
13411335
{
@@ -1359,7 +1353,7 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
13591353
vector<string> url_pars = matchingEndpoint.get_url_pars();
13601354
vector<string> url_pieces = endpoint.get_url_pieces();
13611355
vector<int> chunkes = matchingEndpoint.get_chunk_positions();
1362-
for(int i = 0; i < url_pars.size(); i++)
1356+
for(unsigned int i = 0; i < url_pars.size(); i++)
13631357
{
13641358
supportReq.setArg(url_pars[i], url_pieces[chunkes[i]]);
13651359
}

src/Webserver.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -882,31 +882,31 @@ class Webserver
882882
**/
883883
void sweetKill();
884884
private:
885-
bool running;
886-
bool useSsl;
887-
bool useIpv6;
888-
bool debug;
889-
bool pedantic;
890885
int port;
886+
HttpUtils::StartMethod_T startMethod;
891887
int maxThreads;
892888
int maxConnections;
893889
int memoryLimit;
894890
int connectionTimeout;
895891
int perIPConnectionLimit;
896-
int maxThreadStackSize;
897-
int nonceNcSize;
898892
const LoggingDelegate* logDelegate;
899893
const RequestValidator* validator;
900894
const Unescaper* unescaper;
901895
const struct sockaddr* bindAddress;
902896
int bindSocket;
897+
int maxThreadStackSize;
898+
bool useSsl;
899+
bool useIpv6;
900+
bool debug;
901+
bool pedantic;
903902
std::string httpsMemKey;
904903
std::string httpsMemCert;
905904
std::string httpsMemTrust;
906905
std::string httpsPriorities;
907-
std::string digestAuthRandom;
908906
HttpUtils::CredType_T credType;
909-
HttpUtils::StartMethod_T startMethod;
907+
std::string digestAuthRandom;
908+
int nonceNcSize;
909+
bool running;
910910

911911
std::map<HttpEndpoint, HttpResource* > registeredResources;
912912
struct MHD_Daemon *daemon;
@@ -948,7 +948,7 @@ class Webserver
948948
friend void access_log(Webserver* cls, std::string uri);
949949
friend void* uri_log(void* cls, const char* uri);
950950
friend size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s);
951-
friend size_t internal_unescaper(void * cls, struct MHD_Connection *c, char *s);
951+
friend size_t internal_unescaper(void * cls, char *s);
952952
};
953953

954954
class CreateWebserver
@@ -1070,6 +1070,7 @@ struct ModdedRequest
10701070
struct MHD_PostProcessor *pp;
10711071
std::string* completeUri;
10721072
HttpRequest *dhr;
1073+
Webserver* ws;
10731074
bool second;
10741075
};
10751076

0 commit comments

Comments
 (0)