无法识别 CURL apache 模块中的自定义 header

标签 c apache loops redirect curl

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

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 检索标准 header ,但我想做的是插入一个 header ,通过我的服务器 URL 域 (127.0.0.1) 再次将它传递给我的代码,并使用该 header 确定是否curl 已至少执行一次。如果我没有正确执行此操作,那么将执行越来越多的处理函数,从而导致 apache 服务器锁定。

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

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

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

最佳答案

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

MinSpareServers 1
MaxSpareServers 1
MaxClients 1
StartServers 1

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

关于无法识别 CURL apache 模块中的自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32282561/

相关文章:

c - 实现 container_of 时的指针对齐

web-services - 如何同时运行Apache(httpd)和Tomcat?

c - C 中嵌套 For 循环的泛化

apache - WAMP 不会变绿。和 VCRUNTIME140.dll 错误

java - Apache Http 客户端覆盖用户定义的内容长度 header

Python - For 循环和计数器

JavaScript 原型(prototype)链可能无限循环查找

c - 在 C 中将 char 数组转换为 ULL 时不匹配,反之亦然

c - 在链表 C 中查找最高销量

c - 如何在 Eclipse IDE 中运行简单的 C 程序