Jump to content
Incus J

Send a custom error document for 404 Not Found ?

Recommended Posts

In OverbyteIcsHttpServ, the error procedures such as THttpConnection.Answer404 are called from various places in the code and have hard coded HTML.  I'd like to serve my own error document instead.  Is there a built in mechanism to supply a custom HTML error document for errors such as 404, 403 ?

Share this post


Link to post

To send custom error responses you can create overloaded versions of Answer404, etc, in your version of THttpConnection or THttpAppSrvConnection.

 

Angus

 

  • Thanks 1

Share this post


Link to post

Perfect - I had missed the virtual nature of Answer404 - Thank you!

Share this post


Link to post

I tried to do exactly this.

i.e. I have created my own TManagersHttpConnection derived from THttpAppSrvConnection

 

class TManagersHttpConnection: public THttpAppSrvConnection
{
    typedef THttpAppSrvConnection inherited;
private:
protected:
    virtual void __fastcall Answer404();
public:
    __fastcall TManagersHttpConnection(Classes::TComponent* AOwner);
};

 

 

I then told the OverbyteIcsHttpServ that ClientClass to use is the above one:

 

__fastcall TnManagersHttpAppServer::TnManagersHttpAppServer(Classes::TComponent * AOwner)
    : THttpAppSrv(AOwner)
{
    ClientClass = __classid(TManagersHttpConnection);

 

My TManagersHttpConnection constructor gets called when a new client connects, exactly as I expect.  Yet my Answer404() function does NOT get called for a missing file, instead the content served is that from the base THttpAppSrvConnection class.

 

What might I be doing wrong please?

 

 

FYI this is using ICS version:

Revised: September 27, 2021
Release: V8.67

With Borland C++ 2007

 

Which for various reasons is not viable to update.

 

 

Share this post


Link to post

If you are stuck using an old version, which is understandable for old compilers, why not just customise the Answer404 function?

 

Angus

 

Share this post


Link to post

Thanks for the alternative suggestion

 

In fact it does work just fine, I think as I was only calling  

  inherited::Answer404();

the compiler had optimized it out. 

Onwards and upwards now 🙂

Share this post


Link to post

The ICS web server should probably offer an event to make creating customised error pages easier, I've put it on the wish list.  Our error pages are very minimal. 

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×