Einzelnen Beitrag anzeigen

Benutzerbild von meg91
meg91

Registriert seit: 25. Apr 2006
131 Beiträge
 
Turbo Delphi für Win32
 
#1

TIdHTTPServer als Webserver

  Alt 22. Feb 2008, 21:14
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?
  Mit Zitat antworten Zitat