Пример #1
0
    def set_headers(self, stream):
        """Given an HTTP stream, parse and store the message headers.

        The HTTP stream must be positioned at the second line of a request.

        The message headers are read and a new Message instance is added to
        self. This instance will be updated in set_body once the body has been
        read. We have to do this in two stages because the length of the body is
        communicated to us via the headers.

        """

        # Read in everything until the next blank line, parsing as we go.
        # ===============================================================

        raw_headers = []
        parser_ = FeedParser()
        for line in stream:
            raw_headers.append(line)
            parser_.feed(line)
            if line == '\r\n':
                break

        # Now store on self.
        # ==================

        self.raw.extend(raw_headers)
        self.raw_headers = ''.join(raw_headers)
        self.message = parser_.close()
Пример #2
0
    def parse(self, fp, headersonly=False):
        feedparser = FeedParser(self._class)
        if headersonly:
            feedparser._set_headersonly()
        while True:
            data = fp.read(8192)
            if not data:
                break
            feedparser.feed(data)

        return feedparser.close()
Пример #3
0
 def lineReceived(self, line):
     log.debug("Line In: %s"%line)
     self.parser = FeedParser(Event)        
     self.parser.feed(line)
     self.message = self.parser.close()
     #if self.state is not READ_CONTENT (i.e Content-Type is already read) and the Content-Length is present
     #read rest of the message and set it as payload
     if self.message.has_key('Content-Length') and self.state!= 'READ_CONTENT':
         if self.enterRawMode():
             log.debug("Entering raw mode to read message payload")
             return 
     try:
         self.inspectMessage()   
     except:
         log.error("Exception in message processing ", exc_info=True)
Пример #4
0
    def parse(self, fp, headersonly=False):
        """Create a message structure from the data in a file.

        Reads all the data from the file and returns the root of the message
        structure.  Optional headersonly is a flag specifying whether to stop
        parsing after reading the headers or not.  The default is False,
        meaning it parses the entire contents of the file.
        """
        feedparser = FeedParser(self._class)
        if headersonly:
            feedparser._set_headersonly()
        while True:
            data = fp.read(8192)
            if not data:
                break
            feedparser.feed(data)
        return feedparser.close()
Пример #5
0
    def parse_headers(self):
        parser = FeedParser()
        while True:
            line = self.f.readline()
            self.hbuf.write(line)
            parser.feed(line)
            if not line.strip(): break
        self.m = parser.close()

        L = self.parse_addrs('from')
        self.h_from = L and L[0] or None
        self.h_to = self.parse_addrs('to')
        self.h_cc = self.parse_addrs('cc')
        self.h_rcpts = self.h_to + self.h_cc
        self.h_subject = decode_header(self.m.get('subject'))
        self.h_subject_len1 = len(self.h_subject)
        self.h_subject = self.h_subject.replace(' ', '')
        self.h_subject = self.h_subject.replace(u' ', '')
        self.h_subject_len2 = len(self.h_subject)
Пример #6
0
    def _handle_upload(self, headers, registry, parser):
        # file upload handling

        # mime parser
        from email.FeedParser import FeedParser
        fp = FeedParser()

        # need header
        content_type = headers['content-type']
        fp.feed('CONTENT-TYPE: %s\n' % content_type)

        # size limit
        size_limit = self.stdin_size_limit

        # read in chunks
        chunk_size = 8192

        # number of chunks
        n = size_limit / chunk_size

        # feed contents from stdin to parser
        import sys
        i = 0
        succeeded = False
        while i < n:
            data = sys.stdin.read(chunk_size)
            if not data:
                succeeded = True
                break
            fp.feed(data)
            continue
        if not succeeded:
            raise RuntimeError, "stdin too large"

        # parsed is a mime instance
        parsed = fp.close()

        #
        header = 'Content-Disposition'

        if self.inventory.debug:
            self._cgi_inputs['uploaded mime'] = parsed.as_string()

        args = []
        uploads = {}
        for part in parsed.walk():

            if part.get_content_maintype() == 'multipart':
                # this part is the parent message it self, skip
                continue

            filename = part.get_param('filename', header=header)
            if filename:
                # this means we are dealing with a real file
                # save them to a dictionary so that later actors can handle them
                content = part.get_payload(decode=True)
                uploads[filename] = content
            else:
                # just a key,value pair
                key = part.get_param('name', header=header)
                value = part.get_payload()
                args.append((key, value))

            # pass key,value pairs to pyre option registry
            arg = '&'.join(['%s=%s' % (k, v) for k, v in args])
            if arg: parser.parse(registry, arg, 'form')

        self._uploads = uploads
        return