mormot对http.sys的封装
windows 2003,xp sp2以上版本开始提供http.sys通讯。
windows为http.sys通讯提供httpapp.dll动态库给外部程序调用。mormot也是调用它。
procedure HttpApiInitialize; var api: THttpAPIs; P: PPointer; begin if Http.Module<>0 then exit; // already loaded mormot.core.os.GlobalLock; try // if Http.Module<>0 then // by cxg try Http.Module := LoadLibrary(HTTPAPI_DLL); //加载 httpapp.dll Http.Version.MajorVersion := 2; // API 2.0 if all functions are available if Http.Module<=255 then raise EHttpApiServer.CreateFmt('Unable to find %s',[HTTPAPI_DLL]); P := @@Http.Initialize; for api := low(api) to high(api) do begin P^ := GetProcAddress(Http.Module,HttpNames[api]); if P^=nil then if api<hHttpApi2First then raise EHttpApiServer.CreateFmt('Unable to find %s() in %s',[HttpNames[api],HTTPAPI_DLL]) else Http.Version.MajorVersion := 1; // e.g. Windows XP or Server 2003 inc(P); end; except on E: Exception do begin if Http.Module>255 then begin FreeLibrary(Http.Module); Http.Module := 0; end; raise; end; end; finally mormot.core.os.GlobalUnlock; end; end;
/// direct late-binding access to the HTTP API server 1.0 or 2.0 THttpAPI = packed record /// access to the httpapi.dll loaded library Module: THandle; /// will be either 1.0 or 2.0, depending on the published .dll functions Version: HTTP_VERSION; /// The HttpInitialize function initializes the HTTP Server API driver, starts it, // if it has not already been started, and allocates data structures for the // calling application to support response-queue creation and other operations. // Call this function before calling any other functions in the HTTP Server API. Initialize: function(Version: HTTP_VERSION; Flags: cardinal; pReserved: pointer = nil): HRESULT; stdcall; /// The HttpTerminate function cleans up resources used by the HTTP Server API // to process calls by an application. An application should call HttpTerminate // once for every time it called HttpInitialize, with matching flag settings. Terminate: function(Flags: cardinal; Reserved: integer = 0): HRESULT; stdcall; /// The HttpCreateHttpHandle function creates an HTTP request queue for the // calling application and returns a handle to it. CreateHttpHandle: function(var ReqQueueHandle: THandle; Reserved: integer = 0): HRESULT; stdcall; /// The HttpAddUrl function registers a given URL so that requests that match // it are routed to a specified HTTP Server API request queue. An application // can register multiple URLs to a single request queue using repeated calls to // HttpAddUrl // - a typical url prefix is 'http://+:80/vroot/', 'https://+:80/vroot/' or // 'https://adatum.com:443/secure/database/' - here the '+' is called a // Strong wildcard, i.e. will match every IP or server name AddUrl: function(ReqQueueHandle: THandle; UrlPrefix: PWideChar; Reserved: integer = 0): HRESULT; stdcall; /// Unregisters a specified URL, so that requests for it are no longer // routed to a specified queue. RemoveUrl: function(ReqQueueHandle: THandle; UrlPrefix: PWideChar): HRESULT; stdcall; /// retrieves the next available HTTP request from the specified request queue ReceiveHttpRequest: function(ReqQueueHandle: THandle; RequestId: HTTP_REQUEST_ID; Flags: cardinal; var pRequestBuffer: HTTP_REQUEST; RequestBufferLength: ULONG; var pBytesReceived: ULONG; pOverlapped: pointer = nil): HRESULT; stdcall; /// sent the response to a specified HTTP request // - pLogData optional parameter is handled since HTTP API 2.0 SendHttpResponse: function(ReqQueueHandle: THandle; RequestId: HTTP_REQUEST_ID; Flags: integer; var pHttpResponse: HTTP_RESPONSE; pReserved1: pointer; var pBytesSent: cardinal; pReserved2: pointer = nil; Reserved3: ULONG = 0; pOverlapped: pointer = nil; pLogData: PHTTP_LOG_DATA = nil): HRESULT; stdcall; /// receives additional entity body data for a specified HTTP request ReceiveRequestEntityBody: function(ReqQueueHandle: THandle; RequestId: HTTP_REQUEST_ID; Flags: ULONG; pBuffer: pointer; BufferLength: cardinal; var pBytesReceived: cardinal; pOverlapped: pointer = nil): HRESULT; stdcall; /// sends entity-body data associated with an HTTP response. SendResponseEntityBody: function(ReqQueueHandle: THandle; RequestId: HTTP_REQUEST_ID; Flags: integer; EntityChunkCount: word; pEntityChunks: pointer; var pBytesSent: Cardinal; pReserved1: Pointer = nil; pReserved2: Pointer = nil; pOverlapped: POverlapped = nil; pLogData: PHTTP_LOG_DATA = nil): HRESULT; stdcall; /// set specified data, such as IP addresses or SSL Certificates, from the // HTTP Server API configuration store SetServiceConfiguration: function(ServiceHandle: THandle; ConfigId: THttpServiceConfigID; pConfigInformation: pointer; ConfigInformationLength: ULONG; pOverlapped: pointer = nil): HRESULT; stdcall; /// deletes specified data, such as IP addresses or SSL Certificates, from the // HTTP Server API configuration store DeleteServiceConfiguration: function(ServiceHandle: THandle; ConfigId: THttpServiceConfigID; pConfigInformation: pointer; ConfigInformationLength: ULONG; pOverlapped: pointer = nil): HRESULT; stdcall; /// removes from the HTTP Server API cache associated with a given request // queue all response fragments that have a name whose site portion matches // a specified UrlPrefix FlushResponseCache: function(ReqQueueHandle: THandle; pUrlPrefix: PWideChar; Flags: ULONG; pOverlapped: POverlapped): ULONG; stdcall; /// cancels a specified request // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) CancelHttpRequest: function(ReqQueueHandle: THandle; RequestId: HTTP_REQUEST_ID; pOverlapped: pointer = nil): HRESULT; stdcall; /// creates a server session for the specified HTTP API version // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) CreateServerSession: function(Version: HTTP_VERSION; var ServerSessionId: HTTP_SERVER_SESSION_ID; Reserved: ULONG = 0): HRESULT; stdcall; /// deletes the server session identified by the server session ID // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) CloseServerSession: function(ServerSessionId: HTTP_SERVER_SESSION_ID): HRESULT; stdcall; /// creates a new request queue or opens an existing request queue // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) // - replaces the HTTP version 1.0 CreateHttpHandle() function CreateRequestQueue: function(Version: HTTP_VERSION; pName: PWideChar; pSecurityAttributes: Pointer; Flags: ULONG; var ReqQueueHandle: THandle): HRESULT; stdcall; /// sets a new server session property or modifies an existing property // on the specified server session // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) SetServerSessionProperty: function(ServerSessionId: HTTP_SERVER_SESSION_ID; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG): HRESULT; stdcall; /// queries a server property on the specified server session // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) QueryServerSessionProperty: function(ServerSessionId: HTTP_SERVER_SESSION_ID; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG; pReturnLength: PULONG = nil): HRESULT; stdcall; /// creates a URL Group under the specified server session // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) CreateUrlGroup: function(ServerSessionId: HTTP_SERVER_SESSION_ID; var UrlGroupId: HTTP_URL_GROUP_ID; Reserved: ULONG = 0): HRESULT; stdcall; /// closes the URL Group identified by the URL Group ID // - this call also removes all of the URLs that are associated with // the URL Group // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) CloseUrlGroup: function(UrlGroupId: HTTP_URL_GROUP_ID): HRESULT; stdcall; /// adds the specified URL to the URL Group identified by the URL Group ID // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) // - this function replaces the HTTP version 1.0 AddUrl() function AddUrlToUrlGroup: function(UrlGroupId: HTTP_URL_GROUP_ID; pFullyQualifiedUrl: PWideChar; UrlContext: HTTP_URL_CONTEXT = 0; Reserved: ULONG = 0): HRESULT; stdcall; /// removes the specified URL from the group identified by the URL Group ID // - this function removes one, or all, of the URLs from the group // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) // - it replaces the HTTP version 1.0 RemoveUrl() function RemoveUrlFromUrlGroup: function(UrlGroupId: HTTP_URL_GROUP_ID; pFullyQualifiedUrl: PWideChar; Flags: ULONG): HRESULT; stdcall; /// sets a new property or modifies an existing property on the specified // URL Group // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) SetUrlGroupProperty: function(UrlGroupId: HTTP_URL_GROUP_ID; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG): HRESULT; stdcall; /// queries a property on the specified URL Group // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) QueryUrlGroupProperty: function(UrlGroupId: HTTP_URL_GROUP_ID; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG; pReturnLength: PULONG = nil): HRESULT; stdcall; /// sets a new property or modifies an existing property on the request // queue identified by the specified handle // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) SetRequestQueueProperty: function(ReqQueueHandle: THandle; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG; Reserved: ULONG; pReserved: Pointer): HRESULT; stdcall; /// queries a property of the request queue identified by the // specified handle // - available only for HTTP API 2.0 (since Windows Vista / Server 2008) QueryRequestQueueProperty: function(ReqQueueHandle: THandle; aProperty: HTTP_SERVER_PROPERTY; pPropertyInformation: Pointer; PropertyInformationLength: ULONG; Reserved: ULONG; pReturnLength: PULONG; pReserved: Pointer): HRESULT; stdcall; end;