strcat 不在 C 中添加字符串

strcat not prepending strings in C

我正在开发一个 apache 模块,该模块将多组斜线转换为单斜线以避免网站上的重复内容。我遇到的问题是 strcat 函数似乎没有做任何事情。我正在尝试使用它们将“http://example.com”添加到生成的 URL 之前,以便用户重定向到它最初包含 2 个或更多斜线粘在一起的组。

static int handler(request_rec *r){
    if (strcmp(r->handler,"httpd/unix-directory")==0){return DECLINED;}
    unsigned long flag=0,ct=0;
    char xi[100004],*xuri=xi,*up=r->unparsed_uri;
    *xuri='[=10=]';
    strcat(xuri,"http://");
    strcat(xuri,r->hostname);
    while (*up != '[=10=]'){
        if (*up=='/'){flag++;}else{flag=0;}
        if (flag < 2){*xuri=*up;xuri++;ct++;if (ct >= 100000){break;}}
        if (flag > 1){flag=2;}
        up++;
    }
    if (ct < 100000){
        if (ct > 0){xuri--;}
        if (*xuri=='/'){*xuri='[=10=]';}
        xuri++;*xuri='[=10=]';
        xuri=xi;up=r->unparsed_uri;
        if (strcmp(up,xuri)==0){return DECLINED;} //no redirect for same URL
        r->content_type = "text/html";
        apr_table_set(r->headers_out,"Location",xuri);
        return HTTP_MOVED_PERMANENTLY;
    }else{
        return HTTP_INTERNAL_SERVER_ERROR;
    }
}

当前任何 URL 的尾部(例如,http://example.com/123) is read and only the tail is outputted, but I want the http://example.com/ 部分的 /123 部分前置。

我怎样才能让 strcat 对我有利?

strcat 不更新 xuri。所以 xuri 在两次调用 strcat 之后仍然指向字符串的开头。因此,当代码到达语句 *xuri=*up;xuri++; 时,它会从头开始覆盖字符串。

您可以改用 sprintf 来解决问题,因为这样可以适当地更新 xuri,例如替换这三行

*xuri='[=10=]';
strcat(xuri,"http://");
strcat(xuri,r->hostname);

用这条线

xuri += sprintf( "http://%s", r->hostname );