function HTMLCallback(event)
% HTMLCallback - Callback called by modelit.webserver.Server object for generating a
% HTML response.
%
% CALL:
%     HTMLCallback(event)
%
% INPUT:
%     event: <modelit.webserver.HttpExchange>
%         with the request data and methods to generate a response.
%
% OUTPUT:
%     No output, a response with an HTML text is return to the client.
%
% EXAMPLE:
%  server = modelit.web.server.Server('localhost', 8081, @HTMLCallback).start()
%
%  % Open a webbrowser and type: http://localhost:8081 in the address bar
%  % Now an html message appears in the browser.

% Copyright 2020 Modelit, www.modelit.nl

% Generate the response using the event send by the server
response = HTML(event);

% Set the MIME type
% See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
% Allow Cross Origin Resource Sharing
event.addResponseHeader('Content-Type', 'text/html',...
                        'Access-Control-Allow-Origin', '*');


% Send the response back to the client, with statuscode 200 (OK)
% See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
event.send(200, response);

%__________________________________________________________________________
function text = HTML(event)
% HTML - Make a simple HTML string using the HttpExchange methods
%
% CALL:
%  text = HTML(event)
%
% INPUT:
%     event:
%       <modelit.web.server.HttpExchange> with request details and methods
%       to send the response back to the client.
%
% OUTPUT:
%     text:
%       <string> with an html string

text = {'<html><center>'
    sprintf('<h1>%s</h1>',datestr(now))
    sprintf('<h2>Method: HTTP-%s</h2>',event.getRequestMethod)
    sprintf('<h2>Query: %s%s</h2>', event.getPath, urldecode_utf(event.getQueryString)) %this file requires ##urldecode_utf##
    sprintf('<h2>From: %s</h2>',event.getRemoteAddress)
    sprintf('<h2>To: %s</h2>',event.getLocalAddress)
    '</center></html>'};

text = [text{:}];