扩展mod_helloworld -- 使用stdio-like IO2008-11-10 22:19上一篇文章我介绍了如何编写第一个Apache模块,也是最简单的模块mod_helloworld,这里我们要对mod_helloworld进行一些简单的扩展。
这里我们还是来举一个例子,这个例子做的事情会比上一篇文章中介绍的mod_helloworld模块做的事情要多,但编写模块的整体框架不变,我们只是在处理函数中多做了一些事情。
我们扩展的mod_helloworld模块功能要实现的是根据客户端请求从服务器本地读取文件内容然后反馈给客户端。
要实现这个功能,我们要用到request_rec数据结构中的filename字段,它表示了客户端请求文件的文件名,我们还使用了文件信息对象来实现这一过程的优化。处理函数代码如下:
static int helloworld_handler(request_rec *r)
{
apr_file_t *fd;
apr_size_t sz;
apr_status_t rv;
if (r->filename == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Incomplete request_rec!") ;
return HTTP_INTERNAL_SERVER_ERROR ;
}
ap_set_content_type(r, "text/html;charset=ascii");
/* Now we can usefully set some additional headers from file info
* (1) Content-Length
* (2) Last-Modified
*/
ap_set_content_length(r, r->finfo.size);
if (r->finfo.mtime) {
char *datestring = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
apr_rfc822_date(datestring, r->finfo.mtime);
apr_table_setn(r->headers_out, "Last-Modified", datestring);
}
rv = apr_file_open(&fd, r->filename, APR_READ|APR_SHARELOCK|APR_SENDFILE_ENABLED, APR_OS_DEFAULT, r->pool);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "can't open %s", r->filename);
return HTTP_NOT_FOUND ;
}
ap_send_fd(fd, r, 0, r->finfo.size, &sz);
/* file_close here is purely optional. If we omit it, APR will close
* the file for us when r is destroyed, because apr_file_open
* registered a close on r->pool.
*/
apr_file_close(fd);
return OK;
}
这里的ap_log_rerror是登记日志的函数,他登记的是request类型的日志。
r->finfo中包含了请求文件的统计信息。根据r->finfo.mtime设置Last-Modified,可以使浏览器在请求文件时先检查缓存。
这里我们响应文件内容没有采用先读文件再使用ap_rwrite把文件内容发送出去的方式,而是使用了一个更简单的函数ap_send_fd,直接发送文件
分享到:
相关推荐
api_ms_win_crt_stdio_l1_1_0.dll 32位和64位都在里面
#include<stdio.h>intmain(void){printf("Hello
# the helloworld executable, install it by copying it to the /bin directory on # the router. The $(1) variable represents the root directory on the router running # OpenWrt. The $(INSTALL_DIR) ...
C-源码-(包括stdio,stdlib和string三大部分).rar
android studio 安装过程中出现 notinstall build-tools_r21.1.1需要这个。
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "iostream.h" //定义“空” #define null 0 //定义“进程”结构体 struct process { char process_name[10]; char apply_deviceid; ...
jni简单使用 java通过JNI调用c++的简单配置步骤, Java 通过JNI调用c++ ...JNIEXPORT void JNICALL Java_HelloWorld_print (JNIEnv *env, jobject obj){ printf("HelloWorld JNI!/n"); return; }
visual_stdio_2008测试单元使用方法
ADO.NET(For Free Download)
#include <stdio.h>#include <stdlib.h>#include ............. .............. //:D。这下,你应该发现我所添加的注释的价值了。 本文来自CSDN博客,转载请标明出处:...
使用该文件可配合gdbvisual 使用Visual Stdio2017对ARM_M3系列芯片进行编译
使用链接列表将两个多项式相乘 描述 在将任意顺序的两个多项式相乘时使用链接列表 更多信息 系数和多项式的阶数 输入的多项式和输出 提交时间 经过 等级 先进的 用户评分 5.0(2个用户的10个地球仪) 兼容性 C,C ...
41-STM32F429_STDIO_INPUT_OUTPUT.7z
安装如果使用的是vscode,请安装扩展。码头工人只需您就可以从拉取$ docker pull mtsmfm/language_server-rubyRuby宝石将此行添加到您的应用程序的Gemfile中: gem 'language_server' 然后执行: $ bundle或将其自己...
41-STM32F429_STDIO_OUTPUT.7z
LanguageServer ::协议 用于Ruby的语言服务器协议SDK。...writer = LSP :: Transport :: Stdio :: Writer . new reader = LSP :: Transport :: Stdio :: Reader . new subscribers = { initialize : -> {
$vim hello.c >>>> 进入emacs 编辑器环境 #include <stdio.h> int main() { printf("Hello world!\n"); return 0; }