apache 采用C语言的模块开发

时间:2011-02-22 08:33:07
【文件属性】:
文件名称:apache 采用C语言的模块开发
文件大小:3KB
文件格式:C
更新时间:2011-02-22 08:33:07
apache
我在改造APACHE服务器授权访问时,需要对不合法的客户端请求进行过滤。对不合法请求需要立即发送一个错误提示页面给客户端。
发送错误提示页面的程序片断如下:
//非法请求作错误跳转
char *location = "/error/error.jsp";
r->status = HTTP_OK;
r->method = apr_pstrdup(r->pool, "GET");
r->method_number = M_GET;
ap_internal_redirect_handler(location, r);
//杀死子请求
ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);
后来发现这样写有问题,以上代码对于没有启用mod_proxy的HTTP或HTTPS请求都是可以正确处理的。但是如果启用了mod_proxy功能后,就不会正确执行了。

于是我做了如下修改:
apr_table_setn(r->headers_out, "Http", "302");
//我们设置这个错误跳转到http://www.yahoo.com。
apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");
r->status = HTTP_TEMPORARY_REDIRECT;
//ap_send_error_response函数第二个参数设置为NULL(既0)
ap_send_error_response(r, 0);
//处理掉子请求
ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);
就可以正确跳转了。
ap_send_error_response()在http_protocol.h定义为:
/**
* Send error back to client.
* @param r The current request
* @param recursive_error last arg indicates error status in case we get
* an error in the process of trying to deal with an ErrorDocument
* to handle some other error. In that case, we print the default
* report for the first thing that went wrong, and more briefly report
* on the problem with the ErrorDocument.
* @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
*/
AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);

其实ap_send_error_response函数也可以向客户端发送一个指定的页面。设置好
apr_table_setn(r->headers_out, "Http", "302");
apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");
ap_send_error_response的第二个参数设为"NULL",他就跳转到"http://www.yahoo.com"了。
这是我看了ap_send_error_response函数源代码后发现的。



网友评论

  • 一堆垃圾东西 下了完全没用的