#1 2015-07-10 10:06:30

CycleSoft
Member
Registered: 2013-01-18
Posts: 34

encoding problem with TServiceCustomAnswer

Hi,
I wrote an interface based method that returns a TServiceCustomAnswer. Basically, accept an ID and searches for a corresponding file to send back to the client.
with authentication disabled, if I make a request using the browser and typing directly on the url, it works, an returns, let's say, a PDF.
The browser can view it with no prob and save it on disk where I can compare with the original file. No differences.

But if I enable authentication on the mORMot server, and I use a javascript client to access the same url, the size of the dowloaded file is about the double.
I suspect is an encoding problem, but I don't see any difference on the 2 request to explain it.
In the failing situation, the response from mORMot starts with :

%PDF-1.5

so it seems uncompressed. The request contains
Accept-Encoding gzip, deflate
and the mORMot response :

Accept-Encoding	synlz,gzip
Access-Control-Allow-Meth...	POST, PUT, GET, DELETE, LOCK, OPTIONS
Access-Control-Allow-Orig...	*
Access-Control-Expose-Hea...content-length,location,server-internalstate
Access-Control-Max-Age	1728000
Content-Length	1762067
Content-Type	application/pdf

while the working answer (no auth serverside)  shows a compressed stream

the rilevant mORMot's service code is:

          if UpperCase( extension ) = 'PDF' then
            Result.Header := HEADER_CONTENT_TYPE + PDF_CONTENT_TYPE
          else
            Result.Header := HEADER_CONTENT_TYPE + BINARY_CONTENT_TYPE;
          RawByteStream := TRawByteStringStream.Create;
          MemStream := TMemoryStream.Create;
          if FileExists(PhisicalFileName) then
            MemStream.LoadFromFile(PhisicalFileName)
          else
            raise Exception.Create('File ' + PhisicalFileName + ' not found. ID=' + IntToStr(FileID));
          MemStream.SaveToStream(RawByteStream);
          Result.Content := RawByteStream.DataString;

Another fact that makes me suspect encoding is the problem, is that if the file is a text file, everything is ok in both situations! (with and W/o auth)

Am I miss something?

Last edited by CycleSoft (2015-07-10 10:06:46)

Offline

#2 2015-07-10 11:04:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: encoding problem with TServiceCustomAnswer

You would never see the "compressed" content on the wire, IMHO.
The browser would always deliver uncompressed content.

About compression, AFAIK only text content is compressed by the server.
See SynCrtSock.pas, function CompressDataAndGetHeaders().

Why are you using those streams?
Just use

Result.Content := StringFromFile(PhysicalFileName);
if Result.Content='' then
  raise Exception.CreateFmt('File %s not found. ID=%d',[PhisicalFileName,FileID]);

Offline

#3 2015-07-10 12:40:32

CycleSoft
Member
Registered: 2013-01-18
Posts: 34

Re: encoding problem with TServiceCustomAnswer

Thanks for the StringFromFile() hint!

Sadly this not solve the issue.

You are right , the compression/decompressio is on the wire and should be done transparently by the browser/server. What I see are the debug infos of the developer tools.

In the correct situation I see comprssed data (I gess it, is like garbage) while in the not-working situation I see the same data as in the original file on disk, in the case of a PDF file something starting with the sequence

%PDF-1.4

The only remarkable difference is auth turned on or off on the mORMot server. Auto = on, not working, Auth = off working.

I'm pretty sure that there are something else that is causing the different behaviours, somehow  a conseguence of Auth,  but I don't found it sad

Offline

#4 2015-07-10 13:47:57

CycleSoft
Member
Registered: 2013-01-18
Posts: 34

Re: encoding problem with TServiceCustomAnswer

Update:

maybe it is not a mORMot issue at all, I'm checking with the javascript developer the issue: if I make the request with Auth enabled BUT using the address bar of the browser the response is ok and I see the original file.

Sorry sad

Offline

Board footer

Powered by FluxBB