Skip to content

Commit

Permalink
Merge branch 'master' into fix/mg_server_ports
Browse files Browse the repository at this point in the history
  • Loading branch information
bel2125 authored Dec 23, 2018
2 parents 9640118 + 6399a4b commit 79fcb55
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 334 deletions.
4 changes: 0 additions & 4 deletions VisualStudio/civetweb_lua/civetweb_lua.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\civetweb.h" />
<ClInclude Include="..\..\src\third_party\civetweb_lua.h" />
Expand Down Expand Up @@ -208,7 +205,6 @@
<None Include="..\..\src\mod_duktape.inl" />
<None Include="..\..\src\mod_lua.inl" />
<None Include="..\..\src\timer.inl" />
<None Include="..\..\src\file_ops.inl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
6 changes: 0 additions & 6 deletions VisualStudio/civetweb_lua/civetweb_lua.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
<UniqueIdentifier>{1ef3413b-2315-48f2-ad22-57af6b4f7aca}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\civetweb.h">
<Filter>Header Files</Filter>
Expand Down Expand Up @@ -65,9 +62,6 @@
<None Include="..\..\src\timer.inl">
<Filter>inl files</Filter>
</None>
<None Include="..\..\src\file_ops.inl">
<Filter>inl files</Filter>
</None>
<None Include="..\..\src\mod_duktape.inl">
<Filter>inl files</Filter>
</None>
Expand Down
4 changes: 2 additions & 2 deletions VisualStudio/unit_test/unit_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>REPLACE_CHECK_FOR_LOCAL_DEBUGGING;LOCAL_TEST;USE_IPV6;USE_WEBSOCKET;MEMORY_DEBUGGING;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MAIN_PUBLIC_SERVER=main;LOCAL_TEST;REPLACE_CHECK_FOR_LOCAL_DEBUGGING;LOCAL_TEST;USE_IPV6;USE_WEBSOCKET;MEMORY_DEBUGGING;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src;$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\..\check-0.10.0\;$(ProjectDir)..\..\..\check-0.10.0\src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
Expand All @@ -89,7 +89,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>REPLACE_CHECK_FOR_LOCAL_DEBUGGING;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MAIN_PUBLIC_SERVER=main;LOCAL_TEST;REPLACE_CHECK_FOR_LOCAL_DEBUGGING;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src;$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\..\check-0.10.0\;$(ProjectDir)..\..\..\check-0.10.0\src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
Expand Down
67 changes: 55 additions & 12 deletions examples/embedded_c/embedded_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* License http://opensource.org/licenses/mit-license.php MIT License
*/

#ifdef NO_SSL
#define TEST_WITHOUT_SSL
#endif

/* Simple example program on how to use CivetWeb embedded into a C program. */
#ifdef _WIN32
#include <windows.h>
Expand All @@ -19,7 +23,7 @@


#define DOCUMENT_ROOT "."
#ifdef NO_SSL
#ifndef TEST_WITHOUT_SSL
#ifdef USE_IPV6
#define PORT "[::]:8888,8884"
#else
Expand Down Expand Up @@ -219,6 +223,24 @@ FileHandler(struct mg_connection *conn, void *cbdata)
}


#define MD5_STATIC static
#include "../src/md5.inl"

/* Stringify binary data. Output buffer must be twice as big as input,
* because each byte takes 2 bytes in string representation */
static void
bin2str(char *to, const unsigned char *p, size_t len)
{
static const char *hex = "0123456789abcdef";

for (; len--; p++) {
*to++ = hex[p[0] >> 4];
*to++ = hex[p[0] & 0x0f];
}
*to = '\0';
}


int
field_found(const char *key,
const char *filename,
Expand All @@ -236,7 +258,7 @@ field_found(const char *key,
#else
snprintf(path, pathlen, "/tmp/%s", filename);
#endif
return MG_FORM_FIELD_STORAGE_STORE;
return MG_FORM_FIELD_STORAGE_GET;
}
return MG_FORM_FIELD_STORAGE_GET;
}
Expand All @@ -247,10 +269,33 @@ field_get(const char *key, const char *value, size_t valuelen, void *user_data)
{
struct mg_connection *conn = (struct mg_connection *)user_data;

if (key[0]) {
mg_printf(conn, "%s = ", key);
if ((key != NULL) && (key[0] == '\0')) {
/* Incorrect form data detected */
return MG_FORM_FIELD_HANDLE_ABORT;
}
if ((valuelen > 0) && (value == NULL)) {
/* Unreachable, since this call will not be generated by civetweb. */
return MG_FORM_FIELD_HANDLE_ABORT;
}

if (key) {
mg_printf(conn, "key = %s\n", key);
}
mg_printf(conn, "valuelen = %u\n", valuelen);

if (valuelen > 0) {
/* mg_write(conn, value, valuelen); */

md5_byte_t hash[16];
md5_state_t ctx;
char outputbuf[33];

md5_init(&ctx);
md5_append(&ctx, (const md5_byte_t *)value, valuelen);
md5_finish(&ctx, hash);
bin2str(outputbuf, hash, sizeof(hash));
mg_printf(conn, "value md5 hash = %s\n", outputbuf);
}
mg_write(conn, value, valuelen);

return 0;
}
Expand Down Expand Up @@ -318,8 +363,6 @@ FileUploadForm(struct mg_connection *conn, void *cbdata)
return 1;
}

