`
CharlesCui
  • 浏览: 417154 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Apache模块-下载文件-性能测试打靶用的靶子

阅读更多

apache模块,

下载某一个文件,

性能测试打靶用,

当靶子。

 

/* 
**  mod_preview.c -- Apache sample preview module
**  [Autogenerated via ``apxs -n preview -g'']
**
**  To play with this sample module first compile it into a
**  DSO file and install it into Apache's modules directory 
**  by running:
**
**    $ apxs -c -i mod_preview.c
**
**  Then activate it in Apache's httpd.conf file for instance
**  for the URL /preview in as follows:
**
**    #   httpd.conf
**    LoadModule preview_module modules/mod_preview.so
**    <Location /preview>
**    SetHandler preview
**    </Location>
**
**  Then after restarting Apache via
**
**    $ apachectl restart
**
**  you immediately can request the URL /preview and watch for the
**  output of this module. This can be achieved for instance via:
**
**    $ lynx -mime_header http://localhost/preview 
**
**  The output should be similar to the following one:
**
**    HTTP/1.1 200 OK
**    Date: Tue, 31 Mar 1998 14:42:22 GMT
**    Server: Apache/1.3.4 (Unix)
**    Connection: close
**    Content-Type: text/html
**  
**    The page from mod_preview.c
*/ 

#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 */
					ap_flush_conn(r->connection);
					if (fpos < size) {
						/* file grew by finfo.size - fpos */
						if (APR_SUCCESS != ap_send_fd(f, r, fpos, size - fpos, &sz)) {
							return HTTP_INTERNAL_SERVER_ERROR;
						}
						fpos += sz;
					} else {
						break;
					}
				}
			apr_file_close(f);
			return OK;
		}else{
			ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,r->server,"Open %s error!\n args : %s\n", fn,r->args);
			return DONE;
		}
	}
	return OK;
}

static void preview_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(preview_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA preview_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       */
    preview_register_hooks  /* register hooks                      */
};

 

 

编译:

apxs -c mod_preview.c

 

安装:

apxs -ia mod_preview.la

 

配置:

vim httpd.conf

 

添加:

<Location /preview>
        SetHandler preview
</Location>

 

注意apache进程的用户权限和被访问文件的权限

 

URL参数:

const char *regex = "filename=([^\\&]*)(.*)";

正则扣取filename后面的文件绝对路径。

分享到:
评论
1 楼 CharlesCui 2011-01-04  

问题一:
配置文件中:SetHandler后面的字符串,
也就是
if (strcmp(r->handler, "preview"))中的preview
这个字符串的长度有限制,太长的话模块不起作用。

问题二:
#     if (strcmp(r->handler, "preview")) { 
#         return DECLINED; 
#     }
这个要写在执行代码的最前面,否则每个请求都过来执行一下,然后运行到这步发现进错模块了。我上面写的不好。

相关推荐

    apache-maven-3.8.6.zip

    apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-...

    开发工具 apache-tomcat-8.0.41-windows-x86

    开发工具 apache-tomcat-8.0.41-windows-x86开发工具 apache-tomcat-8.0.41-windows-x86开发工具 apache-tomcat-8.0.41-windows-x86开发工具 apache-tomcat-8.0.41-windows-x86开发工具 apache-tomcat-8.0.41-...

    apache-hive-2.1.1-bin.tar

    apache-hive-2.1.1-bin.tar apache-hive-2.1.1-bin.tar apache-hive-2.1.1-bin.tarapache-hive-2.1.1-bin.tar apache-hive-2.1.1-bin.tar apache-hive-2.1.1-bin.tarapache-hive-2.1.1-bin.tar apache-hive-2.1.1-...

    apache-maven-3.8.6-bin.tar.tz--test

    apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-...

    Apache JMeter (apache-jmeter-5.5.zip)

    Apache JMeter (apache-jmeter-5.5.zip)可用于测试静态和动态资源、Web 动态应用程序的性能。 它可用于模拟服务器、服务器组、网络或对象上的重负载,以测试其强度或分析不同负载类型下的整体性能。 Apache JMeter...

    apacheds-kerberos-codec-2.0.0-M15-API文档-中文版.zip

    赠送jar包:apacheds-kerberos-codec-2.0.0-M15.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻

    apache-maven-3.8.8.zip

    apache-maven-3.8.8.zip压缩包内容: apache-maven-3.8.8-bin.tar.gz apache-maven-3.8.8-bin.zip apache-maven-3.8.8-src.tar.gz apache-maven-3.8.8-src.zip

    apache-tomcat-9.0.45-windows-x64

    apache-tomcat-9.0.45-windows-x64apache-tomcat-9.0.45-windows-x64apache-tomcat-9.0.45-windows-x64apache-tomcat-9.0.45-windows-x64apache-tomcat-9.0.45-windows-x64apache-tomcat-9.0.45-windows-x64apache-...

    Apache JMeter (apache-jmeter-5.5.tgz)

    Apache JMeter (apache-jmeter-5.5.tgz)可用于测试静态和动态资源、Web 动态应用程序的性能。 它可用于模拟服务器、服务器组、网络或对象上的重负载,以测试其强度或分析不同负载类型下的整体性能。 Apache JMeter...

    apache-tomcat-7.0.53

    apache-tomcat-7.0.53apache-tomcat-7.0.53apache-tomcat-7.0.53apache-tomcat-7.0.53

    apache-jmeter-5.2.1性能测试工具.zip

    apache-jmeter-5.2.1用于测试接口性能压力瓶颈,简单易用,打开直接运行bin目录下的jmeter.bat文件打开应用,可以设置并发数量和并发时间,接口开发者的利器

    下载慢?给你apache maven 3.x.x所有Linux, Windows版本下载的百度网盘链接

    apache maven 3.x.x所有Linux, Windows版本下载的百度网盘链接。 apache-maven-3.0.4-bin.tar.gz apache-maven-3.0.4-bin.zip apache-maven-3.0.5-bin.tar.gz apache-maven-3.0.5-bin.zip apache-maven-3.1.0-bin....

    apache-tomcat-7.0.77下载

    apache-tomcat-7.0.77下载apache-tomcat-7.0.77下载apache-tomcat-7.0.77下载apache-tomcat-7.0.77下载apache-tomcat-7.0.77下载

    apache-ant-1.9.16-bin.zip

    apache-ant-1.9.16-bin.zip

    apache-jmeter-5.6.3

    apache-jmeter-5.6.3.zip apache-jmeter-5.6.3.tgz apache-jmeter-5.6.3_src.zip apache-jmeter-5.6.3_src.tgz

    apache-maven-3.8.4-bin.zip maven下载鬼慢

    apache-maven-3.8.4-bin.zip

    apache-maven-3.6.1 安装包

    apache-maven-3.6.1-bin

    apache-tomcat-6.0.53-src

    apache-tomcat-6.0.53-src,apache tomcat 6.0.53的源码。 压缩包文件清单: apache-tomcat-6.0.53-src.tar.gz apache-tomcat-6.0.53-src.tar.gz.asc apache-tomcat-6.0.53-src.tar.gz.md5 apache-tomcat-6.0.53-...

    apache-jmeter-5.1.1.zip

    apache-jmeter-5.1.1 压力测试工具

    apache-maven-3.6.1-bin

    apache-maven-3.6.1-bin

Global site tag (gtag.js) - Google Analytics