Skip to content

MultipartParseError: Did not find boundary character #123

Description

@yecril23pl

I tried using multipart from Python module http.server. It did not work. What am I doing wrong (apart from everything)?

Server script

#!/usr/bin/python3
# coding=ISO-8859-2
import multipart
from os import environ
from sys import stdin

def on_field(field):
	pass

def on_file(file):
	print (file.field_name)

def get_content_type ():
	return environ .get ('CONTENT_TYPE')

def get_content_length ():
	return environ .get ('CONTENT_LENGTH')

print('''Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML ><HTML LANG="pl" ><TITLE >Przyjęcie formularza</TITLE
 ><H1 >Wynik zapytania</H1 ><P >&hellip; '''
  )

if False:
	print (get_content_type (), input ())
else:
  multipart.parse_form (
  {
	  'Content-Type' : get_content_type (),
	  'Content-Length' : get_content_length ()
  }, stdin, on_field, on_file)

Server command

python -m http.server --cgi

Payload

POST /cgi-bin/upload HTTP/1
Host: 0.0.0.0:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------250220064916703015541512867370
Content-Length: 239
Origin: http://0.0.0.0:8000
Connection: keep-alive
Referer: http://0.0.0.0:8000/
Upgrade-Insecure-Requests: 1

-----------------------------250220064916703015541512867370
Content-Disposition: form-data; name="FILE"; filename=".bash_aliases"
Content-Type: application/octet-stream


-----------------------------250220064916703015541512867370--

Server log

127.0.0.1 - - [23/Feb/2024 16:03:37] "POST /cgi-bin/upload HTTP/1.1" 200 -
Did not find boundary character '-' at index 2
Traceback (most recent call last):
  File "/home/krzysztof/cgi-bin/upload", line 28, in <module>
    multipart.parse_form (
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1902, in parse_form
    parser.write(buff)
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1794, in write
    return self.parser.write(data)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1076, in write
    l = self._internal_write(data, data_len)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/multipart/multipart.py", line 1183, in _internal_write
    raise e
multipart.exceptions.MultipartParseError: Did not find boundary character '-' at index 2
127.0.0.1 - - [23/Feb/2024 16:03:37] CGI script exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions