diff Lib/packaging/command/register.py @ 71253:c2785ed52ed4

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.
author Éric Araujo <merwok@netwok.org>
date Fri, 08 Jul 2011 16:27:12 +0200
parents 1a31c3be88b1
children 28e4cd8fd864
line wrap: on
line diff
--- a/Lib/packaging/command/register.py	Sat Jul 02 16:58:25 2011 +0200
+++ b/Lib/packaging/command/register.py	Fri Jul 08 16:27:12 2011 +0200
@@ -10,7 +10,7 @@
 
 from packaging import logger
 from packaging.util import (read_pypirc, generate_pypirc, DEFAULT_REPOSITORY,
-                            DEFAULT_REALM, get_pypirc_path)
+                            DEFAULT_REALM, get_pypirc_path, encode_multipart)
 from packaging.command.cmd import Command
 
 class register(Command):
@@ -231,29 +231,11 @@
         if 'name' in data:
             logger.info('Registering %s to %s', data['name'], self.repository)
         # Build up the MIME payload for the urllib2 POST data
-        boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
-        sep_boundary = '\n--' + boundary
-        end_boundary = sep_boundary + '--'
-        body = io.StringIO()
-        for key, value in data.items():
-            # handle multiple entries for the same name
-            if not isinstance(value, (tuple, list)):
-                value = [value]
-
-            for value in value:
-                body.write(sep_boundary)
-                body.write('\nContent-Disposition: form-data; name="%s"'%key)
-                body.write("\n\n")
-                body.write(value)
-                if value and value[-1] == '\r':
-                    body.write('\n')  # write an extra newline (lurve Macs)
-        body.write(end_boundary)
-        body.write("\n")
-        body = body.getvalue()
+        content_type, body = encode_multipart(data.items(), [])
 
         # build the Request
         headers = {
-            'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary,
+            'Content-type': content_type,
             'Content-length': str(len(body))
         }
         req = urllib.request.Request(self.repository, body, headers)