#1 2018-05-25 13:06:49

squirrel
Member
Registered: 2015-08-13
Posts: 155

Enable compression on TSQLHttpServer

How do I ensure that the json responses from my interfaces are always sent compressed to ajax clients?

fHttpServer.RegisterCompress(CompressGZip) do execute as well as THttpSocketCompress.func
However, when looking at the headers returned from my Mormot server, Content-Encoding is never set, and the transfered data size in the webbrowser is exactly the same as the uncompressed size.  Monitoring traffic confirmed that the 3mb json strings are sent uncompressed to the web browsers.  Is there an additional property that needs to be set, or is this an IIS issue?

Request header:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding	:gzip, deflate
Connection	:keep-alive
Content-Length:290
Content-Type:application/json; charset=utf-8
User-Agent:Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/60.0

Response header:

Accept-Encoding	:synlz,gzip
Access-Control-Allow-Methods:POST, PUT, GET, DELETE, LOCK, OPTIONS
Access-Control-Allow-Origin	:*
Access-Control-Expose-Headers:content-length,location,server-internalstate
Access-Control-Max-Age:1728000
Content-Type:application/json; charset=UTF-8
Server:mORMot (Windows) Microsoft-HTTPAPI/2.0
Server-InternalState:14
Transfer-Encoding:chunked
X-Powered-By:mORMot 1.18 synopse.info

Offline

#2 2018-05-25 14:57:45

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Enable compression on TSQLHttpServer

Compression work perfectly (just checked on HTTP-API based server).
Header you provide is very strange. Should be

Accept-Encoding: gzip, deflate

without space between Accept-Encoding and :

Offline

#3 2018-05-28 06:27:47

squirrel
Member
Registered: 2015-08-13
Posts: 155

Re: Enable compression on TSQLHttpServer

The Accept-Encoding in the response header from Mormot seem to always be Accept-Encoding: synlz,gzip.  But it doesn't look like that affects the outcome.  It looks like compression works fine for small strings, but that bigger strings are chunked and therefor not compressed.  I was (mistakenly) thinking that the chunking happens after compression.  Does this mean that compression specifically only work for small packages?  Wouldn't that negate the advantage of compression?  Is there a property I can set to always enforce compression if deflate is in the request headers?

I've created a test program using the latest nightly to ensure that none of my code affects the outcome.  Does it do anything wrong?

  TMyRestServer = class(TSQLRestServerFullMemory)
  published
    procedure Hello(Ctxt: TSQLRestServerURIContext);
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  aModel := TSQLModel.Create([],'my_test');
  aRestServer := TMyRestServer.Create(aModel);
  aHttpServer := TSQLHttpServer.Create('8080', [aRestServer]);
end;

procedure TMyRestServer.Hello(Ctxt: TSQLRestServerURIContext);
var
  data: RawUtf8;
begin
  data := StringFromFile('c:\sample_2mb.json');
  Ctxt.Returns(data) //not compressed, but chunked
  //Ctxt.Returns('{"hallo": "world"}'); //compressed
end;

Last edited by squirrel (2018-05-28 06:36:21)

Offline

Board footer

Powered by FluxBB