Skip to content

Commit 6b1e195

Browse files
authored
Fix cygwin build - moves to C++11 regex
* Fixing cygwin builds by not mingling them with the rest of win32 * Include sys/select.h in CYGWIN to define fd_set * Add fd_set definition ahead of microhttpd.h inclusion * suppress ignore_sigpipe for cygwin * Add missing includes * Remove runguard (not used) * Remove runguard (unused) * Import definition of NI_MAXHOST in cygwin * Force in definitions needed by cygwin * Define u_char for cygwin * Remove regex_replace method that was never used. * Removed regex_replace method that is never used * Remove regex_replace method that is never used * Move to C++11 regex * Use C++11 regex * Prevent object from not being initalized * Use std::regex rather than std::basic_regex * Use std::regex instead of std::basic_regex * Fix typos * Reorder parameters to avoid warnings * Remove requirements for libregex
1 parent b2d69d9 commit 6b1e195

File tree

14 files changed

+62
-95
lines changed

14 files changed

+62
-95
lines changed

configure.ac

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ NETWORK_LIBS=""
6464
case "$host" in
6565
*-mingw*)
6666
NETWORK_HEADER="winsock2.h"
67-
REGEX_LIBS="-lregex -lpthread -no-undefined"
67+
ADDITIONAL_LIBS="-lpthread -no-undefined"
6868
NETWORK_LIBS="-lws2_32"
6969
native_srcdir=$(cd $srcdir; pwd -W)
7070
;;
7171
*-cygwin*)
72-
NETWORK_HEADER="winsock2.h"
73-
REGEX_LIBS="-lregex -lpthread -no-undefined"
72+
NETWORK_HEADER="arpa/inet.h"
73+
ADDITIONAL_LIBS="-lpthread -no-undefined"
7474
;;
7575
*)
7676
NETWORK_HEADER="arpa/inet.h"
77-
REGEX_LIBS=""
77+
ADDITIONAL_LIBS=""
7878
is_windows=no
7979
;;
8080
esac
@@ -86,7 +86,6 @@ AC_CHECK_HEADER([inttypes.h],[],[AC_MSG_ERROR("inttypes.h not found")])
8686
AC_CHECK_HEADER([errno.h],[],[AC_MSG_ERROR("errno.h not found")])
8787
AC_CHECK_HEADER([unistd.h],[],[AC_MSG_ERROR("unistd.h not found")])
8888
AC_CHECK_HEADER([ctype.h],[],[AC_MSG_ERROR("cctype not found")])
89-
AC_CHECK_HEADER([regex.h],[],[AC_MSG_ERROR("regex.h not found")])
9089
AC_CHECK_HEADER([sys/stat.h],[],[AC_MSG_ERROR("sys/stat.h not found")])
9190
AC_CHECK_HEADER([sys/types.h],[],[AC_MSG_ERROR("sys/types.h not found")])
9291
AC_CHECK_HEADER([$NETWORK_HEADER],[],[AC_MSG_ERROR("$NETWORK_HEADER not found")])
@@ -117,7 +116,7 @@ if test x"$host" = x"$build"; then
117116
)
118117

119118
CXXFLAGS="-std=c++11 -DHTTPSERVER_COMPILATION -D_REENTRANT $LIBMICROHTTPD_CFLAGS $CXXFLAGS"
120-
LDFLAGS="$LIBMICROHTTPD_LIBS $NETWORK_LIBS $REGEX_LIBS $LDFLAGS"
119+
LDFLAGS="$LIBMICROHTTPD_LIBS $NETWORK_LIBS $ADDITIONAL_LIBS $LDFLAGS"
121120

122121
cond_cross_compile="no"
123122
else
@@ -130,7 +129,7 @@ else
130129
)
131130

132131
CXXFLAGS="-std=c++11 -DHTTPSERVER_COMPILATION -D_REENTRANT $CXXFLAGS"
133-
LDFLAGS="$NETWORK_LIBS $REGEX_LIBS $LDFLAGS"
132+
LDFLAGS="$NETWORK_LIBS $ADDITIONAL_LIBS $LDFLAGS"
134133

135134
cond_cross_compile="yes"
136135
fi

src/details/http_endpoint.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ using namespace http;
3535

3636
http_endpoint::~http_endpoint()
3737
{
38-
if(reg_compiled)
39-
{
40-
regfree(&re_url_normalized);
41-
}
4238
}
4339

4440
http_endpoint::http_endpoint
@@ -119,9 +115,7 @@ http_endpoint::http_endpoint
119115
if(use_regex)
120116
{
121117
url_normalized += "$";
122-
regcomp(&re_url_normalized, url_normalized.c_str(),
123-
REG_EXTENDED|REG_ICASE|REG_NOSUB
124-
);
118+
re_url_normalized = std::regex(url_normalized, std::regex::extended | std::regex::icase | std::regex::nosubs);
125119
reg_compiled = true;
126120
}
127121
}
@@ -133,12 +127,9 @@ http_endpoint::http_endpoint(const http_endpoint& h):
133127
url_pieces(h.url_pieces),
134128
chunk_positions(h.chunk_positions),
135129
family_url(h.family_url),
136-
reg_compiled(h.reg_compiled)
130+
reg_compiled(h.reg_compiled),
131+
re_url_normalized(h.re_url_normalized)
137132
{
138-
if(reg_compiled)
139-
regcomp(&re_url_normalized, url_normalized.c_str(),
140-
REG_EXTENDED|REG_ICASE|REG_NOSUB
141-
);
142133
}
143134

144135
http_endpoint& http_endpoint::operator =(const http_endpoint& h)
@@ -147,14 +138,7 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
147138
url_normalized = h.url_normalized;
148139
family_url = h.family_url;
149140
reg_compiled = h.reg_compiled;
150-
if(reg_compiled)
151-
{
152-
regfree(&re_url_normalized);
153-
154-
regcomp(&re_url_normalized, url_normalized.c_str(),
155-
REG_EXTENDED|REG_ICASE|REG_NOSUB
156-
);
157-
}
141+
re_url_normalized = h.re_url_normalized;
158142
url_pars = h.url_pars;
159143
url_pieces = h.url_pieces;
160144
chunk_positions = h.chunk_positions;
@@ -171,7 +155,9 @@ bool http_endpoint::match(const http_endpoint& url) const
171155
if (!reg_compiled) throw std::invalid_argument("Cannot run match. Regex suppressed.");
172156

173157
if(!family_url || url.url_pieces.size() < url_pieces.size())
174-
return regexec(&re_url_normalized, url.url_complete.c_str(), 0, NULL, 0) == 0;
158+
{
159+
return regex_match(url.url_complete, re_url_normalized);
160+
}
175161

176162
string nn = "/";
177163
bool first = true;
@@ -180,7 +166,7 @@ bool http_endpoint::match(const http_endpoint& url) const
180166
nn += (first ? "" : "/") + url.url_pieces[i];
181167
first = false;
182168
}
183-
return regexec(&re_url_normalized, nn.c_str(), 0, NULL, 0) == 0;
169+
return regex_match(nn, re_url_normalized);
184170
}
185171

186172
};

src/http_utils.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020

2121
#include "httpserver/http_utils.hpp"
2222

23-
#if defined(__MINGW32__) || defined(__CYGWIN32__)
23+
#if defined(_WIN32) && ! defined(__CYGWIN__)
2424
#include <winsock2.h>
2525
#include <ws2tcpip.h>
26-
#else
26+
#else // WIN32 check
2727
#if defined(__FreeBSD__)
2828
#include <netinet/in.h>
29-
#endif
30-
#include <sys/socket.h>
31-
#include <netdb.h>
29+
#endif // FreeBSD
3230
#include <arpa/inet.h>
33-
#endif
31+
#include <netdb.h>
32+
#include <netinet/in.h>
33+
#include <sys/socket.h>
34+
#include <sys/types.h>
35+
#endif // WIN32 check
3436

