Re: Using Curl to send a HTTP request to a local OSRM Server from an AGL service

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Denis, Sebastian,

If this may help, I interfaced libcurl with AFB framework to support fragmented download in asynchronous mode leveraging afb-binder mainloop. The API from the application look like:

 * send a request: err = httpSendGet(httpPool, ctxRqt->url, &curlOpts, tokens, sampleCallback, (void *)ctxRqt);
 * callback 'sampleCallback(httpRqtT *httpRqt)' is called when the full request is finished.
 * multiple call may run in parallel

Code is available at https://github.com/fulup-bzh/libcurl-mainloop

Fulup

On 26/05/2021 16:19, Denis Marin wrote:

Hello Sebastian,


Regarding code snippets provided recently. Please bear in mind that the callback for JSON downloads is oversimplified and will only work well for short payloads. 


Actually CURL documentation says, that it might be called multiple times for single payload. This means that sometimes on first call you will receive a half of your JSON, and on second time another half of it, each half is not a valid JSON :\


You might want to use some pre-allocated buffer plus write pointer in it to receive data.


OR you can let CURL and standard library do all this for you by using NULL as CURLOPT_WRITEFUNCTION and use open_memstream to allocate memory buffer which is writable like a file.


the whole download_json is below. just throw away the callback and you are safe.


struct json_object* download_json(const char* link)
{
CURL* curl_handle = curl_easy_init();
char* buf = NULL;
size_t buffer_size;
FILE* json_filestream = open_memstream(&buf, &buffer_size);

curl_easy_setopt(curl_handle, CURLOPT_URL, link);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL); // use default fwrite
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, json_filestream); // file pointer to default fwrite
curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 500);
(void)curl_easy_perform(curl_handle);
fclose(json_filestream); // now buf and buffer_size are valid
struct json_object* result = json_or_null(buf, buffer_size);
free(buf); // buffer allocated by open_memstream must be freed as per man(3)
curl_easy_cleanup(curl_handle);
return result;
}

BRs,

Denis.


От: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx <agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx> от имени sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Отправлено: 25 мая 2021 г. 19:24:54
Кому: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx
Тема: Re: Using Curl to send a HTTP request to a local OSRM Server from an AGL service
 
Hello Denis,

thanks alot for your reply, that definitely helps. I'll give it a try.

Best regards

Sebastian



This e-mail and any attachment(s) are intended only for the recipient(s) named above and others who have been specifically authorized to receive them. They may contain confidential information. If you are not the intended recipient, please do not read this email or its attachment(s). Furthermore, you are hereby notified that any dissemination, distribution or copying of this e-mail and any attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by replying to this e-mail and then delete this e-mail and any attachment(s) or copies thereof from your system. Thank you.


_._,_._,_

Links:

You receive all messages sent to this group.

View/Reply Online (#9207) | Reply To Group | Reply To Sender | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [list-automotive-discussions82@xxxxxxxxxxx]

_._,_._,_

[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux