* [PATCHv2] debuginfod: Check result of calling MHD_add_response_header.
@ 2021-12-01 14:51 Mark Wielaard
2021-12-01 15:23 ` Frank Ch. Eigler
0 siblings, 1 reply; 3+ messages in thread
From: Mark Wielaard @ 2021-12-01 14:51 UTC (permalink / raw)
To: elfutils-devel; +Cc: Mark Wielaard
Although unlikely the MHD_add_response_header can fail for
various reasons. If it fails something odd is going on.
So check we can actually add a response header and log an
error if we cannot.
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
This version only check and logs the error, but still uses the
response object.
debuginfod/ChangeLog | 10 +++++
debuginfod/debuginfod.cxx | 87 +++++++++++++++++++++++++++++----------
2 files changed, 75 insertions(+), 22 deletions(-)
diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index 822bd637..3866e7fa 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,13 @@
+2021-12-01 Mark Wielaard <mark@klomp.org>
+
+ * debuginfod.cxx (reportable_exception::mhd_send_response): Check
+ MHD_add_response_header result.
+ (add_mhd_last_modified): Likewise.
+ (handle_buildid_f_match): Likewise.
+ (handle_buildid_r_match): Likewise.
+ (handle_metrics): Likewise.
+ (handle_root): Likewise.
+
2021-11-10 Érico N. Rolim <erico.erc@gmail.com>
* debuginfod.cxx: include "system.h" under 'extern "C"' block.
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 764e7b94..b3416134 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -1,5 +1,6 @@
/* Debuginfo-over-http server.
Copyright (C) 2019-2021 Red Hat, Inc.
+ Copyright (C) 2021 Mark J. Wielaard <mark@klomp.org>
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
@@ -646,10 +647,11 @@ struct reportable_exception
MHD_Response* r = MHD_create_response_from_buffer (message.size(),
(void*) message.c_str(),
MHD_RESPMEM_MUST_COPY);
- MHD_add_response_header (r, "Content-Type", "text/plain");
- MHD_RESULT rc = MHD_queue_response (c, code, r);
+ MHD_RESULT rc1, rc2;
+ rc1 = MHD_add_response_header (r, "Content-Type", "text/plain");
+ rc2 = MHD_queue_response (c, code, r);
MHD_destroy_response (r);
- return rc;
+ return (rc1 == MHD_NO || rc2 == MHD_NO) ? MHD_NO : MHD_YES;
}
};
@@ -1076,10 +1078,15 @@ add_mhd_last_modified (struct MHD_Response *resp, time_t mtime)
char datebuf[80];
size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT", now);
if (rc > 0 && rc < sizeof (datebuf))
- (void) MHD_add_response_header (resp, "Last-Modified", datebuf);
+ if (MHD_add_response_header (resp, "Last-Modified", datebuf) == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: couldn't add Last-Modified header"
+ << endl;
}
- (void) MHD_add_response_header (resp, "Cache-Control", "public");
+ if (MHD_add_response_header (resp, "Cache-Control", "public") == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: couldn't add Cache-Control header" << endl;
}
@@ -1125,10 +1132,16 @@ handle_buildid_f_match (bool internal_req_t,
}
else
{
- MHD_add_response_header (r, "Content-Type", "application/octet-stream");
std::string file = b_source0.substr(b_source0.find_last_of("/")+1, b_source0.length());
- MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(s.st_size).c_str() );
- MHD_add_response_header (r, "X-DEBUGINFOD-FILE", file.c_str() );
+ if (MHD_add_response_header (r, "Content-Type",
+ "application/octet-stream") == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-SIZE",
+ to_string(s.st_size).c_str()) == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-FILE",
+ file.c_str()) == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: cannot add response headers for "
+ << b_source0 << endl;
add_mhd_last_modified (r, s.st_mtime);
if (verbose > 1)
obatched(clog) << "serving file " << b_source0 << endl;
@@ -1597,10 +1610,17 @@ handle_buildid_r_match (bool internal_req_p,
inc_metric ("http_responses_total","result","archive fdcache");
- MHD_add_response_header (r, "Content-Type", "application/octet-stream");
- MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(fs.st_size).c_str());
- MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
- MHD_add_response_header (r, "X-DEBUGINFOD-FILE", b_source1.c_str());
+ if (MHD_add_response_header (r, "Content-Type",
+ "application/octet-stream") == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-SIZE",
+ to_string(fs.st_size).c_str()) == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE",
+ b_source0.c_str()) == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-FILE",
+ b_source1.c_str()) == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: cannot add response header for "
+ << b_source0 << endl;
add_mhd_last_modified (r, fs.st_mtime);
if (verbose > 1)
obatched(clog) << "serving fdcache archive " << b_source0 << " file " << b_source1 << endl;
@@ -1741,12 +1761,20 @@ handle_buildid_r_match (bool internal_req_p,
}
else
{
- MHD_add_response_header (r, "Content-Type", "application/octet-stream");
std::string file = b_source1.substr(b_source1.find_last_of("/")+1, b_source1.length());
- MHD_add_response_header (r, "X-DEBUGINFOD-SIZE", to_string(fs.st_size).c_str());
- MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
- MHD_add_response_header (r, "X-DEBUGINFOD-FILE", file.c_str());
-
+ if (MHD_add_response_header (r, "Content-Type",
+ "application/octet-stream") == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-SIZE",
+ to_string(fs.st_size).c_str()) == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-ARCHIVE",
+ b_source0.c_str()) == MHD_NO
+ || MHD_add_response_header (r, "X-DEBUGINFOD-FILE",
+ file.c_str()) == MHD_NO)
+ {
+ if (verbose)
+ obatched(clog) << "Error: cannot create response header for "
+ << b_source0 << endl;
+ }
add_mhd_last_modified (r, archive_entry_mtime(e));
if (verbose > 1)
obatched(clog) << "serving archive " << b_source0 << " file " << b_source1 << endl;
@@ -2012,7 +2040,12 @@ and will not query the upstream servers");
auto r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
if (r)
{
- MHD_add_response_header (r, "Content-Type", "application/octet-stream");
+ if (MHD_add_response_header (r, "Content-Type",
+ "application/octet-stream") == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: couldn't add Content-Type header"
+ << endl;
+
add_mhd_last_modified (r, s.st_mtime);
if (verbose > 1)
obatched(clog) << "serving file from upstream debuginfod/cache" << endl;
@@ -2163,8 +2196,13 @@ handle_metrics (off_t* size)
MHD_Response* r = MHD_create_response_from_buffer (os.size(),
(void*) os.c_str(),
MHD_RESPMEM_MUST_COPY);
- *size = os.size();
- MHD_add_response_header (r, "Content-Type", "text/plain");
+ if (r != NULL)
+ {
+ *size = os.size();
+ if (MHD_add_response_header (r, "Content-Type", "text/plain") == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: couldn't add Content-Type header" << endl;
+ }
return r;
}
@@ -2176,8 +2214,13 @@ handle_root (off_t* size)
MHD_Response* r = MHD_create_response_from_buffer (version.size (),
(void *) version.c_str (),
MHD_RESPMEM_PERSISTENT);
- *size = version.size ();
- MHD_add_response_header (r, "Content-Type", "text/plain");
+ if (r != NULL)
+ {
+ *size = version.size ();
+ if (MHD_add_response_header (r, "Content-Type", "text/plain") == MHD_NO)
+ if (verbose)
+ obatched(clog) << "Error: couldn't add Content-Type header" << endl;
+ }
return r;
}
--
2.18.4
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCHv2] debuginfod: Check result of calling MHD_add_response_header.
2021-12-01 14:51 [PATCHv2] debuginfod: Check result of calling MHD_add_response_header Mark Wielaard
@ 2021-12-01 15:23 ` Frank Ch. Eigler
2021-12-08 15:10 ` Mark Wielaard
0 siblings, 1 reply; 3+ messages in thread
From: Frank Ch. Eigler @ 2021-12-01 15:23 UTC (permalink / raw)
To: Mark Wielaard; +Cc: elfutils-devel
Hi -
> Although unlikely the MHD_add_response_header can fail for
> various reasons. If it fails something odd is going on.
> So check we can actually add a response header and log an
> error if we cannot.
TBH I wouldn't bother even this much checking. It just uglifies the
code. If it would make covscan happier, I'd stick a (void) in front
of the add-header calls.
> - MHD_add_response_header (r, "Content-Type", "text/plain");
> - MHD_RESULT rc = MHD_queue_response (c, code, r);
> + MHD_RESULT rc1, rc2;
> + rc1 = MHD_add_response_header (r, "Content-Type", "text/plain");
> + rc2 = MHD_queue_response (c, code, r);
> MHD_destroy_response (r);
> - return rc;
> + return (rc1 == MHD_NO || rc2 == MHD_NO) ? MHD_NO : MHD_YES;
e.g. this part won't work: returning MHD_NO causes libmicrohttpd to
send a 503 error back to the caller, regardless of our intended one.
> + if (MHD_add_response_header (resp, "Last-Modified", datebuf) == MHD_NO)
> + if (verbose)
> + obatched(clog) << "Error: couldn't add Last-Modified header"
> + << endl;
> }
e.g., we normally report errors to the logs, regardless of verbosity
settings.
> + if (MHD_add_response_header (r, "Content-Type",
> + "application/octet-stream") == MHD_NO
> + || MHD_add_response_header (r, "X-DEBUGINFOD-SIZE",
> + to_string(s.st_size).c_str()) == MHD_NO
> + || MHD_add_response_header (r, "X-DEBUGINFOD-FILE",
> + file.c_str()) == MHD_NO)
e.g., this formulation makes it impossible to add some headers if a
previous one failed.
- FChE
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCHv2] debuginfod: Check result of calling MHD_add_response_header.
2021-12-01 15:23 ` Frank Ch. Eigler
@ 2021-12-08 15:10 ` Mark Wielaard
0 siblings, 0 replies; 3+ messages in thread
From: Mark Wielaard @ 2021-12-08 15:10 UTC (permalink / raw)
To: Frank Ch. Eigler; +Cc: elfutils-devel
Hi Frank,
On Wed, 2021-12-01 at 10:23 -0500, Frank Ch. Eigler wrote:
> > Although unlikely the MHD_add_response_header can fail for
> > various reasons. If it fails something odd is going on.
> > So check we can actually add a response header and log an
> > error if we cannot.
>
> TBH I wouldn't bother even this much checking. It just uglifies the
> code. If it would make covscan happier, I'd stick a (void) in front
> of the add-header calls.
That is really just like ignoring the issue imho. But you are right
that it uglifies the code, I'll wrap the calls in an helper function.
> > - MHD_add_response_header (r, "Content-Type", "text/plain");
> > - MHD_RESULT rc = MHD_queue_response (c, code, r);
> > + MHD_RESULT rc1, rc2;
> > + rc1 = MHD_add_response_header (r, "Content-Type",
> > "text/plain");
> > + rc2 = MHD_queue_response (c, code, r);
> > MHD_destroy_response (r);
> > - return rc;
> > + return (rc1 == MHD_NO || rc2 == MHD_NO) ? MHD_NO : MHD_YES;
>
> e.g. this part won't work: returning MHD_NO causes libmicrohttpd to
> send a 503 error back to the caller, regardless of our intended one.
OK, so we only want to report MHD_NO here when MHD_queue_response
fails.
> > + if (MHD_add_response_header (resp, "Last-Modified",
> > datebuf) == MHD_NO)
> > + if (verbose)
> > + obatched(clog) << "Error: couldn't add Last-Modified
> > header"
> > + << endl;
> > }
>
> e.g., we normally report errors to the logs, regardless of verbosity
> settings.
OK, I'll remove the if (verbose).
> > + if (MHD_add_response_header (r, "Content-Type",
> > + "application/octet-stream") ==
> > MHD_NO
> > + || MHD_add_response_header (r, "X-DEBUGINFOD-SIZE",
> > + to_string(s.st_size).c_str()
> > ) == MHD_NO
> > + || MHD_add_response_header (r, "X-DEBUGINFOD-FILE",
> > + file.c_str()) == MHD_NO)
>
> e.g., this formulation makes it impossible to add some headers if a
> previous one failed.
It is likely that if one fails, then all others fail similarly, but I
see your point. Any header is better than no headers at all.
Thanks,
Mark
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-12-08 15:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-01 14:51 [PATCHv2] debuginfod: Check result of calling MHD_add_response_header Mark Wielaard
2021-12-01 15:23 ` Frank Ch. Eigler
2021-12-08 15:10 ` Mark Wielaard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).