Chrome's built-in messaging service doesn’t accept certain message sizes (Windows)

I am developing a Chrome extension that works with my own messaging server. It works in most cases, but I found strange behavior when sending messages of certain sizes. It seems that the message is discarded when the size is from 2560 to 2815 bytes (A00 and AFF in hexadecimal format). All subsequent messages are also not received, which indicates that the stream is damaged for some reason.

Here is a stripped down Python messaging application that you can use to test it:

import sys import struct def output(message): encoded_message = message.encode('utf-8') # Write message size. sys.stdout.write(struct.pack('I', len(encoded_message))) # Write the message itself. sys.stdout.write(encoded_message) sys.stdout.flush() if __name__ == "__main__": output('{"type": "%s"}' % ('x'*2820)) output('{"type": "%s"}' % ('x'*2560)) 

I get the first message, not the second.

I looked at the code in the Chrome repository . A function that appears to be responsible for this functionality has nothing special:

 void NativeMessageProcessHost::ProcessIncomingData( const char* data, int data_size) { DCHECK_CURRENTLY_ON(content::BrowserThread::IO); incoming_data_.append(data, data_size); while (true) { if (incoming_data_.size() < kMessageHeaderSize) return; size_t message_size = *reinterpret_cast<const uint32*>(incoming_data_.data()); if (message_size > kMaximumMessageSize) { LOG(ERROR) << "Native Messaging host tried sending a message that is " << message_size << " bytes long."; Close(kHostInputOuputError); return; } if (incoming_data_.size() < message_size + kMessageHeaderSize) return; content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, destination_port_, incoming_data_.substr(kMessageHeaderSize, message_size))); incoming_data_.erase(0, kMessageHeaderSize + message_size); } } 

Does anyone know what could be happening here?

Update

I ran into this issue on 64-bit versions of Windows 7 and Windows 8.1.

I tried the 64-bit version of Chrome on the Stable, Beta, and Dev channels - versions 37, 38, and 39. I also tried the stable 32-bit Chrome

I am using the 32-bit version of Python 2.7.7 and PyInstaller 2.1 to create an executable for my own messaging server.

+2
c ++ python google-chrome-extension chrome-native-messaging
source share
1 answer

Since you are using Windows, I suspect that Windows is adding carriage returns ( \x0D ) to newlines ( \x0A ).

According to Python 2.x - write binary output to stdout? , a way to prevent the output stream from changing in Windows is to use the following snippet before burning everything you need.

 if sys.platform == "win32": import os, msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) 
+2
source share

All Articles