Search code examples
http-headerswebsphere-liberty

How to redirect a 501 status code from IBM Liberty to an error page?


Have IBM Liberty set up, and some deliberately corrupt requests which arrive receive a 501 Not Implemented response.

Sample corrupt request:

TRACK /<script>alert('TRACK');</script> HTTP/1.1
Referer: ...
Accept: */*
Pragma: no-cache
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
Host: ...
...

The response sent by Liberty is a default, which has the context of:

HTTP/1.1 501 Not Implemented
... headers omitted ...
Date: Tue, 01 Feb 2022 01:01:01 GMT
Content-Length: 92

Error 501: Method TRACK is not defined in RFC 2068 and is not supported by the Servlet API 

Instead of presenting a 501 and the above message to the requester, I want to direct the requester to a custom error page with a simple message of my choosing; at worst a plain HTML file.

I have not yet found a way to get IBM Liberty to produce anything other than the above for a 501 Not Implemented response.


Solution

  • You can handle that by defining error pages for your application. Here is how I configured that:

    • create your error page - error.html

    • create following entry in web.xml

      <error-page>
         <error-code>501</error-code>
         <location>/error.html</location>
      </error-page>
      

    Whenever your app would generate 501 it will return your error.html page instead.

    Tested using Postman and your Track method. First got 501 unimplemented, then error page.

    Make sure you have servlet-x.y and jsp-a.b features in your server.xml