7

How could I create a simple content handler for apache .gz gzip content. I want it to uncompress say http://localhost/doc/FAQ/Linux-FAQ.gz and send it to the browser as plain text. There is a lot of documentation for Linux in /usr/share/doc and localhost/doc/. I don't want to use zless, zcat or vim to read the content. I use apache to browse the documentation on my local machine and have my web browser revive it as standard text so that it does not ask me to download the *.gz file every time.

Alias /doc/ "/usr/share/doc/"
Alias local.doc "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

But Now I want all those .gz file under /usr/share/doc/ to be servered as plain text. I think I could do that very simply with a python script in cgi-bin. I am looking for a nice content handler for those files. Like the way it php files are handled .gz should be uncompressed and sent to the browser.

<IfModule mod_php5.c>
  AddType application/x-httpd-php .php .phtml .php3
  AddType application/x-httpd-php-source .phps
</IfModule>
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

I see there is a mod_deflate, how would this apply. Could this handle the gzip content.

It would make browsing documentation so much easier. Any programing resources to help here would be nice.

Community
  • 1
  • 1
nelaaro
  • 3,006
  • 5
  • 38
  • 56

2 Answers2

7

I've used something like this before for js/css files (I modified the below to match your needs). Add this to your virtualhost entry:

Alias /doc/ "/usr/share/doc/"
Alias local.doc "/usr/share/doc/"
<Directory /usr/share/doc>
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    AddEncoding gzip gz
    <FilesMatch "\.gz$">
      ForceType text/plain
      Header set Content-Encoding: gzip
    </FilesMatch>
</Directory>

Updated above to match your code

In ubuntu ensure that Headers module is enabled

$ sudo a2enmod headers  
$ sudo a2enmod deflate
$ sudo apache2ctl restart

Update2: Realized that "AddEncoding gzip gz" was missing.. otherwise, file kept trying to download.

Update3: Added apache module deflate install command. Here's my deflate.conf:

<IfModule mod_deflate.c>
      # these are known to be safe with MSIE 6
      AddOutputFilterByType DEFLATE text/html text/plain text/xml

      # everything else may cause problems with MSIE 6
      AddOutputFilterByType DEFLATE text/css
      AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
      AddOutputFilterByType DEFLATE application/rss+xml
</IfModule>

You could first try with some other type of file (e.g. a css file). Example:

cd /usr/share/doc
cat ".styles { width: 50px; }" > test.css
gzip -c test.css > test.css.gz

Add this to your virtualhost:

    <FilesMatch "\.css\.gz$">
        ForceType text/css
        Header set Content-Encoding: gzip
    </FilesMatch>

Test http://127.0.0.1/doc/test.css and http://127.0.0.1/doc/test.css.gz and see what result you get.

