RTC Forums
May 13, 2024, 04:15:53 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Issue with Write vs WriteEX  (Read 4280 times)
GregStevenson
RTC Expired
*
Posts: 5


« on: September 20, 2016, 06:43:01 PM »

Porting from D7 to XE8 and came across in issue. Able to reproduce in D7 with with Ansi strings. As shown in code below, if I use Write all works well, but if I switch the comment to use WriteEX as shown below, I keep getting the second "'Error! Sending more data out than specified in header." exception in rtcHttpSrv.pas TRtcHttpServer.WriteEx. What am I doing wrong?

Thanks,
--Greg



procedure WriteBinaryResponse(aDataServer: TRtcDataServer; aContent:
    AnsiString);
begin
{$if CompilerVersion >= 22 }
  aDataServer.WriteEx(RtcStringToBytesZero(aContent));
{$else}
//  aDataServer.WriteEx(RtcStringToBytesZero(aContent));
  aDataServer.Write(aContent);
{$ifend}
end;

procedure WriteStringResponse(aDataServer: TRtcDataServer; aContent: string);
begin
{$if CompilerVersion >= 22 }
  aDataServer.WriteEx(Utf8EncodeEx(aContent));
{$else}
//  aDataServer.WriteEx(RtcStringToBytesZero(aContent));
  aDataServer.Write(aContent);
{$ifend}
end;
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: September 20, 2016, 07:49:43 PM »

You are using the "RtcStringToBytesZero" function, which adds a zero (0) at the end, meaning that you would be sending 1 extra byte when using that function to convert a String to a Byte Array. If you want to converting a String into a RtcByteArray without adding any extras, use the RtcStringToBytes function (no ZERO at the end). And if you want to convert a RtcByteArray back to a String, you can use the RtcBytesToString function (without the zero).

In short, this should work correctly in all Delphi versions:

procedure WriteBinaryResponse(aDataServer: TRtcDataServer; aContent: String);
begin
  aDataServer.WriteEx(RtcStringToBytes(aContent));
end;

... which is actually the exact same as this:

procedure WriteBinaryResponse(aDataServer: TRtcDataServer; aContent: String);
begin
  aDataServer.Write(aContent);
end;

PS. It is OK to use the Write method in Unicode Delphi versions, but if you want your code to be compatible with all platforms, you will have to replace all "AnsiString" type declarations with "String" or "RtcString" and make sure your own code works with Unicode Strings. As for RTC, all methods and functions which expect the "RtcString" type will only be using the lower 8 bits of each character, even if the String type is Unicode.

Best Regards,
Danijel Tkalcec

Best Regards,
Danijel Tkalcec
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #2 on: September 20, 2016, 08:08:02 PM »

By the way ... the Utf8EncodeEx function uses UTF-8 encoding to encode a Unicode Strings into a Byte Array. If you want to send your content out with UTF-8 encoding, please keep in mind that a Byte Array generated using that function will have more bytes than a Byte Array generated using the plain RtcStringToBytes function, which only copies the low byte (8 bits) of each character into a Byte Array.

Best Regards,
Danijel Tkalcec
Logged
GregStevenson
RTC Expired
*
Posts: 5


« Reply #3 on: September 20, 2016, 08:42:33 PM »

I misread the help description and did not think the null at end was part of count.
Still have issues in XE8 vs D7 behavior on browser side, so some more digging is needed, but at least the Error! Sending more data out than specified in header errors are gone.

Thanks,
--Greg
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #4 on: September 20, 2016, 08:57:42 PM »

Ok. Should you bump into any issues which might be related to RTC, let me know.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.025 seconds with 19 queries.