Forráskód Böngészése

Store return value of callbacks as status code for the access log

See #178
bel 10 éve
szülő
commit
0e5206006c
2 módosított fájl, 18 hozzáadás és 11 törlés
  1. 6 3
      include/civetweb.h
  2. 12 8
      src/civetweb.c

+ 6 - 3
include/civetweb.h

@@ -92,8 +92,10 @@ struct mg_callbacks {
 	   Return value:
 	     0: civetweb will process the request itself. In this case,
 	        the callback must not send any data to the client.
-	     1: callback already processed the request. Civetweb will
-	        not send any data after the callback returned. */
+	     1-999: callback already processed the request. Civetweb will
+	            not send any data after the callback returned. The
+	            return code is stored as a HTTP status code for the
+	            access log. */
 	int (*begin_request)(struct mg_connection *);
 
 	/* Called when civetweb has finished processing request. */
@@ -243,7 +245,8 @@ CIVETWEB_API void mg_stop(struct mg_context *);
       cbdata: the callback data configured with mg_set_request_handler().
    Returns:
       0: the handler could not handle the request, so fall through.
-      1: the handler processed the request. */
+      1 - 999: the handler processed the request. The return code is
+               stored as a HTTP status code for the access log. */
 typedef int (*mg_request_handler)(struct mg_connection *conn, void *cbdata);
 
 /* mg_set_request_handler

+ 12 - 8
src/civetweb.c

@@ -8271,14 +8271,14 @@ static void handle_request(struct mg_connection *conn)
 			 * before an authorization check. If an authorization check is
 			 * required, use a request_handler instead. */
 			i = conn->ctx->callbacks.begin_request(conn);
-			switch (i) {
-			case 1:
-				/* callback already processed the request */
+			if (i > 0) {
+				/* callback already processed the request. Store the
+				   return value as a status code for the access log. */
+				conn->status_code = i;
 				return;
-			case 0:
+			} else if (i == 0) {
 				/* civetweb should process the request */
-				break;
-			default:
+			} else {
 				/* unspecified - may change with the next version */
 				return;
 			}
@@ -8364,8 +8364,12 @@ static void handle_request(struct mg_connection *conn)
 		/* 7. check if there are request handlers for this uri */
 		if (is_callback_resource) {
 			if (!is_websocket_request) {
-				if (callback_handler(conn, callback_data)) {
-					/* Do nothing, callback has served the request */
+				i = callback_handler(conn, callback_data);
+				if (i > 0) {
+					/* Do nothing, callback has served the request. Store the
+					 * return value as status code for the log and discard all
+					 * data from the client not used by the callback. */
+					conn->status_code = i;
 					discard_unread_request_data(conn);
 				} else {
 					/* TODO (high): what if the handler did NOT handle the