Recent

Author Topic: (Indy) Web application get page error  (Read 2751 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
(Indy) Web application get page error
« on: November 07, 2018, 09:39:39 am »
Hi guys, I created a web app with integrated web server (indy 10) to replicate errors. I would like your opinion on this. What's wrong?
I have to read a json string through an http call and I tried 3 ways. The first works, the other two do not. But I would like to understand what is different. For me it is absurd. Who can help me? With indy, reading this string generates an error that is not always replicable:

HTTP / 1.1 200 OK Connection: close Content-Length: 397

While synapse the version that I expect to work does not work, while the one written by me. Tips? I enclose an example.

This is my browser result:
Code: Pascal  [Select]
  1. correct: { "token" : "ZVRoQWN0aFF6UExkdmQ4VTVlcmd4VGkxM1duQVdpUFFydnBESGJ4eUhQTUtTUUhoNldGTU9sWlFscjhna0JZdXlaeTBiWFVaWSs5a1dVa0RtbnJtN1NPVm1RT25sd1VuLzdINlBHcUl4NWtoVjl0bFJJWXZ6blFDYytrMEdseStjRitwNFlRY1U4WWJBdjc2U1pXbk11RnBzWFlGZ2hBWThJQThxaTBrLy9MQlBhU1loeTVCYzI1VzdWcTBFVGEyRGxSMkV3WkRqdWxlbmNRMWo2bWlVK1kzRi9sbnEwU2R6Q3BHV3JaR3gwTGYrblpwUmNEaVc1UEdDdC9mQTVUbGs2OUtOanloUlJWN1ZIR2JydlRYQnh0V3ljbz0=" }
  2. error 1: { "token" : "ZVRoQWN0aFF6UExkdmQ4VTVlcmd4VGkxM1duQVdpUFFydnBESGJ4eUhQTUtTUUhoNldGTU9sWlFscjhna0JZdXlaeTBiWFVaWSs5a1dVa0RtbnJtN1NPVm1RT25sd1VuLzdINlBHcUl4NWtoVjl0bFJJWXZ6blFDYytrMEdseStjRitwNFlRY1U4WWJBdjc2U1pXbk11RnBzWFlGZ2hBWThJQThxaTBrLy9MQlBhU1loeTVCYz
  3. error 2: HTTP/1.1 200 OK Connection: close Content-Length: 397 Server: NGIT Bootstrap Application Set-Cookie: IDHTTPSESSIONID=nh2ari4EVMF2sxBnh2ari4EVMF2sxBnh2ari4EVMF2sxBnh2ari4EVMF2sxBnh2ari4EVMF2sxBnh2ari4EVMF2sxB; path=/ { "token" : "ZVRoQWN0aFF6UExkdmQ4VTVlcmd4VGkxM1duQVdpUFFydnBESGJ4eUhQTUtTUUhoNldGTU9sWlFscjhna0JZdXlaeTBiWFVaWSs5a1dVa0RtbnJtN1NPVm1RT25sd1VuLzdINlBHcUl4NWtoVjl0bFJJWXZ6blFDY
  4.  
« Last Edit: November 07, 2018, 10:33:16 am by xinyiman »
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #1 on: November 07, 2018, 12:01:59 pm »
It was pointed out to me that I left dcpcrypt among the dependencies. You can remove it, it is not necessary for the use of the test program
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

Thaddy

  • Hero Member
  • *****
  • Posts: 7178
Re: (Indy) Web application get page error
« Reply #2 on: November 07, 2018, 12:16:56 pm »
The error is obviously a terminating zero because it expects a PChar. Will test after lunch.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 510
    • Lebeau Software
Re: (Indy) Web application get page error
« Reply #3 on: November 07, 2018, 07:34:14 pm »
Hi guys, I created a web app with integrated web server (indy 10) to replicate errors. I would like your opinion on this. What's wrong?

Some mistakes I see:

- your TIdHTTPServer.OnCommandGet event handler is catching and discarding all raised exceptions.  Don't do that!  Let TIdHTTPServer handle exceptions so it can send error messages back to the HTTP client.  If you are going to catch exceptions for your own logging, at least re-raise them when you are done using them.  Otherwise, when the OnCommandGet event handler exits cleanly, TIdHTTPServer will send whatever reply data is currently in the the TIdHTTPResponse object (if it has not already been sent manually by the OnCommandGet event handler), and that will default to sending an HTTP 200 reply.

- your TNGITBootstrapApplication constructor should be initializing its FHTMLDir and FTemplate members before activating the TIdHTTPServer, so they are ready for use before any clients can connect and potentially access them before they are ready.

- TNGITController.GetHTML() is not protecting itself from exceptions when version is 2.  You need to add try..finally blocks to free the TIdHTTP and TMemoryStream objects correctly.  TIdHTTP.Get() will raise an exception if it receives an error from the server (unless you disable that option in the TIdHTTP.HTTPOptions property).
« Last Edit: November 08, 2018, 08:14:31 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #4 on: November 08, 2018, 08:23:42 am »
Thank you Remy Lebea. One question, your suggestion is like my attachment?

Thank you
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #5 on: November 08, 2018, 12:10:10 pm »
Quote
(unless you disable that option in the TIdHTTP.HTTPOptions property)

How? I searched but only HTTPOptions value are:

Code: Pascal  [Select]
  1.   // Protocol options
  2.   TIdHTTPOption = (hoInProcessAuth, hoKeepOrigProtocol, hoForceEncodeParams);
  3.   TIdHTTPOptions = set of TIdHTTPOption;  
  4.  

Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 510
    • Lebeau Software
Re: (Indy) Web application get page error
« Reply #6 on: November 08, 2018, 08:19:07 pm »
One question, your suggestion is like my attachment?

I don't see my suggested changes applied to that code.  Especially the OnCommandGet change in particular.  It is still catching and discarding all exceptions.

How? I searched but only HTTPOptions value are:

Code: Pascal  [Select]
  1.   // Protocol options
  2.   TIdHTTPOption = (hoInProcessAuth, hoKeepOrigProtocol, hoForceEncodeParams);
  3.   TIdHTTPOptions = set of TIdHTTPOption;  
  4.  

You claim to be using Indy 10, but are you maybe using Indy 9 instead?  What you show above is what TIdHTTPOption looks like in Indy 9.  In the current Indy 10 release, TIdHTTPOption has MANY more options:

Code: [Select]
  TIdHTTPOption = (hoInProcessAuth, hoKeepOrigProtocol, hoForceEncodeParams,
    hoNonSSLProxyUseConnectVerb, hoNoParseMetaHTTPEquiv, hoWaitForUnexpectedData,
    hoTreat302Like303, hoNoProtocolErrorException, hoNoReadMultipartMIME,
    hoNoParseXmlCharset, hoWantProtocolErrorContent, hoNoReadChunked
    );

When I mentioned configuring TIdHTTP.Get() to not raise an exception on a server error, I was referring to the hoNoProtocolErrorException option.
« Last Edit: November 08, 2018, 08:27:03 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #7 on: November 08, 2018, 11:45:09 pm »
I see but lazarus have install indylaz v10.2.0.3 but i have only: hoInProcessAuth, hoKeepOrigProtocol, hoForceEncodeParams

where I find the version of indy 10 that contains all the options you say?!
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #8 on: November 09, 2018, 09:31:50 am »
Ok, i find a correct version of indy. Now i change with your suggestion. But now my program closed when into browser page run fast. If you try to quickly reload the page without waiting for the page to load before the program closes without giving any kind of error. Why?
« Last Edit: November 09, 2018, 10:05:26 am by xinyiman »
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 510
    • Lebeau Software
Re: (Indy) Web application get page error
« Reply #9 on: November 09, 2018, 10:27:53 pm »
I see but lazarus have install indylaz v10.2.0.3 but i have only: hoInProcessAuth, hoKeepOrigProtocol, hoForceEncodeParams

That is an extremely old version of Indy.  The current version is 10.6.2.5485.

where I find the version of indy 10 that contains all the options you say?!

You can download Indy from its official download mirrors, which includes Lazarus's own Online Package Manager (though, I don't know which version of Indy is currently in OPM.  IIRC, it does sync up fairly often).

Ok, i find a correct version of indy. Now i change with your suggestion. But now my program closed when into browser page run fast. If you try to quickly reload the page without waiting for the page to load before the program closes without giving any kind of error. Why?

I have no idea what you are referring to.  Please clarify.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #10 on: November 09, 2018, 10:50:17 pm »
Have you tried to download and run my last attached example? Have you tried to run it? You'll see that if you try to run my program and quickly load the page in the browser, the http server closes itself without giving an error.
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 510
    • Lebeau Software
Re: (Indy) Web application get page error
« Reply #11 on: November 09, 2018, 11:46:45 pm »
Have you tried to download and run my last attached example? Have you tried to run it?

No, because I'm not a FreePascal/Lazarus user, so I couldn't compile and run your project even if I wanted to.

You'll see that if you try to run my program and quickly load the page in the browser, the http server closes itself without giving an error.

It is your responsibility to debug your own project.  I can't help you with that.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #12 on: November 10, 2018, 12:27:50 am »
But Is not a Project problem, because whit old version of Indy not exists this problem. For me Is a library problem. You know a Lazarus developer from Indy team?!
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #13 on: November 10, 2018, 04:56:21 pm »
I testes my project example on other O.S. and this is the result

1. Windows: no problem, my project it did not close unexpectedly

2. Linux: problem, my project it closed unexpectedly

3. Mac OSX: problem, my project it closed unexpectedly

Into unix like OS is problem
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4

xinyiman

  • Hero Member
  • *****
  • Posts: 1751
Re: (Indy) Web application get page error
« Reply #14 on: November 10, 2018, 05:58:23 pm »
I found the place where the problem is generated, in the IdHTTP unit there is the function

Code: Pascal  [Select]
  1. function TIdCustomHTTP.Get(AURL: string
  2.   {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
  3.   ): string;
  4. begin
  5.   Result := Get(AURL, []{$IFDEF STRING_IS_ANSI}, ADestEncoding{$ENDIF});
  6. end;  
  7.  

It happens that if the server receives two simultaneous calls it closes. This is on linux and on mac osx. Only I do not understand how to solve. Some idea?
Ubuntu and Mac
Lazarus: 1.8.0
FPC: 3.0.4