浏览代码

Interface change: struct mg_error_data

The "struct mg_error_data" is used to return error information.
It is used only in functions marked as "experimental", and was never fully implemented.
In particular, there was a pointer "code". The latest release did not do anything
useful with this pointer: it was set to 0 once, and neigher read nor written later.
It is only additional complexity with no benefit to have a pointer here instead of a value.
Therefore this experimental interface has been modified to provide a value.

Now the civetweb.c code will set mg_error_data.code to a reasonable error code.
The values of all error codes have been defined in civetweb.h as MG_ERROR_DATA_CODE_*
In addition to "code" there is also a new member "code_sub".
The interpretation of this value depends on "code".
In combination "code" and "code_sub" should provide enough error information, embedding
code cound use it for language specific error messages.
bel2125 3 年之前
父节点
当前提交
e037c4e850
共有 5 个文件被更改,包括 250 次插入292 次删除
  1. 13 2
      docs/api/mg_error_data.md
  2. 1 3
      examples/embed_certificate/ec_example.c
  3. 7 6
      include/civetweb.h
  4. 218 271
      src/civetweb.c
  5. 11 10
      src/main.c

+ 13 - 2
docs/api/mg_error_data.md

@@ -6,13 +6,24 @@
 
 | Field | Type | Description |
 | :--- | :--- | :--- |
-|**`code`**|`unsigned *`| A pointer to an `unsigned` variable, to store the error code. |
+|**`code`**|`unsigned *`| Error code (see `MG_ERROR_DATA_CODE_*`). |
+|**`code_sub`**|`unsigned *`| Error sub code, depending on error code. |
 |**`text`**|`char *`| A text buffer to store the error text. |
 |**`text_buffer_size`**|`size_t`| Size of the text buffer. |
 
 ### Description
 
-The structure `mg_error_data` is used in [`mg_start2()`](mg_start.md), [`mg_start_domain2();`](mg_start_domain2.md), [`mg_connect_client2();`](mg_connect_client2.md) and [`mg_get_response2();`](mg_get_response2.md) to return error information.
+The structure `mg_error_data` is used to return error information.
+The `code` number will be set to one of the `MG_ERROR_DATA_CODE_*` values defined in civetweb.h.
+
+The meaning of the `code_sub` number will depend on the value of `code`.
+The `code_sub` member is experimental and may change in future versions.
+
+The optional pointer `text` can be used to provide storage for a textual error message.
+The size of the provided `text` pointer must be set in `text_buffer_size`.
+If no textual error message is required and no buffer is probided, `text_buffer_size` must be set to 0.
+
+Currently `struct mg_error_data` is used by the functions [`mg_start2()`](mg_start.md), [`mg_start_domain2();`](mg_start_domain2.md), [`mg_connect_client2();`](mg_connect_client2.md) and [`mg_get_response2();`](mg_get_response2.md).
 
 ### See Also
 

+ 1 - 3
examples/embed_certificate/ec_example.c

@@ -348,8 +348,6 @@ main(int argc, char *argv[])
 	init.configuration_options = options;
 
 	struct mg_error_data error = {0};
-	unsigned error_code = 0;
-	error.code = &error_code;
 	char error_text[256] = {0};
 	error.text = error_text;
 	error.text_buffer_size = sizeof(error_text);
@@ -362,7 +360,7 @@ main(int argc, char *argv[])
 		printf("Server exit\n");
 		mg_stop(ctx);
 	} else {
-		printf("Initialization failed: %u\n%s\n\n", error_code, error_text);
+		printf("Initialization failed: %u\n%s\n\n", error.code, error.text);
 	}
 
 	mg_exit_library();

+ 7 - 6
include/civetweb.h

