How to pre-process requests before handling them using BaseHTTPServer?

204 views Asked by At

I want to deny access to certain paths on my server, which is using the CGIHttpServer module. I've come up with a whitelist that I'll need to check on every request, but my problem is how to implement it.

I tried overriding the handle_one_request method (which both processes the request and sends the response) but the problem is that the path information is not available before it's run. Meaning that I'll always be either too early or too late. Now I'm thinking of either copy-pasting the original function source and editing it myself or overriding wfile.flush, which sounds way too ugly.

Can anyone give me a better solution?

Here's the source code for handle_one_request.

1

There are 1 answers

2
Jorgen Schäfer On

You can override run_cgi in the CGIHTTPRequestHandler subclass, and only call super when your whitelist matches.