Recent

Author Topic: LHTTPServer response problem  (Read 6653 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
LHTTPServer response problem
« on: April 11, 2011, 08:30:10 pm »
Hi,

Only LHTTPServer component from LNet package have status "experimental" so I have question. There is demo for TCP server and it works fine but I don't know how to use HTTP server. I know how to handle connections because this is similar like in TCP but what next? How to return HTML document? Example: client send request for some subpage (www.somepage.com/page1, www.somepage.com/page2 etc) and my server should return XML (depending on URL). TLHTTPServerSocket have 3 public methods (SetupEncoding, StartMemoryResponse, StartResponse) but params are not clear for me and each method call raise AV. Can I somehow prepare response template or must I do this manually by calling ASocket.SendMessage('HTTP/1.1 STATUS OK.....')?

Regards

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: LHTTPServer response problem
« Reply #1 on: April 11, 2011, 09:13:45 pm »
I didn't make the HTTP part and I don't remember much from the server side, but there's a working webserver (even with PHP/fastCGI connection!) inside lnet/examples/console/lhttp (fphttpd).

It's a basic console program of course, but it has already it's own config file (fphttpd.ini IIRC located in ~/.fphttpd after 1st start).

Hope this helps with some experimentation, AFAIK we got it up and running and even serving PHP requests. (so there's a file access bound to be coded in there).

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: LHTTPServer response problem
« Reply #2 on: April 11, 2011, 10:18:05 pm »
Hmm it seems that I must send it by my self. Maybe it's better. I have full control of data which I send. Synapse server do this same things:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LHTTPServerComponent1Accept(aSocket: TLSocket);
  2. var
  3.   OutputDataString: String;
  4. begin
  5.   Memo1.Append('accept: ' + aSocket.ClassName);
  6.  
  7.   OutputDataString :=
  8.     '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
  9.     + ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + #13#10
  10.     + '<html><h1>test test</h1></html>' + #13#10;
  11.  
  12.   s.SendMessage(
  13.     'HTTP/1.0 200 '+#13#10+
  14.     'Content-type: Text/Html'+#13#10+
  15.     'Content-length: '+inttostr(Length(OutputDataString))+#13#10+
  16.     'Connection: close'+#13#10+#13#10+
  17.     OutputDataString
  18.   );  
  19. end;  
  20.  
But I think I found a bug in HTTP client. I spent a lot of time to figure it out. I use lNet on client side too. I thought that problem is on server side until I check this same on synapse http server. Internet browser (chromium) retrieve data from my both servers so this is client bug. So, look at example above. If after "HTTP/1.0 200" is no space then client disconnect without error (only disconnect event is fired). I sniffed this request using wireshark and client get server response but can't parse it. I think in this loop:
Code: Pascal  [Select][+][-]
  1. procedure TLHTTPClientSocket.ParseStatusLine(pLineEnd: pchar);
  2. var
  3.   lPos: pchar;
  4. begin
  5.   lPos := FBufferPos;
  6.   repeat
  7.     if lPos >= pLineEnd then
  8.     begin
  9.       Cancel(ceMalformedStatusLine);
  10.       exit;
  11.     end;
  12.     if lPos^ = ' ' then
  13.       break;
  14.     Inc(lPos);
  15.   until false;  
  16. ...........
  17.  
... should be "OR #13#10" in line "if lPos^ = ' '". I don't know how it depends on HTTP standards but internet browser can parse status without space.

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: LHTTPServer response problem
« Reply #3 on: April 11, 2011, 10:53:47 pm »
It seems this is not a proper status line conforming to specs:

Code: [Select]
<neli> #13 and #10 are line terminators
<neli> 'No CR or LF is allowed except in the final CRLF sequence.
<neli> Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
* fpk is now known as fpk^off
<neli> so AFAICS, it is bad server implementation
<neli> does not conform to specification
« Last Edit: April 11, 2011, 11:48:10 pm by Almindor2 »

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: LHTTPServer response problem
« Reply #4 on: April 16, 2011, 09:23:20 pm »
Ok.

I have another problem. I can handle response, headers etc but how to read client data? In TCP server demo only OnReceive event is assigned but in LHTTPServer this event is never fired. Client send me content length in headers and content (XML string) but I don't know how to read it. If I force read data by calling GetMessage it return me that there is no message. FRequestInfo and FRequestInfo objects have only basic info (like method, query, params, headers) but no content :/

Regards

 

TinyPortal © 2005-2018