simpleserver.pas 3.4 KB

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