日期:2014-05-17  浏览次数:22472 次

Apache模块 请大家帮忙分析下哪里有问题?
关于正则匹配的地方详细说下(http://localhost/preview?filename=a.dat)有什么方法把a.dat直接取出来给fn。
#include "httpd.h"  
#include "http_config.h"  
#include "http_protocol.h"  
#include "ap_config.h"  
#include "ap_regex.h"  
#include "http_log.h"  
  
/* The content handler */  
static int preview_handler(request_rec *r)  
{  
  char *fn;// = "/usr/local/httpd-2.3.8/include/httpd.h";  
  apr_file_t *f = NULL;  
  apr_status_t rv;  
  apr_size_t sz;  
  
  ap_regex_t *preg;  
  const char *regex = "filename=([^\\&]*)(.*)";  
  int regRet = AP_REG_NOMATCH;  
  int nmatch = AP_MAX_REG_MATCH;  
  ap_regmatch_t pmatch[nmatch];  
  
  if(strlen(r->args) == 0){  
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"No args.");  
  return HTTP_INTERNAL_SERVER_ERROR;  
  }else{  
  if(ap_regcomp(preg,regex,0) == 0){
regRet = ap_regexec(preg,r->args,nmatch,pmatch,AP_REG_EXTENDED|AP_REG_ICASE);
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"Compile a regular expression. %s",regex);  
  }  
  else{  
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server," Compile regular expression fail.");  
  ap_rputs("ap_regexec error.",r);  
  return DONE;  
  }  
  
  if(regRet == 0){  
  fn = (char *)calloc(pmatch[1].rm_eo - pmatch[1].rm_so + 1,sizeof(char));  
  memcpy(fn,r->args+pmatch[1].rm_so,pmatch[1].rm_eo - pmatch[1].rm_so);  
  rv = apr_file_open(&f,fn,APR_READ|APR_SENDFILE_ENABLED,APR_OS_DEFAULT,r->pool);  
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"Get matched parameter : %s",fn);  
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"File open status : %d",rv);  
  }else{  
  ap_rprintf(r,"Reguler Expression is not matched %s.\n",r->args);  
  ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"Reguler Expression is not matched.");  
  return DONE;  
  }  
  }  
  
  if (strcmp(r->handler, "preview")) {  
  return DECLINED;  
  }  
  r->content_type = "text/html";  
  
  if (!r->header_only){  
  if(rv == APR_SUCCESS){  
  apr_finfo_t info;  
  apr_stat(&info,fn,APR_FINFO_SIZE,r->pool);  
  apr_size_t size = (apr_size_t)info.size;  
  if (APR_SUCCESS != ap_send_fd(f, r, 0, size, &sz)) {  
  return HTTP_INTERNAL_SERVER_ERROR;  
  }  
  apr_off_t fpos = sz;  
  
  while (1) {  
  /* flush output first */