Bläddra i källkod

mg_send_http_error as public API

bel2125 8 år sedan
förälder
incheckning
5bd621ccf1
4 ändrade filer med 207 tillägg och 172 borttagningar
  1. 26 0
      docs/api/mg_send_http_error.md
  2. 7 0
      include/civetweb.h
  3. 167 165
      src/civetweb.c
  4. 7 7
      src/mod_lua.inl

+ 26 - 0
docs/api/mg_send_http_error.md

@@ -0,0 +1,26 @@
+# Civetweb API Reference
+
+### `mg_send_http_error( conn, status_code, fmt, ... );`
+
+### Parameters
+
+| Parameter | Type | Description |
+| :--- | :--- | :--- |
+|**`conn`**|`struct mg_connection *`|The connection over which the file must be sent|
+|**`status_code`**|`int`|The HTTP status code to return|
+|**`fmt`**|`const char *`|Format string specifying the remote command to execute|
+|**`...`**|*various*|Parameters used in the format string|
+
+### Return Value
+
+*none*
+
+### Description
+
+The function `mg_send_http_error()` sends a HTTP reply with the given status code and the content specified by fmt. The textual status code is determined from the numerical status code internally.
+
+### See Also
+
+* [`mg_printf();`](mg_printf.md)
+* [`mg_write();`](mg_write.md)
+* [`mg_send_file();`](mg_send_file.md)

+ 7 - 0
include/civetweb.h

@@ -696,6 +696,13 @@ CIVETWEB_API int mg_send_chunk(struct mg_connection *conn,
 CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
 CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
 
 
 
 
+/* Send HTTP error reply. */
+CIVETWEB_API void mg_send_http_error(struct mg_connection *conn,
+                                     int status_code,
+                                     PRINTF_FORMAT_STRING(const char *fmt),
+                                     ...) PRINTF_ARGS(3, 4);
+
+
 /* Send contents of the entire file together with HTTP headers.
 /* Send contents of the entire file together with HTTP headers.
    Parameters:
    Parameters:
      conn: Current connection information.
      conn: Current connection information.

+ 167 - 165
src/civetweb.c

@@ -3481,13 +3481,8 @@ mg_get_response_code_text(struct mg_connection *conn, int response_code)
 }
 }
 
 
 
 
-static void send_http_error(struct mg_connection *,
-                            int,
-                            PRINTF_FORMAT_STRING(const char *fmt),
-                            ...) PRINTF_ARGS(3, 4);
-
-static void
-send_http_error(struct mg_connection *conn, int status, const char *fmt, ...)
+void
+mg_send_http_error(struct mg_connection *conn, int status, const char *fmt, ...)
 {
 {
 	char buf[MG_BUF_LEN];
 	char buf[MG_BUF_LEN];
 	va_list ap;
 	va_list ap;
@@ -4610,10 +4605,10 @@ spawn_process(struct mg_connection *conn,
 
 
 	if ((pid = fork()) == -1) {
 	if ((pid = fork()) == -1) {
 		/* Parent */
 		/* Parent */
-		send_http_error(conn,
-		                500,
-		                "Error: Creating CGI process\nfork(): %s",
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Creating CGI process\nfork(): %s",
+		                   strerror(ERRNO));
 	} else if (pid == 0) {
 	} else if (pid == 0) {
 		/* Child */
 		/* Child */
 		if (chdir(dir) != 0) {
 		if (chdir(dir) != 0) {
@@ -7695,11 +7690,11 @@ handle_directory_request(struct mg_connection *conn, const char *dir)
 	time_t curtime = time(NULL);
 	time_t curtime = time(NULL);
 
 
 	if (!scan_directory(conn, dir, &data, dir_scan_callback)) {
 	if (!scan_directory(conn, dir, &data, dir_scan_callback)) {
-		send_http_error(conn,
-		                500,
-		                "Error: Cannot open directory\nopendir(%s): %s",
-		                dir,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Cannot open directory\nopendir(%s): %s",
+		                   dir,
+		                   strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
@@ -7843,7 +7838,7 @@ send_file_data(struct mg_connection *conn,
 #endif
 #endif
 		if ((offset > 0) && (fseeko(filep->access.fp, offset, SEEK_SET) != 0)) {
 		if ((offset > 0) && (fseeko(filep->access.fp, offset, SEEK_SET) != 0)) {
 			mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO));
 			mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO));
-			send_http_error(
+			mg_send_http_error(
 			    conn,
 			    conn,
 			    500,
 			    500,
 			    "%s",
 			    "%s",
@@ -7947,10 +7942,10 @@ handle_static_file_request(struct mg_connection *conn,
 		mime_vec.len = strlen(mime_type);
 		mime_vec.len = strlen(mime_type);
 	}
 	}
 	if (filep->stat.size > INT64_MAX) {
 	if (filep->stat.size > INT64_MAX) {
-		send_http_error(conn,
-		                500,
-		                "Error: File size is too large to send\n%" INT64_FMT,
-		                filep->stat.size);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: File size is too large to send\n%" INT64_FMT,
+		                   filep->stat.size);
 	}
 	}
 	cl = (int64_t)filep->stat.size;
 	cl = (int64_t)filep->stat.size;
 	conn->status_code = 200;
 	conn->status_code = 200;
@@ -7963,10 +7958,10 @@ handle_static_file_request(struct mg_connection *conn,
 		mg_snprintf(conn, &truncated, gz_path, sizeof(gz_path), "%s.gz", path);
 		mg_snprintf(conn, &truncated, gz_path, sizeof(gz_path), "%s.gz", path);
 
 
 		if (truncated) {
 		if (truncated) {
-			send_http_error(conn,
-			                500,
-			                "Error: Path of zipped file too long (%s)",
-			                path);
+			mg_send_http_error(conn,
+			                   500,
+			                   "Error: Path of zipped file too long (%s)",
+			                   path);
 			return;
 			return;
 		}
 		}
 
 
@@ -7975,11 +7970,11 @@ handle_static_file_request(struct mg_connection *conn,
 	}
 	}
 
 
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, filep)) {
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, filep)) {
-		send_http_error(conn,
-		                500,
-		                "Error: Cannot open file\nfopen(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Cannot open file\nfopen(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
@@ -7993,7 +7988,7 @@ handle_static_file_request(struct mg_connection *conn,
 		/* actually, range requests don't play well with a pre-gzipped
 		/* actually, range requests don't play well with a pre-gzipped
 		 * file (since the range is specified in the uncompressed space) */
 		 * file (since the range is specified in the uncompressed space) */
 		if (filep->stat.is_gzipped) {
 		if (filep->stat.is_gzipped) {
-			send_http_error(
+			mg_send_http_error(
 			    conn,
 			    conn,
 			    501,
 			    501,
 			    "%s",
 			    "%s",
@@ -8151,17 +8146,17 @@ mg_send_mime_file2(struct mg_connection *conn,
 			                   "yes")) {
 			                   "yes")) {
 				handle_directory_request(conn, path);
 				handle_directory_request(conn, path);
 			} else {
 			} else {
-				send_http_error(conn,
-				                403,
-				                "%s",
-				                "Error: Directory listing denied");
+				mg_send_http_error(conn,
+				                   403,
+				                   "%s",
+				                   "Error: Directory listing denied");
 			}
 			}
 		} else {
 		} else {
 			handle_static_file_request(
 			handle_static_file_request(
 			    conn, path, &file, mime_type, additional_headers);
 			    conn, path, &file, mime_type, additional_headers);
 		}
 		}
 	} else {
 	} else {
-		send_http_error(conn, 404, "%s", "Error: File not found");
+		mg_send_http_error(conn, 404, "%s", "Error: File not found");
 	}
 	}
 }
 }
 
 
@@ -8551,23 +8546,23 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
 	expect = mg_get_header(conn, "Expect");
 	expect = mg_get_header(conn, "Expect");
 	/* assert(fp != NULL); */
 	/* assert(fp != NULL); */
 	if (!fp) {
 	if (!fp) {
-		send_http_error(conn, 500, "%s", "Error: NULL File");
+		mg_send_http_error(conn, 500, "%s", "Error: NULL File");
 		return 0;
 		return 0;
 	}
 	}
 
 
 	if (conn->content_len == -1 && !conn->is_chunked) {
 	if (conn->content_len == -1 && !conn->is_chunked) {
 		/* Content length is not specified by the client. */
 		/* Content length is not specified by the client. */
-		send_http_error(conn,
-		                411,
-		                "%s",
-		                "Error: Client did not specify content length");
+		mg_send_http_error(conn,
+		                   411,
+		                   "%s",
+		                   "Error: Client did not specify content length");
 	} else if ((expect != NULL)
 	} else if ((expect != NULL)
 	           && (mg_strcasecmp(expect, "100-continue") != 0)) {
 	           && (mg_strcasecmp(expect, "100-continue") != 0)) {
 		/* Client sent an "Expect: xyz" header and xyz is not 100-continue. */
 		/* Client sent an "Expect: xyz" header and xyz is not 100-continue. */
-		send_http_error(conn,
-		                417,
-		                "Error: Can not fulfill expectation %s",
-		                expect);
+		mg_send_http_error(conn,
+		                   417,
+		                   "Error: Can not fulfill expectation %s",
+		                   expect);
 	} else {
 	} else {
 		if (expect != NULL) {
 		if (expect != NULL) {
 			(void)mg_printf(conn, "%s", "HTTP/1.1 100 Continue\r\n\r\n");
 			(void)mg_printf(conn, "%s", "HTTP/1.1 100 Continue\r\n\r\n");
@@ -8583,7 +8578,7 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
 		/* assert(conn->consumed_content == 0); */
 		/* assert(conn->consumed_content == 0); */
 
 
 		if ((buffered_len < 0) || (conn->consumed_content != 0)) {
 		if ((buffered_len < 0) || (conn->consumed_content != 0)) {
-			send_http_error(conn, 500, "%s", "Error: Size mismatch");
+			mg_send_http_error(conn, 500, "%s", "Error: Size mismatch");
 			return 0;
 			return 0;
 		}
 		}
 
 
@@ -8624,7 +8619,7 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
 			/* NOTE: Maybe some data has already been sent. */
 			/* NOTE: Maybe some data has already been sent. */
 			/* TODO (low): If some data has been sent, a correct error
 			/* TODO (low): If some data has been sent, a correct error
 			 * reply can no longer be sent, so just close the connection */
 			 * reply can no longer be sent, so just close the connection */
-			send_http_error(conn, 500, "%s", "");
+			mg_send_http_error(conn, 500, "%s", "");
 		}
 		}
 	}
 	}
 
 
