Jelajahi Sumber

Merge branch 'master' of https://github.com/civetweb/civetweb

bel2125 6 tahun lalu
induk
melakukan
0bfcd5cd37

+ 34 - 0
CMakeLists.txt

@@ -35,6 +35,7 @@ set(CIVETWEB_VERSION_MAJOR "${CMAKE_MATCH_1}")
 set(CIVETWEB_VERSION_MINOR "${CMAKE_MATCH_2}")
 set(CIVETWEB_VERSION_PATCH "${CMAKE_MATCH_3}")
 determine_target_architecture(CIVETWEB_ARCHITECTURE)
+include(GNUInstallDirs)
 
 # Detect the platform reliably
 if(NOT MACOSX AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -485,6 +486,39 @@ if (CIVETWEB_BUILD_TESTING)
   add_subdirectory(unittest)
 endif()
 
+# cmake config file
+
+include(CMakePackageConfigHelpers)
+
+install(
+  EXPORT ${PROJECT_NAME}-targets
+  NAMESPACE ${PROJECT_NAME}::
+  FILE ${PROJECT_NAME}-targets.cmake
+  DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
+  COMPONENT civetweb-cmake-config
+)
+
+configure_package_config_file(
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
+  ${PROJECT_NAME}-config.cmake
+  INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
+  NO_CHECK_REQUIRED_COMPONENTS_MACRO
+  PATH_VARS CMAKE_INSTALL_INCLUDEDIR
+)
+
+write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake
+  VERSION ${CIVETWEB_VERSION}
+  COMPATIBILITY AnyNewerVersion
+)
+
+install(
+  FILES
+    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
+    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
+  DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
+  COMPONENT civetweb-cmake-config
+)
+
 # Set up CPack
 include(InstallRequiredSystemLibraries)
 set(CPACK_PACKAGE_VENDOR "civetweb Contributors")

+ 6 - 0
cmake/civetweb-config.cmake.in

@@ -0,0 +1,6 @@
+@PACKAGE_INIT@
+include(CMakeFindDependencyMacro)
+
+set_and_check(civetweb_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
+
+include("${CMAKE_CURRENT_LIST_DIR}/civetweb-targets.cmake")

+ 9 - 0
docs/Contribution.md

@@ -21,3 +21,12 @@ However, a pull request would be preferred.
 
 - In case you think you found a security issue that should be evaluated and fixed before public disclosure, feel free to write an email.  Although CivetWeb is a fork from Mongoose from 2013, the code bases are different now, so security vulnerabilities of Mongoose usually do not affect CivetWeb.  Open an issue for Mongoose vulnerabilities you want to have checked if CivetWeb is affected.
 
+
+Why does a pull request need a description?
+---
+
+I'm asking for this, because I usually review all pull requests.
+Fhe first thing I check is: "What is the intention of the fix **according to the description**?" and "Does the code really fix it?".
+Second: "Do I except side effects?".
+Third: "Is there a better way to fix the issue **explained in the description**?"
+I don't like to "reverse engineer" the intention of the fix from the diff (although it may be obvious to the author of the PR, sometimes it's not for others). But you should also do it for yourself: You will get early feedback if your changes are not doing what you expect, or if there is a much more effective way to reach the same goal. Finally it will help all other users, since it helps writing better release notes.

+ 1 - 1
docs/api/mg_get_response_info.md

@@ -12,7 +12,7 @@
 
 | Type | Description |
 | :--- | :--- |
-|`const struct mg_request_info *`|Pointer to the requested info, or NULL if an error occurred|
+|`const struct mg_response_info *`|Pointer to the response info, or NULL if an error occurred|
 
 ### Description
 

+ 27 - 0
docs/api/mg_read.md

@@ -20,6 +20,33 @@
 
 The function `mg_read()` receives data over an existing connection. The data is handled as binary and is stored in a buffer whose address has been provided as a parameter. The function returns the number of read bytes when successful, the value **0** when the connection has been closed by peer and a negative value when no more data could be read from the connection.
 
+### Example
+
+```
+#define RECV_BUF_SIZE 1 << 20
+
+size_t read_data(struct mg_connection* conn,
+                        uint8_t* buff,
+                        size_t buff_len) {
+  size_t read_len = 0;
+  while (read_len < buff_len) {
+    size_t sz_to_read = std::min<size_t>(RECV_BUF_SIZE, buff_len - read_len);
+    int this_read = mg_read(conn, buff + read_len, sz_to_read);
+    if (this_read < 0) {
+      std::cerr << "[error] Failed to read data" << std::endl;
+      break;
+    } else {
+      read_len += size_t(this_read);
+      if (this_read > 0) {
+        std::cout << "[debug] Received " << this_read << " more bytes" << std::endl;
+      }
+    }
+  }
+
+  return read_len;
+}
+```
+
 ### See Also
 
 * [`mg_printf();`](mg_printf.md)

