Browse Source

Added overload handle functions with http status

The CivetHandler has been extended with overloaded handleGet, handlePut,
handlePatch ... functions with extra int *http_status argument.

This allows applications to return a valid status code if a resource is
not found or an operation is forbidden.
Pieter Cardoen 4 years ago
parent
commit
cf080fd5f1
2 changed files with 182 additions and 8 deletions
  1. 70 0
      include/CivetServer.h
  2. 112 8
      src/CivetServer.cpp

+ 70 - 0
include/CivetServer.h

@@ -68,6 +68,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
 	virtual bool handleGet(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for GET request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handleGet(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for POST request.
 	 * Callback method for POST request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -77,6 +87,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
 	virtual bool handlePost(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for POST request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handlePost(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for HEAD request.
 	 * Callback method for HEAD request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -86,6 +106,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handleHead(CivetServer *server, struct mg_connection *conn);
 	virtual bool handleHead(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for HEAD request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handleHead(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for PUT request.
 	 * Callback method for PUT request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -95,6 +125,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
 	virtual bool handlePut(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for PUT request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handlePut(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for DELETE request.
 	 * Callback method for DELETE request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -104,6 +144,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
 	virtual bool handleDelete(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for DELETE request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handleDelete(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for OPTIONS request.
 	 * Callback method for OPTIONS request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -113,6 +163,16 @@ class CIVETWEB_CXX_API CivetHandler
 	virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
 	virtual bool handleOptions(CivetServer *server, struct mg_connection *conn);
 
 
 	/**
 	/**
+	 * Callback method for OPTIONS request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handleOptions(CivetServer *server, struct mg_connection *conn, int *status_code);
+
+	/**
 	 * Callback method for PATCH request.
 	 * Callback method for PATCH request.
 	 *
 	 *
 	 * @param server - the calling server
 	 * @param server - the calling server
@@ -120,6 +180,16 @@ class CIVETWEB_CXX_API CivetHandler
 	 * @returns true if implemented, false otherwise
 	 * @returns true if implemented, false otherwise
 	 */
 	 */
 	virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
 	virtual bool handlePatch(CivetServer *server, struct mg_connection *conn);
+
+	/**
+	 * Callback method for PATCH request.
+	 *
+	 * @param server - the calling server
+	 * @param conn - the connection information
+	 * @param http - pointer to return status code
+	 * @returns true if implemented, false otherwise
+	 */
+	virtual bool handlePatch(CivetServer *server, struct mg_connection *conn, int *status_code);
 };
 };
 
 
 /**
 /**

+ 112 - 8
src/CivetServer.cpp

@@ -28,6 +28,17 @@ CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -36,6 +47,17 @@ CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -44,6 +66,17 @@ CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+		if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -52,6 +85,17 @@ CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -60,6 +104,17 @@ CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -68,6 +123,17 @@ CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return false;
+}
+
+bool
 CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn)
 CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn)
 {
 {
 	UNUSED_PARAMETER(server);
 	UNUSED_PARAMETER(server);
@@ -76,6 +142,17 @@ CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn)
 }
 }
 
 
 bool
 bool
+CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn, int *status_code)
+{
+	UNUSED_PARAMETER(server);
+	UNUSED_PARAMETER(conn);
+	if(status_code){
+		*status_code = -1;
+	}
+	return -1;
+}
+
+bool
 CivetWebSocketHandler::handleConnection(CivetServer *server,
 CivetWebSocketHandler::handleConnection(CivetServer *server,
                                         const struct mg_connection *conn)
                                         const struct mg_connection *conn)
 {
 {
@@ -124,6 +201,8 @@ CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
 	assert(request_info != NULL);
 	assert(request_info != NULL);
 	CivetServer *me = (CivetServer *)(request_info->user_data);
 	CivetServer *me = (CivetServer *)(request_info->user_data);
 	assert(me != NULL);
 	assert(me != NULL);
+	int http_status_code = -1;
+	bool status_ok = false;
 
 
 	// Happens when a request hits the server before the context is saved
 	// Happens when a request hits the server before the context is saved
 	if (me->context == NULL)
 	if (me->context == NULL)
@@ -137,23 +216,48 @@ CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
 
 
 	if (handler) {
 	if (handler) {
 		if (strcmp(request_info->request_method, "GET") == 0) {
 		if (strcmp(request_info->request_method, "GET") == 0) {
-			return handler->handleGet(me, conn) ? 1 : 0;
+			status_ok = handler->handleGet(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handleGet(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "POST") == 0) {
 		} else if (strcmp(request_info->request_method, "POST") == 0) {
-			return handler->handlePost(me, conn) ? 1 : 0;
+			status_ok = handler->handlePost(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handlePost(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "HEAD") == 0) {
 		} else if (strcmp(request_info->request_method, "HEAD") == 0) {
-			return handler->handleHead(me, conn) ? 1 : 0;
+			status_ok = handler->handleHead(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handleHead(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "PUT") == 0) {
 		} else if (strcmp(request_info->request_method, "PUT") == 0) {
-			return handler->handlePut(me, conn) ? 1 : 0;
+			status_ok = handler->handlePut(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handlePut(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "DELETE") == 0) {
 		} else if (strcmp(request_info->request_method, "DELETE") == 0) {
-			return handler->handleDelete(me, conn) ? 1 : 0;
+			status_ok = handler->handleDelete(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handleDelete(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "OPTIONS") == 0) {
 		} else if (strcmp(request_info->request_method, "OPTIONS") == 0) {
-			return handler->handleOptions(me, conn) ? 1 : 0;
+			status_ok = handler->handleOptions(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handleOptions(me, conn);
+			}
 		} else if (strcmp(request_info->request_method, "PATCH") == 0) {
 		} else if (strcmp(request_info->request_method, "PATCH") == 0) {
-			return handler->handlePatch(me, conn) ? 1 : 0;
+			status_ok = handler->handlePatch(me, conn,&http_status_code);
+			if(http_status_code < 0){
+				status_ok = handler->handlePatch(me, conn);
+			}
 		}
 		}
 	}
 	}
 
 
-	return 0; // No handler found
+	if(http_status_code < 0){
+		http_status_code = status_ok ? 1 : 0;
+	}
+
+	return http_status_code;
 }
 }
 
 
 int
 int