[英]Trying to force Keep-alive via apache module
The task seems simple for me but I'm stumped. 这个任务对我来说似乎很简单,但是我很沮丧。 What I'm trying to achieve with this code is for
connection:keep-alive
to be added to every output header no matter what page I request from the server. 我试图用这段代码实现的目的是
connection:keep-alive
无论我从服务器请求什么页面,都将connection:keep-alive
添加到每个输出标头中。 Then later, I'll only add that header if the page returned gives a 301 or 302 (redirect status). 然后,仅在返回的页面给出301或302(重定向状态)的情况下,我才添加该标头。 This is because I don't want clients to have to open a new connection as a result of reaching a redirect page.
这是因为我不希望客户端由于进入重定向页面而不得不打开新连接。
My code is shown below and I tried following instructions from http://www.apachetutor.org/dev/brigades but instead made things simpler and easier to understand. 我的代码如下所示,我尝试按照http://www.apachetutor.org/dev/brigades上的说明进行操作,但使事情变得更容易理解。
The code compiles fine with apxs but it does not do anything to the output. 该代码可以使用apxs正常编译,但是对输出没有任何作用。 What could I be doing wrong?
我可能做错了什么? I'm looking for a solution that won't waste unnecessary memory.
我正在寻找不会浪费不必要内存的解决方案。
#include "httpd.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_config.h"
#include "apr_buckets.h"
#include "apr_general.h"
#include "apr_lib.h"
#include "util_filter.h"
#include "http_request.h"
static apr_status_t OUTF(ap_filter_t *f,apr_bucket_brigade *pbbIn){
char *lp=0;
const char *data;
request_rec* r=f->r;
conn_rec* c=r->connection;
apr_bucket *pbktIn;
apr_size_t len;
for (pbktIn=APR_BRIGADE_FIRST(pbbIn);pbktIn!=APR_BRIGADE_SENTINEL(pbbIn);pbktIn=APR_BUCKET_NEXT(pbktIn)){
if(APR_BUCKET_IS_EOS(pbktIn)){continue;}
if (apr_bucket_read(pbktIn,&data,&len,APR_BLOCK_READ) != APR_SUCCESS){continue;}
if (!lp){
lp=strstr(data,"\r\n");
if (lp){
apr_bucket_split(pbktIn,(lp-data));
pbktIn=APR_BUCKET_NEXT(pbktIn);
const char* ka="connection:keep-alive\r\n";
//trying to insert "connection:keep-alive" into output data
APR_BUCKET_INSERT_BEFORE(pbktIn,apr_bucket_transient_create(ka,strlen(ka),c->bucket_alloc));
apr_bucket_split(pbktIn,strlen(ka));
APR_BUCKET_REMOVE(pbktIn);
pbktIn=APR_BUCKET_NEXT(pbktIn);
}
}
}
return ap_pass_brigade(f->next,pbbIn);
}
static void INS(request_rec *r){ap_add_output_filter("30X",NULL,r,r->connection);}
static void f301_register_hooks(apr_pool_t *p){
ap_hook_insert_filter(INS,NULL,NULL,APR_HOOK_FIRST);
ap_register_output_filter("30X",OUTF,NULL,AP_FTYPE_RESOURCE);
}
module AP_MODULE_DECLARE_DATA f301_module = {STANDARD20_MODULE_STUFF,NULL,NULL,NULL,NULL,NULL,f301_register_hooks};
In HTTP 1.1, persistent connections are the default. 在HTTP 1.1中,持久连接是默认设置。 Just make sure
KeepAlive on
is in your server config and don't overthink it. 只需确保在您的服务器配置中
KeepAlive on
并且不要过分考虑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.