Recent

Author Topic: Respond with HTML (AResponse.LoadFromFile) CSS missed  (Read 10852 times)

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Respond with HTML (AResponse.LoadFromFile) CSS missed
« on: October 31, 2014, 02:10:19 pm »
Linux - Apache - fcl-web - cgi
I made a cgi program and as response I like to show a HTML document.
Here is the Request answer procedure:
 procedure TGenees_show.geef_lijstRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; var Handled: Boolean);
begin
  AResponse.ContentType := 'text/html;charset=utf-8';
  AResponse.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'public_html/tekst.html');
    Handled:= TRUE;
end;

In tekst.html I have a link to the css files (placed in the same directory as the html)
<link rel="stylesheet" href="normalize.min.css">
        <link rel="stylesheet" href="main.css">

Unfortunately the HTML is displayed in the FieFox browser without style.
How can I get the browser to use the stylesheet?


Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #1 on: October 31, 2014, 02:34:47 pm »
I found an un-elegant solution by adding the stylesheets between <style and </style> in my HTML file.
If someone knows a better solution. Please, let me know

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #2 on: October 31, 2014, 03:31:48 pm »
You don't list an url for the css, so firefox will probably do a request to the pathof(url)+stylesheet name. if that fails, no css is shown.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #3 on: October 31, 2014, 06:05:46 pm »
View source and click the css link, where does it go? Have you configured the server to serve the file?

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #4 on: October 31, 2014, 07:55:28 pm »
Should I place 2 Aresponse in the procedure?? like:
procedure TGenees_show.geef_lijstRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; var Handled: Boolean);
begin
 AResponse.ContentType := 'text/css;charset=utf-8';
  AResponse.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'public_html/main.css');
  AResponse.ContentType := 'text/html;charset=utf-8';
  AResponse.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'public_html/tekst.html');
    Handled:= TRUE;
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #5 on: November 01, 2014, 04:42:56 am »
Should I place 2 Aresponse in the procedure?? like:
No, you misunderstood web request - response flow. When you send a response containing references (external css, js, image, iframe, etc.), the browser will send another request for each of the respective reference, recursively (well, it's possible to send a page containing an iframe containing an iframe containing an iframe...). You can't fix it by sending two responses at once, it's impossible anyway, the last response you assign will always be the one getting sent. In case of CGI, you must decide: who shall be responsible for handling requests for those references? The web server or your CGI? For the former, read your web server's documentation. For the latter, you can create another request handler for those files. fcl-web has a static file request handler in fpwebfile unit. All you have to do is register URI-Directory pair by calling RegisterFileLocation. See testhttp in the examples folder of fcl-web.

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #6 on: November 03, 2014, 11:01:55 pm »
I don't know if I am correct but I changed the following:
initialization
  RegisterHTTPModule('genees', TGenees_show);
  RegisterFileLocation('public_html',ExtractFilePath(ParamStr(0)));
end.
     

The css files are under ~cgi-bin/public_htm/css
ParamStr(1) gives "Bad error header error

Any suggestions how to use RegisterFileLocation with a good result are welcome
Thanks in advance

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #7 on: November 04, 2014, 12:09:52 pm »
I don't know if I am correct but I changed the following:
initialization
  RegisterHTTPModule('genees', TGenees_show);
  RegisterFileLocation('public_html',ExtractFilePath(ParamStr(0)));
end.
     

The css files are under ~cgi-bin/public_htm/css
ParamStr(1) gives "Bad error header error

Any suggestions how to use RegisterFileLocation with a good result are welcome
Thanks in advance
Does the testhttp example work? I don't suggest using relative path through ParamStr(0) since your app may be run differently from its location on disk, use absolute path instead. Use config file if you want the app to be movable.

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #8 on: November 04, 2014, 02:40:35 pm »
The testhttp program runs. I can see the output in "terminal output"
Usage : testhttp DocumentRoot [Port]
Where
 Documentroot   location to serve files from. It is mapped to location /files
 Port           port to listen on (default 8080)

Does it give any hint?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #9 on: November 04, 2014, 06:05:53 pm »
The testhttp program runs. I can see the output in "terminal output"
Usage : testhttp DocumentRoot [Port]
Where
 Documentroot   location to serve files from. It is mapped to location /files
 Port           port to listen on (default 8080)

Does it give any hint?
Not yet, run it to serve files and access it from browser. Ensure you can get the file you requests.

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #10 on: November 04, 2014, 09:28:23 pm »
I ran testhttp as a cgi in my browser this gives
malformed header from script 'testhttp': Bad header: Where 

As a program testhhttp gives the same  terminal output as before.
-----
Browser is MFirefox. When I inspect the page source and the stylesheets I see:
>Invalid action name and no default action

When I inspect the console I see:
Use of getUserData() or setUserData() is deprecated.  Use WeakMap or element.dataset instead.

The HTML is correctly displayed if I go to file:///srv/www/cg-bin/public_html/tekst.html
If I go to http://localhost/cgi-bin/public_html/tekst.html I see the warning:
End of script output before headers: tekst.html 

I also tried the browser Chrome with the same results
Anybody sees some light? Please, tell me.
« Last Edit: November 05, 2014, 05:27:53 am by Middlecope »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #11 on: November 05, 2014, 10:06:31 am »
No, testhttp is not a CGI binary. It's standalone. Run it with the required parameters (see the usage output), then directly access in the browser.

I assume now it works. Now you have to "fix" the call to RegisterFileLocation. Try using hardcoded path for 2nd parameter, let 1st parameter stays 'public_html'. Note that the URL to CSS files should be: /cgi-bin/<your cgi binary>/public_html/<css file name, or path relative from 2nd parameter>

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #12 on: November 05, 2014, 02:21:55 pm »
No RegisterFileLocation did not help with the following links:
('css','/cgi-bin/public_html'); OR ('public_html','/cgi-bin/cgi1/'); etc.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #13 on: November 05, 2014, 04:11:59 pm »
No RegisterFileLocation did not help with the following links:
('css','/cgi-bin/public_html'); OR ('public_html','/cgi-bin/cgi1/'); etc.
Seems like you still don't get it. The 1st parameter is WEB ACTION NAME IN THE URI, 2nd parameter is the path IN YOUR FILE SYSTEM. I believe you don't have /cgi-bin/cgi1 or /cgi-bin/public_html directory. So if you call RegisterFileLocation('public','/home/user/css'); and you have /home/user/css/somefile.css in your file system, you can access it in the browser via: <your host, localhost if on the same computer>/cgi-bin/<your cgi exe>/public/somefile.css.

Middlecope

  • Jr. Member
  • **
  • Posts: 92
Re: Respond with HTML (AResponse.LoadFromFile) CSS missed
« Reply #14 on: November 05, 2014, 08:09:18 pm »
No that does not help.
Here is the code of my program CGI2

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Classes, httpdefs, fpHTTP, fpWeb,fpWebFile;

type

  { TFPWebModule1 }

  TFPWebModule1 = class(TFPWebModule)
    procedure ShowRequest(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: Boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FPWebModule1: TFPWebModule1;

implementation

{$R *.lfm}

{ TFPWebModule1 }

procedure TFPWebModule1.ShowRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; var Handled: Boolean);
begin
  AResponse.ContentType := 'text/html;charset=utf-8';
AResponse.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'public_html/test.html');
Handled:= TRUE;
end;

initialization
  RegisterFileLocation('public_html','/srv/www/cgi-bin/');
  RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
end.   

CGI2 is in /srv/www/cgi-bin/
In /srv/www/cgi-bin/public_html are 2 files my.css and test.html
test.html:
<!DOCTYPE html>
    <head>
      <meta charset="utf-8">
        <link rel="stylesheet" href="my.css">
            </head>
    <body>
    <p>Programmers of the world unite!</p>
    </body>
</html>

my.css:
body {
    background-color: lightblue;
}
h1 {
    color: navy;
    margin-left: 20px;
}

If in FireFox the url localhost/cgi-bin/cgi2/TFPWebModule1/Show is given I see the characters on a white field.
So the css is not used.
IF in FireFox I give as url file:///srv/www/cgi-bin/public_html/test.html I see the file in all its glory
How can I use the css?

 

TinyPortal © 2005-2018