Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi TIdHTTPServer als Webserver (https://www.delphipraxis.net/109019-tidhttpserver-als-webserver.html)

meg91 22. Feb 2008 21:14


TIdHTTPServer als Webserver
 
Hallo
habe ein kleines Problem mit dem TIdHTTPServer. Ich nutze diese Komponente als kleinen Webserver, der auf Anfrage eines Borwsers einfach mit einer *.html Datei antwortet.
Hier ein bisschen Source
Delphi-Quellcode:
var
  HTTPServer: TIdHTTPServer;
begin
  HTTPServer := TIdHTTPServer.Create;
  with HTTPServer do
  begin
    DefaultPort := 180;
    OnConnect := HTTPServerConnect;
    Active := true;
  end;
end;
Delphi-Quellcode:
procedure TServer.HTTPServerConnect(AContext: TIdContext);
var
  s_htdocs: string;
  s_answer: string;

  sl_html: TStringList;
const
  c_innertemplate = 'inner_template.html';
  c_innervalues = 'inner_values.html';
  c_index = 'index4.html';
begin
  s_htdocs := ExtractFilePath(ParamStr(0)) + '\webserver\';

  s_answer := AContext.Connection.IOHandler.ReadLn;
  s_answer := StrUtils.ReplaceStr(s_answer, 'GET /', '');
  s_answer := StrUtils.ReplaceStr(s_answer, ' HTTP/1.1', '');

  sl_html := TStringList.Create;

  sl_html.LoadFromFile(s_htdocs + c_innertemplate);

  u_main.fi_receive.b_get_time.Click;
  u_main.fi_receive.b_get_temp.Click;

  sl_html.Text := StringReplaceMultiple(sl_html.Text,
                                        ['%time%', '%date%'],
                                        [l_time.Caption, l_date.Caption]);

  sl_html.Text := StringReplaceMultiple(sl_html.Text,
                                        ['%temp1%', '%temp2%', '%temp3%', '%temp4%'],
                                        [l_temp1.Caption, l_temp2.Caption, l_temp3.Caption, l_temp4.Caption]);

  sl_html.Text := StringReplaceMultiple(sl_html.Text,
                                        ['°', ''],
                                        ['°', '']);

  try
    sl_html.SaveToFile(s_htdocs + c_innervalues);
  finally
    sl_html.Free;
  end;

  if s_answer = '' then
    AContext.Connection.IOHandler.WriteFile(s_htdocs + c_index);

  if (Pos('.', s_answer) <> 0) then
  begin
    if FileExists(s_htdocs + s_answer) then
      AContext.Connection.IOHandler.WriteFile(s_htdocs + s_answer)
  end;
end;
Im großen ganzen funktioniert es auch, nur leider gibt es noch einen Schönheitsfehler
Der Server hängt jeder Datei "HTTP/1.1 200 OK Connection: close Content-Type: text/html Server: Indy/10.1.5" an
Zitat:

...
</body>
</html>
HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
Server: Indy/10.1.5
was man dann im Browser natürlich zu sehen bekommt
da sich diese Website aus mehreren einzel Dateien aufbaut, welche alle via AJAX nachgeladen werden, kommt dann der oben beschriebene Term nicht nur einmal, sondern gleich mehrmals vor, was nicht sonderlich toll ist

Kann man das irgendwie wegbekommen?

Dani 22. Feb 2008 21:48

Re: TIdHTTPServer als Webserver
 
Hi! Arbeitest du mit Indy10? Falls ja, dann schau dir mal die Events TIdHTTPServer.OnCommandGet und TIdHTTPServer.OnCommandOther an. Da musst du den Webserver implementieren, nicht in OnConnect.

meg91 22. Jun 2008 21:30

Re: TIdHTTPServer als Webserver
 
ja es ist Indy10
mit der neuen Funktion klappt alles wunderbar. Danke.
Delphi-Quellcode:
procedure TServer.HTTPServerCommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:56 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz