curl-library
using libcurl for HTTP PUT -> but curl_easy_perform always returns CURLE_OK
Date: Tue, 03 Jul 2012 11:07:53 +0200
it returns CURLE_OK although the url i want to upload to is not allowed
for upload.
CURLINFO_RESPONSE_CODE returns 403
CURLE_HTTP_RETURNED_ERROR or CURLE_UPLOAD_FAILED would be expected.
Any suggestions for that?
-------------------
CURLcode res;
BOOL bRet = FALSE;
struct _stat file_info;
char curl_errbuf[CURL_ERROR_SIZE];
curl_errbuf[0] = 0;
/* get the file size of the local file */
_tstat(sFile, &file_info);
/* get a FILE * of the same file, could also be made with
fdopen() from the previous descriptor, but hey this is just
an example! */
FILE* hd_src = _tfopen(sFile, _T("rb"));
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_WIN32/*CURL_GLOBAL_ALL*/);
/* get a curl handle */
CURL* curl = curl_easy_init();
if(curl && hd_src)
{
#ifdef _DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* HTTP PUT please */
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
/* specify target URL, and note that this URL should include a file
name, not only a directory */
curl_easy_setopt(curl, CURLOPT_URL, (LPCSTR)
CStringA(sUrlWithFile));
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* Buffer to receive error messages */
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
/* provide the size of the upload, we specicially typecast the
value
to curl_off_t since we must be sure to use the correct data
size */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, prog_cb);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
if(bAuthenticationRequired)
{
/* set the ioctl function */
curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_cb);
/* pass the file descriptor to the ioctl callback as well */
curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd_src);
/* tell libcurl we can use "any" auth, which lets the lib
pick one, but it
also costs one extra round-trip and possibly sending of
all the PUT
data twice!!! */
curl_easy_setopt(curl, CURLOPT_HTTPAUTH,
(long)CURLAUTH_ANY); // or CURLAUTH_ANYSAFE
ASSERT(!sAuthUser.IsEmpty() && !sAuthPW.IsEmpty());
/* set user name and password for the authentication */
curl_easy_setopt(curl, CURLOPT_USERPWD,
CStringA(sAuthUser) + ":" + CStringA(sAuthPW)); //"user:password");
}
StartWaitDlg(_T("Upload"), 0);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
bRet = (res == CURLE_OK);
if(!bRet) // check for errors
{
CStringA sLastError(curl_errbuf);
if(sLastError.IsEmpty())
sLastError = curl_easy_strerror(res);
DebugOutput(0, _T("Upload Error:"));
DebugOutput(0, CString(sLastError));
}
/* test */
long nHttpResonse = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &nHttpResonse);
long nOSErrno = 0;
curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &nOSErrno);
/* end test */
EndWaitDlg();
/* always cleanup */
curl_easy_cleanup(curl);
}
if(hd_src)
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return bRet;
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-07-03