Cgi scripts and external css

Consider this CGI script:

#!ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out {
  CGI.pretty(
    cgi.html {
     cgi.head { cgi.title{"Example"}+"<link rel=\"stylesheet\"
type=\"text/css\" href=\"example.css\">" } +
     cgi.body{"This should have a green background"}
    }
  )
}

with this example.css, both residing in the same directory:

body {
  background-color: #00ff00;
  }

it produces this html output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  <HEAD>
    <TITLE>
      Example
    </TITLE>
    <link rel="stylesheet" type="text/css" href="example.css">
  </HEAD>
  <BODY>
    This should have a green background
  </BODY>
</HTML>

Perfectly fine, it seems. The only problem is, that the background isn't
green, it's white (Firefox' default color, on my system).
However, if I save the html-source to a static example.html, put in the
same directory as my example.rb and open it in the browser, the css is
correctly applied and the background turns green.

It works if I put the css directly in the example.rb like this:

#!ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out {
  CGI.pretty(
    cgi.html {
      cgi.head {cgi.title{"Example"}+
            cgi.style('type'=>'text/css'){File.read("./example.css")}} +

      cgi.body{"This should have a green background"}
    }
  )
}

But this way the css is not cacheable...

Where lies the problem in my script?

(FYI I use ruby 1.8.2 (2004-12-25) [i386-mswin32], Apache and Windows XP
Pro)

···

--
Posted via http://www.ruby-forum.com/.

Peter Seidel wrote:

Consider this CGI script:

#!ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out {
  CGI.pretty(
    cgi.html {
     cgi.head { cgi.title{"Example"}+"<link rel=\"stylesheet\"
type=\"text/css\" href=\"example.css\">" } +
     cgi.body{"This should have a green background"}
    }
  )
}

with this example.css, both residing in the same directory:

body {
background-color: #00ff00;
}

it produces this html output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
<HTML>
  <HEAD>
    <TITLE>
      Example
    </TITLE>
    <link rel="stylesheet" type="text/css" href="example.css">
  </HEAD>
  <BODY>
    This should have a green background
  </BODY>
</HTML>

Is this HTML (or the script that generates it) located in the same directory
as your CSS example file? If not, it won't work. When there is no path
given in "href", just a filename, the target file must reside in the same
directory as the source file, whether that is a CGI script or a Web page.

You don't show how you are making the generated HTML available to the
browser. It (or its generating script) must be located in the same
directory as the example CSS, or a path must be provided as part of the
"href" address.

Perfectly fine, it seems. The only problem is, that the background isn't
green, it's white (Firefox' default color, on my system).
However, if I save the html-source to a static example.html, put in the
same directory as my example.rb and open it in the browser, the css is
correctly applied and the background turns green.

And that is the only way this example would work. The script that generates
the HTML must be located in some specific way with regard to the CSS file,
in order for the CSS files to be located by the browser.

It works if I put the css directly in the example.rb like this:

No need for that.

/...

Where lies the problem in my script?

The problem is not your script per se, it is in the part you don't show --
how you locate it with respect to the CSS file.

···

--
Paul Lutus
http://www.arachnoid.com

The problem is not your script per se, it is in the part you don't
show -- how you locate it with respect to the CSS file.

Both files are in /cgi-bin/

http://127.0.0.1/cgi-bin/example.rb

- -> white background

http://127.0.0.1/cgi-bin/example.html

- -> green background

Peter Seidel wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

The problem is not your script per se, it is in the part you don't
show -- how you locate it with respect to the CSS file.

Both files are in /cgi-bin/

http://127.0.0.1/cgi-bin/example.rb

- -> white background

http://127.0.0.1/cgi-bin/example.html

- -> green background

Okay. I will try again.

Where is the example.css file located? Is it located in the same directory
as the example.rb script, or not?

IF the example.css file is located in the same directory as the example.rb
CGI script, and assuming the script is really emitting the HTML you posted
(check using "view source"), the browser will read it correctly.

IF, by contrast, the example.css file is NOT located in the same directory
as the example.rb CGI script, the browser will NOT be able to find it.

So. Where is the example.css file located?

You know, you can always create an address for the CSS file that
unambiguously identifies its location. Let's say it is located on your
server at /styles/example.css. Then you can say:

<link rel="stylesheet" type="text/css" href="/styles/example.css">

This absolute addressing style is commonly used to avoid the kinds of
problems you are having.

···

--
Paul Lutus
http://www.arachnoid.com

Sorry, my language was ambiguous. With "both files" I meant the script
and the css file. In fact, all the files are in /cgi-bin/.

Peter Seidel wrote:

Sorry, my language was ambiguous. With "both files" I meant the script
and the css file. In fact, all the files are in /cgi-bin/.

I just put example.rbx and example.css in ~/ruby on my server and it
works. The page has a green background.

I did have a problem with browser cache, though... I had tried it
before example.css existed, and it was a white background. Making
example.css and refreshing several times didn't fix the problem. I had
to tell it to do a full page reload (ctrl-click reload) before it showed
the green background.

···

--
Posted via http://www.ruby-forum.com/\.

When in doubt about browser caching you might want to add version parameter to the URL, ie:

      cgi.head { '<link rel="stylesheet" type="text/css" href="example.css?v=' + Time.now.to_i.to_s + '">' } ... etc.

This works for external JavaScript files as well.

Mike Dvorkin
http://www.rubywizards.com

···

On Sep 12, 2006, at 4:47 AM, William Crawford wrote:

Peter Seidel wrote:

Sorry, my language was ambiguous. With "both files" I meant the script
and the css file. In fact, all the files are in /cgi-bin/.

I just put example.rbx and example.css in ~/ruby on my server and it
works. The page has a green background.

I did have a problem with browser cache, though... I had tried it
before example.css existed, and it was a white background. Making
example.css and refreshing several times didn't fix the problem. I had
to tell it to do a full page reload (ctrl-click reload) before it showed
the green background.

--
Posted via http://www.ruby-forum.com/\.

I've found the problem. It seems that Apache does not serve the file
correctly if it's located in the /cgi-bin/ directory. If I move it to
/htdocs and adapt the script it works fine.
Thank you for your kind replies.