nelaaro
  • 3,006
  • 5
  • 38
  • 56
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
  • @dolan could I put the section in the section I have above. – nelaaro Mar 09 '11 at 06:29
  • @nelaar as far as I know, that should work fine.. I updated my post to include your code. Post back if that doesn't work – Dolan Antenucci Mar 09 '11 at 06:33
  • Syntax error on line 41 of /etc/apache2/sites-enabled/000-default: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration 41 Header set Content-Encoding: gzip – nelaaro Mar 09 '11 at 06:34
  • I'd guess the apache module headers or rewrite is missing.. but here is my complete list to help debug what is missing: alias, autoindex, cgi, dir, env, expires, headers, mime, negotiation, rewrite, setenvif, status (go to /etc/apache2/mods-enabled/ , at least on an ubuntu server to see what is installed) – Dolan Antenucci Mar 09 '11 at 06:41
  • @dolan that fixed the error but sorry it is not working as I expected. My browsers are still trying to download the files. The web server is not sending it as plain text. – nelaaro Mar 09 '11 at 07:05
  • I need to uncompress the file on the server side and then send it. I think some code needs to written to do that. – nelaaro Mar 09 '11 at 07:09
  • I know with my CSS & JS files, they are compressed in the folder, and Apache is uncompressing them when they are requested. If nobody else has a better idea what the problem is, I'll check back tomorrow mornining, with some testing on my end.. need some sleep first :) – Dolan Antenucci Mar 09 '11 at 07:12
  • Okay, I was finally able to get this working. I was missing "AddEncoding gzip gz". See my updated answer with this included. I got this working for /usr/share/doc as well (e.g. http://localhost/doc/yelp/NEWS.gz) – Dolan Antenucci Mar 10 '11 at 04:34
  • @dolan which browser are you using, and have you changed any settings in it. Just want to double check that we are working in similar setups. I am testing using firefox 3.6.3, and chrome 8.0.552.237, chromium 5.0.342.9. – nelaaro Mar 10 '11 at 05:51
  • @nelaar I'm using FF 3.6.14 & Chrome 9.0.597.107 on Windows 7 64-bit. My Linux box is Ubuntu 10.04 w/ Apache2, w/ default virtual host and the above additions, and the following modules: alias, auth_basic, authn_file, authz_default, authz_groupfile, authz_host, authz_user, autoindex, cgid, deflate, dir, env, headers, mime, negotiation, reqtimeout, rewrite, setenvif, status. I just realized, I didn't mention "deflate" module. Be sure you have that. One other thing, try a gz file you haven't tried yet (Chrome was kept downloading ones I tried before I got it working) – Dolan Antenucci Mar 10 '11 at 06:07
  • @dolan can you post $ cat /etc/apache2/mods-enabled/deflate.conf – nelaaro Mar 10 '11 at 06:22
  • @dolan have the necessary modules headers, deflate, here is a complete list of my modules ubuntu 10.04 alias auth_basic authn_file authz_default authz_groupfile authz_host authz_user autoindex cgi deflate dir env expires headers jk mime mod_mono negotiation perl php5 python reqtimeout ruby setenvif status. as you can see I have few extras for the other stuff I am messing with – nelaaro Mar 10 '11 at 06:24
  • @nelaar deflate.conf posted in answer. "rewrite" shouldn't be needed, that's only one i see that you don't have. Did you try accessing a file you haven't tried yet? What brought me to my solution is I tried with a "css" file first. Maybe give that a try. I'll post some code in the answer – Dolan Antenucci Mar 10 '11 at 06:31
  • @dolan thanks your test works, will keep trying to figure out why it is not working for other /usr/share/doc/*/*.gz files. – nelaaro Mar 10 '11 at 07:06
  • ForceType plain/text I expect the problems i am still having have to do with the mime types, which are partially controlled by /etc/mime.types. – nelaaro Mar 11 '11 at 07:01
  • What O/S are you running Apache on? As noted earlier, my setup is on Ubuntu 10.04 w/ standard apache2 from apt-get – Dolan Antenucci Mar 11 '11 at 07:05
  • @dolan Ubuntu 10.04 2.6.32-28-generic Server version: Apache/2.2.14 (Ubuntu) – nelaaro Mar 11 '11 at 10:13
  • @dolan I have looked at the cat /etc/apache2/mods-enabled/mime.conf | head -n 30. Can I see yours. – nelaaro Mar 11 '11 at 10:15
  • @dolan got it working changed the forcetype dirctive to ForceType text/plain from ForceType plain/text – nelaaro Mar 11 '11 at 10:51
  • @dolan thanks for all your help. I will go and check out some of your other answers and upvote the ones that seam reasonable to me. For all your effort try to give you a bit more rep. – nelaaro Mar 11 '11 at 10:53
  • @nelaar awesome on getting it working! No worries on the help, my pleasure – Dolan Antenucci Mar 12 '11 at 17:48
0
cat /etc/apache2/mods-enabled/mime.conf | head -n 30
<IfModule mod_mime.c>

#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /etc/mime.types

#
# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
# Despite the name similarity, the following Add* directives have
# nothing to do with the FancyIndexing customization directives above.
#
AddEncoding x-compress .Z
AddEncoding x-gzip .gz .tgz
AddEncoding x-bzip2 .bz2
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-bzip2 .bz2
nelaaro
  • 3,006
  • 5
  • 38
  • 56