WebStack

Changeset

203:c5b686b95871
2004-08-29 paulb raw files shortlog changelog graph [project @ 2004-08-29 02:09:47 by paulb] Fixed the Stream class's readline method. Fixed the handling of multipart form data using various dubious buffering techniques.
WebStack/JavaServlet.py (file)
     1.1 --- a/WebStack/JavaServlet.py	Sun Aug 29 01:58:51 2004 +0000
     1.2 +++ b/WebStack/JavaServlet.py	Sun Aug 29 02:09:47 2004 +0000
     1.3 @@ -43,7 +43,7 @@
     1.4  
     1.5          "Read a line from the stream, returning it as a string."
     1.6  
     1.7 -        return self.stream.readLine()
     1.8 +        return self.stream.readLine() + "\n"
     1.9  
    1.10  class Transaction(Generic.Transaction):
    1.11  
    1.12 @@ -439,7 +439,21 @@
    1.13          "Get fields from a multipart message."
    1.14  
    1.15          session = javax.mail.Session.getDefaultInstance(java.util.Properties())
    1.16 -        message = javax.mail.internet.MimeMessage(session, self.get_request_stream())
    1.17 +
    1.18 +        # Fake a multipart message.
    1.19 +
    1.20 +        str_buffer = java.io.StringWriter()
    1.21 +        fp = self.get_request_stream()
    1.22 +        boundary = fp.readline()
    1.23 +        str_buffer.write('Content-Type: multipart/mixed; boundary="%s"\n\n' % boundary[2:-1])
    1.24 +        str_buffer.write(boundary)
    1.25 +        str_buffer.write(fp.read())
    1.26 +        str_buffer.close()
    1.27 +
    1.28 +        # Re-read that message.
    1.29 +
    1.30 +        input_stream = java.io.StringBufferInputStream(str_buffer.toString())
    1.31 +        message = javax.mail.internet.MimeMessage(session, input_stream)
    1.32          content = message.getContent()
    1.33          return self._get_fields_from_multipart(content)
    1.34  
    1.35 @@ -452,13 +466,32 @@
    1.36              part = content.getBodyPart(i)
    1.37              subcontent = part.getContent()
    1.38  
    1.39 +            # Convert input stream content.
    1.40 +
    1.41 +            if isinstance(subcontent, java.io.InputStream):
    1.42 +                subcontent = Stream(subcontent).read()
    1.43 +
    1.44              # Record string content.
    1.45  
    1.46              if type(subcontent) == type(""):
    1.47 -                name = part.getDisposition()
    1.48 -                if not fields.has_key(name):
    1.49 -                    fields[name] = []
    1.50 -                fields[name].append(subcontent)
    1.51 +
    1.52 +                # Should get: form-data; name="x"
    1.53 +
    1.54 +                disposition = part.getHeader("Content-Disposition")[0].split(";")
    1.55 +
    1.56 +                # Locate: name="x"
    1.57 +
    1.58 +                if len(disposition) > 1:
    1.59 +                    for attribute in disposition[1:]:
    1.60 +                        t = attribute.split("=")
    1.61 +                        if len(t) == 2 and t[0].strip() == "name":
    1.62 +
    1.63 +                            # Locate: "x" -> Locate: x
    1.64 +
    1.65 +                            name = t[1].strip()[1:-1]
    1.66 +                            if not fields.has_key(name):
    1.67 +                                fields[name] = []
    1.68 +                            fields[name].append(subcontent)
    1.69              else:
    1.70                  fields.update(self._get_fields_from_multipart(subcontent))
    1.71