Blame SOURCES/0088-secrets-always-add-Content-Length-header.patch

ecf709
From 07271dbd7c8f28a6aace48787040580973eb5a4e Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
ecf709
Date: Wed, 15 Mar 2017 15:15:08 +0100
ecf709
Subject: [PATCH 88/90] secrets: always add Content-Length header
ecf709
ecf709
If custodia server does not reply with Content-Length header, curl may
ecf709
wait for non-existing body of http reply if such body does not exist
ecf709
(for example during POST operation when creating a container).
ecf709
ecf709
Reviewed-by: Simo Sorce <simo@redhat.com>
ecf709
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
ecf709
(cherry picked from commit 13d720de13e490850c1139eea865bcd5195a2630)
ecf709
---
ecf709
 src/responder/secrets/providers.c | 72 ++++++++++++++++++++++++++++++++++++---
ecf709
 1 file changed, 68 insertions(+), 4 deletions(-)
ecf709
ecf709
diff --git a/src/responder/secrets/providers.c b/src/responder/secrets/providers.c
ecf709
index 80a443d91135447ec8ce8d424b692a6d7e26a907..a27fb720b394e7c76d1b65f656146bcd00755449 100644
ecf709
--- a/src/responder/secrets/providers.c
ecf709
+++ b/src/responder/secrets/providers.c
ecf709
@@ -388,20 +388,84 @@ int sec_http_reply_with_headers(TALLOC_CTX *mem_ctx, struct sec_data *reply,
ecf709
     return EOK;
ecf709
 }
ecf709
 
ecf709
+static errno_t
ecf709
+sec_http_iobuf_split(struct sss_iobuf *response,
ecf709
+                     const char **headers,
ecf709
+                     const char **body)
ecf709
+{
ecf709
+    const char *data = (const char *)sss_iobuf_get_data(response);
ecf709
+    char *delim;
ecf709
+
ecf709
+    /* The last header ends with \r\n and then comes \r\n again as a separator
ecf709
+     * of body from headers. We can use this to find this point. */
ecf709
+    delim = strstr(data, "\r\n\r\n");
ecf709
+    if (delim == NULL) {
ecf709
+        return EINVAL;
ecf709
+    }
ecf709
+
ecf709
+    /* Skip to the body delimiter. */
ecf709
+    delim = delim + sizeof("\r\n") - 1;
ecf709
+
ecf709
+    /* Replace \r\n with zeros turning data into:
ecf709
+     * from HEADER\r\nBODY into HEADER\0\0BODY format. */
ecf709
+    delim[0] = '\0';
ecf709
+    delim[1] = '\0';
ecf709
+
ecf709
+    /* Split the buffer. */
ecf709
+    *headers = data;
ecf709
+    *body = delim + 2;
ecf709
+
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+static const char *
ecf709
+sec_http_iobuf_add_content_length(TALLOC_CTX *mem_ctx,
ecf709
+                                  const char *headers,
ecf709
+                                  size_t body_len)
ecf709
+{
ecf709
+    /* If Content-Length is already present we do nothing. */
ecf709
+    if (strstr(headers, "Content-Length:") != NULL) {
ecf709
+        return headers;
ecf709
+    }
ecf709
+
ecf709
+    return talloc_asprintf(mem_ctx, "%sContent-Length: %zu\r\n",
ecf709
+                           headers, body_len);
ecf709
+}
ecf709
+
ecf709
 errno_t sec_http_reply_iobuf(TALLOC_CTX *mem_ctx,
ecf709
                              struct sec_data *reply,
ecf709
                              int response_code,
ecf709
                              struct sss_iobuf *response)
ecf709
 {
ecf709
+    const char *headers;
ecf709
+    const char *body;
ecf709
+    size_t body_len;
ecf709
+    errno_t ret;
ecf709
+
ecf709
     DEBUG(SSSDBG_TRACE_LIBS, "HTTP reply %d\n", response_code);
ecf709
 
ecf709
-    reply->data = (char *)sss_iobuf_get_data(response);
ecf709
-    reply->length = sss_iobuf_get_len(response);
ecf709
+    ret = sec_http_iobuf_split(response, &headers, &body);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE,
ecf709
+              "Unexpected HTTP reply, returning what we got from server\n");
ecf709
+        reply->data = (char *)sss_iobuf_get_data(response);
ecf709
+        reply->length = sss_iobuf_get_len(response);
ecf709
 
ecf709
-    talloc_steal(mem_ctx, reply->data);
ecf709
+        return EOK;
ecf709
+    }
ecf709
 
ecf709
+    /* Add Content-Length header if not present so client does not await
ecf709
+     * not-existing incoming data. */
ecf709
+    body_len = strlen(body);
ecf709
+    headers = sec_http_iobuf_add_content_length(mem_ctx, headers, body_len);
ecf709
+    if (headers == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    reply->length = strlen(headers) + sizeof("\r\n") - 1 + body_len;
ecf709
+    reply->data = talloc_asprintf(mem_ctx, "%s\r\n%s", headers, body);
ecf709
     if (reply->data == NULL) {
ecf709
-        return EINVAL;
ecf709
+        return ENOMEM;
ecf709
     }
ecf709
 
ecf709
     return EOK;
ecf709
-- 
ecf709
2.9.3
ecf709