#define MD5_STATIC static
#include "../src/md5.inl"

struct tfile_checksum {
char name[128];
Expand Down Expand Up @@ -846,7 +889,7 @@ get_dh2236()
#endif


#ifndef NO_SSL
#ifndef TEST_WITHOUT_SSL
int
init_ssl(void *ssl_context, void *user_data)
{
Expand Down Expand Up @@ -900,7 +943,7 @@ main(int argc, char *argv[])
"websocket_timeout_ms",
"3600000",
#endif
#ifndef NO_SSL
#ifndef TEST_WITHOUT_SSL
"ssl_certificate",
"../../resources/cert/server.pem",
"ssl_protocol_version",
Expand All @@ -921,7 +964,7 @@ main(int argc, char *argv[])
int port_cnt, n;
int err = 0;

/* Check if libcivetweb has been built with all required features. */
/* Check if libcivetweb has been built with all required features. */
#ifdef USE_IPV6
if (!mg_check_feature(8)) {
fprintf(stderr,
Expand All @@ -938,7 +981,7 @@ main(int argc, char *argv[])
err = 1;
}
#endif
#ifndef NO_SSL
#ifndef TEST_WITHOUT_SSL
if (!mg_check_feature(2)) {
fprintf(stderr,
"Error: Embedded example built with SSL support, "
Expand All @@ -953,7 +996,7 @@ main(int argc, char *argv[])

/* Start CivetWeb web server */
memset(&callbacks, 0, sizeof(callbacks));
#ifndef NO_SSL
#ifndef TEST_WITHOUT_SSL
callbacks.init_ssl = init_ssl;
#endif
callbacks.log_message = log_message;
Expand Down
20 changes: 10 additions & 10 deletions include/CivetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,16 @@ class CIVETWEB_CXX_API CivetServer
* @return A vector of ports
*/

std::vector<int> getListeningPorts();

/**
* getListeningPorts()
*
* Variant of getListeningPorts() returning the full port information
* (protocol, SSL, ...)
*
* @return A vector of ports
*/
std::vector<int> getListeningPorts();

/**
* getListeningPorts()
*
* Variant of getListeningPorts() returning the full port information
* (protocol, SSL, ...)
*
* @return A vector of ports
*/
std::vector<struct mg_server_port> getListeningPortsFull();

/**
Expand Down
67 changes: 33 additions & 34 deletions src/CivetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ CivetServer::urlDecode(const char *src,
dst.clear();
for (i = j = 0; i < (int)src_len; i++, j++) {
if (i < (int)src_len - 2 && src[i] == '%'
&& isxdigit(*(const unsigned char *)(src + i + 1))
&& isxdigit(*(const unsigned char *)(src + i + 2))) {
a = tolower(*(const unsigned char *)(src + i + 1));
b = tolower(*(const unsigned char *)(src + i + 2));
&& isxdigit((unsigned char)src[i + 1])
&& isxdigit((unsigned char)src[i + 2])) {
a = tolower((unsigned char)src[i + 1]);
b = tolower((unsigned char)src[i + 2]);
dst.push_back((char)((HEXTOI(a) << 4) | HEXTOI(b)));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
Expand Down Expand Up @@ -608,43 +608,42 @@ CivetServer::urlEncode(const char *src,
dst.clear();

for (; src_len > 0; src++, src_len--) {
if (isalnum(*(const unsigned char *)src)
|| strchr(dont_escape, *(const unsigned char *)src) != NULL) {
if (isalnum((unsigned char)*src) || strchr(dont_escape, *src) != NULL) {
dst.push_back(*src);
} else {
dst.push_back('%');
dst.push_back(hex[(*(const unsigned char *)src) >> 4]);
dst.push_back(hex[(*(const unsigned char *)src) & 0xf]);
dst.push_back(hex[(unsigned char)*src >> 4]);
dst.push_back(hex[(unsigned char)*src & 0xf]);
}
}
}

std::vector<int>
CivetServer::getListeningPorts()
{
std::vector<struct mg_server_port> server_ports = getListeningPortsFull();

std::vector<int> ports(server_ports.size());
for (size_t i = 0; i < server_ports.size(); i++) {
ports[i] = server_ports[i].port;
}

return ports;
}

std::vector<struct mg_server_port>
CivetServer::getListeningPortsFull()
{
std::vector<struct mg_server_port> server_ports(50);
int size = mg_get_server_ports(context,
(int)server_ports.size(),
&server_ports[0]);
if (size <= 0) {
server_ports.resize(0);
return server_ports;
}
server_ports.resize(size);
return server_ports;
std::vector<int>
CivetServer::getListeningPorts()
{
std::vector<struct mg_server_port> server_ports = getListeningPortsFull();

std::vector<int> ports(server_ports.size());
for (size_t i = 0; i < server_ports.size(); i++) {
ports[i] = server_ports[i].port;
}

return ports;
}

std::vector<struct mg_server_port>
CivetServer::getListeningPortsFull()
{
std::vector<struct mg_server_port> server_ports(50);
int size = mg_get_server_ports(context,
(int)server_ports.size(),
&server_ports[0]);
if (size <= 0) {
server_ports.resize(0);
return server_ports;
}
server_ports.resize(size);
return server_ports;
}

CivetServer::CivetConnection::CivetConnection()
Expand Down
Loading

0 comments on commit 79fcb55

Please sign in to comment.