marcovaldo 1 Posted January 10, 2023 Hi All, Beside using MARS as a REST server using JSON interface I also want to serve a few plain HTML pages used for status display with browsers. There shall be a table returned containing text and if possible images/icons beside that like Device1 online [an image] Device2 offline [ an other image] Is there any example how to do this? The images are shown as broken in browser. Does this need multiple endpoints to serve ? (One for the text and then one extra for every image, as the browser will do several HTTP connections to retrieve them) I am using Indy in a windows service (alternatively startable via /GUI as application) Kind Greetings, M. Share this post Link to post
Attila Kovacs 631 Posted January 10, 2023 (edited) of course, tons of https://www.w3schools.com/html/html_tables.asp https://www.w3schools.com/html/html_images.asp 37 minutes ago, marcovaldo said: Does this need multiple endpoints to serve ? No, you have to serve the requested file. https://www.rfc-editor.org/rfc/rfc2616 9.3 GET Indy should have already implemented that for you. Edited January 10, 2023 by Attila Kovacs Share this post Link to post
marcovaldo 1 Posted January 11, 2023 Many Thanks Attila, for your quick help! I am trying to play with the Bootstrap Server example (as I can edit html without compile) In Mars this page is served as follows function THelloWorldResource.SayHelloWorld: string; var LStreamReader: TStreamReader; begin LStreamReader := TStreamReader.Create('..\www\helloworld.html', TEncoding.UTF8); try Result := LStreamReader.ReadToEnd; finally LStreamReader.Free; end; end; What works: if the IMG is served from external server What doesn't work: if the IMG is referenced to local filesystem (location of binary) i thought that WEB root is application folder (maybee I am wrong?) Just try to find something in the web about this .... Greetings! Share this post Link to post
marcovaldo 1 Posted January 11, 2023 Fixed: 1) Created a separate handler in MARS for images (similar to MARSContentType Demo): [GET, Path('/img/{*}'), Produces('image/jpg')] function ServerImg([PathParam('*')] AllStrings: string): TStream; and function TDominoServerResource.ServerImg([PathParam('*')] AllStrings: string): TStream; begin Result := TFileStream.Create('Sample.jpg', fmOpenRead or fmShareDenyWrite); end; Note: This sample serves only one fixed image to all requests. AllStrings needs to be processed still... 2) In HTML I had to take care not only on HTML SRC path but also on MARS/REST path <img src="/rest/default/myservername/img/Sample.jpg"> Waterfall mode of Chrome Browser is your friend! (as it showed what is requested by browser) Greetings to All, M. Share this post Link to post
Attila Kovacs 631 Posted January 11, 2023 Ahh I see now! This is great. You can decorate the handler method in mars and that's it? Share this post Link to post
marcovaldo 1 Posted January 11, 2023 The slightly extended version of the handler now sends differnt images from [Application Bin folder]\img folder function TDominoServerResource.ServerImg([PathParam('*')] AllStrings: string): TStream; begin //DebLog.DebOut('ImageRequest:'+AllStrings+'*'); //AllStrings contains plain image filename Result := TFileStream.Create('img\'+AllStrings, fmOpenRead or fmShareDenyWrite); end; Why I wanted that MARS is also serving HTML pages for browser - Status Display - Setup functions (as my app normally is running as service) My old application used a telnet connection for that purpose but I bet that customer IT is nowadays unhappy having an open telnet connection on a productive server. The http connection can be protected by various auth methods.. Greetings Share this post Link to post
Attila Kovacs 631 Posted January 11, 2023 Make sure you are not exposing more that you want. Make a root directory where your server not going below that dir. One could request with /../ .... files you don't want to expose. Share this post Link to post
marcovaldo 1 Posted January 11, 2023 Attila, thank you for advice! Any advice that this is very welcome, as security is always a very important aspect of IT... Share this post Link to post