cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Segmentation fault in SCO Unixware with libcurl,why?

From: 黄志军 <hzhijun_at_huawei.com>
Date: Wed, 20 Oct 2004 17:50:47 +0800

Hi, Daniel,

     below is more information

**********************************************************
sco-cry.sco-cry% http-post
* About to connect() to 10.71.115.69 port 80
* Connected to 10.71.115.69 (10.71.115.69) port 80
> POST /sendpost.php HTTP/1.1
Host: 10.71.115.69
Pragma: no-cache
Accept: */*
Content-Length: 20
Content-Type: application/x-www-form-urlencoded

name=daniel&tel=curlSegmentation Fault (core dumped)

*************************************************************
sco-cry.sco-cry% post-callback
* About to connect() to 10.71.115.69 port 80
* Connected to 10.71.115.69 (10.71.115.69) port 80
> POST /sendpost.php HTTP/1.1
Host: 10.71.115.69
Pragma: no-cache
Accept: */*
Content-Length: 14
Content-Type: application/x-www-form-urlencoded

Segmentation Fault (core dumped)

**************************http-post.c****************************
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    //curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
    curl_easy_setopt(curl, CURLOPT_URL, "http://10.71.115.69/sendpost.php");
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&tel=curl");
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    printf("res=%d\n", res);
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

****************************post-callback.c***************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>

#define TRUE 1
//char data[]="this is what we post to the silly web server";
char data[]="name=ha&tel=23";

struct WriteThis {
  char *readptr;
  int sizeleft;
};

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if(pooh->sizeleft) {
    *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
    pooh->readptr++; /* advance pointer */
    pooh->sizeleft--; /* less data left */
    return 1; /* we return 1 byte at a time! */
  }

  return -1; /* no more data left to deliver */
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  struct WriteThis pooh;

  pooh.readptr = data;
  pooh.sizeleft = strlen(data);

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. */
// curl_easy_setopt(curl, CURLOPT_URL,
// "http://receivingsite.com.pooh/index.cgi");
    curl_easy_setopt(curl, CURLOPT_URL, "http://10.71.115.69/sendpost.php");
    /* Now specify we want to POST data */
    curl_easy_setopt(curl, CURLOPT_POST, TRUE);

    /* Set the expected POST size */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);

    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* pointer to pass to our read function */
    curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);

    /* get verbose debug output please */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
Received on 2004-10-20