根据 Rob 对这个问题的评论,我很快意识到这很简单。使用字符串列表,将路径信息分解为不同的列表项,划分每个斜线。然后,使用任何自定义机制来评估第一个,然后是第二个,依此类推。每个变化都会触发不同的处理程序,就像这样......
unit uUpdateServerWebModule;
interface
uses System.SysUtils, System.Classes, Web.HTTPApp, Update.Common;
type
TWebModule1 = class(TWebModule)
procedure WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
procedure HandleVersionCheck(Request: TWebRequest; Response: TWebResponse;
PathInfo: TStringList);
procedure HandleInfoCheck(Request: TWebRequest; Response: TWebResponse;
PathInfo: TStringList);
procedure HandleException(Request: TWebRequest; Response: TWebResponse;
PathInfo: TStringList; const Code: Integer);
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TWebModule1;
implementation
{$R *.dfm}
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
L: TStringList;
S, T: String;
P: Integer;
procedure A(const Text: String);
begin
S:= S + Text + sLineBreak;
end;
begin
//Break down URL by slashes into list
L:= TStringList.Create;
try
S:= Request.PathInfo;
if Copy(S, Length(S)-1, 1) <> '/' then
S:= S + '/';
Delete(S, 1, 1);
while Length(S) > 0 do begin
P:= Pos('/', S);
T:= Copy(S, 1, P-1);
Delete(S, 1, P);
L.Append(T);
end;
if L.Count > 0 then begin
//Handle path info list
if LowerCase(L[0]) = 'ver' then begin
HandleVersionCheck(Request, Response, L);
end else
if LowerCase(L[0]) = 'info' then begin
HandleInfoCheck(Request, Response, L);
end else begin
HandleException(Request, Response, L, 0);
end;
end else begin
//No page specified, return default page
Response.Content:= 'Application Update Server';
end;
finally
L.Free;
end;
end;
procedure TWebModule1.HandleVersionCheck(Request: TWebRequest;
Response: TWebResponse; PathInfo: TStringList);
var
EC: String;
FN: String;
Ver: TVersion;
begin
if LowerCase(PathInfo[1]) = 'info' then begin
//Next parameter: Edition Code
EC:= LowerCase(PathInfo[2]);
if (EC = 'ml') or (EC = 'sl') or (EC = 'lt') then begin
//Return current version info for specified software edition...
end else begin
HandleException(Request, Response, PathInfo, 4);
end;
end else begin
//Next parameter: Version Number(s)
Ver:= TVersion.Create;
try
Ver.Version:= PathInfo[1];
if (Ver.Ver1 > 0) and (Ver.Ver2 > 0) then begin
//Next parameter: Edition Code
EC:= LowerCase(PathInfo[2]);
if (EC = 'ml') or (EC = 'sl') or (EC = 'lt') then begin
//Next parameter: Version/Edition Commands
if LowerCase(PathInfo[3]) = 'update' then begin
//Return stream of full verion update installer app...
Response.ContentType:= 'application/octet-stream';
end else
if LowerCase(PathInfo[3]) = 'files' then begin
//Next parameter: Specific filename
FN:= PathInfo[4];
//Return stream of specific file...
Response.ContentType:= 'application/octet-stream';
end else begin
HandleException(Request, Response, PathInfo, 1);
end;
end else begin
HandleException(Request, Response, PathInfo, 2);
end;
end else begin
HandleException(Request, Response, PathInfo, 3);
end;
finally
Ver.Free;
end;
end;
end;
procedure TWebModule1.HandleInfoCheck(Request: TWebRequest;
Response: TWebResponse; PathInfo: TStringList);
begin
//Return information about all software and their current versions...
end;
procedure TWebModule1.HandleException(Request: TWebRequest;
Response: TWebResponse; PathInfo: TStringList; const Code: Integer);
begin
//Return error information...
Response.Content:= 'EXCEPTION: '+IntToStr(Code);
end;
end.
那就是Web Module的完整代码(主要逻辑)
主要处理是从默认处理程序完成的。基本上所有传入的请求都是通过这个WebModule1DefaultHandlerAction
处理每个传入请求的过程来处理的。从这里,它首先确定是否有任何指定的路径。如果没有,它只会返回主登录页面。否则,它将首先读取列出的第一个路径,并根据请求的名称调用另一个处理程序。在这种情况下,它会处理versions
请求并进一步下载SomeFile.exe
要保存在特定目标中的特定文件 ( )。