Changing HTTP or CGI Response Code

200 OK, 404 Not Found, etc. These are the standard HTTP response code (they may also be called CGI status code). Say, you want to replace standard response code 404 to a custom response code (e.g. 999) for testing or debugging purpose, you can do it easily with Apache Web Server.

  1. Edit the “http.conf”.  In Debian, it’s full path is “/etc/apache2/http.conf”.  Add in the following lines:
    ScriptAlias /cgi-bin/ /var/www/cgi/
    ErrorDocument 404 /cgi-bin/999.pl

    The first line indicates the whereabout of the CGI script directory.
    The second line instruct the Apache to run the “999.pl” script when a HTTP 404 is encountered

  2. Create a custom CGI script in the “/var/www/cgi” directory. Name the script “999.pl”. The content of the script is listed below:
    #!/usr/bin/perl
    use strict;
    use warnings;
    print "Status: 999 CUSTOM RESP\n";
    print "Content-Type: text/html; charset=ISO-8859-1\n";
    print "\n";
    print "<h1>999 CUSTOM RESP</h> \r\n";
    print "Custom response code 999\r\n";
    
  3. Enable execution bit of the script.
    $ chmod a+x /var/www/cgi/999.pl
  4. Reload the configurations.
    # /etc/init.d/apache2 reload

Now if you try to access a non-existing page, you'll get HTTP 999 instead of HTTP 404 like the following:

$ wget -S http://localhost/notExitPage.html
--2010-06-07 10:07:14--  http://localhost/notExistPage.html
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 999 CUSTOM RESP
Date: Mon, 07 Jun 2010 02:07:14 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny8 with Suhosin-Patch
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=ISO-8859-1
2010-06-07 10:07:14 ERROR 999: CUSTOM RESP.

Background:
In work, my system (a web service) has been accused of sending HTTP response code 503 to one of the client but the requests were actually processed. The response code 503 (service not available) could never have been sent by my system if the requests had been accepted and processed. My guess is: there are some gateways that sit between the client and my system that returned 503 to the client yet forwarded the requests to my system. To justify my idea, I use the above method to replace 503 response code with 555 (a non-existing code). After the change, the said client is still receiving response code 503. Now, I can be sure the fault is not with my system.  The sad part is, the person in charge of the client system is still not believing that the HTTP 503 is not from my system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s