[vlc-commits] [Git][videolan/vlc][master] 4 commits: network: httpd: move URL callbacks in a new function

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sun Jun 4 05:02:34 UTC 2023



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
42eebe57 by Alaric Senat at 2023-06-04T04:44:42+00:00
network: httpd: move URL callbacks in a new function

No functional changes.

- - - - -
4f17b5d4 by Alaric Senat at 2023-06-04T04:44:42+00:00
network: httpd: always NULL-check before url cb

- - - - -
db33ae13 by Alaric Senat at 2023-06-04T04:44:42+00:00
network: httpd: remove unused variable

- - - - -
d17cb406 by Alaric Senat at 2023-06-04T04:44:42+00:00
network: httpd: fix lock usage of URL callbacks

This lock was left unused in the current code-base. Previously locking
in `httpd_UrlCatch` for nothing.

Locking before calling the callbacks allows non-racy modifications
of the callback function pointer and/or opaque data after the start
of the HTTPD thread-loop and I think that it was the original intent of
the mutex.

This potential race-condition was encountered on the development of the
new HLS sout module while trying to hot-swap the httpd URL catch
callback context.

Here's a sample of the racy use-case:

```c
httpd_host_t *host = vlc_http_HostNew(obj);
httpd_url_t *url = httpd_UrlNew("/index.html", host, NULL, NULL);

const char *sys = "value1";
httpd_UrlCatch(url, HTTPD_MSG_GET, pf_callback, sys);

// ... Normal program execution ...

const char *new_sys = "value2";
// New context on the HTTP url callback.
httpd_UrlCatch(url, HTTPD_MSG_GET, pf_callback, new_sys);
```

- - - - -


1 changed file:

- src/network/httpd.c


Changes:

=====================================
src/network/httpd.c
=====================================
@@ -293,6 +293,22 @@ static size_t httpd_HtmlError (char **body, int code, const char *url)
     return (size_t)res;
 }
 
+static inline int httpd_UrlCatchCall(httpd_url_t *url, httpd_client_t *client)
+{
+    const uint8_t msg = client->query.i_type;
+
+    int status = VLC_EGENERIC;
+    vlc_mutex_lock(&url->lock);
+    if (url->catch[msg].cb != NULL)
+    {
+        status = url->catch[msg].cb(
+            url->catch[msg].p_sys, client, &client->answer, &client->query);
+    }
+    vlc_mutex_unlock(&url->lock);
+
+    return status;
+}
+
 
 /*****************************************************************************
  * High Level Functions: httpd_file_t
@@ -1639,14 +1655,12 @@ static int httpd_ClientSend(httpd_client_t *cl)
     if (cl->i_buffer >= cl->i_buffer_size) {
         if (cl->answer.i_body == 0  && cl->answer.i_body_offset > 0) {
             /* catch more body data */
-            int     i_msg = cl->query.i_type;
             int64_t i_offset = cl->answer.i_body_offset;
 
             httpd_MsgClean(&cl->answer);
             cl->answer.i_body_offset = i_offset;
 
-            cl->url->catch[i_msg].cb(cl->url->catch[i_msg].p_sys, cl,
-                                     &cl->answer, &cl->query);
+            httpd_UrlCatchCall(cl->url, cl);
         }
 
         if (cl->answer.i_body > 0) {
@@ -1865,8 +1879,6 @@ static void httpdLoop(httpd_host_t *host)
                         vlc_list_foreach(url, &host->urls, node) {
                             if (strcmp(url->psz_url, query->psz_url))
                                 continue;
-                            if (!url->catch[i_msg].cb)
-                                continue;
 
                             if (answer) {
                                 b_auth_failed = !httpdAuthOk(url->psz_user,
@@ -1876,7 +1888,7 @@ static void httpdLoop(httpd_host_t *host)
                                    break;
                             }
 
-                            if (url->catch[i_msg].cb(url->catch[i_msg].p_sys, cl, answer, query))
+                            if (httpd_UrlCatchCall(url, cl))
                                 continue;
 
                             if (answer->i_proto == HTTPD_PROTO_NONE)



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/7179b5ee6e24641844c2a1ba075187ee12216534...d17cb406dd1f03a96de568246455d43f0d5405a7

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/7179b5ee6e24641844c2a1ba075187ee12216534...d17cb406dd1f03a96de568246455d43f0d5405a7
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list