Unit OSM.NetworkRequest

Description

Generic (no real network implementation) classes and declarations for requesting OSM tile images from network. Real network function from any framework must be supplied to actually execute request.

DEFINES:

(c) Fr0sT-Brutal https://github.com/Fr0sT-Brutal/Delphi_OSMMap

Overview

Classes, Interfaces, Objects and Records

Name Description
Class THttpRequestProps Generic properties of request.
Class TNetworkRequestQueue Queuer of network requests.

Functions and Procedures

procedure CheckEngineCap(NeededCap: THttpRequestCapability; Caps: THttpRequestCapabilities);
procedure CheckEngineCaps(RequestProps: THttpRequestProps; EngineCapabilities: THttpRequestCapabilities);
function IsHTTPError(ResponseCode: Word): Boolean;
procedure CheckHTTPError(ResponseCode: Word; const ResponseText: string);
function GetSystemProxy(const URL: string; out Bypass: TArray<string>): string; overload;
function GetSystemProxy(const URL: string): string; overload;

Types

THttpRequestCapability = (...);
THttpRequestCapabilities = set of THttpRequestCapability;
TNetworkClient = TObject;
TBlockingNetworkRequestProc = procedure (RequestProps: THttpRequestProps; ResponseStm: TStream; var Client: TNetworkClient);
TGotTileCallbackBgThr = procedure (const Tile: TTile; Ms: TMemoryStream; const Error: string) of object;

Constants

HTTPProto = 'http';
HTTPTLSProto = 'https';
ProtoSep = '://';
HTTPProxyProto = HTTPProto + ProtoSep;
SystemProxy = HTTPProxyProto + 'SYSTEM';
ReqTimeout = 5*MSecsPerSec;
SampleUserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0';
SampleHeaders: array[0..2] of string = ( 'User-Agent: ' + SampleUserAgent, 'Accept-Language: en-US;q=0.7,en;q=0.3', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' );

Description

Functions and Procedures

procedure CheckEngineCap(NeededCap: THttpRequestCapability; Caps: THttpRequestCapabilities);

Check if capability is in set of capabilities and raise exception if not

procedure CheckEngineCaps(RequestProps: THttpRequestProps; EngineCapabilities: THttpRequestCapabilities);

Check if a network engine is capable of handling all the request properties. Checks for: htcProxy, htcSystemProxy, htcProxyAuth, htcAuth, htcAuthURL, htcTLS, htcHeaders.

function IsHTTPError(ResponseCode: Word): Boolean;

Return true if response code means HTTP error

procedure CheckHTTPError(ResponseCode: Word; const ResponseText: string);

Check if response code means HTTP error, raise exception then

function GetSystemProxy(const URL: string; out Bypass: TArray<string>): string; overload;

Retrieve system proxy for given URL (http, https). Only returns HTTP proxies (not Socks). On Windows uses InternetQueryOption, on Linux takes env variables HTTP(S)_PROXY. Returns addresses for direct connection in Bypass array.

function GetSystemProxy(const URL: string): string; overload;

Overload when addresses to bypass proxy are not of interest

Types

THttpRequestCapability = (...);

Capabilities that a network engine has

Values
  • htcProxy: Support HTTP proxy
  • htcDirect: Support direct connect bypassing OS-wide proxy. In fact, only WinInet-based engines (WinInet, RTL in Windows) use OS-wide proxy by default. In other engines and in Linux the proxy must be set explicitly so this cap is actual for all engines.
  • htcSystemProxy: Can use OS-wide proxy
  • htcProxyAuth: Support auth to proxy defined in URL
  • htcAuth: Support auth to host
  • htcAuthURL: Support auth to host defined in URL
  • htcHeaders: Support sending custom headers
  • htcTimeout: Support request timeout
  • htcTLS: Support HTTPS
THttpRequestCapabilities = set of THttpRequestCapability;

This item has no description.

TNetworkClient = TObject;

Base class for network client object. Used when request queue is performed through the same connection. Destructor of the object is called when the queue is empty and it must free all allocated resources.

TBlockingNetworkRequestProc = procedure (RequestProps: THttpRequestProps; ResponseStm: TStream; var Client: TNetworkClient);

Generic type of blocking network request procedure. Procedure must:

  • Ensure URL requisites have priority over field requisites

  • Set timeouts for request to ReqTimeout

  • Raise exception on validation/connection/request error

  • Free all resources it has allocated

Parameters
RequestProps
- all details regarding a request
ResponseStm
- stream that accepts response data
Client
- [IN/OUT] If the engine supports multiple requests inside the same client, this parameter is the current client object. Request properties are supposed to remain unchanged throughout the whole queue (only URL changes) so it's enough to assign them at client creation only
IN: client object to use for requests.
OUT: newly created client object if Client was Nil at input.
Exceptions raised
exception
on error
TGotTileCallbackBgThr = procedure (const Tile: TTile; Ms: TMemoryStream; const Error: string) of object;

Generic type of method to call when request is completed
! Called from the context of a background thread !

Parameters
Tile
- tile that has been received
Ms
- stream with tile image data. In case of error, it could be Nil even if Error is empty
Error
- error description if any

Constants

HTTPProto = 'http';

This item has no description.

HTTPTLSProto = 'https';

This item has no description.

ProtoSep = '://';

This item has no description.

HTTPProxyProto = HTTPProto + ProtoSep;

Prefix to add to proxy URLs if it only contains host:port - some URL parsers handle such inputs as proto:path

SystemProxy = HTTPProxyProto + 'SYSTEM';

Internal constant to designate OS-wide proxy

ReqTimeout = 5*MSecsPerSec;

Timeout for connect and request

SampleUserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0';

This item has no description.

SampleHeaders: array[0..2] of string = ( 'User-Agent: ' + SampleUserAgent, 'Accept-Language: en-US;q=0.7,en;q=0.3', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' );

Headers that you could add to TNetworkRequestQueue.RequestProps.HeaderLines. F.ex., openstreetmap.org dislikes requests without user-agent.

Author


Generated by PasDoc 0.16.0.