一、Apache Models代码
mod_mergecss.c
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "apr_pools.h"
#include "apr_strings.h"
#include <string.h>
/* The sample content handler */
static int mergejs_handler(request_rec *r)
{
int rc, exists;
apr_finfo_t finfo;
apr_file_t *file;
char *filenames;
char *tokenPtr;
char *filename;
char buffer[256];
apr_size_t readBytes;
apr_pool_t *pool = NULL;
apr_pool_initialize();
apr_pool_create(&(pool), NULL);
if (strcmp(r->handler, "mergecss")) {
return DECLINED;
}
r->content_type = "text/html";
//if (!r->header_only)
// ap_rputs("The sample page from mod_mergejs.c\n", r);
if (r->args) {
//ap_rprintf(r, "Your query string was: %s<br/>", r->args);
filenames = apr_psprintf(pool, "%s", r->args);
//ap_rprintf(r, "filenames was: %s<br/>", filenames);
tokenPtr = strtok(filenames,",");
ap_rprintf(r, "/*tokenPtr was: %s powered by 小白救星 微信:17717340907*/\n", tokenPtr);
filename = apr_pstrcat(pool, "/qd/css/", tokenPtr);
//ap_rprintf(r, "afilename1 was: %s<br/>", filename);
rc = apr_file_open(&file,filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rc == APR_SUCCESS) {
readBytes = 256;
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
buffer[readBytes] = 0;
ap_rputs(buffer, r);
}
}
apr_file_close(file);
while(tokenPtr!=NULL){
tokenPtr=strtok(NULL,",");
if(tokenPtr!=NULL){
filename = apr_pstrcat(pool, "/qd/css/", tokenPtr);
//ap_rprintf(r, "afilenamen was: %s<br/>", filename);
rc = apr_file_open(&file,filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rc == APR_SUCCESS) {
readBytes = 256;
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
buffer[readBytes] = 0;
ap_rputs(buffer, r);
}
}
apr_file_close(file);
}
}
}
//apr_file_close(file);
return OK;
}
static void mergejs_register_hooks(apr_pool_t *p)
{
ap_hook_handler(mergejs_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA mergejs_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
mergejs_register_hooks /* register hooks */
};
2、mod_mergejs.c
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "apr_pools.h"
#include "apr_strings.h"
#include <string.h>
/* The sample content handler */
static int mergejs_handler(request_rec *r)
{
int rc, exists;
apr_finfo_t finfo;
apr_file_t *file;
char *filenames;
char *tokenPtr;
char *filename;
char buffer[256];
apr_size_t readBytes;
apr_pool_t *pool = NULL;
apr_pool_initialize();
apr_pool_create(&(pool), NULL);
if (strcmp(r->handler, "mergejs")) {
return DECLINED;
}
r->content_type = "text/html";
//if (!r->header_only)
// ap_rputs("The sample page from mod_mergejs.c\n", r);
if (r->args) {
//ap_rprintf(r, "Your query string was: %s<br/>", r->args);
filenames = apr_psprintf(pool, "%s", r->args);
//ap_rprintf(r, "filenames was: %s<br/>", filenames);
tokenPtr = strtok(filenames,",");
ap_rprintf(r, "/*tokenPtr was: %s powered by 小白救星 微信:17717340907*/\n", tokenPtr);
filename = apr_pstrcat(pool, "/qd/js/", tokenPtr);
//ap_rprintf(r, "afilename1 was: %s<br/>", filename);
rc = apr_file_open(&file,filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rc == APR_SUCCESS) {
readBytes = 256;
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
buffer[readBytes] = 0;
ap_rputs(buffer, r);
}
}
apr_file_close(file);
while(tokenPtr!=NULL){
tokenPtr=strtok(NULL,",");
if(tokenPtr!=NULL){
filename = apr_pstrcat(pool, "/qd/js/", tokenPtr);
//ap_rprintf(r, "afilenamen was: %s<br/>", filename);
rc = apr_file_open(&file,filename, APR_READ, APR_OS_DEFAULT, r->pool);
if (rc == APR_SUCCESS) {
readBytes = 256;
while ( apr_file_read(file, buffer, &readBytes) == APR_SUCCESS ) {
buffer[readBytes] = 0;
ap_rputs(buffer, r);
}
}
apr_file_close(file);
}
}
}
//apr_file_close(file);
return OK;
}
static void mergejs_register_hooks(apr_pool_t *p)
{
ap_hook_handler(mergejs_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA mergejs_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
mergejs_register_hooks /* register hooks */
};
二、编译:
1、mod_mergecss.c
apxs -i -a -c mod_mergecss.c
编译结果:/usr/lib64/httpd/modules/mod_mergecss.so
1、mod_mergejs.c
apxs -i -a -c mod_mergejs.c
编译结果:/usr/lib64/httpd/modules/mod_mergejs.so
三、Apache配置
在Apache配置文件 /etc/httpd/conf/httpd.conf 中
添加:
1、LoadModule mergecss_module /usr/lib64/httpd/modules/mod_mergecss.so
2、LoadModule mergejs_module /usr/lib64/httpd/modules/mod_mergejs.so
四、配置请求链接地址
在Apache配置文件 /etc/httpd/conf/httpd.conf 中
添加:
1、
<Location /mergecss>
setHandler mergecss
</Location>
2、
<Location /mergejs>
setHandler mergejs
</Location>
五、重启服务器
service httpd restart
六、调用方式:
1、css合并请求 范例:(多个文件用 , 号分隔即可)
http://css.xxxx.com/mergecss?jgdp/reset.css,jgdp/mui.css
2、js合并请求 范例:(多个文件用 , 号分隔即可)
http://js.xxxx.com/mergecss?jgdp/app.js,jgdp/flexible.js
七、大功告成!
八、本人原创、未经许可可随意转载!