C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)

本文介绍了如何使用Visual Studio 2017在Windows环境下开发Apache模块mod_helloworld,通过编译和配置Apache服务器,实现访问/helloworld URL时显示自定义的C语言响应内容。
摘要由CSDN通过智能技术生成
C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)

名词解释:apxs

apxs is a tool for building and installing extension modules for the Apache HyperText Transfer Protocol (HTTP) server. 

apxs是用来编译和安装 apache 服务器的扩展模块(mod)、也能生成项目模版(下面有具体使用说明)


名词解释:MinGW
MinGW,是Minimalist GNUfor Windows的缩写。它是一个可自由使用和自由发布的Windows特定头文件和使用GNU工具集导入库的集合,
允许你在GNU/Linux和Windows平台生成本地的Windows程序而不需要第三方C运行时(C Runtime)库。
MinGW 是一组包含文件和端口库,其功能是允许控制台模式的程序使用微软的标准C运行时(C Runtime)库(MSVCRT.DLL),该库在所有的 NT OS 上有效,
在所有的 Windows 95发行版以上的 Windows OS 有效,使用基本运行时,你可以使用 GCC 写控制台模式的符合美国标准化组织(ANSI)程序,
可以使用微软提供的 C 运行时(C Runtime)扩展,与基本运行时相结合,就可以有充分的权利既使用 CRT(C Runtime)又使用 WindowsAPI功能。

一、apache mod模块 windows开发
1)

下载apxs apxs_win32.tar.gz,下载地址
http://download.csdn.net/detail/tengyunjiawu_com/9811400

2)
安装strawberryperl或active perl
下载地址: http://strawberryperl.com/releases.html
我安装的是active perl,所以还需要安装 dmake
dos命令行执行:ppm install dmake

3)解压apxs_win32.tar.gz 
   解压到C:\apache\apache2.4.9\bin\apxs

4)进入C:\apache\apache2.4.9\bin\apxs目录
perl Configure.pl --with-apache2=D:/wamp/bin/apache/apache2.4.9 --with-apache-prog
<br>我在改造APACHE服务器授权访问时,需要对不合法的客户端请求进行过滤。对不合法请求需要立即发送一个错误提示页面给客户端。<br>发送错误提示页面的程序片断如下:<br> //非法请求作错误跳转<br> char *location = "/error/error.jsp";<br> r->status = HTTP_OK;<br> r->method = apr_pstrdup(r->pool, "GET");<br> r->method_number = M_GET;<br> ap_internal_redirect_handler(location, r);<br> //杀死子请求<br> ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);<br>后来发现这样写有问题,以上代码对于没有启用mod_proxy的HTTP或HTTPS请求都是可以正确处理的。但是如果启用了mod_proxy功能后,就不会正确执行了。<br><br>于是我做了如下修改:<br> apr_table_setn(r->headers_out, "Http", "302");<br> //我们设置这个错误跳转到http://www.yahoo.com。<br> apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");<br> r->status = HTTP_TEMPORARY_REDIRECT; <br> //ap_send_error_response函数第二个参数设置为NULL(既0)<br> ap_send_error_response(r, 0); <br> //处理掉子请求<br> ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);<br>就可以正确跳转了。<br>ap_send_error_response()在http_protocol.h定义为:<br>/**<br> * Send error back to client.<br> * @param r The current request<br> * @param recursive_error last arg indicates error status in case we get <br> * an error in the process of trying to deal with an ErrorDocument <br> * to handle some other error. In that case, we print the default <br> * report for the first thing that went wrong, and more briefly report <br> * on the problem with the ErrorDocument.<br> * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)<br> */<br>AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);<br><br>其实ap_send_error_response函数也可以向客户端发送一个指定的页面。设置好<br>apr_table_setn(r->headers_out, "Http", "302");<br>apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");<br>ap_send_error_response的第二个参数设为"NULL",他就跳转到"http://www.yahoo.com"了。<br>这是我看了ap_send_error_response函数源代码后发现的。<br><br><br>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值