1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer, IdHTTPServer, StdCtrls, ExtCtrls;
type TForm1 = class(TForm) Memo1: TMemo; IdHTTPServer1: TIdHTTPServer; Label1: TLabel; Timer1: TTimer; ListBox1: TListBox; Label2: TLabel; ListBox2: TListBox; procedure IdHTTPServer1Connect(AThread: TIdPeerThread); procedure send(AThread: TIdPeerThread); procedure splitt(zeichenkette: string; leerzeichen: char; sList: TStringList); function directory(verzeichniss: string): string; function GetFileSizeEx(const AFileName: string): Int64; private public end;
var Form1: TForm1; Intzaehler, msekunden: Integer; sollichzaehln, connectedf, open: boolean; sList, savinglist, sList2: TStringlist; implementation
{$R *.dfm}
procedure TForm1.IdHTTPServer1Connect(AThread: TIdPeerThread); begin if not open then begin open := True; send(AThread); end; end;
procedure TFOrm1.send(AThread: TIdPeerThread); var items, test, filename, verzeichnissausgabe: string; maybe, size, maul, multi, filetyp, fauli, points: Integer; fs, fn: TFileStream; zeichen: char; fileyes: Boolean; begin open := true; filename := AThread.Connection.ReadLn(); ListBox1.Clear; Application.ProcessMessages; sList := TStringList.Create; try splitt(filename, ' ', sList); filename := sList.Strings[1]; finally sList := TStringList.Create; end; filename[1] := '\'; filename := trim(filename); points := 0; for maybe := length(filename) downto 0 do begin if ((filename[maybe] = '/') or (filename[maybe] = '\')) and (points = 0) then begin fileyes := False; break; end; if ((filename[maybe] = '/') or (filename[maybe] = '\')) and (points > 0) then begin fileyes := True; break; end; if (filename[maybe] = '.') then inc(points); end; if (filename[length(filename)] <> '\') and (not fileyes) then filename := filename + '\'; Label2.Caption := filename; if (filename[length(filename)] = '/') or (filename[length(filename)] = '\') then begin filetyp := 0; open := false; verzeichnissausgabe := directory('files' + filename); savinglist := TStringList.Create; try savinglist.Text := verzeichnissausgabe; savinglist.SaveToFile('Temp.temp'); finally savinglist.free; end; AThread.Start; AThread.COnnection.WriteLn('HTTP/1.1 200 OK'); AThread.Connection.WriteLn('Server: hui1991s Fileserver'); AThread.COnnection.WriteLn('Accept-Ranges: bytes'); AThread.COnnection.WriteLn('Content-Length: ' + inttostr(GetFileSizeEx(ExtractFilePath(ParamStr(0)) + 'temp.temp'))); AThread.COnnection.WriteLn('Connection: close'); AThread.COnnection.WriteLn('Content-Type: text/html'); AThread.COnnection.WriteLn(''); fauli := AThread.Connection.WriteFile(ExtractFilePath(ParamStr(0)) + 'Temp.temp'); AThread.Stop; end else begin filetyp := 1; end; if filetyp = 1 then begin Memo1.Clear; connectedf := True; sollichzaehln := False; items := ''; size := GetFileSizeEx(ExtractFilePath(ParamStr(0)) + 'files' + filename); AThread.Start; AThread.COnnection.WriteLn('HTTP/1.1 200 OK'); AThread.Connection.WriteLn('Server: hui1991s Fileserver'); AThread.COnnection.WriteLn('Accept-Ranges: bytes'); AThread.COnnection.WriteLn('Content-Length: ' + inttostr(size)); AThread.COnnection.WriteLn('Connection: close'); AThread.COnnection.WriteLn(''); multi := AThread.Connection.WriteFile(ExtractFilePath(ParamStr(0)) + 'files' + filename); AThread.Stop; open := false; end; end;
procedure TForm1.splitt(zeichenkette: string; leerzeichen: char; sList: TStringList); var s, r: string; i, x, j, k: integer; arr: array[0..200] of string; begin s := zeichenkette; i := 0; sList.Clear; repeat j := Pos(leerzeichen, s); if j > 0 then begin r := copy(s, 1, j - 1); arr[i] := r; inc(i); delete(s, 1, j);
end else break; until j = 0; if length(s) > 0 then begin arr[i] := s; for k := 0 to i do slIst.Add(arr[k]); end end;
function TForm1.directory(verzeichniss: string): string; function SlashSep(const Path, S: string): string; begin if AnsiLastChar(Path)^ <> '\' then Result := Path + '\' + S else Result := Path + S; end;
var SearchRec: TSearchRec; ADirectory, schwaze: string; x: Integer; begin ADirectory := verzeichniss; ListBox2.Clear; if FindFirst(SlashSep(ADirectory, '*.*'), faAnyFile, SearchRec) = 0 then begin try repeat if ((SearchRec.Name <> '.') and (SearchRec.Name <> '..')) then begin if (SearchRec.Attr = 32) and (ADirectory = verzeichniss) then ListBox2.Items.Add('2|' + SearchRec.Name + '|' + inttostr(SearchRec.FindData.nFileSizeLow or (SearchRec.FindData.nFileSizeHigh shl 32))); if (SearchRec.Attr = 16) and (ADirectory = verzeichniss) then ListBox2.Items.Add('1|' + SearchRec.Name + '|-'); end; until FindNext(SearchRec) <> 0; finally SysUtils.FindClose(SearchRec); end; end;
sList2 := TStringList.Create; sList := TStringList.Create; try Memo1.Clear; sList2.Add('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'); sList2.Add('<html>'); sList2.Add(' <head>'); sList2.Add(' <title>Index of ' + verzeichniss + '</title>'); sList2.Add(' </head>'); sList2.Add(' <body>'); sList2.Add('<h1>Index of ' + verzeichniss + '</h1>'); sList2.Add('<pre><img src="/icons/blank.gif" alt="Icon "> Name Last modified Size Description<hr><a href="/">Parent Directory</a></pre><br>'); for x := 0 to ListBox2.Items.Count - 1 do begin splitt(ListBox2.Items.Strings[x], '|', sList); if sList.Strings[0] = '1' then schwaze := '<img src="/icons/folder.gif" alt="[DIR]">'; if sList.Strings[0] = '2' then schwaze := '<img src="/icons/unknown.gif" alt="[ ]">'; sList2.Add('<pre>' + schwaze + '<a href="' + sList.Strings[1] + '">' + sList.Strings[1] + '</a> - ' + sList.Strings[2] + ' </pre>'); end; sList2.Add('<hr>'); sList2.Add('<address>Apache/2.2.2 (Win32) DAV/2 mod_ssl/2.2.2 OpenSSL/0.9.8b mod_autoindex_color PHP/5.1.4 Server at localhost Port ' + inttostr(IdHTTPServer1.DefaultPort) + '</address>'); sList2.Add('</body></html>'); Result := sList2.Text; finally sList2.free; sList.free; end; end;
function TForm1.GetFileSizeEx(const AFileName: string): Int64; var F: TSearchRec; begin Result := -1; if FindFirst(AFileName, faAnyFile, F) = 0 then begin try Result := F.FindData.nFileSizeLow or (F.FindData.nFileSizeHigh shl 32); finally SysUtils.FindClose(F); end; end; end;
end. |