Lucas van Dongen
Lucas van Dongen

Reputation: 9868

BaseHTTPRequestHandler cgi.parse_multipart never returns

I have the following snippet in my do_POST:

            c_type, p_dict = cgi.parse_header(
                self.headers.get('Content-Type')
            )
            content_len = int(self.headers.get('Content-length'))
            p_dict['boundary'] = bytes(p_dict['boundary'], "utf-8")
            p_dict['CONTENT-LENGTH'] = content_len
            postvars = cgi.parse_multipart(self.rfile, p_dict)

It continues all the way up until the last postvars line, where it just stops. Subsequent requests aren't even processed until I restart the debug process.

For completeness, this is the JS I use to post the FormData:

    function send() {
        var xhr = new XMLHttpRequest();
        var url = "URL_WAS_HERE";
        xhr.open("POST", url, true);
        xhr.onreadystatechange = function () {

        };
        var form = new FormData(document.querySelector('form'));
        xhr.send(form);
    }

Other forms that simply POST a JSON string work fine, but this needs to be multi-part because I also upload a file in the form. I cannot figure out what is wrong with my code, it looks more or less the same as everybody else's multipart handling code for Python 3.

What also could be an interesting lead is that if I try the following in the debug console:

form = cgi.FieldStorage(
    fp=self.rfile,
    headers=self.headers,
    environ={'REQUEST_METHOD':'POST'})

...this debug command also stays stuck forever.

Upvotes: 2

Views: 267

Answers (1)

Lucas van Dongen
Lucas van Dongen

Reputation: 9868

I had read the rfile before in a fork I thought I didn't hit. Still weird behavior, I would expect an error instead.

Upvotes: 2

Related Questions