keepalive.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. program keepalive;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, fphttpclient, opensslsockets;
  5. const
  6. URL_DIRECT = 'https://www.google.com/humans.txt';
  7. URL_REDIRECTED = 'https://google.com/humans.txt';
  8. type
  9. { TKeepConnectionDemo }
  10. TKeepConnectionDemo = class(TCustomApplication)
  11. private
  12. FURL : String;
  13. FShowResult : Boolean;
  14. FCount : Integer;
  15. FHttp: TFPHTTPClient;
  16. FData: TBytesStream;
  17. procedure DoRequests;
  18. procedure Usage(Msg: string);
  19. Protected
  20. Procedure DoRun; override;
  21. public
  22. constructor Create(AOwner: TComponent); override;
  23. destructor Destroy; override;
  24. end;
  25. constructor TKeepConnectionDemo.Create(AOwner: TComponent);
  26. begin
  27. inherited Create(AOwner);
  28. StopOnException:=True;
  29. FHttp := TFPHTTPClient.Create(nil);
  30. FData := TBytesStream.Create;
  31. end;
  32. destructor TKeepConnectionDemo.Destroy;
  33. begin
  34. FData.Free;
  35. FHttp.Free;
  36. inherited Destroy;
  37. end;
  38. procedure TKeepConnectionDemo.DoRequests;
  39. var
  40. U: string;
  41. B, E: TDateTime;
  42. L : TStrings;
  43. I : Integer;
  44. begin
  45. for I:=1 to FCount do
  46. begin
  47. FData.Clear;
  48. B := Now;
  49. if (FURL<>'') then
  50. U:=FURL
  51. else if FHTTP.AllowRedirect then
  52. U := URL_REDIRECTED
  53. else
  54. U := URL_DIRECT;
  55. FHttp.Get(U, FData);
  56. E := Now;
  57. Writeln('Request ',i,', Duration: ',FormatDateTime('hh:nn:ss.zzz', E - B));
  58. If FShowResult then
  59. begin
  60. FData.Seek(0, TSeekOrigin.soBeginning);
  61. With TStringList.Create do
  62. try
  63. LoadFromStream(FData);
  64. Writeln(text);
  65. finally
  66. Free;
  67. end;
  68. end;
  69. end;
  70. end;
  71. procedure TKeepConnectionDemo.Usage(Msg : string);
  72. begin
  73. if (Msg<>'') then
  74. Writeln('Error : ',Msg);
  75. Writeln(' Usage : keepalive [options]');
  76. Writeln('Where options is one or more of:');
  77. Writeln('-h --help This help');
  78. Writeln('-r --redirect Allow HTTP Redirect');
  79. Writeln('-k --keep-connection Keep connection');
  80. Writeln('-c --count=N Number of requests');
  81. Writeln('-u --URL=uri Specify url');
  82. Halt(Ord(Msg<>''));
  83. end;
  84. procedure TKeepConnectionDemo.DoRun;
  85. Var
  86. S : String;
  87. begin
  88. S:=CheckOptions('hrksc:u:',['count:','show','url:','redirect','keep-connection','help']);
  89. if (S<>'') or HasOption('h','help') then
  90. Usage(S);
  91. FCount:=StrToIntDef(GetOptionValue('c','count'),10);
  92. FShowResult:=HasOption('s','show');
  93. FURL:=GetOptionValue('u','url');
  94. FHTTP.AllowRedirect:=HasOption('r','redirect');
  95. FHTTP.KeepConnection:=HasOption('k','keep-connection');
  96. DoRequests;
  97. Terminate;
  98. end;
  99. begin
  100. With TKeepConnectionDemo.Create(Nil) do
  101. try
  102. Initialize;
  103. Run;
  104. Finally
  105. Free;
  106. end;
  107. end.