I don’t think this is really right for the vanilla -m http.server
super-simple server. It should be pretty easy to do this with your own script (subclass the request handler and add your code there), but given that this isn’t meant for production work anyway, it’s never going to be able to do everything that you want it to.
Maybe this would be better done as a recipe in the docs, showing how easy it is to extend the basic functionality?