Parcourir la source

Add error msg to http_error callback (#560)

bel2125 il y a 7 ans
Parent
commit
1d32f0ec41
3 fichiers modifiés avec 13 ajouts et 7 suppressions
  1. 1 1
      docs/api/mg_callbacks.md
  2. 10 5
      include/civetweb.h
  3. 2 1
      src/civetweb.c

+ 1 - 1
docs/api/mg_callbacks.md

@@ -14,7 +14,7 @@
 | |The callback function `end_request()` is called by CivetWeb when a request has been completely processed. It sends the reply status code which was sent to the client to the application.|
 | |The callback function `end_request()` is called by CivetWeb when a request has been completely processed. It sends the reply status code which was sent to the client to the application.|
 |**`exit_context`**|**`void (*exit_context)( const struct mg_context *ctx );`**|
 |**`exit_context`**|**`void (*exit_context)( const struct mg_context *ctx );`**|
 | |The callback function `exit_context()` is called by CivetWeb when the server is stopped. It allows the application to do some cleanup on the application side.|
 | |The callback function `exit_context()` is called by CivetWeb when the server is stopped. It allows the application to do some cleanup on the application side.|
-|**`http_error`**|**`int (*http_error)( struct mg_connection *conn, int status );`**|
+|**`http_error`**|**`int (*http_error)( struct mg_connection *conn, int status, const char *msg );`**|
 | |The callback function `http_error()` is called by CivetWeb just before an HTTP error is to be sent to the client. The function allows the application to send a custom error page. The status code of the error is provided as a parameter. If the application sends their own error page, it must return 1 to signal CivetWeb that no further processing is needed. If the returned value is 0, CivetWeb will send a built-in error page to the client.|
 | |The callback function `http_error()` is called by CivetWeb just before an HTTP error is to be sent to the client. The function allows the application to send a custom error page. The status code of the error is provided as a parameter. If the application sends their own error page, it must return 1 to signal CivetWeb that no further processing is needed. If the returned value is 0, CivetWeb will send a built-in error page to the client.|
 |**`init_context`**|**`void (*init_context)( const struct mg_context *ctx );`**|
 |**`init_context`**|**`void (*init_context)( const struct mg_context *ctx );`**|
 | |The callback function `init_context()` is called after the CivetWeb server has been started and initialized, but before any requests are served. This allowes the application to perform some initialization activities before the first requests are handled.|
 | |The callback function `init_context()` is called after the CivetWeb server has been started and initialized, but before any requests are served. This allowes the application to perform some initialization activities before the first requests are handled.|

+ 10 - 5
include/civetweb.h

@@ -253,14 +253,14 @@ struct mg_callbacks {
 	/* Called when civetweb is about to create or free a SSL_CTX.
 	/* Called when civetweb is about to create or free a SSL_CTX.
 	Parameters:
 	Parameters:
 	   ssl_ctx: SSL_CTX pointer. NULL at creation time, Not NULL when mg_context
 	   ssl_ctx: SSL_CTX pointer. NULL at creation time, Not NULL when mg_context
-	will be freed
+	            will be freed
 	     user_data: parameter user_data passed when starting the server.
 	     user_data: parameter user_data passed when starting the server.
 	   Return value:
 	   Return value:
 	     0: civetweb will continue to create the context, just as if the
 	     0: civetweb will continue to create the context, just as if the
-	callback would not be present.
+	        callback would not be present.
 	        The value in *ssl_ctx when the function returns is ignored.
 	        The value in *ssl_ctx when the function returns is ignored.
 	     1: civetweb will copy the value from *ssl_ctx to the civetweb context
 	     1: civetweb will copy the value from *ssl_ctx to the civetweb context
-	and doesn't create its own.
+	        and doesn't create its own.
 	    -1: initializing ssl fails.*/
 	    -1: initializing ssl fails.*/
 	int (*external_ssl_ctx)(void **ssl_ctx, void *user_data);
 	int (*external_ssl_ctx)(void **ssl_ctx, void *user_data);
 
 
@@ -312,8 +312,9 @@ struct mg_callbacks {
 	/* Called when civetweb is about to serve Lua server page, if
 	/* Called when civetweb is about to serve Lua server page, if
 	   Lua support is enabled.
 	   Lua support is enabled.
 	   Parameters:
 	   Parameters:
+	     conn: current connection.
 	     lua_context: "lua_State *" pointer. */
 	     lua_context: "lua_State *" pointer. */
-	void (*init_lua)(const struct mg_connection *, void *lua_context);
+	void (*init_lua)(const struct mg_connection *conn, void *lua_context);
 
 
 #if defined(MG_LEGACY_INTERFACE) /* 2016-05-14 */
 #if defined(MG_LEGACY_INTERFACE) /* 2016-05-14 */
 	/* Called when civetweb has uploaded a file to a temporary directory as a
 	/* Called when civetweb has uploaded a file to a temporary directory as a
@@ -327,11 +328,15 @@ struct mg_callbacks {
 	/* Called when civetweb is about to send HTTP error to the client.
 	/* Called when civetweb is about to send HTTP error to the client.
 	   Implementing this callback allows to create custom error pages.
 	   Implementing this callback allows to create custom error pages.
 	   Parameters:
 	   Parameters:
+	     conn: current connection.
 	     status: HTTP error status code.
 	     status: HTTP error status code.
+	     errmsg: error message text.
 	   Return value:
 	   Return value:
 	     1: run civetweb error handler.
 	     1: run civetweb error handler.
 	     0: callback already handled the error. */
 	     0: callback already handled the error. */
-	int (*http_error)(struct mg_connection *, int status);
+	int (*http_error)(struct mg_connection *conn,
+	                  int status,
+	                  const char *errmsg);
 
 
 	/* Called after civetweb context has been created, before requests
 	/* Called after civetweb context has been created, before requests
 	   are processed.
 	   are processed.

+ 2 - 1
src/civetweb.c

@@ -4331,7 +4331,8 @@ mg_send_http_error_impl(struct mg_connection *conn,
 		/* Mark in_error_handler to avoid recursion and call user callback. */
 		/* Mark in_error_handler to avoid recursion and call user callback. */
 		conn->in_error_handler = 1;
 		conn->in_error_handler = 1;
 		handled_by_callback =
 		handled_by_callback =
-		    (conn->phys_ctx->callbacks.http_error(conn, status) == 0);
+		    (conn->phys_ctx->callbacks.http_error(conn, status, errmsg_buf)
+		     == 0);
 		conn->in_error_handler = 0;
 		conn->in_error_handler = 0;
 	}
 	}