cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: HTTP client as new child process

From: Ajay Tanpure <ajaytanpure92_at_gmail.com>
Date: Tue, 19 Feb 2013 15:35:03 +0530

When i run stand alone http client, it works i.e. request is sent to the
server and received the response from server.
But in case of child process, as have mentioned below, functions like
curl_easy_setopt(), didn't get called.
I have searched on web also regarding that, global_init() is NOT thread
safe.

I am using libcurl version 7.9.6

int main(void)
> {
> CURL *curl_handle;
> CURL *curl;
> struct MemoryStruct chunk;
> FILE *fp;
> fp = fopen("received.txt","w");
> CURLcode res;
> int fd[2],er, pid;
> char *a;
> //curl_global_init(CURL_GLOBAL_ALL);
>
> er = pipe(fd);
> if(er == -1)
> printf("\n*************************************\nerror while creating
> the pipe\n");
> else
> printf("\n*************************************\nSuccessfully created
> pipe\n");
>
> pid = fork();
>
> if(pid == 0)
> {
> close(fd[1]);
> read(fd[0],a,11);
> fprintf(fp,"%s", a);
> fclose(fp);
> chunk.memory = malloc(1);
> chunk.size = 0;
> // curl_global_init(CURL_GLOBAL_ALL);
> curl = curl_easy_init();
>
> if (curl)
> {
> curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/a.html"); //
> this is the URL to be fetched
> curl_easy_setopt(curl, CURLOPT_POSTFIELDS, a); // actual post
> DATA
> res = curl_easy_perform(curl); // perform a POST request
> if(res != CURLE_OK)
> fprintf(stderr, "curl_easy_perform() failed: %s\n",
> curl_easy_strerror(res));
> printf("POST success!!"); // check for success of POST
> request , depending on this you may call
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
> curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
> curl_easy_perform(curl);
> curl_easy_cleanup(curl);
>
> printf("%lu bytes retrieved\n", (long)chunk.size);
> printf("\n\nchunkmem-->\n\n%s",chunk.memory);
>
> if(chunk.memory)
> free(chunk.memory);
>
> curl_global_cleanup();
> }
>
> }
>
> else{
> close(fd[0]);
> write(fd[1],"he am here",11);
> }
> return 0;
> }
>
>
>
>

On Tue, Feb 19, 2013 at 3:23 PM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Tue, 19 Feb 2013, Ajay Tanpure wrote:
>
> I have used the ubuntu 12.04 LTS.
>>
>
> Which libcurl version is that?
>
>
> I have created the new child process using fork() system call.
>> I get data from parent process to child using pipe.
>> But curl_global_init() function didn't work because of which other curl
>> functions like curl_setopt(), curl_init() didn't work.
>>
>
> Those two latter ones are not libcurl functions, they look like PHP
> functions.
>
> What do you mean with they "didn't work" ? What exactly happens?
>
>
> global_init function is called in child process (i.e pid == 0).
>> Can anyone tell me how to make curl function work in child process.
>>
>
> Can you please show us an example program that shows your problem?
>
> --
>
> / daniel.haxx.se
> ------------------------------**------------------------------**-------
> List admin: http://cool.haxx.se/list/**listinfo/curl-library<http://cool.haxx.se/list/listinfo/curl-library>
> Etiquette: http://curl.haxx.se/mail/**etiquette.html<http://curl.haxx.se/mail/etiquette.html>
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-02-19