cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: reagrding error in getting complete server response

From: m brandenberg <mcbinc_at_panix.com>
Date: Mon, 18 Nov 2013 02:57:47 -0500 (EST)

Your problems have little to do with curl and much
to do with basic programming. This may not be the best
forum for the latter.

On Mon, 18 Nov 2013, Sunil Chandrasekharan wrote:

> Hi All,
>
> Kindly find my complete code
>
> =================
>
> struct pageInfo_t {
> char *data;
> size_t len;
> };
> static size_t HTTPData(void *buffer, size_t size, size_t nmemb, void
> *userData)
> {
> size_t length = (size * nmemb);
> struct pageInfo_t *page = (struct pageInfo_t *)userData;
> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n data size=%ld
> nmemb=%ld ====== \n",size,nmemb);

Get your data types and print specifiers in agreement.

> page->data = realloc(page->data,page->len + length +1);
> if(page->data == NULL)
> {
> // out of memory!
> return 0;
> }
> memcpy(&page->data[page->len], buffer, length);
> page->len += length;
> page->data[page->len] = 0;
> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n Need to print
> size : %d ====== \n", length);

Same: get data types and specifiers in agreement.

> return length;
> }
>
> char *buffer;

Better as an 'auto' variable in-function.

> struct pageInfo_t page;
> jstring Java_com_samsung_jnitest_MainActivity_JNIGetWebpage( JNIEnv* env,
> jobject entryObject, jstring webpageJStr)
> {
> CURL *curl;
> CURLcode res;
> long response_code = 0;
> double content_length = 0;
> const jbyte *webpage;
> char *hostname, *username, *password;
> webpage = (*env)->GetStringUTFChars(env, webpageJStr, NULL);
> if (webpage == NULL) {
> return NULL; /* OutOfMemoryError already thrown */
> }
> page.data = (char *)malloc(4096);
> page.len = 0;
>
> curl = curl_easy_init();
> if(curl)
> {
> curl_easy_setopt(curl, CURLOPT_URL, webpage);
> curl_easy_setopt(curl, CURLOPT_TIMEOUT, 100);
> curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 50);
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPData);
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&page);
> curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
> res = curl_easy_perform(curl);
> curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
> curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
> &content_length);

Extract response_code and content_length after checking the
status of curl_easy_perform() and verifying success.

> if(res != CURLE_OK)
> {
> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "curl_easy_perform()
> failed: %s\n", curl_easy_strerror(res));
> return (*env)->NewStringUTF(env,curl_easy_strerror(res));

Leaks curl, page.data and webpage heap on error return.

> }
> else
> {
> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:content
> length(%d)======\n", strlen(page.data));

Refers to 'content length' but prints 'page.data'.

Get format specification and data type in agreement.

> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:page
> size(%ld)======\n", content_length);

Refers to 'page size' but prints 'content_length.

Get format specification and data type in agreement.

'content_length' need not be supplied by the HTTP server.

> __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n OUTPUT:%s
> ======\n", page.data);

If HTTPData is never called (because there is no data),
page.data will not be NUL-terminated as expected.

> buffer = (char *)malloc(2 * strlen(page.data));

If a small amount of data is in 'page.data', (2 * strlen(page.data))
may be shorter than the string generated below.

'page.len' has been correctly maintained. Use it.

> sprintf(buffer, "%ld:%s \n",response_code, page.data);

snprintf() and related variants are better for this usage.

> }
>
> /* always cleanup */
> (*env)->ReleaseStringUTFChars(env, webpageJStr, webpage);
> curl_easy_cleanup(curl);
> if(page.data){
> free(page.data);

Leaves a stale pointer in page.data between invocations.

> }
> return (*env)->NewStringUTF(env,buffer);

Expects 'buffer' to be valid 'Modified utf-8'. No such
guarantee from sprintf().

Leaks 'buffer' on return.

> }
>

--
Monty Brandenberg
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-11-18