示例#1
0
 def processBeatMessage(self, msg):
     msg = files.trim(msg)
     if len(msg) > 0:
         beat = Beat(msg)
         self.mutex.acquire()
         self.beatList.append(beat)
         self.beatCount += 1
         if len(self.beatList) > 1:
             thisAge = beat.age()
             prevAge = self.beatList[-2].age()
             interval = thisAge - prevAge
             self.maxInterval = max(self.maxInterval, interval)
         self.mutex.release()
示例#2
0
 def parseLine(self, line):
     # In case need to generate error message
     sline = files.showLine(line)
     # Check termination
     if self.strict.getBoolean() and not self.checkTerm(line):
         return (False, False, "Line terminated improperly: '%s'" % sline)
     # Remove initial and final spaces:
     line = files.trim(line)
     if len(line) == 0:
         # Empty line marks end
         return (True, True, "")
     # Get token
     token = ''
     while len(line) > 0 and line[0] != ':' and line[
             0] not in files.spaceCharacters:
         token += line[0]
         line = line[1:]
     # Skip spaces before colon
     line = files.preTrim(line)
     if len(line) == 0 or line[0] != ':':
         # No token on line
         if self.strict.getBoolean():
             return (False, False, "No token on line: '%s'" % sline)
         else:
             # Ignore
             return (True, False, "")
     line = line[1:]
     # Skip initial spaces in field
     line = files.preTrim(line)
     if len(token) > 0:
         self.headerDict[token.lower()] = line
         return (True, False, "")
     else:
         # Empty token
         if self.strict.getBoolean():
             return (False, False, "No token defined on line: '%s'" % sline)
         else:
             # Ignore
             return (True, False, "")
示例#3
0
    def finishRequest(self, event=None):
        if event is None:
            if self.verbose.getBoolean():
                self.outMsg("Attempted to finish request with empty event")
            return
        sockFile = event.sockFile
        url = event.url
        if sockFile is None:
            event.error("Cannot complete request.  No socket")
            return
        if url is None:
            event.error("Cannot complete request.  No URL")
            sockFile.close()
            return
        host, port, uri = parseURL(url)[1:]
        id = event.id
        # Get response
        found = False
        try:
            response = sockFile.readlineb()
        except Exception as ex:
            event.error("Could not read response for URL request %s (%s)" %
                        (url, str(ex)))
            sockFile.close()
            return
        if len(response) == 0:
            event.error("Got empty response for URL request %s" % url)
            sockFile.close()
            return
        responseLine = files.trim(response)
        fields = responseLine.split(None, 2)
        if len(fields) != 3:
            event.error("Can't parse response from line '%s'" %
                        files.showLine(response))
            sockFile.close()
            return
        version, status, statusMsg = fields
        tag = self.httpStatus.getTag(status)
        event.setTag(tag)
        responseHeader = HeaderReader(self.strict)
        (ok, reason) = responseHeader.readHeader(sockFile)
        if self.verbose.getBoolean():
            self.outMsg("Read response from proxy.  ok = %s, reason = %s" %
                        (ok, reason))
            self.outMsg("Header:")
            header = "".join(responseHeader.headerLines)
            self.outMsg(header)

        event.receivedHeaderLines = [responseLine] + responseHeader.headerLines
        if not ok:
            event.error("Invalid response header %s" % reason)
            sockFile.close()
            return
        try:
            length = int(responseHeader.getValue("content-length", "-1"))
        except:
            event.error(
                "Invalid or missing content length from response header")
            sockFile.close()
            return
        if length < 0:
            event.error(
                "Invalid or missing content length from response header")
            sockFile.close()
            return
        # Get response
        fname = uri.split("/")[-1] if tag == "ok" else "status.html"
        if fname == "":
            fname = "index.html"
        isBinary = self.fileManager.isBinary(
            self.fileManager.getExtension(fname))
        outfname = fname if id == "" else id + "-" + fname
        outPath = self.fileManager.responsePath(outfname)
        event.addPath(outPath)
        try:
            outfile = open(outPath, "wb" if isBinary else "w")
        except Exception as ex:
            event.error(
                "Couldn't open file %s in which to save response (%s)" %
                (outPath, str(ex)))
            sockFile.close()
            return
        remaining = length
        while (remaining > 0):
            try:
                buf = sockFile.read()
            except files.ShutdownException:
                outfile.close()
                sockFile.close()
                return
            except Exception as ex:
                self.errMsg("Error reading response (%s)" % ex)
                outfile.close()
                sockFile.close()
                return
            if len(buf) == 0:
                outfile.close()
                event.error("Socket closed after reading %d/%d bytes" %
                            (length - remaining, length))
                sockFile.close()
                return
            if len(buf) > 0:
                outfile.write(buf)
            remaining -= len(buf)
        outfile.close()
        sockFile.close()
        if self.verbose.getBoolean():
            self.outMsg(
                "URL = %s, Status = %s.  Result stored in %s.  %d bytes" %
                (url, event.tag, outPath, length))
        # Now check that the results are as expected
        if host == "localhost" and tag == "ok":
            sourcePath = self.fileManager.sourcePath(fname)
            if not self.fileManager.testPath(sourcePath):
                event.error("Internal error.  Couldn't find file %s" %
                            sourcePath)
                return
            (match,
             reason) = self.fileManager.compareFiles(sourcePath, outPath)
            if not match:
                event.error(reason)
                return
            if self.verbose.getBoolean():
                self.outMsg("Files %s and %s match" % (outPath, sourcePath))