11

Is it possible, to get a list of all handlers from apache? With setHandler it is possible to create handlers.

One possible usage would be to add a handler for a specific file extension (addHandler). Knowing all the possible handlers and their identifier would ease the process of matching them to a file extension (or in case of URL handlers creating a list of possible URLs).

The handlers must be managed in apache core in some kind of list - how is it possible to get this list out?

MaoPU
  • 246
  • 2
  • 8

2 Answers2

4

Apache does not expose a list of configured handlers. Not using apachectl, not with anything else.

The best method that I could find to get a list of configured handlers is to simply grep the Apache configuration folder for Handler to catch all AddHandler and SetHandler declarations.

For Debian-based (Ubuntu) hosts

$ grep -ir "Handler" /etc/apache2/*
mods-enabled/php5.conf: SetHandler application/x-httpd-php
mods-enabled/php5.conf: SetHandler application/x-httpd-php-source
apache2.conf:           SetHandler server-status
mods-available/info.conf:    SetHandler server-info
mods-available/ldap.conf:    SetHandler ldap-status
mods-available/status.conf:  SetHandler server-status
mods-available/php5.conf:    SetHandler application/x-httpd-php
mods-available/php5.conf:    SetHandler application/x-httpd-php-source
sites-available/default:     AddHandler cgi-script .cgi
sites-enabled/000-default:   AddHandler cgi-script .cgi

For Redhat-based (Fedora, CentOS) hosts

$ grep -ir "Handler" /etc/httpd/*
conf.d/php.conf:  AddHandler php5-script .php
conf/httpd.conf:  #AddHandler cgi-script .cgi
conf/httpd.conf:  #AddHandler send-as-is asis
conf/httpd.conf:  AddHandler type-map var
conf/httpd.conf:  #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
conf/httpd.conf:     AddHandler type-map var
conf/httpd.conf:#    SetHandler server-status
conf/httpd.conf:#    SetHandler server-info
conf.d/fcgid.conf:   AddHandler fcgid-script fcg fcgi fpl
conf.d/perl.conf:#   SetHandler perl-script
conf.d/perl.conf:#   SetHandler perl-script

Note that not all handlers found are in fact registered! Search in mods-enabled (Debian) and disregard lines starting with # to narrow down only the registered handlers.

As suggested by Jenny in the comments, commented lines can be removed by filtering the output with grep -Pv '^[^ ]*:\s*#'. Here is the final command for Debian-based machines:

$ grep -ir "Handler" /etc/apache2/* | grep -Pv '^[^ ]*:\s*#'

And for Redhat-based machines:

$ grep -ir "Handler" /etc/httpd/* | grep -Pv '^[^ ]*:\s*#'

In the comments user gogoud provides an additional way to strip out commented handlers, thus returning only registered handlers:

// Debian or Ubuntu
$ grep -R "Handler" /etc/apache2/*enabled* | sed 's/#.*//;/^[^:]*:\s*$/d'

// Redhat, CentoOS, Fedora
$ grep -R "Handler" /etc/httpd/*enabled* | sed 's/#.*//;/^[^:]*:\s*$/d'
dotancohen
  • 2,590
  • 2
  • 25
  • 39
  • 2
    To get rid of the commented-out handlers, use some find and grep magic: `find /etc/httpd/ -type f -exec grep -H -v '#' {} \; | grep Handler` – Jenny D Apr 17 '15 at 08:22
  • @JennyD: I think that would remove lines with comments after the declarations, such as `AddHandler cgi-script .cgi # Ben needs CGI`. – dotancohen Apr 17 '15 at 08:27
  • 1
    True. You could do it with `egrep` and a suitable regexp, which is left as an exercise for the reader (as unfortunately I have to do some paid work right now...) – Jenny D Apr 17 '15 at 08:35
  • @JennyD: Challenge accepted! I've added a grep filter for commented lines. Nice thinking, thanks! – dotancohen Apr 17 '15 at 09:26
  • Why the sudden downvotes? This answer directly answers the question and even includes examples for the two most common Linux variants. If the answer could be improved I would appreciate some _constructive_ criticism. – dotancohen Jun 01 '16 at 06:25
  • For Debian this will show only *enabled* handlers, stripping comments whether on their own line or at the end of declaration lines: `grep -R "Handler" /etc/apache2/*enabled* | sed 's/#.*//;/^[^:]*:\s*$/d'` – gogoud Jun 26 '16 at 06:49
0

This is isn't quite what you want, but if you enable the server-info handler in your config, the output will tell you which modules have handlers and which don't, like this:

Module Name: mod_negotiation.c
Content handlers: yes
....
Module Name: mod_mime.c
Content handlers: none
....

This was with Apache 2.2, so perhaps later versions will give you more useful output.

Flup
  • 7,978
  • 2
  • 32
  • 43
  • Thank you, however I'm not seeing that info. [This page](http://192.117.111.61/apache.html) shows the output of `server-info`. Do note, however, that the output is a static page copied from the real server and served to you via a Raspberry Pi. – dotancohen Apr 03 '14 at 09:54