简体   繁体   English

在C ++中通过CURL上传文件时,curl_easy_perform()崩溃

[英]Crash on curl_easy_perform() when uploading a file on CURL in C++

I'm having an issue with a crash when uploading a file via the curl library in C++. 通过C ++中的curl库上传文件时出现崩溃问题。 I'm using the exact demo code from this location: https://curl.haxx.se/libcurl/c/fileupload.html 我正在从以下位置使用确切的演示代码: https : //curl.haxx.se/libcurl/c/fileupload.html

The only thing I change in the code is the upload location, to upload to a local wamp server on windows, and the file to upload, which I've verified that its opening ok. 我在代码中唯一更改的是上载位置(要上载到Windows上的本地wamp服务器)和要上载的文件,我已经证实它可以打开。

I'm running through visual studio 2014, and building CURL through DLL 我正在运行Visual Studio 2014,并通过DLL构建CURL

The output from the program is: 该程序的输出为:

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> PUT /replayupload.php HTTP/1.1
Host: 127.0.0.1
Accept: */*
Content-Length: 43
Expect: 100-continue

< HTTP/1.1 100 Continue

*then I get a crash at line 66 in the program. *然后我在程序的第66行崩溃了。 It seems the line: 看来这行:

 res = curl_easy_perform(curl);

is causing the problem with an invalid parameter. 导致参数无效的问题。 I have verified that the curl variable is not null, but I'm finding it very difficult to get any more debug info than that, the call stack just references a memory address within the DLL. 我已经验证了curl变量不为null,但是我发现很难获得比此更多的调试信息,调用堆栈仅引用DLL中的内存地址。

I'm able to run the demo to just upload post variables and get a page, this runs fine without a crash. 我可以运行该演示程序,以仅上传帖子变量并获取页面,这可以正常运行而不会崩溃。 The crash only occurs when uploading a file. 仅在上传文件时发生崩溃。

my exact code is: 我的确切代码是:

int main(void)
{
    CURL *curl;
    CURLcode res;
    struct stat file_info;
    double speed_upload, total_time;
    FILE *fd;

    fd = fopen("E:\\testfiles\\test.txt", "rb"); /* open file to upload */
    if (!fd)
        return 1; /* can't continue */

              /* to get the file size */
    if (fstat(_fileno(fd), &file_info) != 0)
        return 1; /* can't continue */

    curl = curl_easy_init();
    if (curl) {
        /* upload to this place */
        curl_easy_setopt(curl, CURLOPT_URL,
            "http://127.0.0.1/testupload.php");

        /* tell it to "upload" to the URL */
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* set where to read from (on Windows you need to use READFUNCTION too) */
        curl_easy_setopt(curl, CURLOPT_READDATA, fd);

        /* and give the size of the upload (optional) */
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
            (curl_off_t)file_info.st_size);

        /* enable verbose for easier tracing */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        }
        else {
            /* now extract transfer info */
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

           fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
                speed_upload, total_time);

        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    fclose(fd);
    return 0;
}

Thanks to Tkausl for spotting the line 感谢Tkausl发现了这条线

/* set where to read from (on Windows you need to use READFUNCTION too) */

I added this line to my code 我将此行添加到我的代码中

curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread);

And now everything seems to work. 现在一切似乎都正常了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM