Unit2.pas 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. unit Unit2;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.Messages,
  6. System.SysUtils,
  7. System.Variants,
  8. System.Classes,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. SynHttpSrv,
  14. ssl_openssl_lib,
  15. ssl_openssl,
  16. Vcl.StdCtrls,
  17. SynSrv;
  18. type
  19. TForm2 = class(TForm)
  20. Button1: TButton;
  21. CheckBox1: TCheckBox;
  22. procedure Button1Click(Sender: TObject);
  23. procedure CheckBox1Click(Sender: TObject);
  24. private
  25. FSynHttpServer: TSynHttpServer;
  26. { Private declarations }
  27. procedure SynHttpServer1HttpGet(Sender: TObject; Connection: TSynTcpSrvConnection;
  28. ARequestInfo, AResponseInfo: THttpRequest);
  29. public
  30. { Public declarations }
  31. end;
  32. var
  33. Form2: TForm2;
  34. implementation
  35. {$R *.dfm}
  36. procedure TForm2.Button1Click(Sender: TObject);
  37. procedure TryToOpenWebPort;
  38. var
  39. s: string;
  40. begin
  41. try
  42. if not FSynHttpServer.Active then
  43. FSynHttpServer.Active := True;
  44. except
  45. on E: Exception do
  46. begin
  47. s := Format('Exception %s occurred while trying activate http or https connection. Message:"%s".',
  48. [E.ClassName, E.Message]);
  49. //Log(s);
  50. end;
  51. end;
  52. end;
  53. begin
  54. FSynHttpServer := TSynHttpServer.Create(Self);
  55. FSynHttpServer.OnHttpGet := SynHttpServer1HttpGet;
  56. FSynHttpServer.Port := '8080';
  57. FSynHttpServer.HTTPSEnabled := CheckBox1.Checked;
  58. TryToOpenWebPort;
  59. if FSynHttpServer.Active then
  60. FSynHttpServer.InitHttps('server.crt', 'server.key', 'w1z2rd', '');
  61. end;
  62. procedure TForm2.CheckBox1Click(Sender: TObject);
  63. begin
  64. if CheckBox1.Checked then
  65. if InitSSLInterface then
  66. ShowMessage('SSL initialized')
  67. else
  68. begin
  69. ShowMessage('SSL does not initialized');
  70. CheckBox1.Checked := False;
  71. end;
  72. end;
  73. procedure TForm2.SynHttpServer1HttpGet(Sender: TObject; Connection: TSynTcpSrvConnection;
  74. ARequestInfo, AResponseInfo: THttpRequest);
  75. procedure WriteData;
  76. begin
  77. try
  78. FSynHttpServer.SendReply(Connection, ARequestInfo, AResponseInfo);
  79. except
  80. On E: Exception do
  81. if (Pos('10054', E.Message) = 0) and (Pos('10053', E.Message) = 0) then
  82. {Log('Error; Exception occured. ' + E.Message)};
  83. end;
  84. end;
  85. procedure RespString(const Str: string; const CharSet: string = '');
  86. begin
  87. if Str.IsEmpty then
  88. AResponseInfo.Content := ' '
  89. else
  90. AResponseInfo.Content := Str;
  91. AResponseInfo.ContentType := 'text/html';
  92. AResponseInfo.CharSet := CharSet;
  93. AResponseInfo.StatusCode := 200;
  94. WriteData;
  95. end;
  96. begin
  97. if ARequestInfo.Params.Values['Ping'] = 'Ping' then
  98. RespString('Pong');
  99. end;
  100. end.