Example #1
0
 def readJSONBool(self):
     self.context.read(self.reader)
     self.skipWhitespace()
     if self.context.escapeNum():
         self.readJSONSyntaxChar(JSON_STRING_DELIMITER)
     if self.reader.peek() == b't':
         true_string = b'true'
         for i in range(4):
             if self.reader.read() != true_string[i:i+1]:
                 raise TProtocolException(TProtocolException.INVALID_DATA,
                         "Bad data encountered in bool")
         boolVal = True
     elif self.reader.peek() == b'f':
         false_string = b'false'
         for i in range(5):
             if self.reader.read() != false_string[i:i+1]:
                 raise TProtocolException(TProtocolException.INVALID_DATA,
                         "Bad data encountered in bool")
         boolVal = False
     else:
         raise TProtocolException(TProtocolException.INVALID_DATA,
                 "Bad data encountered in bool")
     if self.context.escapeNum():
         self.readJSONSyntaxChar(JSON_STRING_DELIMITER)
     return boolVal
Example #2
0
def hexVal(ch):
    if ch >= '0' and ch <= '9':
        return int(ch) - int('0')
    elif ch >= 'a' and ch <= 'f':
        return int(ch) - int('a') + 10
    raise TProtocolException(TProtocolException.INVALID_DATA,
                             "Unexpected hex value")
Example #3
0
    def readFieldBegin(self):
        self.skipWhitespace()
        ch = self.reader.peek()
        if ch == JSON_OBJECT_END:
            return (None, TType.STOP, 0)
        self.context.read(self.reader)
        self.popContext()
        self.pushContext(self.pair_context_class(
            protocol=self,
            indentLevel=len(self.contexts)))
        self.skipWhitespace()
        fname = self.readJSONString()
        self.skipWhitespace()
        self.readJSONSyntaxChar(JSON_PAIR_SEPARATOR)
        self.context.skipColon = True
        self.skipWhitespace()
        if self.reader.peek() == b'n':
            for i in range(4):
                if self.reader.read() != b'null'[i:i + 1]:
                    raise TProtocolException(
                        TProtocolException.INVALID_DATA,
                        "Bad data encountered in null",
                    )

            self.context.read(self.reader)  # "consume" the colon we skipped
            return self.readFieldBegin()

        assert isinstance(self.spec, StructSpec)
        return self.spec.readFieldBegin(
                fname,
                self.guessTypeIdFromFirstByte)
Example #4
0
 def readJSONString(self, skipContext=False):
     self.skipWhitespace()
     if skipContext is False:
         self.context.read(self.reader)
     self.skipWhitespace()
     self.readJSONSyntaxChar(JSON_STRING_DELIMITER)
     string = []
     while True:
         ch = self.reader.read()
         if ch == JSON_STRING_DELIMITER:
             break
         if ch == JSON_BACKSLASH:
             ch = self.reader.read()
             if ch == b'u':
                 self.readJSONSyntaxChar(JSON_ZERO_CHAR)
                 self.readJSONSyntaxChar(JSON_ZERO_CHAR)
                 data = self.trans.read(2)
                 if sys.version_info[0] >= 3 and isinstance(data, bytes):
                     ch = json.JSONDecoder().decode(
                         '"\\u00%s"' % str(data, 'utf-8')).encode('utf-8')
                 else:
                     ch = json.JSONDecoder().decode('"\\u00%s"' % data)
             else:
                 idx = ESCAPE_CHARS.find(ch)
                 if idx == -1:
                     raise TProtocolException(
                             TProtocolException.INVALID_DATA,
                             "Expected control char")
                 ch = ESCAPE_CHAR_VALS[idx]
         string.append(ch)
     return b''.join(string)
Example #5
0
 def skip(self, _type):
     self.context.read(self.reader)
     self.skipWhitespace()
     type = self.guessTypeIdFromFirstByte()
     # Since self.context.read is called at the beginning of all readJSONxxx
     # methods and we have already called it here, push an empty context so that
     # it becomes a no-op.
     self.pushContext(TJSONContext(protocol=self))
     if type == TType.STRUCT:
         self.readJSONObjectStart()
         while True:
             (_, ftype, _) = self.readFieldBegin()
             if ftype == TType.STOP:
                 break
             self.skip(TType.VOID)
         self.readJSONObjectEnd()
     elif type == TType.LIST:
         self.readJSONArrayStart()
         while self.peekList():
             self.skip(TType.VOID)
         self.readJSONArrayEnd()
     elif type == TType.STRING:
         self.readJSONString()
     elif type == TType.DOUBLE:
         self.readJSONDouble()
     elif type == TType.BOOL:
         self.readJSONBool()
     else:
         raise TProtocolException(
             TProtocolException.INVALID_DATA,
             "Unexpected type {} guessed when skipping".format(type)
         )
     self.popContext()
Example #6
0
    def readMessageBegin(self):
        self.readJSONArrayStart()
        self.skipWhitespace()
        if self.readJSONInteger() != THRIFT_VERSION_1:
            raise TProtocolException(TProtocolException.BAD_VERSION,
                                     "Message contained bad version.")
        name = self.readJSONString()
        mtype = self.readJSONInteger()
        seqid = self.readJSONInteger()

        return (name, mtype, seqid)
Example #7
0
 def readJSONInteger(self):
     self.context.read(self.reader)
     self.skipWhitespace()
     if self.context.escapeNum():
         self.readJSONSyntaxChar(JSON_STRING_DELIMITER)
     numeric = self.readJSONNumericChars()
     if self.context.escapeNum():
         self.readJSONSyntaxChar(JSON_STRING_DELIMITER)
     try:
         return int(numeric)
     except ValueError:
         raise TProtocolException(TProtocolException.INVALID_DATA,
                                  "Bad data encounted in numeric data")
Example #8
0
 def readJSONDouble(self):
     self.context.read(self.reader)
     self.skipWhitespace()
     if self.reader.peek() == JSON_STRING_DELIMITER:
         string = self.readJSONString(True)
         try:
             double = float(string)
             if (self.context.escapeNum is False and
                 double != float('inf') and
                 double != float('-inf') and
                 double != float('nan')
             ):
                 raise TProtocolException(TProtocolException.INVALID_DATA,
                         "Numeric data unexpectedly quoted")
             return double
         except ValueError:
             raise TProtocolException(TProtocolException.INVALID_DATA,
                     "Bad data encountered in numeric data")
     else:
         try:
             return float(self.readJSONNumericChars())
         except ValueError:
             raise TProtocolException(TProtocolException.INVALID_DATA,
                     "Bad data encountered in numeric data")
Example #9
0
 def guessTypeIdFromFirstByte(self):
     self.skipWhitespace()
     byte = self.reader.peek()
     if byte == JSON_OBJECT_END or byte == JSON_ARRAY_END:
         return TType.STOP
     elif byte == JSON_STRING_DELIMITER:
         return TType.STRING
     elif byte == JSON_OBJECT_START:
         return TType.STRUCT
     elif byte == JSON_ARRAY_START:
         return TType.LIST
     elif byte == b't' or byte == b'f':
         return TType.BOOL
     elif byte in (b'+', b'-', b'0', b'1', b'2', b'3', b'4', b'5',
             b'6', b'7', b'8', b'9'):
         return TType.DOUBLE
     else:
         raise TProtocolException(TProtocolException.INVALID_DATA,
                 "Unrecognized byte: {}".format(byte))
Example #10
0
 def readJSONSyntaxChar(self, char):
     ch = self.reader.read()
     if ch != char:
         raise TProtocolException(TProtocolException.INVALID_DATA,
                                  "Unexpected character: %s" % ch)