strcat没有在C中添加字符串

时间:2015-08-11 06:08:44

标签: c string prepend strcat

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

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='\0';
    strcat(xuri,"http://");
    strcat(xuri,r->hostname);
    while (*up != '\0'){
        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='\0';}
        xuri++;*xuri='\0';
        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;
    }
}

目前读取任何网址的尾部(例如,来自http://example.com/123的/ 123部分)并且仅输出尾部,但我希望http://example.com/部分位于前面。

我能做些什么才能让strcat对我有利?

1 个答案:

答案 0 :(得分:2)

function submit() { var userName = $("#username").val(); var passWord = $("#password").val(); $.ajax({ url:"login.php" ,// let say your php file name within same location method:POST, data:{ username:userName, password:passWord }, success:function(data){ console.log(data) }, error:function() { console.log(error); } }); } <html> <body> <form> username:<input type="text" id="username"/> password:<input type="password" id="password" /> </form> <button type="button" id="submit-button" onclick="submit()">Submit</button> </body> </html> hope it works for you!! 无法更新strcat。因此xuri在两次调用xuri之后仍指向字符串的开头。因此,当代码到达语句strcat时,它会从头开始覆盖字符串。

您可以使用*xuri=*up;xuri++;来解决问题,因为这样可以让您适当地更新sprintf,例如替换这三行

xuri

这一行

*xuri='\0';
strcat(xuri,"http://");
strcat(xuri,r->hostname);