simpleserver.pas 3.4 KB

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