@@ -8939,7 +8934,7 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 
 
 	if (truncated) {
 	if (truncated) {
 		mg_cry(conn, "Error: CGI program \"%s\": Path too long", prog);
 		mg_cry(conn, "Error: CGI program \"%s\": Path too long", prog);
-		send_http_error(conn, 500, "Error: %s", "CGI path too long");
+		mg_send_http_error(conn, 500, "Error: %s", "CGI path too long");
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -8956,7 +8951,10 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 		       "Error: CGI program \"%s\": Can not create CGI pipes: %s",
 		       "Error: CGI program \"%s\": Can not create CGI pipes: %s",
 		       prog,
 		       prog,
 		       status);
 		       status);
-		send_http_error(conn, 500, "Error: Cannot create CGI pipe: %s", status);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Cannot create CGI pipe: %s",
+		                   status);
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -8968,11 +8966,11 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 		       "Error: CGI program \"%s\": Can not spawn CGI process: %s",
 		       "Error: CGI program \"%s\": Can not spawn CGI process: %s",
 		       prog,
 		       prog,
 		       status);
 		       status);
-		send_http_error(conn,
-		                500,
-		                "Error: Cannot spawn CGI process [%s]: %s",
-		                prog,
-		                status);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Cannot spawn CGI process [%s]: %s",
+		                   prog,
+		                   status);
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -8999,10 +8997,10 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 		       "Error: CGI program \"%s\": Can not open stdin: %s",
 		       "Error: CGI program \"%s\": Can not open stdin: %s",
 		       prog,
 		       prog,
 		       status);
 		       status);
-		send_http_error(conn,
-		                500,
-		                "Error: CGI can not open fdin\nfopen: %s",
-		                status);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: CGI can not open fdin\nfopen: %s",
+		                   status);
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -9012,10 +9010,10 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 		       "Error: CGI program \"%s\": Can not open stdout: %s",
 		       "Error: CGI program \"%s\": Can not open stdout: %s",
 		       prog,
 		       prog,
 		       status);
 		       status);
-		send_http_error(conn,
-		                500,
-		                "Error: CGI can not open fdout\nfopen: %s",
-		                status);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: CGI can not open fdout\nfopen: %s",
+		                   status);
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -9025,10 +9023,10 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 		       "Error: CGI program \"%s\": Can not open stderr: %s",
 		       "Error: CGI program \"%s\": Can not open stderr: %s",
 		       prog,
 		       prog,
 		       status);
 		       status);
-		send_http_error(conn,
-		                500,
-		                "Error: CGI can not open fdout\nfopen: %s",
-		                status);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: CGI can not open fdout\nfopen: %s",
+		                   status);
 		goto done;
 		goto done;
 	}
 	}
 
 
@@ -9060,10 +9058,10 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 	data_len = 0;
 	data_len = 0;
 	buf = (char *)mg_malloc(buflen);
 	buf = (char *)mg_malloc(buflen);
 	if (buf == NULL) {
 	if (buf == NULL) {
-		send_http_error(conn,
-		                500,
-		                "Error: Not enough memory for CGI buffer (%u bytes)",
-		                (unsigned int)buflen);
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Not enough memory for CGI buffer (%u bytes)",
+		                   (unsigned int)buflen);
 		mg_cry(conn,
 		mg_cry(conn,
 		       "Error: CGI program \"%s\": Not enough memory for buffer (%u "
 		       "Error: CGI program \"%s\": Not enough memory for buffer (%u "
 		       "bytes)",
 		       "bytes)",
@@ -9084,13 +9082,13 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 			       prog,
 			       prog,
 			       i,
 			       i,
 			       buf);
 			       buf);
-			send_http_error(conn,
-			                500,
-			                "Error: CGI program \"%s\" sent error "
-			                "message: [%.*s]",
-			                prog,
-			                i,
-			                buf);
+			mg_send_http_error(conn,
+			                   500,
+			                   "Error: CGI program \"%s\" sent error "
+			                   "message: [%.*s]",
+			                   prog,
+			                   i,
+			                   buf);
 		} else {
 		} else {
 			mg_cry(conn,
 			mg_cry(conn,
 			       "Error: CGI program sent malformed or too big "
 			       "Error: CGI program sent malformed or too big "
@@ -9099,13 +9097,13 @@ handle_cgi_request(struct mg_connection *conn, const char *prog)
 			       data_len,
 			       data_len,
 			       buf);
 			       buf);
 
 
-			send_http_error(conn,
-			                500,
-			                "Error: CGI program sent malformed or too big "
-			                "(>%u bytes) HTTP headers: [%.*s]",
-			                (unsigned)buflen,
-			                data_len,
-			                buf);
+			mg_send_http_error(conn,
+			                   500,
+			                   "Error: CGI program sent malformed or too big "
+			                   "(>%u bytes) HTTP headers: [%.*s]",
+			                   (unsigned)buflen,
+			                   data_len,
+			                   buf);
 		}
 		}
 
 
 		goto done;
 		goto done;
@@ -9209,7 +9207,7 @@ mkcol(struct mg_connection *conn, const char *path)
 		return;
 		return;
 	}
 	}
 
 
-	/* TODO (mid): Check the send_http_error situations in this function */
+	/* TODO (mid): Check the mg_send_http_error situations in this function */
 
 
 	memset(&de.file, 0, sizeof(de.file));
 	memset(&de.file, 0, sizeof(de.file));
 	if (!mg_stat(conn, path, &de.file)) {
 	if (!mg_stat(conn, path, &de.file)) {
@@ -9224,14 +9222,14 @@ mkcol(struct mg_connection *conn, const char *path)
 		/* TODO (mid): This check does not seem to make any sense ! */
 		/* TODO (mid): This check does not seem to make any sense ! */
 		/* TODO (mid): Add a webdav unit test first, before changing
 		/* TODO (mid): Add a webdav unit test first, before changing
 		 * anything here. */
 		 * anything here. */
-		send_http_error(
+		mg_send_http_error(
 		    conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		    conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
 	body_len = conn->data_len - conn->request_len;
 	body_len = conn->data_len - conn->request_len;
 	if (body_len > 0) {
 	if (body_len > 0) {
-		send_http_error(
+		mg_send_http_error(
 		    conn, 415, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		    conn, 415, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		return;
 		return;
 	}
 	}
@@ -9254,16 +9252,17 @@ mkcol(struct mg_connection *conn, const char *path)
 		          suggest_connection_header(conn));
 		          suggest_connection_header(conn));
 	} else if (rc == -1) {
 	} else if (rc == -1) {
 		if (errno == EEXIST) {
 		if (errno == EEXIST) {
-			send_http_error(
+			mg_send_http_error(
 			    conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 			    conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		} else if (errno == EACCES) {
 		} else if (errno == EACCES) {
-			send_http_error(
+			mg_send_http_error(
 			    conn, 403, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 			    conn, 403, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		} else if (errno == ENOENT) {
 		} else if (errno == ENOENT) {
-			send_http_error(
+			mg_send_http_error(
 			    conn, 409, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 			    conn, 409, "Error: mkcol(%s): %s", path, strerror(ERRNO));
 		} else {
 		} else {
-			send_http_error(conn, 500, "fopen(%s): %s", path, strerror(ERRNO));
+			mg_send_http_error(
+			    conn, 500, "fopen(%s): %s", path, strerror(ERRNO));
 		}
 		}
 	}
 	}
 }
 }
@@ -9298,7 +9297,7 @@ put_file(struct mg_connection *conn, const char *path)
 
 
 			if (file.access.membuf != NULL) {
 			if (file.access.membuf != NULL) {
 				/* This is an "in-memory" file, that can not be replaced */
 				/* This is an "in-memory" file, that can not be replaced */
-				send_http_error(
+				mg_send_http_error(
 				    conn,
 				    conn,
 				    405,
 				    405,
 				    "Error: Put not possible\nReplacing %s is not supported",
 				    "Error: Put not possible\nReplacing %s is not supported",
@@ -9312,7 +9311,7 @@ put_file(struct mg_connection *conn, const char *path)
 				conn->status_code = 200;
 				conn->status_code = 200;
 				rc = 1;
 				rc = 1;
 			} else {
 			} else {
-				send_http_error(
+				mg_send_http_error(
 				    conn,
 				    conn,
 				    403,
 				    403,
 				    "Error: Put not possible\nReplacing %s is not allowed",
 				    "Error: Put not possible\nReplacing %s is not allowed",
@@ -9349,21 +9348,21 @@ put_file(struct mg_connection *conn, const char *path)
 
 
 	if (rc == -1) {
 	if (rc == -1) {
 		/* put_dir returns -1 if the path is too long */
 		/* put_dir returns -1 if the path is too long */
-		send_http_error(conn,
-		                414,
-		                "Error: Path too long\nput_dir(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   414,
+		                   "Error: Path too long\nput_dir(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
 	if (rc == -2) {
 	if (rc == -2) {
 		/* put_dir returns -2 if the directory can not be created */
 		/* put_dir returns -2 if the directory can not be created */
-		send_http_error(conn,
-		                500,
-		                "Error: Can not create directory\nput_dir(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Can not create directory\nput_dir(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
@@ -9372,11 +9371,11 @@ put_file(struct mg_connection *conn, const char *path)
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &file)
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &file)
 	    || file.access.fp == NULL) {
 	    || file.access.fp == NULL) {
 		(void)mg_fclose(&file.access);
 		(void)mg_fclose(&file.access);
-		send_http_error(conn,
-		                500,
-		                "Error: Can not create file\nfopen(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Can not create file\nfopen(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 		return;
 		return;
 	}
 	}
 
 
@@ -9425,17 +9424,17 @@ delete_file(struct mg_connection *conn, const char *path)
 	memset(&de.file, 0, sizeof(de.file));
 	memset(&de.file, 0, sizeof(de.file));
 	if (!mg_stat(conn, path, &de.file)) {
 	if (!mg_stat(conn, path, &de.file)) {
 		/* mg_stat returns 0 if the file does not exist */
 		/* mg_stat returns 0 if the file does not exist */
-		send_http_error(conn,
-		                404,
-		                "Error: Cannot delete file\nFile %s not found",
-		                path);
+		mg_send_http_error(conn,
+		                   404,
+		                   "Error: Cannot delete file\nFile %s not found",
+		                   path);
 		return;
 		return;
 	}
 	}
 
 
 #if 0 /* Ignore if a file in memory is inside a folder */
 #if 0 /* Ignore if a file in memory is inside a folder */
 	if (de.access.membuf != NULL) {
 	if (de.access.membuf != NULL) {
 		/* the file is cached in memory */
 		/* the file is cached in memory */
-		send_http_error(
+		mg_send_http_error(
 		    conn,
 		    conn,
 		    405,
 		    405,
 		    "Error: Delete not possible\nDeleting %s is not supported",
 		    "Error: Delete not possible\nDeleting %s is not supported",
@@ -9447,10 +9446,10 @@ delete_file(struct mg_connection *conn, const char *path)
 	if (de.file.is_directory) {
 	if (de.file.is_directory) {
 		if (remove_directory(conn, path)) {
 		if (remove_directory(conn, path)) {
 			/* Delete is successful: Return 204 without content. */
 			/* Delete is successful: Return 204 without content. */
-			send_http_error(conn, 204, "%s", "");
+			mg_send_http_error(conn, 204, "%s", "");
 		} else {
 		} else {
 			/* Delete is not successful: Return 500 (Server error). */
 			/* Delete is not successful: Return 500 (Server error). */
-			send_http_error(conn, 500, "Error: Could not delete %s", path);
+			mg_send_http_error(conn, 500, "Error: Could not delete %s", path);
 		}
 		}
 		return;
 		return;
 	}
 	}
@@ -9459,7 +9458,7 @@ delete_file(struct mg_connection *conn, const char *path)
 	 * Check if write permission is granted. */
 	 * Check if write permission is granted. */
 	if (access(path, W_OK) != 0) {
 	if (access(path, W_OK) != 0) {
 		/* File is read only */
 		/* File is read only */
-		send_http_error(
+		mg_send_http_error(
 		    conn,
 		    conn,
 		    403,
 		    403,
 		    "Error: Delete not possible\nDeleting %s is not allowed",
 		    "Error: Delete not possible\nDeleting %s is not allowed",
@@ -9470,14 +9469,14 @@ delete_file(struct mg_connection *conn, const char *path)
 	/* Try to delete it. */
 	/* Try to delete it. */
 	if (mg_remove(conn, path) == 0) {
 	if (mg_remove(conn, path) == 0) {
 		/* Delete was successful: Return 204 without content. */
 		/* Delete was successful: Return 204 without content. */
-		send_http_error(conn, 204, "%s", "");
+		mg_send_http_error(conn, 204, "%s", "");
 	} else {
 	} else {
 		/* Delete not successful (file locked). */
 		/* Delete not successful (file locked). */
-		send_http_error(conn,
-		                423,
-		                "Error: Cannot delete file\nremove(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   423,
+		                   "Error: Cannot delete file\nremove(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 	}
 	}
 }
 }
 #endif /* !NO_FILES */
 #endif /* !NO_FILES */
@@ -9711,11 +9710,11 @@ handle_ssi_file_request(struct mg_connection *conn,
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, filep)) {
 	if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, filep)) {
 		/* File exists (precondition for calling this function),
 		/* File exists (precondition for calling this function),
 		 * but can not be opened by the server. */
 		 * but can not be opened by the server. */
-		send_http_error(conn,
-		                500,
-		                "Error: Cannot read file\nfopen(%s): %s",
-		                path,
-		                strerror(ERRNO));
+		mg_send_http_error(conn,
+		                   500,
+		                   "Error: Cannot read file\nfopen(%s): %s",
+		                   path,
+		                   strerror(ERRNO));
 	} else {
 	} else {
 		conn->must_close = 1;
 		conn->must_close = 1;
 		gmt_time_string(date, sizeof(date), &curtime);
 		gmt_time_string(date, sizeof(date), &curtime);
@@ -10322,15 +10321,15 @@ handle_websocket_request(struct mg_connection *conn,
 			conn->content_len = 8;
 			conn->content_len = 8;
 			if (8 == mg_read(conn, key3, 8)) {
 			if (8 == mg_read(conn, key3, 8)) {
 				/* This is the hixie version */
 				/* This is the hixie version */
-				send_http_error(conn,
-				                426,
-				                "%s",
-				                "Protocol upgrade to RFC 6455 required");
+				mg_send_http_error(conn,
+				                   426,
+				                   "%s",
+				                   "Protocol upgrade to RFC 6455 required");
 				return;
 				return;
 			}
 			}
 		}
 		}
 		/* This is an unknown version */
 		/* This is an unknown version */
-		send_http_error(conn, 400, "%s", "Malformed websocket request");
+		mg_send_http_error(conn, 400, "%s", "Malformed websocket request");
 		return;
 		return;
 	}
 	}
 
 
@@ -10338,7 +10337,7 @@ handle_websocket_request(struct mg_connection *conn,
 	/* The RFC version (https://tools.ietf.org/html/rfc6455) is 13. */
 	/* The RFC version (https://tools.ietf.org/html/rfc6455) is 13. */
 	if (version == NULL || strcmp(version, "13") != 0) {
 	if (version == NULL || strcmp(version, "13") != 0) {
 		/* Reject wrong versions */
 		/* Reject wrong versions */
-		send_http_error(conn, 426, "%s", "Protocol upgrade required");
+		mg_send_http_error(conn, 426, "%s", "Protocol upgrade required");
 		return;
 		return;
 	}
 	}
 
 
@@ -10446,13 +10445,13 @@ handle_websocket_request(struct mg_connection *conn,
 		/* Reply with a 404 Not Found. We are still at a standard
 		/* Reply with a 404 Not Found. We are still at a standard
 		 * HTTP request here, before the websocket handshake, so
 		 * HTTP request here, before the websocket handshake, so
 		 * we can still send standard HTTP error replies. */
 		 * we can still send standard HTTP error replies. */
-		send_http_error(conn, 404, "%s", "Not found");
+		mg_send_http_error(conn, 404, "%s", "Not found");
 		return;
 		return;
 	}
 	}
 
 
 	/* Step 5: The websocket connection has been accepted */
 	/* Step 5: The websocket connection has been accepted */
 	if (!send_websocket_handshake(conn, websock_key)) {
 	if (!send_websocket_handshake(conn, websock_key)) {
-		send_http_error(conn, 500, "%s", "Websocket handshake failed");
+		mg_send_http_error(conn, 500, "%s", "Websocket handshake failed");
 		return;
 		return;
 	}
 	}
 
 
@@ -11205,10 +11204,10 @@ handle_request(struct mg_connection *conn)
 		} else {
 		} else {
 			/* A http to https forward port has been specified,
 			/* A http to https forward port has been specified,
 			 * but no https port to forward to. */
 			 * but no https port to forward to. */
-			send_http_error(conn,
-			                503,
-			                "%s",
-			                "Error: SSL forward not configured properly");
+			mg_send_http_error(conn,
+			                   503,
+			                   "%s",
+			                   "Error: SSL forward not configured properly");
 			mg_cry(conn, "Can not redirect to SSL, no SSL port available");
 			mg_cry(conn, "Can not redirect to SSL, no SSL port available");
 		}
 		}
 		return;
 		return;
@@ -11325,10 +11324,10 @@ handle_request(struct mg_connection *conn)
 #endif
 #endif
 			/* This server does not have any real files, thus the
 			/* This server does not have any real files, thus the
 			 * PUT/DELETE methods are not valid. */
 			 * PUT/DELETE methods are not valid. */
-			send_http_error(conn,
-			                405,
-			                "%s method not allowed",
-			                conn->request_info.request_method);
+			mg_send_http_error(conn,
+			                   405,
+			                   "%s method not allowed",
+			                   conn->request_info.request_method);
 			return;
 			return;
 		}
 		}
 
 
@@ -11436,7 +11435,7 @@ handle_request(struct mg_connection *conn)
 				                         &conn->ctx->callbacks);
 				                         &conn->ctx->callbacks);
 			} else {
 			} else {
 				/* Script was in an illegal path */
 				/* Script was in an illegal path */
-				send_http_error(conn, 403, "%s", "Forbidden");
+				mg_send_http_error(conn, 403, "%s", "Forbidden");
 			}
 			}
 		} else {
 		} else {
 #if defined(MG_LEGACY_INTERFACE)
 #if defined(MG_LEGACY_INTERFACE)
@@ -11451,7 +11450,7 @@ handle_request(struct mg_connection *conn)
 			    NULL,
 			    NULL,
 			    &conn->ctx->callbacks);
 			    &conn->ctx->callbacks);
 #else
 #else
-			send_http_error(conn, 404, "%s", "Not found");
+			mg_send_http_error(conn, 404, "%s", "Not found");
 #endif
 #endif
 		}
 		}
 		return;
 		return;
@@ -11462,13 +11461,13 @@ handle_request(struct mg_connection *conn)
 		/* 9a. In case the server uses only callbacks, this uri is
 		/* 9a. In case the server uses only callbacks, this uri is
 		 * unknown.
 		 * unknown.
 		 * Then, all request handling ends here. */
 		 * Then, all request handling ends here. */
-		send_http_error(conn, 404, "%s", "Not Found");
+		mg_send_http_error(conn, 404, "%s", "Not Found");
 
 
 #else
 #else
 	/* 9b. This request is either for a static file or resource handled
 	/* 9b. This request is either for a static file or resource handled
 	 * by a script file. Thus, a DOCUMENT_ROOT must exist. */
 	 * by a script file. Thus, a DOCUMENT_ROOT must exist. */
 	if (conn->ctx->config[DOCUMENT_ROOT] == NULL) {
 	if (conn->ctx->config[DOCUMENT_ROOT] == NULL) {
-		send_http_error(conn, 404, "%s", "Not Found");
+		mg_send_http_error(conn, 404, "%s", "Not Found");
 		return;
 		return;
 	}
 	}
 
 
@@ -11498,17 +11497,17 @@ handle_request(struct mg_connection *conn)
 		/* 11.4. PATCH method
 		/* 11.4. PATCH method
 		 * This method is not supported for static resources,
 		 * This method is not supported for static resources,
 		 * only for scripts (Lua, CGI) and callbacks. */
 		 * only for scripts (Lua, CGI) and callbacks. */
-		send_http_error(conn,
-		                405,
-		                "%s method not allowed",
-		                conn->request_info.request_method);
+		mg_send_http_error(conn,
+		                   405,
+		                   "%s method not allowed",
+		                   conn->request_info.request_method);
 		return;
 		return;
 	}
 	}
 
 
 	/* 11. File does not exist, or it was configured that it should be
 	/* 11. File does not exist, or it was configured that it should be
 	 * hidden */
 	 * hidden */
 	if (!is_found || (must_hide_file(conn, path))) {
 	if (!is_found || (must_hide_file(conn, path))) {
-		send_http_error(conn, 404, "%s", "Not found");
+		mg_send_http_error(conn, 404, "%s", "Not found");
 		return;
 		return;
 	}
 	}
 
 
@@ -11550,10 +11549,10 @@ handle_request(struct mg_connection *conn)
 	/* 13.3. everything but GET and HEAD (e.g. POST) */
 	/* 13.3. everything but GET and HEAD (e.g. POST) */
 	if (0 != strcmp(ri->request_method, "GET")
 	if (0 != strcmp(ri->request_method, "GET")
 	    && 0 != strcmp(ri->request_method, "HEAD")) {
 	    && 0 != strcmp(ri->request_method, "HEAD")) {
-		send_http_error(conn,
-		                405,
-		                "%s method not allowed",
-		                conn->request_info.request_method);
+		mg_send_http_error(conn,
+		                   405,
+		                   "%s method not allowed",
+		                   conn->request_info.request_method);
 		return;
 		return;
 	}
 	}
 
 
@@ -11566,7 +11565,10 @@ handle_request(struct mg_connection *conn)
 		                   "yes")) {
 		                   "yes")) {
 			handle_directory_request(conn, path);
 			handle_directory_request(conn, path);
 		} else {
 		} else {
-			send_http_error(conn, 403, "%s", "Error: Directory listing denied");
+			mg_send_http_error(conn,
+			                   403,
+			                   "%s",
+			                   "Error: Directory listing denied");
 		}
 		}
 		return;
 		return;
 	}
 	}
