From fad77c99eaf476128d36226285f379c2e59f4e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20D=C3=ADaz=20Fau?= Date: Wed, 31 Jan 2018 18:52:34 +0100 Subject: [PATCH] MiniBrowser : Added a context menu option to inspect the response/request headers --- demos/MiniBrowser/MiniBrowser.dpr | 4 +- demos/MiniBrowser/MiniBrowser.dproj | 10 +++ demos/MiniBrowser/uMiniBrowser.dfm | 3 + demos/MiniBrowser/uMiniBrowser.pas | 112 +++++++++++++++++++++--- demos/MiniBrowser/uSimpleTextViewer.dfm | 29 ++++++ demos/MiniBrowser/uSimpleTextViewer.pas | 31 +++++++ 6 files changed, 175 insertions(+), 14 deletions(-) create mode 100644 demos/MiniBrowser/uSimpleTextViewer.dfm create mode 100644 demos/MiniBrowser/uSimpleTextViewer.pas diff --git a/demos/MiniBrowser/MiniBrowser.dpr b/demos/MiniBrowser/MiniBrowser.dpr index 44a8ff28..71cb8502 100644 --- a/demos/MiniBrowser/MiniBrowser.dpr +++ b/demos/MiniBrowser/MiniBrowser.dpr @@ -50,7 +50,8 @@ uses {$ENDIF } uCEFApplication, uMiniBrowser in 'uMiniBrowser.pas' {MiniBrowserFrm}, - uPreferences in 'uPreferences.pas' {PreferencesFrm}; + uPreferences in 'uPreferences.pas' {PreferencesFrm}, + uSimpleTextViewer in 'uSimpleTextViewer.pas' {SimpleTextViewerFrm}; {$R *.res} @@ -67,6 +68,7 @@ begin {$ENDIF} Application.CreateForm(TMiniBrowserFrm, MiniBrowserFrm); Application.CreateForm(TPreferencesFrm, PreferencesFrm); + Application.CreateForm(TSimpleTextViewerFrm, SimpleTextViewerFrm); Application.Run; end; diff --git a/demos/MiniBrowser/MiniBrowser.dproj b/demos/MiniBrowser/MiniBrowser.dproj index b574a708..dc36658c 100644 --- a/demos/MiniBrowser/MiniBrowser.dproj +++ b/demos/MiniBrowser/MiniBrowser.dproj @@ -138,6 +138,10 @@
PreferencesFrm
+ +
SimpleTextViewerFrm
+ dfm +
Cfg_2 Base @@ -166,6 +170,12 @@ + + + MiniBrowser.exe + true + + 1 diff --git a/demos/MiniBrowser/uMiniBrowser.dfm b/demos/MiniBrowser/uMiniBrowser.dfm index 16cb45fe..088cc60f 100644 --- a/demos/MiniBrowser/uMiniBrowser.dfm +++ b/demos/MiniBrowser/uMiniBrowser.dfm @@ -12,6 +12,8 @@ object MiniBrowserFrm: TMiniBrowserFrm Font.Style = [] OldCreateOrder = False Position = poScreenCenter + OnCreate = FormCreate + OnDestroy = FormDestroy OnShow = FormShow PixelsPerInch = 96 TextHeight = 13 @@ -256,6 +258,7 @@ object MiniBrowserFrm: TMiniBrowserFrm OnBeforeDownload = Chromium1BeforeDownload OnDownloadUpdated = Chromium1DownloadUpdated OnAfterCreated = Chromium1AfterCreated + OnBeforeResourceLoad = Chromium1BeforeResourceLoad OnResourceResponse = Chromium1ResourceResponse Left = 32 Top = 224 diff --git a/demos/MiniBrowser/uMiniBrowser.pas b/demos/MiniBrowser/uMiniBrowser.pas index 4a34268f..9750f03d 100644 --- a/demos/MiniBrowser/uMiniBrowser.pas +++ b/demos/MiniBrowser/uMiniBrowser.pas @@ -179,9 +179,16 @@ type procedure Chromium1DownloadUpdated(Sender: TObject; const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); + procedure FormCreate(Sender: TObject); + procedure FormDestroy(Sender: TObject); + procedure Chromium1BeforeResourceLoad(Sender: TObject; + const browser: ICefBrowser; const frame: ICefFrame; + const request: ICefRequest; const callback: ICefRequestCallback; + out Result: TCefReturnValue); protected - FResponse : string; + FResponse : TStringList; + FRequest : TStringList; procedure AddURL(const aURL : string); @@ -192,6 +199,9 @@ type procedure HandleKeyUp(const aMsg : TMsg; var aHandled : boolean); procedure HandleKeyDown(const aMsg : TMsg; var aHandled : boolean); + procedure InspectRequest(const aRequest : ICefRequest); + procedure InspectResponse(const aResponse : ICefResponse); + procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED; procedure ShowDevToolsMsg(var aMessage : TMessage); message MINIBROWSER_SHOWDEVTOOLS; procedure HideDevToolsMsg(var aMessage : TMessage); message MINIBROWSER_HIDEDEVTOOLS; @@ -220,7 +230,7 @@ implementation {$R *.dfm} uses - uPreferences; + uPreferences, uCefStringMultimap, uSimpleTextViewer; procedure TMiniBrowserFrm.BackBtnClick(Sender: TObject); begin @@ -282,7 +292,7 @@ begin model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_JSWRITEDOC, 'Modify HTML document'); model.AddItem(MINIBROWSER_CONTEXTMENU_JSPRINTDOC, 'Print using Javascript'); - model.AddItem(MINIBROWSER_CONTEXTMENU_SHOWRESPONSE, 'Show last server response'); + model.AddItem(MINIBROWSER_CONTEXTMENU_SHOWRESPONSE, 'Show server headers'); if DevTools.Visible then model.AddItem(MINIBROWSER_CONTEXTMENU_HIDEDEVTOOLS, 'Hide DevTools') @@ -338,6 +348,17 @@ begin callback.cont(TempFullPath, False); end; +procedure TMiniBrowserFrm.Chromium1BeforeResourceLoad(Sender: TObject; + const browser: ICefBrowser; const frame: ICefFrame; + const request: ICefRequest; const callback: ICefRequestCallback; + out Result: TCefReturnValue); +begin + Result := RV_CONTINUE; + + if (frame <> nil) and frame.IsMain then + InspectRequest(request); +end; + procedure TMiniBrowserFrm.Chromium1ContextMenuCommand(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; @@ -569,6 +590,52 @@ begin 'Error code : ' + inttostr(result)); end; +procedure TMiniBrowserFrm.InspectRequest(const aRequest : ICefRequest); +var + TempHeaderMap : ICefStringMultimap; + i, j : integer; +begin + if (aRequest <> nil) then + begin + FRequest.Clear; + + TempHeaderMap := TCefStringMultimapOwn.Create; + aRequest.GetHeaderMap(TempHeaderMap); + + i := 0; + j := TempHeaderMap.Size; + + while (i < j) do + begin + FRequest.Add(TempHeaderMap.Key[i] + '=' + TempHeaderMap.Value[i]); + inc(i); + end; + end; +end; + +procedure TMiniBrowserFrm.InspectResponse(const aResponse : ICefResponse); +var + TempHeaderMap : ICefStringMultimap; + i, j : integer; +begin + if (aResponse <> nil) then + begin + FResponse.Clear; + + TempHeaderMap := TCefStringMultimapOwn.Create; + aResponse.GetHeaderMap(TempHeaderMap); + + i := 0; + j := TempHeaderMap.Size; + + while (i < j) do + begin + FResponse.Add(TempHeaderMap.Key[i] + '=' + TempHeaderMap.Value[i]); + inc(i); + end; + end; +end; + procedure TMiniBrowserFrm.Chromium1ResourceResponse(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const response: ICefResponse; @@ -576,15 +643,8 @@ procedure TMiniBrowserFrm.Chromium1ResourceResponse(Sender: TObject; begin Result := False; - if (frame <> nil) and frame.IsMain and (response <> nil) and (request <> nil) then - begin - FResponse := 'URL : ' + request.Url + CRLF + - 'Status : ' + inttostr(response.Status) + CRLF + - 'MimeType : ' + response.MimeType; - - if (request.postdata <> nil) then - FResponse := FResponse + CRLF + 'Post data elements : ' + inttostr(request.postdata.GetCount); - end; + if (frame <> nil) and frame.IsMain then + InspectResponse(response); end; procedure TMiniBrowserFrm.ShowStatusText(const aText : string); @@ -617,6 +677,18 @@ begin caption := 'MiniBrowser'; end; +procedure TMiniBrowserFrm.FormCreate(Sender: TObject); +begin + FResponse := TStringList.Create; + FRequest := TStringList.Create; +end; + +procedure TMiniBrowserFrm.FormDestroy(Sender: TObject); +begin + FResponse.Free; + FRequest.Free; +end; + procedure TMiniBrowserFrm.FormShow(Sender: TObject); begin ShowStatusText('Initializing browser. Please wait...'); @@ -801,7 +873,21 @@ end; procedure TMiniBrowserFrm.ShowResponseMsg(var aMessage : TMessage); begin - if (length(FResponse) > 0) then MessageDlg(FResponse, mtInformation, [mbOk], 0); + SimpleTextViewerFrm.Memo1.Lines.Clear; + + SimpleTextViewerFrm.Memo1.Lines.Add('--------------------------'); + SimpleTextViewerFrm.Memo1.Lines.Add('Request headers : '); + SimpleTextViewerFrm.Memo1.Lines.Add('--------------------------'); + if (FRequest <> nil) then SimpleTextViewerFrm.Memo1.Lines.AddStrings(FRequest); + + SimpleTextViewerFrm.Memo1.Lines.Add(''); + + SimpleTextViewerFrm.Memo1.Lines.Add('--------------------------'); + SimpleTextViewerFrm.Memo1.Lines.Add('Response headers : '); + SimpleTextViewerFrm.Memo1.Lines.Add('--------------------------'); + if (FResponse <> nil) then SimpleTextViewerFrm.Memo1.Lines.AddStrings(FResponse); + + SimpleTextViewerFrm.ShowModal; end; procedure TMiniBrowserFrm.SavePreferencesMsg(var aMessage : TMessage); diff --git a/demos/MiniBrowser/uSimpleTextViewer.dfm b/demos/MiniBrowser/uSimpleTextViewer.dfm new file mode 100644 index 00000000..50ff2fe7 --- /dev/null +++ b/demos/MiniBrowser/uSimpleTextViewer.dfm @@ -0,0 +1,29 @@ +object SimpleTextViewerFrm: TSimpleTextViewerFrm + Left = 0 + Top = 0 + BorderIcons = [biSystemMenu] + BorderStyle = bsSingle + Caption = 'Simple text viewer' + ClientHeight = 572 + ClientWidth = 694 + Color = clBtnFace + Font.Charset = DEFAULT_CHARSET + Font.Color = clWindowText + Font.Height = -11 + Font.Name = 'Tahoma' + Font.Style = [] + OldCreateOrder = False + Position = poScreenCenter + PixelsPerInch = 96 + TextHeight = 13 + object Memo1: TMemo + Left = 0 + Top = 0 + Width = 694 + Height = 572 + Align = alClient + ReadOnly = True + ScrollBars = ssBoth + TabOrder = 0 + end +end diff --git a/demos/MiniBrowser/uSimpleTextViewer.pas b/demos/MiniBrowser/uSimpleTextViewer.pas new file mode 100644 index 00000000..35ff5c5e --- /dev/null +++ b/demos/MiniBrowser/uSimpleTextViewer.pas @@ -0,0 +1,31 @@ +unit uSimpleTextViewer; + +interface + +uses + {$IFDEF DELPHI16_UP} + Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, + Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; + {$ELSE} + Windows, Messages, SysUtils, Variants, Classes, Graphics, + Controls, Forms, Dialogs, StdCtrls; + {$ENDIF} + + +type + TSimpleTextViewerFrm = class(TForm) + Memo1: TMemo; + private + { Private declarations } + public + { Public declarations } + end; + +var + SimpleTextViewerFrm: TSimpleTextViewerFrm; + +implementation + +{$R *.dfm} + +end.