Пример #1
0
 def doTest(self, data, expected_args):
     for bytes in range(1, 20):
         s = TestStream(data, maxReturn=bytes)
         d = waitForDeferred(fileupload.parse_urlencoded(s))
         yield d
         args = d.getResult()
         self.assertEquals(args, expected_args)
Пример #2
0
 def doTest(self, data, expected_args):
     for bytes in range(1, 20):
         s = TestStream(data, maxReturn=bytes)
         d = waitForDeferred(fileupload.parse_urlencoded(s))
         yield d
         args = d.getResult()
         self.assertEquals(args, expected_args)
Пример #3
0
	def http_POST(self, request):
		# we override the upstream version because it doesn't handle
		# JSON mime types
		from twisted.web2 import http, fileupload, responsecode, stream
		
		if request.stream.length == 0:
			d = defer.succeed(None)
		else:
			parser = None
			ctype = request.headers.getHeader('content-type')

			if ctype is None:
				d = defer.succeed(None)
			else:
				def updateArgs(data):
					args = data
					request.args.update(args)
				
				def updateJson(data):
					request.args['message'] = [data]
				
				def error(f):
					raise http.HTTPError(responsecode.BAD_REQUEST)
				
				if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded':
					d = fileupload.parse_urlencoded(request.stream)
					d.addCallbacks(updateArgs, error)
				elif ctype.mediaType in ('application','text') and ctype.mediaSubtype == 'json':
					d = stream.readStream(request.stream, updateJson)
					d.addErrback(error)
				else:
					raise http.HTTPError(responsecode.BAD_REQUEST)
		
		return d.addCallback(lambda res: self.render(request))
Пример #4
0
def parsePOSTData(request):
    if request.stream.length == 0:
        return defer.succeed(None)

    parser = None
    ctype = request.headers.getHeader('content-type')

    if ctype is None:
        return defer.succeed(None)

    def updateArgs(data):
        args = data
        request.args.update(args)

    def updateArgsAndFiles(data):
        args, files = data
        request.args.update(args)
        request.files.update(files)

    def error(f):
        f.trap(fileupload.MimeFormatError)
        raise http.HTTPError(responsecode.BAD_REQUEST)

    if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded':
        d = fileupload.parse_urlencoded(request.stream)
        d.addCallbacks(updateArgs, error)
        return d
    elif ctype.mediaType == 'multipart' and ctype.mediaSubtype == 'form-data':
        boundary = ctype.params.get('boundary')
        if boundary is None:
            return failure.Failure(
                fileupload.MimeFormatError(
                    "Boundary not specified in Content-Type."))
        d = fileupload.parseMultipartFormData(request.stream, boundary)
        d.addCallbacks(updateArgsAndFiles, error)
        return d
    else:
        raise http.HTTPError(responsecode.BAD_REQUEST)
Пример #5
0
    def http_POST(self, request):
        # we override the upstream version because it doesn't handle
        # JSON mime types
        from twisted.web2 import http, fileupload, responsecode, stream

        if request.stream.length == 0:
            d = defer.succeed(None)
        else:
            parser = None
            ctype = request.headers.getHeader('content-type')

            if ctype is None:
                d = defer.succeed(None)
            else:

                def updateArgs(data):
                    args = data
                    request.args.update(args)

                def updateJson(data):
                    request.args['message'] = [data]

                def error(f):
                    raise http.HTTPError(responsecode.BAD_REQUEST)

                if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded':
                    d = fileupload.parse_urlencoded(request.stream)
                    d.addCallbacks(updateArgs, error)
                elif ctype.mediaType in ('application', 'text'
                                         ) and ctype.mediaSubtype == 'json':
                    d = stream.readStream(request.stream, updateJson)
                    d.addErrback(error)
                else:
                    raise http.HTTPError(responsecode.BAD_REQUEST)

        return d.addCallback(lambda res: self.render(request))
