curl源码分析(一)webkit中curl库的使用

来源:互联网 发布:2017中国云计算500强 编辑:程序博客网 时间:2024/05/29 19:09
webkit使用curl库,他就要去适应curl。当然curl也会做出努力能够让别人容易使用,这也是他份内的事。
这里主要是分析几个函数,了解回调函数的使用过程。也可以帮助我们在以后使用curl库。
webkit中有这么连续的四句,注册了在接收到网页的头和网页内容时候的回调函数。
curl_easy_setopt(d->m_handle, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(d->m_handle, CURLOPT_WRITEDATA, job);
curl_easy_setopt(d->m_handle, CURLOPT_HEADERFUNCTION, headerCallback);
curl_easy_setopt(d->m_handle, CURLOPT_WRITEHEADER, job);
首先我们的需求是通过回调函数把数据传送给webkit内核。完成这一步需要ResourceHandle,也就是这里的job.
我们把它注册到curl内部,最后再通过回调函数中第四个参数把这个值给传递回来,这样整个过程就完成了。
cur中回调函数的定义:

typedef size_t (*curl_write_callback)(char *buffer,                                      size_t size,                                      size_t nitems,                                      void *outstream);

curl内部处理过程:
1.注册
#define CURLOPT_WRITEDATA CURLOPT_FILE//这个宏定义需要注意curl_easy_setopt(d->m_handle, CURLOPT_WRITEFUNCTION, writeCallback);case CURLOPT_WRITEFUNCTION:    /*      * Set data write callback      */    data->set.fwrite_func = va_arg(param, curl_write_callback);    if(!data->set.fwrite_func) {data->set.is_fwrite_set = 0;/* When set to NULL, reset to our internal default function */data->set.fwrite_func = (curl_write_callback)fwrite;//注册write call back,用于传回数据    }    elsedata->set.is_fwrite_set = 1;    break;curl_easy_setopt(d->m_handle, CURLOPT_WRITEDATA, job);case CURLOPT_FILE:    /*      * FILE pointer to write to or include in the data write callback      */    data->set.out = va_arg(param, FILE *);    break;curl_easy_setopt(d->m_handle, CURLOPT_HEADERFUNCTION, headerCallback);case CURLOPT_HEADERFUNCTION:    /*      * Set header write callback      */    data->set.fwrite_header = va_arg(param, curl_write_callback);//注册头部数据callback函数    break;curl_easy_setopt(d->m_handle, CURLOPT_WRITEHEADER, job);case CURLOPT_WRITEHEADER:    /*      * Custom pointer to pass the header write callback function      */    data->set.writeheader = (void *)va_arg(param, void *);    break;


2.回调
最终的回调在Curl_client_write完成。
if(type & CLIENTWRITE_BODY) {  if((conn->protocol&PROT_FTP) && conn->proto.ftpc.transfertype == 'A') {#ifdef CURL_DOES_CONVERSIONS    /* convert from the network encoding */    size_t rc;    rc = Curl_convert_from_network(data, ptr, len);    /* Curl_convert_from_network calls failf if unsuccessful */    if(rc != CURLE_OK)      return rc;#endif /* CURL_DOES_CONVERSIONS */#ifdef CURL_DO_LINEEND_CONV    /* convert end-of-line markers */    len = convert_lineends(data, ptr, len);#endif /* CURL_DO_LINEEND_CONV */  }  /* If the previous block of data ended with CR and this block of data is      just a NL, then the length might be zero */  if(len) {    wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);//调用注册回调函数传回数据  }  else {    wrote = len;  }  if(CURL_WRITEFUNC_PAUSE == wrote)    return pausewrite(data, type, ptr, len);  if(wrote != len) {    failf(data, "Failed writing body (%zu != %zu)", wrote, len);    return CURLE_WRITE_ERROR;  }}if((type & CLIENTWRITE_HEADER) &&    (data->set.fwrite_header || data->set.writeheader) ) {  /*    * Write headers to the same callback or to the especially setup    * header callback function (added after version 7.7.1).    */  curl_write_callback writeit=    data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite_func;  /* Note: The header is in the host encoding      regardless of the ftp transfer mode (ASCII/Image) */  wrote = writeit(ptr, 1, len, data->set.writeheader);  if(CURL_WRITEFUNC_PAUSE == wrote)    /* here we pass in the HEADER bit only since if this was body as wellthen it was passed already and clearly that didn't trigger the pause,so this is saved for later with the HEADER bit only */    return pausewrite(data, CLIENTWRITE_HEADER, ptr, len);  if(wrote != len) {    failf (data, "Failed writing header");    return CURLE_WRITE_ERROR;  }}