simpleserver.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. {$mode objfpc}
  2. {$h+}
  3. program simpleserver;
  4. uses sysutils, custhttpapp, fpwebfile, sslbase, opensslsockets;
  5. Type
  6. { THTTPApplication }
  7. THTTPApplication = Class(TCustomHTTPApplication)
  8. private
  9. FQuiet: Boolean;
  10. procedure Usage(Msg: String);
  11. published
  12. procedure DoLog(EventType: TEventType; const Msg: String); override;
  13. Procedure DoRun; override;
  14. property Quiet : Boolean read FQuiet Write FQuiet;
  15. end;
  16. Var
  17. Application : THTTPApplication;
  18. { THTTPApplication }
  19. procedure THTTPApplication.DoLog(EventType: TEventType; const Msg: String);
  20. begin
  21. if Quiet then
  22. exit;
  23. if IsConsole then
  24. Writeln(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Now),' [',EventType,'] ',Msg)
  25. else
  26. inherited DoLog(EventType, Msg);
  27. end;
  28. procedure THTTPApplication.Usage(Msg : String);
  29. begin
  30. if (Msg<>'') then
  31. Writeln('Error: ',Msg);
  32. Writeln('Usage ',ExtractFileName(ParamStr(0)),' [options] ');
  33. Writeln('Where options is one or more of : ');
  34. Writeln('-d --directory=dir Base directory from which to serve files.');
  35. Writeln(' Default is current working directory: ',GetCurrentDir);
  36. Writeln('-h --help This help text');
  37. Writeln('-i --indexpage=name Directory index page to use (default: index.html)');
  38. Writeln('-n --noindexpage Do not allow index page.');
  39. Writeln('-p --port=NNNN TCP/IP port to listen on (default is 3000)');
  40. Writeln('-m --mimetypes=file path of mime.types, default under unix: /etc/mime.types');
  41. Writeln('-q --quiet Do not write diagnostic messages');
  42. Writeln('-s --ssl Use SSL');
  43. Writeln('-H --hostname=NAME set hostname for self-signed SSL certificate');
  44. Halt(Ord(Msg<>''));
  45. end;
  46. procedure THTTPApplication.DoRun;
  47. Var
  48. S,IndexPage,D : String;
  49. begin
  50. S:=Checkoptions('hqd:ni:p:sH:',['help','quiet','noindexpage','directory:','port:','indexpage:','ssl','hostname:']);
  51. if (S<>'') or HasOption('h','help') then
  52. usage(S);
  53. Quiet:=HasOption('q','quiet');
  54. Port:=StrToIntDef(GetOptionValue('p','port'),3000);
  55. D:=GetOptionValue('d','directory');
  56. if D='' then
  57. D:=GetCurrentDir;
  58. Log(etInfo,'Listening on port %d, serving files from directory: %s',[Port,D]);
  59. UseSSL:=HasOption('s','ssl');
  60. if HasOption('H','hostname') then
  61. HostName:=GetOptionValue('H','hostname');
  62. if HasOption('m','mimetypes') then
  63. MimeTypesFile:=GetOptionValue('m','mimetypes');
  64. {$ifdef unix}
  65. if MimeTypesFile='' then
  66. begin
  67. MimeTypesFile:='/etc/mime.types';
  68. if not FileExists(MimeTypesFile) then
  69. begin
  70. {$ifdef darwin}
  71. MimeTypesFile:='/private/etc/apache2/mime.types';
  72. if not FileExists(MimeTypesFile) then
  73. {$endif}
  74. MimeTypesFile:='';
  75. end;
  76. end;
  77. if (MimeTypesFile<>'') and not FileExists(MimeTypesFile) then
  78. Log(etWarning,'mimetypes file not found: '+MimeTypesFile);
  79. {$endif}
  80. TSimpleFileModule.BaseDir:=IncludeTrailingPathDelimiter(D);
  81. TSimpleFileModule.OnLog:=@Log;
  82. If not HasOption('n','noindexpage') then
  83. begin
  84. IndexPage:=GetOptionValue('i','indexpage');
  85. if IndexPage='' then
  86. IndexPage:='index.html';
  87. Log(etInfo,'Using index page %s',[IndexPage]);
  88. TSimpleFileModule.IndexPageName:=IndexPage;
  89. end;
  90. inherited;
  91. end;
  92. begin
  93. TSimpleFileModule.RegisterDefaultRoute;
  94. Application:=THTTPApplication.Create(Nil);
  95. Application.Initialize;
  96. Application.Run;
  97. Application.Free;
  98. end.