+ 2 - 2
examples/ws_client/ws_client.c

@@ -43,11 +43,11 @@ msgtypename(int flags)
 	case MG_WEBSOCKET_OPCODE_BINARY:
 		return "binary";
 	case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
-		return "clonnection close";
+		return "connection close";
 	case MG_WEBSOCKET_OPCODE_PING:
 		return "PING";
 	case MG_WEBSOCKET_OPCODE_PONG:
-		return "PING";
+		return "PONG";
 	}
 	return "unknown";
 }

+ 14 - 4
include/CivetServer.h

@@ -366,18 +366,28 @@ class CIVETWEB_CXX_API CivetServer
 	 * @return A vector of ports
 	 */
 
-	std::vector<int> getListeningPorts();
+	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_ports> getListeningPortsFull();
 
 	/**
 	 * getCookie(struct mg_connection *conn, const std::string &cookieName,
-	 *std::string &cookieValue)
+	 * std::string &cookieValue)
 	 *
 	 * Puts the cookie value string that matches the cookie name in the
-	 *cookieValue destinaton string.
+	 * cookieValue destination string.
 	 *
 	 * @param conn - the connection information
 	 * @param cookieName - cookie name to get the value from
-	 * @param cookieValue - cookie value is returned using thiis reference
+	 * @param cookieValue - cookie value is returned using this reference
 	 * @returns the size of the cookie value string read.
 	 */
 	static int getCookie(struct mg_connection *conn,

+ 31 - 21
src/CMakeLists.txt

@@ -6,6 +6,7 @@ endif()
 add_library(civetweb-c-library ${LIB_TYPE} civetweb.c)
 set_target_properties(civetweb-c-library PROPERTIES
   OUTPUT_NAME "civetweb"
+  EXPORT_NAME "civetweb"
   VERSION ${CIVETWEB_VERSION}
   SOVERSION ${CIVETWEB_VERSION}
 )
@@ -14,16 +15,18 @@ if (BUILD_SHARED_LIBS)
 endif()
 target_include_directories(
   civetweb-c-library PUBLIC
-  ${PROJECT_SOURCE_DIR}/include)
+  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
 install(
   TARGETS civetweb-c-library
-  ARCHIVE DESTINATION lib
-  LIBRARY DESTINATION lib
-  RUNTIME DESTINATION bin
-  COMPONENT civetweb-c-library)
+  EXPORT ${PROJECT_NAME}-targets
+  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+  COMPONENT civetweb-c-library
+  INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 install(FILES
   ${PROJECT_SOURCE_DIR}/include/civetweb.h
-  DESTINATION include
+  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
   COMPONENT civetweb-c-library)
 
 # Need Windows sockets if available
@@ -247,19 +250,21 @@ endif()
 if (CIVETWEB_ENABLE_SERVER_EXECUTABLE)
     add_executable(civetweb-c-executable main.c)
     set_target_properties(civetweb-c-executable PROPERTIES
+        EXPORT_NAME "server"
         OUTPUT_NAME "civetweb"
     )
     if (CIVETWEB_INSTALL_EXECUTABLE)
         install(
             TARGETS civetweb-c-executable
-            ARCHIVE DESTINATION lib
-            LIBRARY DESTINATION lib
-            RUNTIME DESTINATION bin
+            EXPORT ${PROJECT_NAME}-targets
+            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
             COMPONENT server)
     endif()
     target_include_directories(
         civetweb-c-executable PUBLIC
-        ${PROJECT_SOURCE_DIR}/include)
+        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
     target_link_libraries(civetweb-c-executable civetweb-c-library)
     if (LIBRT_FOUND)
         target_link_libraries(civetweb-c-executable LIBRT::LIBRT)
@@ -275,19 +280,22 @@ if (CIVETWEB_ENABLE_LUA)
   )
   target_include_directories(
     lua-library PUBLIC
-    ${PROJECT_SOURCE_DIR}/src/third_party/lua-5.2.4)
+    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/third_party/lua-5.2.4>)
   install(
     TARGETS lua-library
-    ARCHIVE DESTINATION lib
-    LIBRARY DESTINATION lib
-    RUNTIME DESTINATION bin
-    COMPONENT lua-library)
+    EXPORT ${PROJECT_NAME}-targets
+    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+    COMPONENT lua-library
+    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 endif()
 
 # The C++ API library
 if (CIVETWEB_ENABLE_CXX)
   add_library(civetweb-cpp ${LIB_TYPE} CivetServer.cpp)
   set_target_properties(civetweb-cpp PROPERTIES
+    EXPORT_NAME "civetweb-cpp"
     OUTPUT_NAME "civetweb-cpp"
     VERSION ${CIVETWEB_VERSION}
     SOVERSION ${CIVETWEB_VERSION}
@@ -300,15 +308,17 @@ if (CIVETWEB_ENABLE_CXX)
 	civetweb-c-library)
   target_include_directories(
     civetweb-cpp PUBLIC
-    ${PROJECT_SOURCE_DIR}/include)
+    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
   install(
     TARGETS civetweb-cpp
-    ARCHIVE DESTINATION lib
-    LIBRARY DESTINATION lib
-    RUNTIME DESTINATION bin
-    COMPONENT civetweb-cpp)
+    EXPORT ${PROJECT_NAME}-targets
+    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+    COMPONENT civetweb-cpp
+    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
   install(FILES
     ${PROJECT_SOURCE_DIR}/include/CivetServer.h
-    DESTINATION include
+    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
     COMPONENT civetweb-cpp)
 endif()

+ 27 - 22
src/CivetServer.cpp

@@ -582,9 +582,7 @@ CivetServer::getPostData(struct mg_connection *conn)
 	char buf[2048];
 	int r = mg_read(conn, buf, sizeof(buf));
 	while (r > 0) {
-		std::string p = std::string(buf);
-		p.resize(r);
-		postdata += p;
+		postdata += std::string(buf, r);
 		r = mg_read(conn, buf, sizeof(buf));
 	}
 	mg_unlock_connection(conn);
@@ -621,25 +619,32 @@ CivetServer::urlEncode(const char *src,
 	}
 }
 
-std::vector<int>
-CivetServer::getListeningPorts()
-{
-	std::vector<int> ports(50);
-	std::vector<struct mg_server_ports> server_ports(50);
-	int size = mg_get_server_ports(context,
-	                               (int)server_ports.size(),
-	                               &server_ports[0]);
-	if (size <= 0) {
-		ports.resize(0);
-		return ports;
-	}
-	ports.resize(size);
-	server_ports.resize(size);
-	for (int i = 0; i < size; i++) {
-		ports[i] = server_ports[i].port;
-	}
-
-	return ports;
+std::vector<int>
+CivetServer::getListeningPorts()
+{
+	std::vector<struct mg_server_ports> 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_ports>
+CivetServer::getListeningPortsFull()
+{
+	std::vector<struct mg_server_ports> 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()

+ 14 - 16
src/civetweb.c

@@ -9083,15 +9083,14 @@ print_dir_entry(struct de *de)
 	}
 	mg_url_encode(de->file_name, href, hrefsize);
 	mg_printf(de->conn,
-	          "<tr><td><a href=\"%s%s%s\">%s%s</a></td>"
-	          "<td>&nbsp;%s</td><td>&nbsp;&nbsp;%s</td></tr>\n",
-	          de->conn->request_info.local_uri,
-	          href,
-	          de->file.is_directory ? "/" : "",
-	          de->file_name,
-	          de->file.is_directory ? "/" : "",
-	          mod,
-	          size);
+              "<tr><td><a href=\"%s%s\">%s%s</a></td>"
+              "<td>&nbsp;%s</td><td>&nbsp;&nbsp;%s</td></tr>\n",
+              href,
+              de->file.is_directory ? "/" : "",
+              de->file_name,
+              de->file.is_directory ? "/" : "",
+              mod,
+              size);
 	mg_free(href);
 	return 0;
 }
@@ -9373,13 +9372,12 @@ handle_directory_request(struct mg_connection *conn, const char *dir)
 
 	/* Print first entry - link to a parent directory */
 	mg_printf(conn,
-	          "<tr><td><a href=\"%s%s\">%s</a></td>"
-	          "<td>&nbsp;%s</td><td>&nbsp;&nbsp;%s</td></tr>\n",
-	          conn->request_info.local_uri,
-	          "..",
-	          "Parent directory",
-	          "-",
-	          "-");
+              "<tr><td><a href=\"%s\">%s</a></td>"
+              "<td>&nbsp;%s</td><td>&nbsp;&nbsp;%s</td></tr>\n",
+              "..",
+              "Parent directory",
+              "-",
+              "-");
 
 	/* Sort and print directory entries */
 	if (data.entries != NULL) {