@@ -1687,13 +1687,14 @@ CIVETWEB_API int mg_get_connection_info(const struct mg_context *ctx,
    Note: Experimental interfaces may change
 */
 struct mg_error_data {
-	unsigned *code;          /* buffer for error code (number) */
+	unsigned code;           /* error code (number) */
+	unsigned code_sub;       /* error sub code (number) */
 	char *text;              /* buffer for error text */
 	size_t text_buffer_size; /* size of buffer of "text" */
 };
 
 
-/* Error codes in mg_error_data */
+/* Values for error "code" in mg_error_data */
 enum {
 	/* No error */
 	MG_ERROR_DATA_CODE_OK = 0u,
@@ -1775,20 +1776,20 @@ mg_connect_client2(const char *host,
                    int port,
                    const char *path,
                    struct mg_init_data *init,
-                   const struct mg_error_data *error);
+                   struct mg_error_data *error);
 
 CIVETWEB_API int mg_get_response2(struct mg_connection *conn,
-                                  const struct mg_error_data *error,
+                                  struct mg_error_data *error,
                                   int timeout);
 #endif
 
 
 CIVETWEB_API struct mg_context *mg_start2(struct mg_init_data *init,
-                                          const struct mg_error_data *error);
+                                          struct mg_error_data *error);
 
 CIVETWEB_API int mg_start_domain2(struct mg_context *ctx,
                                   const char **configuration_options,
-                                  const struct mg_error_data *error);
+                                  struct mg_error_data *error);
 
 
 #ifdef __cplusplus

文件差异内容过多而无法显示
+ 218 - 271
src/civetweb.c


+ 11 - 10
src/main.c

@@ -173,7 +173,7 @@ extern char *_getcwd(char *buf, size_t size);
 #define PATH_MAX (1024)
 #endif
 
-#define MAX_OPTIONS (50)
+#define MAX_OPTIONS (100) /* TODO: Read from civetweb.c ? */
 #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
 
 struct tuser_data {
@@ -1171,8 +1171,10 @@ sanitize_options(char *options[] /* server options */,
 }
 
 
-/* Forward declaration: */
+#ifdef _WIN32
+/* Forward declaration for Windows only */
 static void show_settings_dialog(void);
+#endif
 
 
 static void
@@ -1183,7 +1185,6 @@ start_civetweb(int argc, char *argv[])
 	struct mg_init_data init;
 	struct mg_error_data error;
 	char error_text[256];
-	unsigned error_code;
 	int i;
 
 	/* Start option -I:
@@ -1331,7 +1332,6 @@ start_civetweb(int argc, char *argv[])
 	memset(&error, 0, sizeof(error));
 	error.text = error_text;
 	error.text_buffer_size = sizeof(error_text);
-	error.code = &error_code;
 
 	/* Start Civetweb */
 	g_ctx = mg_start2(&init, &error);
@@ -1346,14 +1346,15 @@ start_civetweb(int argc, char *argv[])
 	if (g_ctx == NULL) {
 #ifdef _WIN32
 		/* On Windows: provide option to edit configuration file. */
-		char errtxt[1024];
+		char msgboxtxt[1024];
 		int ret;
-		sprintf(errtxt,
+		sprintf(msgboxtxt,
 		        "Failed to start %s with code %u:\n%s\n\nEdit settings?",
 		        g_server_name,
-		        error_code,
+		        error.code,
 		        error_text);
-		ret = MessageBox(NULL, errtxt, "Error", MB_ICONERROR | MB_YESNOCANCEL);
+		ret =
+		    MessageBox(NULL, msgboxtxt, "Error", MB_ICONERROR | MB_YESNOCANCEL);
 		if (ret == IDYES) {
 			show_settings_dialog();
 
@@ -1366,7 +1367,7 @@ start_civetweb(int argc, char *argv[])
 #else
 		die("Failed to start %s with code %u:\n%s",
 		    g_server_name,
-		    error_code,
+		    error.code,
 		    error_text);
 #endif
 	}
@@ -2200,7 +2201,7 @@ optioncmp(const char *o1, const char *o2)
 
 
 static void
-show_settings_dialog()
+show_settings_dialog(void)
 {
 	/* Parameter for size/format tuning of the dialog */
 	short HEIGHT = 15;

部分文件因为文件数量过多而无法显示