Skip to content

Commit 6d77ed4

Browse files
author
Sebastiano Merlino
committed
Modified in order to add options on start. (SSL, DEBUG, PEDANTIC, IPV6)
1 parent 43d17c3 commit 6d77ed4

File tree

7 files changed

+77
-48
lines changed

7 files changed

+77
-48
lines changed

src/HttpUtils.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class HttpUtils
4242
IA = GNUTLS_CRD_IA
4343
};
4444

45+
enum StartMethod_T
46+
{
47+
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY,
48+
THREADS = MHD_USE_THREAD_PER_CONNECTION,
49+
POOL = MHD_USE_POLL
50+
};
51+
4552
#ifdef SWIG
4653
%immutable;
4754
#endif

src/Webserver.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <unistd.h>
2828
#include <sys/stat.h>
2929
#include <sys/types.h>
30+
#include <signal.h>
3031
#ifdef WITH_PYTHON
3132
#include <Python.h>
3233
#endif
@@ -40,6 +41,26 @@ void access_log(Webserver*, string);
4041
size_t unescaper_func(void*, struct MHD_Connection*, char*);
4142
size_t internal_unescaper(void*, struct MHD_Connection*, char*);
4243

44+
static void catcher (int sig)
45+
{
46+
}
47+
48+
static void ignore_sigpipe ()
49+
{
50+
struct sigaction oldsig;
51+
struct sigaction sig;
52+
53+
sig.sa_handler = &catcher;
54+
sigemptyset (&sig.sa_mask);
55+
#ifdef SA_INTERRUPT
56+
sig.sa_flags = SA_INTERRUPT; /* SunOS */
57+
#else
58+
sig.sa_flags = SA_RESTART;
59+
#endif
60+
if (0 != sigaction (SIGPIPE, &sig, &oldsig))
61+
fprintf (stderr, "Failed to install SIGPIPE handler: %s\n", strerror (errno));
62+
}
63+
4364
//ENDPOINT
4465
HttpEndpoint::HttpEndpoint(bool family):
4566
url_complete("/"),
@@ -753,15 +774,6 @@ const std::vector<std::pair<std::string, std::string> > HttpResponse::getHeaders
753774
return toRet;
754775
}
755776

756-
const std::vector<std::pair<std::string, std::string> > HttpResponse::getCookies()
757-
{
758-
std::vector<std::pair<std::string, std::string> > toRet;
759-
map<string, string, HeaderComparator>::const_iterator it;
760-
for(it = cookies.begin(); it != cookies.end(); it++)
761-
toRet.push_back(make_pair<string, string>((*it).first,(*it).second));
762-
return toRet;
763-
}
764-
765777
const std::vector<std::pair<std::string, std::string> > HttpResponse::getFooters()
766778
{
767779
std::vector<std::pair<std::string, std::string> > toRet;
@@ -785,13 +797,6 @@ void HttpResponse::setFooters(const map<string, string>& footers)
785797
this->footers[it->first] = it->second;
786798
}
787799

788-
void HttpResponse::setCookies(const map<string, string>& cookies)
789-
{
790-
map<string, string>::const_iterator it;
791-
for(it = cookies.begin(); it != cookies.end(); it ++)
792-
this->cookies[it->first] = it->second;
793-
}
794-
795800
int HttpResponse::getResponseCode()
796801
{
797802
return this->responseCode;
@@ -829,6 +834,7 @@ void Unescaper::unescape(char* s) const {}
829834
Webserver::Webserver
830835
(
831836
const int port,
837+
const HttpUtils::StartMethod_T startMethod,
832838
const int maxThreads,
833839
const int maxConnections,
834840
const int memoryLimit,
@@ -838,6 +844,10 @@ Webserver::Webserver
838844
const RequestValidator* validator,
839845
const Unescaper* unescaper,
840846
const int maxThreadStackSize,
847+
const bool useSsl,
848+
const bool useIpv6,
849+
const bool debug,
850+
const bool pedantic,
841851
const string& httpsMemKey,
842852
const string& httpsMemCert,
843853
const string& httpsMemTrust,
@@ -855,6 +865,11 @@ Webserver::Webserver
855865
logDelegate(logDelegate),
856866
validator(validator),
857867
unescaper(unescaper),
868+
maxThreadStackSize(maxThreadStackSize),
869+
useSsl(useSsl),
870+
useIpv6(useIpv6),
871+
debug(debug),
872+
pedantic(pedantic),
858873
httpsMemKey(httpsMemKey),
859874
httpsMemCert(httpsMemCert),
860875
httpsMemTrust(httpsMemTrust),
@@ -864,6 +879,7 @@ Webserver::Webserver
864879
nonceNcSize(nonceNcSize),
865880
running(false)
866881
{
882+
ignore_sigpipe();
867883
}
868884

869885
Webserver::~Webserver()
@@ -903,11 +919,9 @@ bool Webserver::start(bool blocking)
903919
vector<struct MHD_OptionItem> iov;
904920

905921
iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) &requestCompleted, NULL ));
906-
iov.push_back(gen(MHD_OPTION_URI_LOG_CALLBACK, (intptr_t)&uri_log, this));
907-
iov.push_back(gen(MHD_OPTION_EXTERNAL_LOGGER, (intptr_t)&error_log, this));
908-
iov.push_back(gen(MHD_OPTION_UNESCAPE_CALLBACK, (intptr_t)&unescaper_func, this));
909-
if(connectionTimeout == 0)
910-
connectionTimeout = DEFAULT_DROP_WS_TIMEOUT;
922+
iov.push_back(gen(MHD_OPTION_URI_LOG_CALLBACK, (intptr_t) &uri_log, this));
923+
iov.push_back(gen(MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) &error_log, this));
924+
iov.push_back(gen(MHD_OPTION_UNESCAPE_CALLBACK, (intptr_t) &unescaper_func, this));
911925
iov.push_back(gen(MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout));
912926
if(maxThreads != 0)
913927
iov.push_back(gen(MHD_OPTION_THREAD_POOL_SIZE, maxThreads));
@@ -942,9 +956,19 @@ bool Webserver::start(bool blocking)
942956
ops[i] = iov[i];
943957
}
944958

