使用LibCurl库的一些疑问
我使用libCurl的Pop3功能,连上邮箱后,获取邮箱列表,但是在CURLOPT_WRITEFUNCTION的回调函数里,内存指向的数据正确,但是获取的长度跟实际长度不相符,例如我的邮箱里有2封未读邮件,回调函数里,指向的内存数据为:31 20 33 36 36 34 0d 0a 32 20 31 38 35 38 0d 0a 2e 0d 0a(1 3664 和 2 1858),但是回调函数里第二个参数和第三个参数分别是 1和6 ,按照官方给出的方法,计算出来的数据长度是1*6=6 ,但实际有效长度应该是19(如果除去CRLF和'.',那么实际长度应该是12)
跟踪curl的源码,发觉在Curl_pop3_write里面,获取的实际长度也是19,但是在解析过程中,发现只解析到第一个CRLF就开始使用回调,造成获取的参数不对。
由于对pop3的协议不是很了解,看这个代码也不知道该如何修改。希望有知道的人可以指点一下。
下面附上Curl解析数据时,调用上层回调的源码,它是通过Curl_client_write来调用回调函数的
跟踪curl的源码,发觉在Curl_pop3_write里面,获取的实际长度也是19,但是在解析过程中,发现只解析到第一个CRLF就开始使用回调,造成获取的参数不对。
由于对pop3的协议不是很了解,看这个代码也不知道该如何修改。希望有知道的人可以指点一下。
下面附上Curl解析数据时,调用上层回调的源码,它是通过Curl_client_write来调用回调函数的
CURLcode Curl_pop3_write(struct connectdata* conn, char* str, size_t nread)
{
/* This code could be made into a special function in the handler struct */
CURLcode result = CURLE_OK;
struct Curl_easy* data = conn->data;
struct SingleRequest* k = &data->req;
struct pop3_conn* pop3c = &conn->proto.pop3c;
bool strip_dot = FALSE;
size_t last = 0;
size_t i;
/* Search through the buffer looking for the end-of-body marker which is
5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
the eob so the server will have prefixed it with an extra dot which we
need to strip out. Additionally the marker could of course be spread out
over 5 different data chunks. */
for (i = 0; i < nread-1; i++) {
size_t prev = pop3c->eob;
switch (str[i]) {
case 0x0d:
if (pop3c->eob == 0) {
pop3c->eob++;
if (i) {
/* Write out the body part that didn't match */
result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
i - last);
if (result)
return result;
last = i;
}
}
else if (pop3c->eob == 3)
pop3c->eob++;
else
/* If the character match wasn't at position 0 or 3 then restart the
pattern matching */
pop3c->eob = 1;
break;
case 0x0a:
if (pop3c->eob == 1 || pop3c->eob == 4)
pop3c->eob++;
else
/* If the character match wasn't at position 1 or 4 then start the
search again */
pop3c->eob = 0;
break;
case 0x2e:
if (pop3c->eob == 2)
pop3c->eob++;
else if (pop3c->eob == 3) {
/* We have an extra dot after the CRLF which we need to strip off */
strip_dot = TRUE;
pop3c->eob = 0;
}
else
/* If the character match wasn't at position 2 then start the search
again */
pop3c->eob = 0;
break;
default:
pop3c->eob = 0;
break;
}
/* Did we have a partial match which has subsequently failed? */
if (prev && prev >= pop3c->eob) {
/* Strip can only be non-zero for the very first mismatch after CRLF
and then both prev and strip are equal and nothing will be output
below */
while (prev && pop3c->strip) {
prev--;
pop3c->strip--;
}
if (prev) {
/* If the partial match was the CRLF and dot then only write the CRLF
as the server would have inserted the dot */
result = Curl_client_write(conn, CLIENTWRITE_BODY, (char*)POP3_EOB,
strip_dot ? prev - 1 : prev);
if (result)
return result;
last = i;
strip_dot = FALSE;
}
}
}
if (pop3c->eob == POP3_EOB_LEN) {
/* We have a full match so the transfer is done, however we must transfer
the CRLF at the start of the EOB as this is considered to be part of the
message as per RFC-1939, sect. 3 */
result = Curl_client_write(conn, CLIENTWRITE_BODY, (char*)POP3_EOB, 2);
k->keepon &= ~KEEP_RECV;
pop3c->eob = 0;
return result;
}
if (pop3c->eob)
/* While EOB is matching nothing should be output */
return CURLE_OK;
if (nread - last) {
result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
nread - last);
}
return result;
}
...全文
相关推荐
libcurl编译好的文件
**libcurl库详解**libcurl是一个强大的开源库,主要用于处理各种网络协议的文件传输,包括HTTP、HTTPS、FTP、FTPS等。
libcurl库(C++)快速使用
简介libcurl作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP这些协议,同时支持使用SSL
libcurl库支持ssl
在本文中,我们将深入探讨libcurl库与SSL的结合,以及如何在Windows环境下配置和使用这些库。
windows下libcurl库
**Windows下的libcurl库详解**libcurl是一个广泛使用的开源库,用于在各种操作系统平台上执行文件传输,特别是HTTP、HTTPS、FTP、FTPS等网络协议。
ios libcurl库的简单使用
libcurl库源于C语言编写,因此可以在多种平台上运行,包括iOS。本篇文章将深入探讨如何在iOS项目中集成和使用libcurl库进行网络通信。首先,我们需要在iOS项目中引入libcurl库。


工具平台和程序库

