1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-08-13 16:53:59 -04:00
Simba/trunk/Units/MMLAddon/internets.pas
Raymond 87c496ca85 Fixed issue with settings file being saved in the wrong directory.
Simba now has some parameters (-o/--open to open a file and -r/--run to run that file at startup).
Simba now opens a file correctly if you double click an associated .simb file.
Initalized several TPA-functions-results.. (BoxFromTPA could return random values when sending an empty TPA).
Opening/Creating a 'script' will now happen in a new tab (unless the current tab is the same as the default script)
Restructured the internets unit (+ implemented all of the HTTP-client stuff).
And updated the Synapse units.


git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@538 3f818213-9676-44b0-a9b4-5e4c4e03d09d
2010-02-13 16:56:46 +00:00

187 lines
4.6 KiB
ObjectPascal

unit internets;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,httpsend;
function GetPage(URL: String): String;
type
{ THTTPClient }
THTTPClient = class(TObject)
private
HTTPSend : THTTPSend;
fHandleCookies : boolean;
PostVariables : TStringList;
public
procedure SetHTTPUserAgent(agent : string);
function GetHTTPPage(url : string ) : string;
function PostHTTPPage(Url: string; PostData: string): string;overload;
function PostHTTPPage(Url: string): string;overload;
function GetRawHeaders: string;
procedure ClearPostData;
procedure AddPostVariable(VarName, VarValue: string);
constructor Create(HandleCookies : boolean = true);
destructor Destroy;override;
end;
{ TMInternet }
TMInternet = class(TObject)
protected
Client : TObject;
Connections : TList;
HTTPClients : TList;
public
function CreateHTTPClient(HandleCookies : boolean = true) : integer;
function GetHTTPClient(Index : integer) : THTTPClient;
procedure FreeHTTPClient(Index: Integer);
constructor Create(Owner : TObject);
destructor Destroy;override;
end;
implementation
uses
synacode;
{ OTHER }
function GetPage(URL: String): String;
var
s: TStringList;
begin
s:=TStringList.Create;
HttpGetText(URL, s);
result := s.Text;
s.Free;
end;
{ TMInternet }
function TMInternet.CreateHTTPClient(HandleCookies: boolean = true): integer;
begin;
Result := HTTPClients.Add(THTTPClient.Create(HandleCookies));
end;
function TMInternet.GetHTTPClient(Index: integer): THTTPClient;
begin
if (index < 0) or (index >= HTTPClients.Count) then
raise exception.CreateFmt('GetHTTPClient: Trying to acces an index(%d) that is out of range',[index]);
if HTTPClients[index] = nil then
raise exception.CreateFmt('GetHTTPClient: Trying to acces an index(%d) that is freed',[index]);
result := THTTPClient(httpclients[index]);
end;
procedure TMInternet.FreeHTTPClient(Index: Integer);
begin
if (index < 0) or (index >= HTTPClients.Count) then
raise exception.CreateFmt('FreeHTTPClient: Trying to free an index(%d) that is out of range',[index]);
if HTTPClients[index] = nil then
raise exception.CreateFmt('FreeHTTPClient: Trying to free an index(%d) that is already freed',[index]);
THTTPClient(HTTPClients[index]).Free;
end;
constructor TMInternet.Create(Owner: TObject);
begin
inherited Create;
client := Owner;
Connections := TList.Create;
HTTPClients := TList.Create;
end;
destructor TMInternet.Destroy;
var
i : integer;
begin
for i := Connections.Count -1 downto 0 do
if Connections[i] <> nil then
TObject(Connections[i]).Free;
for i := HTTPClients.Count -1 downto 0 do
if HTTPClients[i] <> nil then
THTTPClient(HTTPClients[i]).Free;
Connections.Free;
HTTPClients.Free;
inherited Destroy;
end;
{ THTTPClient }
procedure THTTPClient.SetHTTPUserAgent(agent: string);
begin
HTTPSend.UserAgent := agent;
end;
function THTTPClient.GetHTTPPage(url: string): string;
begin
if not fHandleCookies then
HTTPSend.Cookies.Clear;
HTTPSend.MimeType := 'text/html';
if HTTPSend.HTTPMethod('GET',url) then
begin;
SetLength(result,HTTPSend.Document.Size);
HTTPSend.Document.Read(result[1],length(result));
end else
result := '';
end;
function THTTPClient.PostHTTPPage(Url: string; PostData: string): string;
begin
HTTPSend.MimeType := 'application/x-www-form-urlencoded';
HTTPSend.Document.Clear;
HTTPSend.Document.Write(Postdata[1],length(postdata));
if HTTPSend.HTTPMethod('POST',url) then
begin;
SetLength(result,HTTPSend.Document.Size);
HTTPSend.Document.Read(result[1],Length(result));
end else
result := '';
end;
function THTTPClient.PostHTTPPage(Url: string): string;
var
PostData : string;
i : integer;
begin
PostData := '';
for i := 0 to PostVariables.Count - 1 do
PostData := PostData + PostVariables[i] +'&';
if Length(PostData) > 1 then
setlength(postdata,length(postdata) - 1); //Wipe away that last &
result := PostHTTPPage(url,postdata);
end;
function THTTPClient.GetRawHeaders: string;
begin
Result := HTTPSend.Headers.Text;
end;
procedure THTTPClient.ClearPostData;
begin
PostVariables.Clear;
end;
procedure THTTPClient.AddPostVariable(VarName, VarValue: string);
begin
PostVariables.Add(Varname + '=' + VarValue);
end;
constructor THTTPClient.Create(HandleCookies : boolean = true);
begin
inherited Create;
HTTPSend := THTTPSend.Create;
fHandleCookies:= HandleCookies;
PostVariables := TStringList.Create;
end;
destructor THTTPClient.Destroy;
begin
HTTPSend.Free;
PostVariables.Free;
inherited Destroy;
end;
end.