959+
int startConf = startMethod;
960+
if(useSsl)
961+
startConf |= MHD_USE_SSL;
962+
if(useIpv6)
963+
startConf |= MHD_USE_IPv6;
964+
if(debug)
965+
startConf |= MHD_USE_DEBUG;
966+
if(pedantic)
967+
startConf |= MHD_USE_PEDANTIC_CHECKS;
968+
945969
this->daemon = MHD_start_daemon
946970
(
947-
MHD_USE_SELECT_INTERNALLY, this->port, &policyCallback, this,
971+
startConf, this->port, &policyCallback, this,
948972
&answerToConnection, this, MHD_OPTION_ARRAY, ops, MHD_OPTION_END
949973
);
950974

src/Webserver.hpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ namespace std {
143143
#define METHOD_ERROR "{\"description\":\"METHOD NOT ALLOWED\"}"
144144
#define NOT_METHOD_ERROR "{\"description\":\"METHOD NOT ACCEPTABLE\"}"
145145
#define GENERIC_ERROR "{\"description\":\"INTERNAL ERROR\"}"
146-
147-
#define DEFAULT_DROP_WS_PORT 9898
148-
#define DEFAULT_DROP_WS_TIMEOUT 180
146+
#define DEFAULT_WS_PORT 9898
147+
#define DEFAULT_WS_TIMEOUT 180
149148

150149
#include <sys/types.h>
151150
#include <sys/select.h>
@@ -548,12 +547,6 @@ class HttpResponse
548547
* @param value The value assumed by the footer
549548
**/
550549
void setFooter(const std::string& key, const std::string& value);
551-
/**
552-
* Method used to set a cookie value by key.
553-
* @param key The name identifying the cookie
554-
* @param value The value assumed by the cookie
555-
**/
556-
void setCookie(const std::string& key, const std::string& value);
557550
/**
558551
* Method used to set the content type for the request. This is a shortcut of setting the corresponding header.
559552
* @param contentType the content type to use for the request
@@ -574,11 +567,6 @@ class HttpResponse
574567
* @return a map<string,string> containing all footers.
575568
**/
576569
const std::vector<std::pair<std::string, std::string> > getFooters();
577-
/**
578-
* Method used to get all cookies passed with the request.
579-
* @return a map<string,string> containing all cookies.
580-
**/
581-
const std::vector<std::pair<std::string, std::string> > getCookies();
582570
/**
583571
* Method used to set all headers of the response.
584572
* @param headers The headers key-value map to set for the response.
@@ -611,7 +599,6 @@ class HttpResponse
611599
int responseCode;
612600
std::map<std::string, std::string, HeaderComparator> headers;
613601
std::map<std::string, std::string, ArgComparator> footers;
614-
std::map<std::string, std::string, HeaderComparator> cookies;
615602
int fp;
616603
std::string filename;
617604
};
@@ -808,6 +795,7 @@ class Webserver
808795
/**
809796
* Constructor of the class.
810797
* @param port Integer representing the port the webserver is listening on.
798+
* @param startMethod
811799
* @param maxThreads max number of serving threads (0 -> infty)
812800
* @param maxConnections max number of allowed connections (0 -> infty).
813801
* @param memoryLimit max memory allocated to serve requests (0 -> infty).
@@ -826,16 +814,21 @@ class Webserver
826814
**/
827815
Webserver
828816
(
829-
const int port = DEFAULT_DROP_WS_PORT,
817+
const int port = DEFAULT_WS_PORT,
818+
const HttpUtils::StartMethod_T startMethod = HttpUtils::INTERNAL_SELECT,
830819
const int maxThreads = 0,
831820
const int maxConnections = 0,
832821
const int memoryLimit = 0,
833-
const int connectionTimeout = 0,
822+
const int connectionTimeout = DEFAULT_WS_TIMEOUT,
834823
const int perIPConnectionLimit = 0,
835824
const LoggingDelegate* logDelegate = 0x0,
836825
const RequestValidator* validator = 0x0,
837826
const Unescaper* unescaper = 0x0,
838827
const int maxThreadStackSize = 0,
828+
const bool useSsl = false,
829+
const bool useIpv6 = false,
830+
const bool debug = false,
831+
const bool pedantic = false,
839832
const std::string& httpsMemKey = "",
840833
const std::string& httpsMemCert = "",
841834
const std::string& httpsMemTrust = "",
@@ -885,6 +878,10 @@ class Webserver
885878
void sweetKill();
886879
private:
887880
bool running;
881+
bool useSsl;
882+
bool useIpv6;
883+
bool debug;
884+
bool pedantic;
888885
int port;
889886
int maxThreads;
890887
int maxConnections;
@@ -902,6 +899,7 @@ class Webserver
902899
std::string httpsPriorities;
903900
std::string digestAuthRandom;
904901
HttpUtils::CredType_T credType;
902+
HttpUtils::StartMethod_T startMethod;
905903

906904
std::map<HttpEndpoint, HttpResource* > registeredResources;
907905
struct MHD_Daemon *daemon;
-12 KB
Binary file not shown.

src/java/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ CPPFLAGS =
109109
CXX = g++
110110
CXXCPP = g++ -E
111111
CXXDEPMODE = depmode=gcc3
112-
CXXFLAGS = -g -O0
112+
CXXFLAGS = -g -O2
113113
CYGPATH_W = echo
114114
DEFS = -DHAVE_CONFIG_H
115115
DEPDIR = .deps
@@ -232,9 +232,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
232232
exit 1;; \
233233
esac; \
234234
done; \
235-
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/java/Makefile'; \
235+
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/java/Makefile'; \
236236
$(am__cd) $(top_srcdir) && \
237-
$(AUTOMAKE) --foreign src/java/Makefile
237+
$(AUTOMAKE) --gnu src/java/Makefile
238238
.PRECIOUS: Makefile
239239
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
240240
@case '$?' in \

src/php/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ CPPFLAGS =
109109
CXX = g++
110110
CXXCPP = g++ -E
111111
CXXDEPMODE = depmode=gcc3
112-
CXXFLAGS = -g -O0
112+
CXXFLAGS = -g -O2
113113
CYGPATH_W = echo
114114
DEFS = -DHAVE_CONFIG_H
115115
DEPDIR = .deps
@@ -231,9 +231,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
231231
exit 1;; \
232232
esac; \
233233
done; \
234-
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/php/Makefile'; \
234+
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/php/Makefile'; \
235235
$(am__cd) $(top_srcdir) && \
236-
$(AUTOMAKE) --foreign src/php/Makefile
236+
$(AUTOMAKE) --gnu src/php/Makefile
237237
.PRECIOUS: Makefile
238238
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
239239
@case '$?' in \

src/python/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ CPPFLAGS =
107107
CXX = g++
108108
CXXCPP = g++ -E
109109
CXXDEPMODE = depmode=gcc3
110-
CXXFLAGS = -g -O0
110+
CXXFLAGS = -g -O2
111111
CYGPATH_W = echo
112112
DEFS = -DHAVE_CONFIG_H
113113
DEPDIR = .deps
@@ -229,9 +229,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
229229
exit 1;; \
230230
esac; \
231231
done; \
232-
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/python/Makefile'; \
232+
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/python/Makefile'; \
233233
$(am__cd) $(top_srcdir) && \
234-
$(AUTOMAKE) --foreign src/python/Makefile
234+
$(AUTOMAKE) --gnu src/python/Makefile
235235
.PRECIOUS: Makefile
236236
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
237237
@case '$?' in \

0 commit comments

Comments
 (0)