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
Use TStaticModule to serve static files from a Sparkle server, pretty much like a regular static web server.
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;
The local directory to serve from can be passed to the constructor or set later through TStaticModule.RootDir.
Serving index files
TStaticModule.IndexFiles lists the file names the module looks for when the requested address is a directory rather than a specific file. By default it contains "index.html" and "index.htm". So 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 looks for a file matching any entry in TStaticModule.IndexFiles and, if found, returns it. If no file is found, or the list is empty, a not found (404) error is returned.
Trailing slash handling
Use TStaticModule.FolderUrlMode to control how the module treats requests to folder URLs with or without a trailing slash (for example, "<server>/foldername" versus "<server>/foldername/"). The options of TFolderUrlMode are:
TFolderUrlMode.RedirectToSlash: requests without a trailing slash are 301-redirected to the URL with a trailing slash. This is the default behavior.
TFolderUrlMode.RedirectFromSlash: requests with a trailing slash are 301-redirected to the URL without a trailing slash.
TFolderUrlMode.NoRedirect: no redirect is performed.
TAnonymousServerModule
Use TAnonymousServerModule to perform raw, low-level HTTP request processing in a direct way, without having to inherit a new class from THttpServerModule and override its TBaseHttpServerModule.ProcessRequest method.
The following example responds with HTTP status code 200 to all requests and sends back the same content received.
uses
{...}, Sparkle.HttpServer.Module;
begin
Server.AddModule(TAnonymousServerModule.Create(
'http://+:2001/tms/echo',
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;