Пример #6
0
def parsePOSTData(request,
                  maxMem=100 * 1024,
                  maxFields=1024,
                  maxSize=10 * 1024 * 1024):
    """
    Parse data of a POST request.

    @param request: the request to parse.
    @type request: L{twisted.web2.http.Request}.
    @param maxMem: maximum memory used during the parsing of the data.
    @type maxMem: C{int}
    @param maxFields: maximum number of form fields allowed.
    @type maxFields: C{int}
    @param maxSize: maximum size of file upload allowed.
    @type maxSize: C{int}

    @return: a deferred that will fire when the parsing is done. The deferred
        itself doesn't hold a return value, the request is modified directly.
    @rtype: C{defer.Deferred}
    """
    if request.stream.length == 0:
        return defer.succeed(None)

    parser = None
    ctype = request.headers.getHeader('content-type')

    if ctype is None:
        return defer.succeed(None)

    def updateArgs(data):
        args = data
        request.args.update(args)

    def updateArgsAndFiles(data):
        args, files = data
        request.args.update(args)
        request.files.update(files)

    def error(f):
        f.trap(fileupload.MimeFormatError)
        raise http.HTTPError(
            http.StatusResponse(responsecode.BAD_REQUEST, str(f.value)))

    if (ctype.mediaType == 'application'
            and ctype.mediaSubtype == 'x-www-form-urlencoded'):
        d = fileupload.parse_urlencoded(request.stream)
        d.addCallbacks(updateArgs, error)
        return d
    elif (ctype.mediaType == 'multipart'
          and ctype.mediaSubtype == 'form-data'):
        boundary = ctype.params.get('boundary')
        if boundary is None:
            return defer.fail(
                http.HTTPError(
                    http.StatusResponse(
                        responsecode.BAD_REQUEST,
                        "Boundary not specified in Content-Type.")))
        d = fileupload.parseMultipartFormData(request.stream, boundary, maxMem,
                                              maxFields, maxSize)
        d.addCallbacks(updateArgsAndFiles, error)
        return d
    else:
        return defer.fail(
            http.HTTPError(
                http.StatusResponse(
                    responsecode.BAD_REQUEST, "Invalid content-type: %s/%s" %
                    (ctype.mediaType, ctype.mediaSubtype))))
Пример #7
0
def parsePOSTData(request, maxMem=100*1024, maxFields=1024,
                  maxSize=10*1024*1024):
    """
    Parse data of a POST request.

    @param request: the request to parse.
    @type request: L{twisted.web2.http.Request}.
    @param maxMem: maximum memory used during the parsing of the data.
    @type maxMem: C{int}
    @param maxFields: maximum number of form fields allowed.
    @type maxFields: C{int}
    @param maxSize: maximum size of file upload allowed.
    @type maxSize: C{int}

    @return: a deferred that will fire when the parsing is done. The deferred
        itself doesn't hold a return value, the request is modified directly.
    @rtype: C{defer.Deferred}
    """
    if request.stream.length == 0:
        return defer.succeed(None)

    parser = None
    ctype = request.headers.getHeader('content-type')

    if ctype is None:
        return defer.succeed(None)

    def updateArgs(data):
        args = data
        request.args.update(args)

    def updateArgsAndFiles(data):
        args, files = data
        request.args.update(args)
        request.files.update(files)

    def error(f):
        f.trap(fileupload.MimeFormatError)
        raise http.HTTPError(
            http.StatusResponse(responsecode.BAD_REQUEST, str(f.value)))

    if (ctype.mediaType == 'application'
        and ctype.mediaSubtype == 'x-www-form-urlencoded'):
        d = fileupload.parse_urlencoded(request.stream)
        d.addCallbacks(updateArgs, error)
        return d
    elif (ctype.mediaType == 'multipart'
          and ctype.mediaSubtype == 'form-data'):
        boundary = ctype.params.get('boundary')
        if boundary is None:
            return defer.fail(http.HTTPError(
                    http.StatusResponse(
                        responsecode.BAD_REQUEST,
                        "Boundary not specified in Content-Type.")))
        d = fileupload.parseMultipartFormData(request.stream, boundary,
                                              maxMem, maxFields, maxSize)
        d.addCallbacks(updateArgsAndFiles, error)
        return d
    else:
        return defer.fail(http.HTTPError(
            http.StatusResponse(
                responsecode.BAD_REQUEST,
                "Invalid content-type: %s/%s" % (
                    ctype.mediaType, ctype.mediaSubtype))))