@@ -11605,7 +11607,7 @@ handle_file_based_request(struct mg_connection *conn,
 			handle_lsp_request(conn, path, file, NULL);
 			handle_lsp_request(conn, path, file, NULL);
 		} else {
 		} else {
 			/* Script was in an illegal path */
 			/* Script was in an illegal path */
-			send_http_error(conn, 403, "%s", "Forbidden");
+			mg_send_http_error(conn, 403, "%s", "Forbidden");
 		}
 		}
 
 
 	} else if (match_prefix(conn->ctx->config[LUA_SCRIPT_EXTENSIONS],
 	} else if (match_prefix(conn->ctx->config[LUA_SCRIPT_EXTENSIONS],
@@ -11618,7 +11620,7 @@ handle_file_based_request(struct mg_connection *conn,
 			mg_exec_lua_script(conn, path, NULL);
 			mg_exec_lua_script(conn, path, NULL);
 		} else {
 		} else {
 			/* Script was in an illegal path */
 			/* Script was in an illegal path */
-			send_http_error(conn, 403, "%s", "Forbidden");
+			mg_send_http_error(conn, 403, "%s", "Forbidden");
 		}
 		}
 #endif
 #endif
 #if defined(USE_DUKTAPE)
 #if defined(USE_DUKTAPE)
@@ -11631,7 +11633,7 @@ handle_file_based_request(struct mg_connection *conn,
 			mg_exec_duktape_script(conn, path);
 			mg_exec_duktape_script(conn, path);
 		} else {
 		} else {
 			/* Script was in an illegal path */
 			/* Script was in an illegal path */
-			send_http_error(conn, 403, "%s", "Forbidden");
+			mg_send_http_error(conn, 403, "%s", "Forbidden");
 		}
 		}
 #endif
 #endif
 #if !defined(NO_CGI)
 #if !defined(NO_CGI)
@@ -11643,7 +11645,7 @@ handle_file_based_request(struct mg_connection *conn,
 			handle_cgi_request(conn, path);
 			handle_cgi_request(conn, path);
 		} else {
 		} else {
 			/* Script was in an illegal path */
 			/* Script was in an illegal path */
-			send_http_error(conn, 403, "%s", "Forbidden");
+			mg_send_http_error(conn, 403, "%s", "Forbidden");
 		}
 		}
 #endif /* !NO_CGI */
 #endif /* !NO_CGI */
 	} else if (match_prefix(conn->ctx->config[SSI_EXTENSIONS],
 	} else if (match_prefix(conn->ctx->config[SSI_EXTENSIONS],
@@ -11653,7 +11655,7 @@ handle_file_based_request(struct mg_connection *conn,
 			handle_ssi_file_request(conn, path, file);
 			handle_ssi_file_request(conn, path, file);
 		} else {
 		} else {
 			/* Script was in an illegal path */
 			/* Script was in an illegal path */
-			send_http_error(conn, 403, "%s", "Forbidden");
+			mg_send_http_error(conn, 403, "%s", "Forbidden");
 		}
 		}
 #if !defined(NO_CACHING)
 #if !defined(NO_CACHING)
 	} else if ((!conn->in_error_handler)
 	} else if ((!conn->in_error_handler)
@@ -14103,7 +14105,7 @@ process_new_connection(struct mg_connection *conn)
 				 * error message and close the connection. */
 				 * error message and close the connection. */
 				if (reqerr > 0) {
 				if (reqerr > 0) {
 					/*assert(ebuf[0] != '\0');*/
 					/*assert(ebuf[0] != '\0');*/
-					send_http_error(conn, reqerr, "%s", ebuf);
+					mg_send_http_error(conn, reqerr, "%s", ebuf);
 				}
 				}
 			} else if (strcmp(ri->http_version, "1.0")
 			} else if (strcmp(ri->http_version, "1.0")
 			           && strcmp(ri->http_version, "1.1")) {
 			           && strcmp(ri->http_version, "1.1")) {
@@ -14113,7 +14115,7 @@ process_new_connection(struct mg_connection *conn)
 				            sizeof(ebuf),
 				            sizeof(ebuf),
 				            "Bad HTTP version: [%s]",
 				            "Bad HTTP version: [%s]",
 				            ri->http_version);
 				            ri->http_version);
-				send_http_error(conn, 505, "%s", ebuf);
+				mg_send_http_error(conn, 505, "%s", ebuf);
 			}
 			}
 
 
 			if (ebuf[0] == '\0') {
 			if (ebuf[0] == '\0') {
@@ -14145,7 +14147,7 @@ process_new_connection(struct mg_connection *conn)
 					            ebuf,
 					            ebuf,
 					            sizeof(ebuf),
 					            sizeof(ebuf),
 					            "Invalid URI");
 					            "Invalid URI");
-					send_http_error(conn, 400, "%s", ebuf);
+					mg_send_http_error(conn, 400, "%s", ebuf);
 					conn->request_info.local_uri = NULL;
 					conn->request_info.local_uri = NULL;
 					break;
 					break;
 				}
 				}

+ 7 - 7
src/mod_lua.inl

@@ -1657,7 +1657,7 @@ handle_lsp_request(struct mg_connection *conn,
 
 
 		/* File not found */
 		/* File not found */
 		if (ls == NULL) {
 		if (ls == NULL) {
-			send_http_error(conn, 500, "Error: File %s not found", path);
+			mg_send_http_error(conn, 500, "Error: File %s not found", path);
 		} else {
 		} else {
 			luaL_error(ls, "File [%s] not found", path);
 			luaL_error(ls, "File [%s] not found", path);
 		}
 		}
@@ -1669,10 +1669,10 @@ handle_lsp_request(struct mg_connection *conn,
 
 
 		/* File not found or not accessible */
 		/* File not found or not accessible */
 		if (ls == NULL) {
 		if (ls == NULL) {
-			send_http_error(conn,
-			                500,
-			                "Error: Cannot open script file %s",
-			                path);
+			mg_send_http_error(conn,
+			                   500,
+			                   "Error: Cannot open script file %s",
+			                   path);
 		} else {
 		} else {
 			luaL_error(ls, "Cannot  [%s] not found", path);
 			luaL_error(ls, "Cannot  [%s] not found", path);
 		}
 		}
@@ -1696,7 +1696,7 @@ handle_lsp_request(struct mg_connection *conn,
 
 
 		/* mmap failed */
 		/* mmap failed */
 		if (ls == NULL) {
 		if (ls == NULL) {
-			send_http_error(
+			mg_send_http_error(
 			    conn,
 			    conn,
 			    500,
 			    500,
 			    "Error: Cannot open script\nFile %s can not be mapped",
 			    "Error: Cannot open script\nFile %s can not be mapped",
@@ -1718,7 +1718,7 @@ handle_lsp_request(struct mg_connection *conn,
 	} else {
 	} else {
 		L = lua_newstate(lua_allocator, NULL);
 		L = lua_newstate(lua_allocator, NULL);
 		if (L == NULL) {
 		if (L == NULL) {
-			send_http_error(
+			mg_send_http_error(
 			    conn,
 			    conn,
 			    500,
 			    500,
 			    "%s",
 			    "%s",