summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/command/upload.py
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-07-08 14:27:12 (GMT)
committerÉric Araujo <merwok@netwok.org>2011-07-08 14:27:12 (GMT)
commitce5fe83878c56ac17007e88148714ee523e64c94 (patch)
treed93797d6393b3836dc31f40a21f568889eb58de0 /Lib/packaging/command/upload.py
parentf8bebf8566bc31e4fe90c5c5f10bfa72f3b90c91 (diff)
downloadcpython-ce5fe83878c56ac17007e88148714ee523e64c94.zip
cpython-ce5fe83878c56ac17007e88148714ee523e64c94.tar.gz
cpython-ce5fe83878c56ac17007e88148714ee523e64c94.tar.bz2
Factor out code used by packaging commands for HTTP requests (#12169).
We now have one function to prepare multipart POST requests, and we use CRLF, as recommended by the HTTP spec (#10150). Initial patch by John Edmonds.
Diffstat (limited to 'Lib/packaging/command/upload.py')
-rw-r--r--Lib/packaging/command/upload.py52
1 files changed, 10 insertions, 42 deletions
diff --git a/Lib/packaging/command/upload.py b/Lib/packaging/command/upload.py
index df265c9..e39016c 100644
--- a/Lib/packaging/command/upload.py
+++ b/Lib/packaging/command/upload.py
@@ -14,7 +14,7 @@ from urllib.request import urlopen, Request
from packaging import logger
from packaging.errors import PackagingOptionError
from packaging.util import (spawn, read_pypirc, DEFAULT_REPOSITORY,
- DEFAULT_REALM)
+ DEFAULT_REALM, encode_multipart)
from packaging.command.cmd import Command
@@ -131,54 +131,22 @@ class upload(Command):
auth = b"Basic " + standard_b64encode(user_pass)
# Build up the MIME payload for the POST data
- boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
- sep_boundary = b'\n--' + boundary
- end_boundary = sep_boundary + b'--'
- body = BytesIO()
-
- file_fields = ('content', 'gpg_signature')
-
- for key, value in data.items():
- # handle multiple entries for the same name
- if not isinstance(value, tuple):
- value = [value]
-
- content_dispo = '\nContent-Disposition: form-data; name="%s"' % key
-
- if key in file_fields:
- filename_, content = value
- filename_ = ';filename="%s"' % filename_
- body.write(sep_boundary)
- body.write(content_dispo.encode('utf-8'))
- body.write(filename_.encode('utf-8'))
- body.write(b"\n\n")
- body.write(content)
- else:
- for value in value:
- value = str(value).encode('utf-8')
- body.write(sep_boundary)
- body.write(content_dispo.encode('utf-8'))
- body.write(b"\n\n")
- body.write(value)
- if value and value.endswith(b'\r'):
- # write an extra newline (lurve Macs)
- body.write(b'\n')
-
- body.write(end_boundary)
- body.write(b"\n")
- body = body.getvalue()
+ files = []
+ for key in ('content', 'gpg_signature'):
+ if key in data:
+ filename_, value = data.pop(key)
+ files.append((key, filename_, value))
+
+ content_type, body = encode_multipart(data.items(), files)
logger.info("Submitting %s to %s", filename, self.repository)
# build the Request
- headers = {'Content-type':
- 'multipart/form-data; boundary=%s' %
- boundary.decode('ascii'),
+ headers = {'Content-type': content_type,
'Content-length': str(len(body)),
'Authorization': auth}
- request = Request(self.repository, data=body,
- headers=headers)
+ request = Request(self.repository, body, headers)
# send the data
try:
result = urlopen(request)