]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
PR28284 - Debuginfod header functionality implemented
authorNoah Sanci <nsanci@redhat.com>
Wed, 15 Jun 2022 14:07:29 +0000 (10:07 -0400)
committerFrank Ch. Eigler <fche@redhat.com>
Tue, 6 Sep 2022 15:32:13 +0000 (11:32 -0400)
Debuginfod and debuginfod clients are now equipped to send
and receive http headers prefixed with X-DEBUGINFOD and
print them in verbose mode for more context

Signed-off-by: Noah Sanci <nsanci@redhat.com>
debuginfod/ChangeLog
debuginfod/debuginfod-client.c
debuginfod/debuginfod-find.c
debuginfod/debuginfod.cxx
debuginfod/debuginfod.h.in
debuginfod/libdebuginfod.map
doc/ChangeLog
doc/debuginfod_find_debuginfo.3
doc/debuginfod_get_headers.3 [new file with mode: 0644]
tests/ChangeLog
tests/run-debuginfod-response-headers.sh

index c692a3899e93dc15e653c5f91ee2001034f86990..8c843f3a0af291888aeb4e8aacc75e1106451ca1 100644 (file)
@@ -1,3 +1,17 @@
+2022-07-15  Noah Sanci  <nsanci@redhat.com>
+
+       * debuginfod-client.c (header_callback): Handle headers without
+       X-DEBUGINFOD prefix.
+       (debuginfod_query_server): Removed verbose printing headers when
+       undesired.
+       (debuginfod_get_headers): Created.
+       * debuginfod-find.c (main): Verboes printing headers.
+       * debuginfod.cxx (handle_buildid): Add headers prefixed with
+       X-DEBUGINFOD from federated servers to this server's response
+       headers.
+       * debuginfod.h.in (debuginfod_get_headers): Created.
+       * libdebuginfod.map: New elfutils version added.
+
 2022-09-02  Aaron Merey  <amerey@redhat.com>
 
        * debuginfod.cxx (parse_opt): If '-C' is given with no arg, do not
index b7b65affe7553d10ce7ccd3a788710c431696e83..a3565f57c35acd873be107fdea1de293b6f8a02c 100644 (file)
@@ -501,6 +501,12 @@ header_callback (char * buffer, size_t size, size_t numitems, void * userdata)
 {
   if (size != 1)
     return 0;
+  // X-DEBUGINFOD is 11 characters long.
+  // Some basic checks to ensure the headers received are of the expected format
+  if ( strncmp(buffer, "X-DEBUGINFOD", 11) || buffer[numitems-1] != '\n'
+       || (buffer == strstr(buffer, ":")) ){
+    return numitems;
+  }
   /* Temporary buffer for realloc */
   char *temp = NULL;
   struct handle_data *data = (struct handle_data *) userdata;
@@ -1111,8 +1117,6 @@ debuginfod_query_server (debuginfod_client *c,
                 if (c->winning_headers == NULL)
                   {
                     c->winning_headers = data[committed_to].response_data;
-                    if (vfd >= 0 && c->winning_headers != NULL)
-                      dprintf(vfd, "\n%s", c->winning_headers);
                     data[committed_to].response_data = NULL;
                     data[committed_to].response_data_size = 0;
                   }
@@ -1542,6 +1546,12 @@ debuginfod_get_url(debuginfod_client *client)
   return client->url;
 }
 
+const char *
+debuginfod_get_headers(debuginfod_client *client)
+{
+  return client->winning_headers;
+}
+
 void
 debuginfod_end (debuginfod_client *client)
 {
index f60b5463ce1bbd08f39aad6fd659b651c4288c46..fb1f294cec9815d8e42d57a2fcb289a96cc423db 100644 (file)
@@ -215,6 +215,9 @@ main(int argc, char** argv)
 
   if (verbose)
     {
+      const char* headers = debuginfod_get_headers(client);
+      if (headers)
+        fprintf(stderr, "Headers:\n%s", headers);
       const char* url = debuginfod_get_url (client);
       if (url != NULL)
         fprintf(stderr, "Downloaded from %s\n", url);
index 8680c0487c56aed74f2f4e0299e49862d546fec6..27b671d3bcf97f6e55e5a929d06299c447b42f96 100644 (file)
@@ -2093,6 +2093,24 @@ and will not query the upstream servers");
             {
               add_mhd_response_header (r, "Content-Type",
                                       "application/octet-stream");
+              const char * hdrs = debuginfod_get_headers(client);
+              string header_dup;
+              if (hdrs)
+                header_dup = string(hdrs);
+              size_t pos = 0;
+              // Clean winning headers to add all X-DEBUGINFOD lines to the package we'll send
+              while( (pos = header_dup.find("X-DEBUGINFOD")) != string::npos)
+                {
+                  // Focus on where X-DEBUGINFOD- begins
+                  header_dup = header_dup.substr(pos);
+                  size_t newline =  header_dup.find('\n');
+                  if (newline == string::npos)
+                    break;
+                  add_mhd_response_header(r, header_dup.substr(0,header_dup.find(':')).c_str(),
+                                             header_dup.substr(header_dup.find(':')).c_str());
+                  header_dup = header_dup.substr(newline);
+                }
+
               add_mhd_last_modified (r, s.st_mtime);
               if (verbose > 1)
                 obatched(clog) << "serving file from upstream debuginfod/cache" << endl;
index c358df4d60adcb986da9afd137a56da63b237303..6ae8b91cfb2aeab2645851e1fd60f7305f74b08a 100644 (file)
@@ -93,6 +93,10 @@ void* debuginfod_get_user_data (debuginfod_client *client);
 /* Get the current or last active URL, if known.  */
 const char* debuginfod_get_url (debuginfod_client *client);
 
+/* Returns all headers sent to this client which were prefixed
+ * with X-DEBUGINFOD */
+const char* debuginfod_get_headers(debuginfod_client *client);
+
 /* Add an outgoing HTTP request  "Header: Value".  Copies string.  */
 int debuginfod_add_http_header (debuginfod_client *client, const char* header);
 
index 7d2f5882fed3e22c60329244970a356f07377b0b..f95b5b9a1b92be93b8d2825583cf844896868e23 100644 (file)
@@ -18,3 +18,6 @@ ELFUTILS_0.179 {
 ELFUTILS_0.183 {
   debuginfod_set_verbose_fd;
 } ELFUTILS_0.179;
+ELFUTILS_0.189 {
+  debuginfod_get_headers;
+} ELFUTILS_0.183;
index 3fff1a845f812bc0cd7513f246dee7f9423f6128..c2a01a04502c8dd7cc35ecf7846ae6f85e81256e 100644 (file)
@@ -1,3 +1,8 @@
+2022-07-15  Noah Sanci  <nsanci@redhat.com>
+       * debuginfod_find_debuginfo.3: Explained debuginfod_get_headers
+       usage.
+       * debuginfod_get_headers.3: Created.
+
 2022-09-02  Aaron Merey  <amerey@redhat.com>
 
        * debuginfod.8 (-C): Update description.
index 30cef3c1c324233882d3554d78e98675508bfaef..984fda12c8fd62d524b1e71c0eeb8452bc8e5903 100644 (file)
@@ -58,6 +58,7 @@ OPTIONAL FUNCTIONS
 .BI "const char* debuginfod_get_url(debuginfod_client *" client ");"
 .BI "int debuginfod_add_http_header(debuginfod_client *" client ","
 .BI "                               const char* " header ");"
+.BI "const char* debuginfod_get_headers(debuginfod_client *" client ");"
 
 .SH DESCRIPTION
 
@@ -198,6 +199,18 @@ By default, the library adds a descriptive \fIUser-Agent:\fP
 header to outgoing requests.  If the client application adds
 a header with the same name, this default is suppressed.
 
+.BR \%debuginfod_get_headers ()
+may be called with a debuginfod client. This function will return the
+http response headers prefixed with
+.BR X-DEBUGINFOD
+received from the first handle to get a response from a debuginfod server.
+Note that all other http headers aren't stored in the libcurl header
+callback function since they aren't of as much interest. The caller should
+copy the returned string if it is needed beyond the release of the client object.
+The returned string may be NULL if no headers are prefixed with
+.BR X-DEBUGINFOD
+\.
+
 .SH "MACROS"
 
 .SS "DEBUGINFOD_SONAME"
diff --git a/doc/debuginfod_get_headers.3 b/doc/debuginfod_get_headers.3
new file mode 100644 (file)
index 0000000..1db5598
--- /dev/null
@@ -0,0 +1,2 @@
+.so man3/debuginfod_find_debuginfo.3
+
index d2952cc91a897be4ba3616e55805403f1a0209a7..659bfa19cb05943f9fa28543ca615c279bf989e7 100644 (file)
@@ -1,3 +1,9 @@
+2022-07-15  Noah Sanci  <nsanci@redhat.com>
+
+       * run-debuginfod-response-headers.sh: Added test
+       to ensure that federated servers pass headers down to
+       queried server.
+
 2022-08-04  Sergei Trofimovich  <slyich@gmail.com>
 
        * low_high_pc.c (handle_die): Drop redundant 'lx' suffix.
index 62c43887b6f749d62c491c0175e7e8303cb67758..e5698cc9b1461e2d1f956d4a2c75bfec313c06d3 100755 (executable)
@@ -73,17 +73,21 @@ rm -rf $DEBUGINFOD_CACHE_PATH
 env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
     -vvv executable F/prog > vlog-find$PORT1.1 2>&1
 tempfiles vlog-find$PORT1.1
-grep 'Content-Length: ' vlog-find$PORT1.1
-grep 'X-DEBUGINFOD-FILE: ' vlog-find$PORT1.1
-grep 'X-DEBUGINFOD-SIZE: ' vlog-find$PORT1.1
+errfiles vlog-find$PORT1.1
+cat vlog-find$PORT1.1
+grep 'Headers:' vlog-find$PORT1.1
+grep 'X-DEBUGINFOD-FILE: prog' vlog-find$PORT1.1
+grep 'X-DEBUGINFOD-SIZE: '     vlog-find$PORT1.1
 
 # Check to see if an executable file located in an archive prints the file's description and archive
 env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
     -vvv executable c36708a78618d597dee15d0dc989f093ca5f9120 > vlog-find$PORT1.2 2>&1
 tempfiles vlog-find$PORT1.2
-grep 'Content-Length: ' vlog-find$PORT1.2
-grep 'X-DEBUGINFOD-FILE: ' vlog-find$PORT1.2
-grep 'X-DEBUGINFOD-SIZE: ' vlog-find$PORT1.2
+errfiles vlog-find$PORT1.2
+cat vlog-find$PORT1.2
+grep 'Headers:'               vlog-find$PORT1.2
+grep 'X-DEBUGINFOD-FILE: '    vlog-find$PORT1.2
+grep 'X-DEBUGINFOD-SIZE: '    vlog-find$PORT1.2
 grep 'X-DEBUGINFOD-ARCHIVE: ' vlog-find$PORT1.2
 
 # Check that X-DEBUGINFOD-SIZE matches the size of each file
@@ -94,6 +98,38 @@ do
     test $st_size -eq $x_debuginfod_size
 done
 
+rm -rf $DEBUGINFOD_CACHE_PATH
+BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
+          -a F/prog | grep 'Build ID' | cut -d ' ' -f 7`
+netcat_dir="buildid/$BUILDID/"
+mkdir -p ${PWD}/$netcat_dir
+cp F/prog ${PWD}/$netcat_dir/executable
+tempfiles F/prog
+
+# Netcat dies after answering the request
+nc -l -p $PORT2 -c 'echo -e "HTTP/1.1 200 OK\nX-DEBUGINFOD-SIZE: ba:d_size\nX-DEBUGINFOD-\rFILE:\=\+ \r213\n\n $(date)"' & < ${PWD}/$netcat_dir"executable" &
+# Wait until the netcat port is in use. Otherwise debuginfod-find can query
+# before netcat is ready.
+SECONDS=0
+nc_start=$SECONDS
+while [ ! $(lsof -i -P -n | grep LISTEN | grep "nc.*$PORT2")  ]
+do
+  # If it takes longer than 5 seconds for netcat to start up, then fail
+  duration=$(( SECONDS - nc_start ))
+  if [ $SECONDS -gt 5 ]
+  then
+    err
+  fi
+done
+
+env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT2 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
+    -vvv executable $BUILDID > vlog-find$PORT2 2>&1
+errfiles vlog-find$PORT2
+tempfiles vlog-find$PORT2
+cat vlog-find$PORT2 | grep "X-DEBUGINFOD-"
+rm -f "$netcat_dir"executable
+rmdir -p $netcat_dir
+
 kill $PID1
 wait $PID1
 PID1=0