def actionPerformed(self, event): content, message, info, parameter = self.tab._current try: body = content[info.getBodyOffset():].tostring() if parameter is not None: param = self.tab.helpers.getRequestParameter( content, parameter.getName()) if param is not None: rules = self.tab.extender.table.getParameterRules().get(parameter.getName(), {}) body = param.getValue().encode('utf-8') for rule in rules.get('before', []): body = rule(body) message = parse_message(self.descriptor, body) self.tab.editor.setText(str(message)) self.tab.editor.setEditable(True) self.tab._current = (content, message, info, parameter) except Exception as error: title = "Error parsing message as %s!" % (self.descriptor.name, ) JOptionPane.showMessageDialog(self.tab.getUiComponent(), error.message, title, JOptionPane.ERROR_MESSAGE) return
def actionPerformed(self, event): content, message, info, parameter = self.tab._current try: # check if body is compressed (gzip) # gunzip the content first if required if isGzip(info): body = gUnzip(content[info.getBodyOffset():].tostring()) else: body = content[info.getBodyOffset():].tostring() if parameter is not None: param = self.tab.helpers.getRequestParameter( content, parameter.getName()) if param is not None: rules = self.tab.extender.table.getParameterRules().get(parameter.getName(), {}) body = param.getValue().encode('utf-8') for rule in rules.get('before', []): body = rule(body) # cut 5 bytes for grpc web rawBytes = (content[info.getBodyOffset():]) global oldPadding global hasPadding hasPadding = False oldPadding= rawBytes[0:4] if rawBytes[0] == 0 and rawBytes[1] == 0 and rawBytes[2] == 0 and rawBytes[3] == 0: rawBytes = rawBytes[5:] hasPadding = True body = rawBytes.tostring() print "Parsing message with proto descriptor %s (by user)." % (self.descriptor.name) message = parse_message(self.descriptor, body) self.tab.editor.setText(str(message)) self.tab.editor.setEditable(True) self.tab._current = (content, message, info, parameter) except Exception as error: title = "Error parsing message as %s!" % (self.descriptor.name, ) JOptionPane.showMessageDialog(self.tab.getUiComponent(), error.message, title, JOptionPane.ERROR_MESSAGE) return
def setMessage(self, content, isRequest): if content is None: self.editor.setText(None) self.editor.setEditable(False) return if isRequest: info = self.helpers.analyzeRequest(content) else: info = self.helpers.analyzeResponse(content) # by default, let's assume the entire body is a protobuf message body = content[info.getBodyOffset():].tostring() # process parameters via rules defined in Protobuf Editor ui tab parameter = None for name, rules in self.extender.table.getParameterRules().iteritems(): parameter = self.helpers.getRequestParameter(content, name) if parameter is not None: # no longer use the entire message body as the protobuf # message, just the value of the parameter according # to our ui defined rules body = parameter.getValue().encode('utf-8') for rule in rules.get('before', []): body = rule(body) break # Loop through all proto descriptors loaded for package, descriptors in self.descriptors.iteritems(): for name, descriptor in descriptors.iteritems(): try: message = parse_message(descriptor, body) except Exception: continue # Stop parsing on the first valid message we encounter # this may result in a false positive, so we should still # allow users to specify a proto manually (select from a # context menu). if message.IsInitialized(): self.editor.setText(str(message)) self.editor.setEditable(True) self._current = (content, message, info, parameter) return # If we get to this point, then no loaded protos could deserialize # the message. Shelling out to protoc should be a last resort. process = subprocess.Popen(['protoc', '--decode_raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = error = None try: output, error = process.communicate(body) except OSError: pass finally: if process.poll() != 0: process.wait() if error: self.editor.setText(error) else: self.editor.setText(output) self.editor.setEditable(False) self._current = (content, None, info, parameter) return
def setMessage(self, content, isRequest): if content is None: self.editor.setText(None) self.editor.setEditable(False) return if isRequest: info = self.helpers.analyzeRequest(content) else: info = self.helpers.analyzeResponse(content) # by default, let's assume the entire body is a protobuf message # check if body is compressed (gzip) # gunzip the content first if required if isGzip(info): #if isRequest: # print "Request body is using gzip: Uncompressing..." #else: # print "Response body is using gzip: Uncompressing..." body = gUnzip(content[info.getBodyOffset():].tostring()) else: body = content[info.getBodyOffset():].tostring() # process parameters via rules defined in Protobuf Decoder ui tab parameter = None for name, rules in self.extender.table.getParameterRules().iteritems(): parameter = self.helpers.getRequestParameter(content, name) if parameter is not None: # no longer use the entire message body as the protobuf # message, just the value of the parameter according # to our ui defined rules body = parameter.getValue().encode('utf-8') for rule in rules.get('before', []): body = rule(body) break #set message rawBytes = (content[info.getBodyOffset():]) global oldPadding global hasPadding hasPadding = False oldPadding= rawBytes[0:4] if rawBytes[0] == 0 and rawBytes[1] == 0 and rawBytes[2] == 0 and rawBytes[3] == 0: rawBytes = rawBytes[5:rawBytes[4]+5] hasPadding = True body = rawBytes.tostring() # Loop through all proto descriptors loaded for package, descriptors in self.descriptors.iteritems(): for name, descriptor in descriptors.iteritems(): try: print "Parsing message with proto descriptor %s (auto)." % (name) message = parse_message(descriptor, body) except Exception: print "(exception parsing message... - continue)" continue # Stop parsing on the first valid message we encounter # this may result in a false positive, so we should still # allow users to specify a proto manually (select from a # context menu). if message.IsInitialized(): # The message is initialized if all of its # required fields are set. #print "Message: [%s]" % (message) if str(message) == "": # parse_message() returned an empty message, but no # error or exception: continue to the next proto descriptor print "(message is empty, trying other proto descriptors...)" else: self.editor.setText(str(message)) self.editor.setEditable(True) self._current = (content, message, info, parameter) return # If we get to this point, then no loaded protos could deserialize # the message. Shelling out to protoc should be a last resort. process = subprocess.Popen([PROTOC_BINARY_LOCATION, '--decode_raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = error = None try: output, error = process.communicate(body) except OSError: pass finally: if process.poll() != 0: process.wait() if error: #print "protoc displaying message - error..." self.editor.setText(error) else: #print "protoc displaying message - output..." self.editor.setText(output) self.editor.setEditable(False) self._current = (content, None, info, parameter) return