3537
#include <stdio.h>
3638
#include <stdlib.h>
@@ -40,6 +42,7 @@
4042
#include <iostream>
4143
#include <sstream>
4244
#include <stdexcept>
45+
#include <utility>
4346

4447
#include "httpserver/string_utilities.hpp"
4548

@@ -48,6 +51,19 @@
4851
#define SET_BIT(var,pos) ((var) |= 1 << (pos))
4952
#define CLEAR_BIT(var,pos) ((var) &= ~(1<<(pos)))
5053

54+
#if defined (__CYGWIN__)
55+
56+
#if ! defined (NI_MAXHOST)
57+
#define NI_MAXHOST 1025
58+
#endif // NI_MAXHOST
59+
60+
#ifndef __u_char_defined
61+
typedef unsigned char u_char;
62+
#define __u_char_defined
63+
#endif // __u_char_defined
64+
65+
#endif // CYGWIN
66+
5167
using namespace std;
5268

5369
namespace httpserver {

src/httpserver/details/http_endpoint.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef _HTTP_ENDPOINT_HPP_
2626
#define _HTTP_ENDPOINT_HPP_
2727

28-
#include <regex.h>
28+
#include <regex>
2929
#include <stdexcept>
3030
#include <string>
3131
#include <utility>
@@ -136,6 +136,7 @@ class http_endpoint
136136
http_endpoint():
137137
url_complete("/"),
138138
url_normalized("/"),
139+
re_url_normalized(std::regex("")), // initialize empty
139140
family_url(false),
140141
reg_compiled(false)
141142
{
@@ -186,7 +187,7 @@ class http_endpoint
186187
/**
187188
* Regex used in comparisons
188189
**/
189-
regex_t re_url_normalized;
190+
std::regex re_url_normalized;
190191

191192
/**
192193
* Boolean indicating wheter the endpoint represents a family

src/httpserver/http_utils.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
#define _WIN32_WINNT 0x600
3838
#endif
3939

40+
// needed to have the fd_set definition ahead of microhttpd.h import
41+
#if defined(__CYGWIN__)
42+
#include <sys/select.h>
43+
#endif
44+
4045
#include <microhttpd.h>
4146
#include <algorithm>
4247
#include <cctype>
@@ -72,7 +77,7 @@ class http_utils
7277

7378
enum start_method_T
7479
{
75-
#if defined(__MINGW32__) || defined(__CYGWIN32__)
80+
#if defined(__MINGW32__) || defined(__CYGWIN__)
7681
#ifdef ENABLE_POLL
7782
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY | MHD_USE_POLL,
7883
#else

src/httpserver/string_utilities.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ const std::string to_lower_copy(const std::string& str);
4444
const std::vector<std::string> string_split(const std::string& s,
4545
char sep = ' ', bool collapse = true
4646
);
47-
const std::string regex_replace(const std::string& str, const std::string& pattern,
48-
const std::string& replace_str
49-
);
5047
void to_upper(std::string& str);
5148
};
5249
};

src/httpserver/webserver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ class webserver
174174
bool single_resource;
175175
bool tcp_nodelay;
176176
pthread_mutex_t mutexwait;
177-
pthread_rwlock_t runguard;
178177
pthread_cond_t mutexcond;
179178
render_ptr not_found_resource;
180179
render_ptr method_not_allowed_resource;

src/string_utilities.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "httpserver/string_utilities.hpp"
2222

23-
#include <regex.h>
2423
#include <algorithm>
2524
#include <cctype>
2625
#include <cstdlib>
@@ -85,43 +84,5 @@ const std::vector<std::string> string_split(
8584
return result;
8685
}
8786

88-
const std::string regex_replace(const std::string& str,
89-
const std::string& pattern,
90-
const std::string& replace_str
91-
)
92-
{
93-
regex_t preg;
94-
regmatch_t substmatch[1];
95-
regcomp(&preg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
96-
std::string result;
97-
if ( regexec(&preg, str.c_str(), 1, substmatch, 0) == 0 )
98-
{
99-
char ns[substmatch[0].rm_so + 1 +
100-
replace_str.size() + (str.size() - substmatch[0].rm_eo) + 2
101-
];
102-
103-
memcpy(ns, str.c_str(), substmatch[0].rm_so+1);
104-
105-
memcpy(&ns[substmatch[0].rm_so],
106-
replace_str.c_str(),
107-
replace_str.size()
108-
);
109-
110-
memcpy(&ns[substmatch[0].rm_so+replace_str.size()],
111-
&str[substmatch[0].rm_eo], str.substr(substmatch[0].rm_eo).size()
112-
);
113-
114-
ns[substmatch[0].rm_so +
115-
replace_str.size() +
116-
str.substr(substmatch[0].rm_eo).size()
117-
] = 0;
118-
119-
result = std::string((char*)ns);
120-
}
121-
regfree(&preg);
122-
123-
return result;
124-
}
125-
12687
};
12788
};

src/webserver.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020

2121
#include "httpserver/webserver.hpp"
2222

23-
#if defined(__MINGW32__) || defined(__CYGWIN32__)
23+
#if defined(_WIN32) && ! defined(__CYGWIN__)
2424
#include <winsock2.h>
2525
#include <ws2tcpip.h>
2626
#define _WINDOWS
2727
#else
2828
#if defined(__FreeBSD__)
2929
#include <netinet/in.h>
3030
#endif
31+
#if defined(__CYGWIN__)
32+
#include <sys/select.h>
33+
#endif
3134
#include <netinet/ip.h>
3235
#include <netinet/tcp.h>
3336
#endif
@@ -47,6 +50,13 @@
4750
#include <unistd.h>
4851
#include <algorithm>
4952
#include <iostream>
53+
#include <strings.h>
54+
#include <cstring>
55+
#include <exception>
56+
#include <memory>
57+
#include <stdexcept>
58+
#include <utility>
59+
#include <vector>
5060

5161
#include "gettext.h"
5262
#include "httpserver/create_webserver.hpp"
@@ -88,7 +98,7 @@ struct compare_value
8898
}
8999
};
90100

91-
#ifndef __MINGW32__
101+
#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
92102
static void catcher (int sig)
93103
{
94104
}
@@ -97,7 +107,7 @@ static void catcher (int sig)
97107
static void ignore_sigpipe ()
98108
{
99109
//Mingw doesn't implement SIGPIPE
100-
#ifndef __MINGW32__
110+
#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
101111
struct sigaction oldsig;
102112
struct sigaction sig;
103113

@@ -160,15 +170,13 @@ webserver::webserver(const create_webserver& params):
160170
{
161171
ignore_sigpipe();
162172
pthread_mutex_init(&mutexwait, NULL);
163-
pthread_rwlock_init(&runguard, NULL);
164173
pthread_cond_init(&mutexcond, NULL);
165174
}
166175

167176
webserver::~webserver()
168177
{
169178
stop();
170179
pthread_mutex_destroy(&mutexwait);
171-
pthread_rwlock_destroy(&runguard);
172180
pthread_cond_destroy(&mutexcond);
173181
}
174182

test/integ/authentication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
USA
1919
*/
2020

21-
#if defined(__MINGW32__) || defined(__CYGWIN32__)
21+
#if defined(_WIN32) && ! defined(__CYGWIN__)
2222
#define _WINDOWS
2323
#undef _WIN32_WINNT
2424
#define _WIN32_WINNT 0x600

0 commit comments

Comments
 (0)