Apache module杂记
可以基于正则表达式修改文本内容的apache module:
mod_sed:实现了类似sed功能的module,可以通过正则表达式修改文本内容。apache2.3中加入了这个module,但是这个module也可以用于apache 2.0版本。可以到http://src.opensolaris.org/source/xref/webstack/mod_sed/下载源代码,readme里有相应的编译命令:/http安装路径/bin/apxs -i -c mod_sed.c regexp.c sed0.c sed1.c
mod_substitute:功能和mod_sed类似,默认加入到apache2.2中http://httpd.apache.org/docs/2.2/mod/mod_substitute.html
mod_line_edit:也可以基于正则表达式替换文本内容,可以就该html/css/javascript。但是它和前两者不同的是mod_line_edit的to-pattern可以使用apache的环境变量http://apache.webthing.com/mod_line_edit/,这个功能正是我最近需要的。
例如下面的配置可以在<head>标签后插入一个<meta/>标签并且可以将环境变量unique_id的值添加到meta的属性中去(unique_id环境变量需要mod_unique_id的支持http://lamp.linux.gov.cn/apache/apachemenu/mod/mod_unique_id.html)
lerewriterule "<head>" "<head><meta http-equiv='request-id' content='${unique_id}' />" iv
mod_proxy_html:可以基于html标签进行比较比较精细的内容修改操作http://apache.webthing.com/mod_proxy_html/
以上这些module都是基于apache的过滤器的功能来完成对相应内容的修改(类似servlet里的filter)http://lamp.linux.gov.cn/apache/apachemenu/filter.html
mod_line_edit使用经验:
1.mod_line_edit的性能问题:考虑到系统中有500-700k的html页面,因此我就对mod_line_edit在展现1m的静态html做了一下压力测试。测试结果非常不理想,在不添加mod_line_edit时tps在500+,加上mod_line_edit后tps只有1(狂汗…)。
2.mod_line_edit的代码分析:
/* mod_line_edit顾名思义就是对文本内容按行进行编辑, * 因此mod要先对输出流进行整理,每一行内容收集到一个apr_bucket里, * 然后将所有行数据放到bbline中,下面的代码实现的就是这个用途。 */ bbline = apr_brigade_create(f->r->pool, f->c->bucket_alloc) ; /* first ensure we have no mid-line breaks that might be in the * middle of a search string causing us to miss it! at the same * time we split into lines to avoid pattern-matching over big * chunks of memory. */ while ( b != apr_brigade_sentinel(bb) ) { if ( !apr_bucket_is_metadata(b) ) { if ( apr_bucket_read(b, &buf, &bytes, apr_block_read) == apr_success ) { if ( bytes == 0 ) { apr_bucket_remove(b) ; } else while ( bytes > 0 ) { switch (cfg->lineend) { case lineend_unix: le = memchr(buf, '\n', bytes) ; break ; case lineend_mac: le = memchr(buf, '\r', bytes) ; break ; case lineend_dos: /* edge-case issue: if a \r\n spans buckets it'll get missed. * not a problem for present purposes, but would be an issue * if we claimed to support pattern matching on the lineends. */ found = 0 ; le = memchr(buf+1, '\n', bytes-1) ; while ( le && !found ) { if ( le[-1] == '\r' ) { found = 1 ; } else { le = memchr(le+1, '\n', bytes-1 - (le+1 - buf)) ; } } if ( !found ) le = 0 ; break; case lineend_any: case lineend_unset: /* edge-case notabug: if a \r\n spans buckets it'll get seen as * two line-ends. it'll insert the \n as a one-byte bucket. */ le_n = memchr(buf, '\n', bytes) ; le_r = memchr(buf, '\r', bytes) ; if ( le_n != null ) if ( le_n == le_r + sizeof(char)) le = le_n ; else if ( (le_r < le_n) && (le_r != null) ) le = le_r ; else le = le_n ; else le = le_r ; break; case lineend_none: le = 0 ; break; case lineend_custom: le = memchr(buf, cfg->lechar, bytes) ; break; } if ( le ) { /* found a lineend in this bucket. */ offs = 1 + ((unsigned int)le-(unsigned int)buf) / sizeof(char) ; apr_bucket_split(b, offs) ; bytes -= offs ; buf += offs ; b1 = apr_bucket_next(b) ; apr_bucket_remove(b); /* is there any previous unterminated content ? */ if ( !apr_brigade_empty(ctx->bbsave) ) { /* append this to any content waiting for a lineend */ apr_brigade_insert_ta