【问题标题】:custom headers in CURL apache module not being recognizedCURL apache 模块中的自定义标头无法识别
【发布时间】:2015-08-29 03:59:08
【问题描述】:

这是我到目前为止的代码。请忽略未使用的变量,因为我只显示重要的代码片段:

typedef struct{
    char* rheaders[500][500]; //HTML headers
    char* curltemp[50000];    //Temp string space for CURL function
    char* alias[5000];
    char* idx[5000];
    char* redirurl[5000];
    unsigned long rhdrct;
    unsigned long idxct;
    unsigned long aliasct;
    unsigned long notrail;
    unsigned long nologall;
    unsigned long httpstatus;
    unsigned long print;
}Iconfig;
static Iconfig conf;
static CURL *curl;

static size_t curlH(char *buffer,size_t size,size_t nitems,void *userdata){
    //store data as it comes in. This works
    snprintf((char*)conf.rheaders[conf.rhdrct++],(size*nitems)-1,"%s\0",(char*)buffer);
    return (size*nitems);
}

static int getnewURL(request_rec *r){
    curl=curl_easy_init();
    if (curl){
        struct curl_slist *chunk=NULL;char cc[1000];
        //This header below isn't being sent with request
        chunk = curl_slist_append(chunk, "x-custom: 1;"); 
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
        //Request from server this code is executed on
        curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/test");
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_HEADERDATA,conf.curltemp);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,curlH);
        CURLcode res=curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        //iterate and display all headers after request is done
        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, r->server, "Headers...");
        unsigned long i;
        for (i=0;i<conf.rhdrct;i++){
            ap_log_error(APLOG_MARK, APLOG_CRIT, 0, r->server, "%d %s",i,conf.rheaders[i]);
        }
    }
}

static int handler(request_rec *r){
    // This happens on every request including those from CURL
    if (!ap_is_initial_req(r)){return DECLINED;}
    //check headers to see if x-custom is set to 1
    const char*a=apr_table_get(r->headers_in,"x-custom"); 
    if (a){
        if (strcmp("1",a)==0){
            //pass on new x-received header sent via CURL and process normally
            //but this stage never happens
            char b[1000];sprintf(b,"%s recvd\0",a);
            apr_table_set(r->headers_out,"x-received:",b);
            ap_log_error(APLOG_MARK, APLOG_CRIT, 0, r->server, "Subrequest");
            return DECLINED;
        }
    }
    //execute request in CURL and stop this apache process
    getnewURL(r);
    return DONE;
}

我设法通过我的代码从不同的 URL 检索标准标头,但我想做的是插入一个标头,通过我的服务器 URL 域 (127.0.0.1) 再次将其传递给我的代码,并使用该标头确定是否curl 至少执行过一次。如果我没有正确执行此操作,则会执行越来越多的处理程序函数,从而导致 apache 服务器锁定。

我的最终目标是创建一个内部重定向系统,以便当客人从服务器请求一个导致 http 301 状态重定向的 URL 到导致另一个 http 301 状态重定向等的 url 时,服务器将整合这些重定向,以便用户只需要忍受一个重定向。

我的问题在于//but this stage never happens 行。

有谁知道我该如何解决这个问题,或者有没有更好的方法让 apache 模块(处理函数)识别传入的 URL 请求来自我的 getnewURL 函数?

【问题讨论】:

    标签: c apache loops redirect curl


    【解决方案1】:

    没关系。我想到了。我的 apache 配置文件中有这些设置:

    MinSpareServers 1
    MaxSpareServers 1
    MaxClients 1
    StartServers 1
    

    然后我将数字从 1 更改为 5,现在一切正常。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多