Built-in Modules
Sparkle is based in a server module system. You can build your own Sparkle modules by processing the requests manually. There are also big Sparkle module implementations which are complex enough to be separate frameworks themselves, like TMS RemoteDB and TMS XData.
And in TMS Sparkle itself there are some modules that are available and ready-to-use for several purposes. Below you will find the list of available Sparkle modules.
TStaticModule
You can use TStaticModule to serve static files from a Sparkle server,
pretty much like a regular static web server. It's declared in unit
Sparkle.Module.Static
.
The following example will serve all files under directory "C:\myfiles" at the address "http://<server>:2001/tms/files".
uses
{...}, Sparkle.Module.Static;
var
Module: TStaticModule;
begin
Module := TStaticModule.Create('http://+:2001/tms/files', 'C:\myfiles');
Server.AddModule(Module);
end;
Constructors
Name | Description |
---|---|
constructor Create(const ABaseUri: string) | Creates the static module that handle requests for the specified ABaseUri address. |
constructor Create(const ABaseUri, ARootDir: string) | Creates the static module that handle requests for the specified ABaseUri address, and servers files from the ARootDir directory. |
Properties
Name | Description |
---|---|
RootDir: string | The base local directory where files will be served from. |
IndexFiles: TStrings | A list of files names that would be displayed (if present) when the requested address is the directory itself. By default, this property includes "index.html" and "index.htm". This means that whenever the client requests an url with no file name (for example, "http://localhost:2001/tms/files" or "http://localhost:2001/tms/files/subdir", the module will look for any file with the same name as any entry in IndexFiels property. If found, the file will be returned to the client. If no file is found or IndexFiles is empty, a not found (404) error will be returned. |
LastModified: Boolean | If True, server will add a Last-Modifed header in response with the date/time when the provided file was last modified, locally. Can be used by clients and browsers for cached responses. It's true by default. |
property FolderUrlMode: TFolderUrlMode | Indicates how static module should treat requests to URL addresses (that represent folders, not files) with trailing slash or without trailing slash (for example, "<server>/foldername" and "<server>/foldername/". See below the options for this property. |
TFolderUrlMode
TFolderUrlMode.RedirectToSlash: All requests without a trailing slash in URL will 301 redirect to the URL with trailing slash. This is default behavior.
TFolderUrlMode.RedirectFromSlash: All requests with trailing slash in URL will 301 redirect to the URL without trailing slash.
TFolderUrlMode.NoRedirect: No redirect will be performed.
TAnonymousServerModule
You can use TAnonymousServerModule to perform raw, low-level HTTP
request processing, in a direct way without having to inherit a new
class from THttpServerModule and overriding ProcessRequest method. It's
declared in unit Sparkle.HttpServer.Module
.
The following example will respond with HTTP status code 200 to all requests and send back the same content received.
uses
{...}, Sparkle.HttpServer.Module;
begin
Server.AddModule(TAnonymousServerModule.Create(
procedure(const C: THttpServerContext)
begin
C.Response.StatusCode := 200;
C.Response.ContentType := C.Request.Headers.Get('content-type');
C.Response.ContentLength := Length(C.Request.Content);
C.Response.Content.Write(C.Request.Content[0], C.Response.ContentLength);
end;
));
end;
Constructors
Name | Description |
---|---|
constructor Create(const ABaseUri, AProc: TRequestProc) | Creates the static module that handle requests for the specified ABaseUri address, and servers files from the ARootDir directory. |