Exemple #1
0
int
ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime)
{
	int stat = OC_NOERR;
	CURLcode cstat = CURLE_OK;
	size_t len;

	/* Set the URL */
	cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
	if (cstat != CURLE_OK)
		goto fail;

	/* send all data to this function  */
	cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
	if (cstat != CURLE_OK)
		goto fail;

	/* we pass our file to the callback function */
	cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf);
	if (cstat != CURLE_OK)
		goto fail;

        /* One last thing; always try to get the last modified time */
	cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);

	cstat = curl_easy_perform(curl);
	if(cstat == CURLE_PARTIAL_FILE) {
	    /* Log it but otherwise ignore */
	    oc_log(LOGWARN, "curl error: %s; ignored",
		   curl_easy_strerror(cstat));
	    cstat = CURLE_OK;
	}
	if(cstat != CURLE_OK) goto fail;

        /* Get the last modified time */
	if(filetime != NULL)
            cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
        if(cstat != CURLE_OK) goto fail;

	/* Null terminate the buffer*/
	len = ocbyteslength(buf);
	ocbytesappend(buf, '\0');
	ocbytessetlength(buf, len); /* dont count null in buffer size*/
#ifdef OCDEBUG
	oc_log(LOGNOTE,"buffersize: %lu bytes",(unsigned long)ocbyteslength(buf));
#endif

	return OCTHROW(stat);

fail:
	oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
	return OCTHROW(OC_ECURL);
}
Exemple #2
0
static void
dapaddyytext(DAPlexstate* lex, int c)
{
    ocbytesappend(lex->yytext,c);
}
Exemple #3
0
int
ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime,
           struct OCcredentials* creds)
{
    int stat = OC_NOERR;
    CURLcode cstat = CURLE_OK;
    size_t len;
    long httpcode = 0;
    char tbuf[1024];
    /* Set the URL */
    cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
    if (cstat != CURLE_OK)
        goto fail;

    if(creds != NULL && creds->password != NULL  && creds->username != NULL) {
        /* Set user and password */
#if defined (HAVE_CURLOPT_USERNAME) && defined (HAVE_CURLOPT_PASSWORD)
        cstat = curl_easy_setopt(curl, CURLOPT_USERNAME, creds->username);
        if (cstat != CURLE_OK)
            goto fail;
        cstat = curl_easy_setopt(curl, CURLOPT_PASSWORD, creds->password);
        if (cstat != CURLE_OK)
            goto fail;
#else
        snprintf(tbuf,1023,"%s:%s",creds->username,creds->password);
        cstat = curl_easy_setopt(curl, CURLOPT_USERPWD, tbuf);
        if (cstat != CURLE_OK)
            goto fail;
#endif
    }

    /* send all data to this function  */
    cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    if (cstat != CURLE_OK)
        goto fail;

    /* we pass our file to the callback function */
    cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf);
    if (cstat != CURLE_OK)
        goto fail;

    /* One last thing; always try to get the last modified time */
    cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);

    cstat = curl_easy_perform(curl);

    if(cstat == CURLE_PARTIAL_FILE) {
        /* Log it but otherwise ignore */
        oclog(OCLOGWARN, "curl error: %s; ignored",
              curl_easy_strerror(cstat));
        cstat = CURLE_OK;
    }
    httpcode = ocfetchhttpcode(curl);

    if(cstat != CURLE_OK) goto fail;

    /* Get the last modified time */
    if(filetime != NULL)
        cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
    if(cstat != CURLE_OK) goto fail;

    /* Null terminate the buffer*/
    len = ocbyteslength(buf);
    ocbytesappend(buf, '\0');
    ocbytessetlength(buf, len); /* dont count null in buffer size*/
#ifdef OCDEBUG
    oclog(OCLOGNOTE,"buffersize: %lu bytes",(off_t)ocbyteslength(buf));
#endif

    return OCTHROW(stat);

fail:
    oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
    switch (httpcode) {
    case 401:
        stat = OC_EAUTH;
        break;
    case 404:
        stat = OC_ENOFILE;
        break;
    case 500:
        stat = OC_EDAPSVC;
        break;
    case 200:
        break;
    default:
        stat = OC_ECURL;
        break;
    }
    return OCTHROW(stat);
}