]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: server: Implemented http_server_request_fail_text(), which allows creating...
authorStephan Bosch <stephan.bosch@dovecot.fi>
Tue, 17 Oct 2017 14:16:22 +0000 (16:16 +0200)
committerStephan Bosch <stephan.bosch@dovecot.fi>
Tue, 24 Oct 2017 20:29:18 +0000 (22:29 +0200)
src/lib-http/http-server-request.c
src/lib-http/http-server.h

index 23aba2a567fa90bd265fb3425b1d7ce7d6b85dab..1763528afba401ff271bbb09a222cc4bb26b238e 100644 (file)
@@ -359,9 +359,10 @@ void http_server_request_finished(struct http_server_request *req)
        http_server_connection_trigger_responses(conn);
 }
 
-static         struct http_server_response *
+static struct http_server_response *
 http_server_request_create_fail_response(struct http_server_request *req,
-       unsigned int status, const char *reason)
+       unsigned int status, const char *reason, const char *text)
+       ATTR_NULL(4)
 {
        struct http_server_response *resp;
 
@@ -373,9 +374,11 @@ http_server_request_create_fail_response(struct http_server_request *req,
        if (!http_request_method_is(&req->req, "HEAD")) {
                http_server_response_add_header
                        (resp, "Content-Type", "text/plain; charset=utf-8");
-               reason = t_strconcat(reason, "\r\n", NULL);
+               if (text == NULL)
+                       text = reason;
+               text = t_strconcat(text, "\r\n", NULL);
                http_server_response_set_payload_data
-                       (resp, (const unsigned char *)reason, strlen(reason));
+                       (resp, (const unsigned char *)text, strlen(text));
        }
 
        return resp;
@@ -383,12 +386,14 @@ http_server_request_create_fail_response(struct http_server_request *req,
 
 static void
 http_server_request_fail_full(struct http_server_request *req,
-       unsigned int status, const char *reason)
+       unsigned int status, const char *reason, const char *text)
+       ATTR_NULL(4)
 {
        struct http_server_response *resp;
 
        req->failed = TRUE;
-       resp = http_server_request_create_fail_response(req, status, reason);
+       resp = http_server_request_create_fail_response(req,
+               status, reason, text);
        http_server_response_submit(resp);
        if (req->conn->input_broken)
                req->connection_close = TRUE;
@@ -397,14 +402,25 @@ http_server_request_fail_full(struct http_server_request *req,
 void http_server_request_fail(struct http_server_request *req,
        unsigned int status, const char *reason)
 {
-       http_server_request_fail_full(req, status, reason);
+       http_server_request_fail_full(req, status, reason, NULL);
 }
 
 void http_server_request_fail_close(struct http_server_request *req,
        unsigned int status, const char *reason)
 {
        http_server_request_connection_close(req, TRUE);
-       http_server_request_fail_full(req, status, reason);
+       http_server_request_fail_full(req, status, reason, NULL);
+}
+
+void http_server_request_fail_text(struct http_server_request *req,
+       unsigned int status, const char *reason, const char *format, ...)
+{
+       va_list args;
+
+       va_start(args, format);
+       http_server_request_fail_full(req, status, reason,
+               t_strdup_vprintf(format, args));
+       va_end(args);
 }
 
 void http_server_request_fail_auth(struct http_server_request *req,
@@ -417,7 +433,8 @@ void http_server_request_fail_auth(struct http_server_request *req,
        if (reason == NULL)
                reason = "Unauthenticated";
 
-       resp = http_server_request_create_fail_response(req, 401, reason);
+       resp = http_server_request_create_fail_response(req,
+               401, reason, reason);
        http_server_response_add_auth(resp, chlng);
        http_server_response_submit(resp);
 }
index 6daf0f500fb0d8893584e860b060727c9c9840ee..eadc799f0fc4386ac55ed2679413638cfcb522c5 100644 (file)
@@ -221,6 +221,11 @@ void http_server_request_fail(struct http_server_request *req,
    and close the connection. */
 void http_server_request_fail_close(struct http_server_request *req,
        unsigned int status, const char *reason);
+/* Send a failure response for the request with given status/reason/text.
+   The text is sent as the response payload, if appropriate. */
+void http_server_request_fail_text(struct http_server_request *req,
+       unsigned int status, const char *reason, const char *format, ...)
+        ATTR_FORMAT(4, 5);
 /* Send an authentication failure response for the request with given reason.
    The provided challenge is set in the WWW-Authenticate header of the
    response. */