# Helper for the mirror on GAE
# GAE GETs an action gae_file, giving GAE host and a secret
# PyPI GETs /mkupload/secret, learning path and upload session
# PyPI POSTs to upload session
import urllib2, httplib, threading, os, binascii, urlparse

POST="""\
--%(boundary)s
Content-Disposition: form-data; name=secret

%(secret)s
--%(boundary)s
Content-Disposition: form-data; name=path

%(path)s
--%(boundary)s
Content-Disposition: form-data; name=file; filename=data.bin

%(data)s
"""
POST = "\r\n".join(POST.splitlines())

# XXX Not sure how to report errors
def doit(host, secret, srcdir):
    x = urllib2.urlopen('http://%s/mkupload/%s' % (host, secret))
    if x.code != 200:
        #print "mkupload failed (%s)" % r.code
        return
    path,url = x.read().splitlines()
    host, session = urlparse.urlsplit(url)[1:3]
    try:
        data = open(srcdir+"/"+path).read()
    except IOError, e:
        #print "Read of %s failed:%s"%(path,str(e))
        return
    boundary = ""
    while boundary in data:
        boundary = binascii.hexlify(os.urandom(10))
    body = POST % locals()
    if ':' in host:
        host, port = host.split(':')
    else:
        port = 80
    c = httplib.HTTPConnection(host, port)
    c.request('POST', session,
              headers = {'Content-type':'multipart/form-data; boundary='+boundary,
                         'Content-length':str(len(body)),
                         'Host':host},
              body=body)
    resp = c.getresponse()
    data = resp.read()
    # result code should be redirect
    c.close()

def transfer(host, secret, srcdir):
    secret = secret.encode('ascii')
    t = threading.Thread(target=doit, args=(host, secret, srcdir))
    t.start()

@@ -300,22 +300,24 @@
 a; name=
+%22
 secret
+%22
 %0A%0A%25(secr
@@ -373,20 +373,22 @@
 a; name=
+%22
 path
+%22
 %0A%0A%25(path
@@ -442,20 +442,22 @@
 a; name=
+%22
 file
+%22
 ; filena
@@ -463,24 +463,80 @@
 ame=
-data.bin%0A%0A%25(data
+%22%25(path)s%22%0AContent-Type: application/octet-stream%0A%0A%25(data)s%0A--%25(boundary
 )s%0A%22
@@ -579,45 +579,16 @@
 s())
-%0A%0A# XXX Not sure how to report errors
++%22%5Cr%5Cn%22%0A
 %0Adef
@@ -708,55 +708,8 @@
 00:%0A
-        #print %22mkupload failed (%25s)%22 %25 r.code%0A
     
@@ -884,60 +884,8 @@
  e:%0A
-        #print %22Read of %25s failed:%25s%22%25(path,str(e))%0A
     

Put quotes around filename Add boundary after last field.