]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
rtsp: split out a subfunction from rtsp_do
authorDaniel Stenberg <daniel@haxx.se>
Wed, 3 Sep 2025 22:03:04 +0000 (00:03 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 4 Sep 2025 06:31:27 +0000 (08:31 +0200)
Takes complexity down from 71 to 52.

Closes #18471

lib/rtsp.c

index dbe3e63cf315e50ef99e6a3cce1d5a003d144d4d..1d5f44f91b248526c38e6591441801665f4e9198 100644 (file)
@@ -278,6 +278,84 @@ static CURLcode rtsp_done(struct Curl_easy *data,
   return httpStatus;
 }
 
+
+static CURLcode rtsp_setup_body(struct Curl_easy *data,
+                                Curl_RtspReq rtspreq,
+                                struct dynbuf *reqp)
+{
+  CURLcode result;
+  if(rtspreq == RTSPREQ_ANNOUNCE ||
+     rtspreq == RTSPREQ_SET_PARAMETER ||
+     rtspreq == RTSPREQ_GET_PARAMETER) {
+    curl_off_t req_clen; /* request content length */
+
+    if(data->state.upload) {
+      req_clen = data->state.infilesize;
+      data->state.httpreq = HTTPREQ_PUT;
+      result = Curl_creader_set_fread(data, req_clen);
+      if(result)
+        return result;
+    }
+    else {
+      if(data->set.postfields) {
+        size_t plen = strlen(data->set.postfields);
+        req_clen = (curl_off_t)plen;
+        result = Curl_creader_set_buf(data, data->set.postfields, plen);
+      }
+      else if(data->state.infilesize >= 0) {
+        req_clen = data->state.infilesize;
+        result = Curl_creader_set_fread(data, req_clen);
+      }
+      else {
+        req_clen = 0;
+        result = Curl_creader_set_null(data);
+      }
+      if(result)
+        return result;
+    }
+
+    if(req_clen > 0) {
+      /* As stated in the http comments, it is probably not wise to
+       * actually set a custom Content-Length in the headers */
+      if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
+        result = curlx_dyn_addf(reqp, "Content-Length: %" FMT_OFF_T"\r\n",
+                                req_clen);
+        if(result)
+          return result;
+      }
+
+      if(rtspreq == RTSPREQ_SET_PARAMETER ||
+         rtspreq == RTSPREQ_GET_PARAMETER) {
+        if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
+          result = curlx_dyn_addn(reqp,
+                                  STRCONST("Content-Type: "
+                                           "text/parameters\r\n"));
+          if(result)
+            return result;
+        }
+      }
+
+      if(rtspreq == RTSPREQ_ANNOUNCE) {
+        if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
+          result = curlx_dyn_addn(reqp,
+                                  STRCONST("Content-Type: "
+                                           "application/sdp\r\n"));
+          if(result)
+            return result;
+        }
+      }
+    }
+    else if(rtspreq == RTSPREQ_GET_PARAMETER) {
+      /* Check for an empty GET_PARAMETER (heartbeat) request */
+      data->state.httpreq = HTTPREQ_HEAD;
+      data->req.no_body = TRUE;
+    }
+  }
+  else
+    result = Curl_creader_set_null(data);
+  return result;
+}
+
 static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
 {
   struct connectdata *conn = data->conn;
@@ -561,79 +639,9 @@ static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
   if(result)
     goto out;
 
-  if(rtspreq == RTSPREQ_ANNOUNCE ||
-     rtspreq == RTSPREQ_SET_PARAMETER ||
-     rtspreq == RTSPREQ_GET_PARAMETER) {
-    curl_off_t req_clen; /* request content length */
-
-    if(data->state.upload) {
-      req_clen = data->state.infilesize;
-      data->state.httpreq = HTTPREQ_PUT;
-      result = Curl_creader_set_fread(data, req_clen);
-      if(result)
-        goto out;
-    }
-    else {
-      if(data->set.postfields) {
-        size_t plen = strlen(data->set.postfields);
-        req_clen = (curl_off_t)plen;
-        result = Curl_creader_set_buf(data, data->set.postfields, plen);
-      }
-      else if(data->state.infilesize >= 0) {
-        req_clen = data->state.infilesize;
-        result = Curl_creader_set_fread(data, req_clen);
-      }
-      else {
-        req_clen = 0;
-        result = Curl_creader_set_null(data);
-      }
-      if(result)
-        goto out;
-    }
-
-    if(req_clen > 0) {
-      /* As stated in the http comments, it is probably not wise to
-       * actually set a custom Content-Length in the headers */
-      if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
-        result =
-          curlx_dyn_addf(&req_buffer, "Content-Length: %" FMT_OFF_T"\r\n",
-                         req_clen);
-        if(result)
-          goto out;
-      }
-
-      if(rtspreq == RTSPREQ_SET_PARAMETER ||
-         rtspreq == RTSPREQ_GET_PARAMETER) {
-        if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
-          result = curlx_dyn_addn(&req_buffer,
-                                  STRCONST("Content-Type: "
-                                           "text/parameters\r\n"));
-          if(result)
-            goto out;
-        }
-      }
-
-      if(rtspreq == RTSPREQ_ANNOUNCE) {
-        if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
-          result = curlx_dyn_addn(&req_buffer,
-                                  STRCONST("Content-Type: "
-                                           "application/sdp\r\n"));
-          if(result)
-            goto out;
-        }
-      }
-    }
-    else if(rtspreq == RTSPREQ_GET_PARAMETER) {
-      /* Check for an empty GET_PARAMETER (heartbeat) request */
-      data->state.httpreq = HTTPREQ_HEAD;
-      data->req.no_body = TRUE;
-    }
-  }
-  else {
-    result = Curl_creader_set_null(data);
-    if(result)
-      goto out;
-  }
+  result = rtsp_setup_body(data, rtspreq, &req_buffer);
+  if(result)
+    goto out;
 
   /* Finish the request buffer */
   result = curlx_dyn_addn(&req_buffer, STRCONST("\r\n"));