[Klone-users] Custom error page with GET/POST data

Mickael Auger mickael.auger at gmail.com
Wed Dec 17 06:24:13 EST 2008


Hi,

I know that I can customize my error pages (404, ...) with "error.num" key
(ex : error.404   myError.html) in kloned.conf.
But I wish use the same error page (.kl1 or .klx) for all errors.
To do this I must give a GET or POST variable at my error page (ex :
error.kl1?no=404).

For example :

(kloned.conf)
=================================================
app_http
{
    type            http
    addr.type       IPv4
    addr.port       80
    dir_root        /www

    error
    {
            400                        /error.kl1?no=400
            404                        /error.kl1?no=404
            ...
    }
}
=================================================

(error.kl1)
=================================================
<%!
static const char *errorno;
%><%
errorno = request_get_arg(request,"no");
%><html>
<body>
  <h1><%= errorno %> Error</h1>
</body>
</html>
=================================================

However this example doesn't actually.


I have searched a solution to perform this.
If I comment the line 421 "if(http_is_valid_uri(rq, err_page,
strlen(err_page)))"
of "http_print_error_page()" function in "src/libhttp/http.c", my custom
error page with GET parameter is printed in my web-browser.
So the solution is to modify "http_print_error_page()" function OR to pass
the single URL (without GET parameter) at this function. It should be just
see the base of URI is valid (ex : http://localhost/myerror.kl1?no=404   =>
http://localhost/myerror.kl1 ).

Here a solution that I tested with successfull :
=================================================
static int http_print_error_page(http_t *h, request_t *rq, response_t *rs,
    int http_status)
{
    enum { BUFSZ = 64 };
    const char *err_page;
    char buf[BUFSZ];
    vhost_t *vhost;

    int i; // new meter for new "for" loop
    char uri[255]; // URL wihout GET parameter. 255 is the max length of URL

    dbg_err_if (h == NULL);
    dbg_err_if (rq == NULL);
    dbg_err_if (rs == NULL);
    dbg_err_if (http_status == 0);

    /* clean dirty header fields (not for redirects) */
    if(http_status != 302)
        dbg_err_if(header_clear(response_get_header(rs)));

    /* add default header fields */
    dbg_err_if(http_add_default_header(h, rq, rs));

    /* disable page caching */
    dbg_err_if(response_disable_caching(rs));

    /* looking for user provided error page */
    dbg_err_if(u_snprintf(buf, BUFSZ, "error.%d", http_status));
    if((vhost = http_get_vhost(h, rq)) == NULL)
        err_page = u_config_get_subkey_value(h->config, buf);
    else
        err_page = u_config_get_subkey_value(vhost->config, buf);

    if(err_page && !request_set_uri(rq, err_page, NULL, NULL))
    {
        dbg_err_if(http_resolv_request(h, rq));

        //copy of err_page without GET parameter
        memset(uri,'\0',255);
        for(i=0;i<strlen(err_page);i++)
        {
            if(err_page[i]=='?')
                break;
            uri[i]=err_page[i];
        }
        //if(http_is_valid_uri(rq, err_page, strlen(err_page))) //old test
of URI
        if(http_is_valid_uri(rq, uri, strlen(uri))) //new test of URI
        {
            /* user provided error page found */
            broker_serve(h->broker, h, rq, rs);
            return 0;
        }

        /* page not found */
        warn("%d handler page (%s) not found", http_status, err_page);
    }

    /* be sure that the status code is properly set */
    response_set_status(rs, http_status);

    response_print_header(rs);

    if(request_get_method(rq) == HM_HEAD)
        return 0; /* just the header is requested */

    /* print default error page */
    dbg_err_if(io_printf(response_io(rs),
        "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"
        "<html><head><title>%d %s</title></head>\n"
        "<body><h1>%s</h1><p>URL: %s</p><hr>"
        "<address>KLone/%s web server - www.koanlogic.com</address>"
        "</body></html>",
        http_status, http_get_status_desc(http_status),
        http_get_status_desc(http_status),
        (request_get_uri(rq) ? request_get_uri(rq) : ""),
        KLONE_VERSION
        ) < 0);

    return 0;
err:
    return ~0;
}
=================================================

In this solution, I choose 255 for the max length of URI. This value should
be your value that I don't known.

What do you think of this solution ?


Bye.

-- 
Mickaël AUGER
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://koanlogic.com/pipermail/klone-users/attachments/20081217/38c6d71d/attachment.html


More information about the klone-users mailing list