Jump to content
Sign in to follow this  
marcovaldo

Serving html page containing images

Recommended Posts

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

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 by Attila Kovacs

Share this post


Link to post

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

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×