Reputation: 31
I'm currently learning how to develop HTTP proxy using libcurl. Upon I successfully got HTTP responses, I wrote them into a string before I do further process using callback function. In the further process function, I need to convert those string into char* at first which leaded to a losing data problem for those HTTP responses contain image data like png and gif. While rest of HTTP responses contain pure text data like HTML and css were working fine.
My query and question is why the c_str() function eliminated those unreadable data like image data during conversion? Is there any way to solve this losing data problem?
Code: char* sData_source_ ;
void Client::send(string msg)
{
sData_source_ = (char*)msg.c_str();
cout << "Your string is " << strlen(sData_source_)<<endl;
}
Output: Sending HTTP Response[608]: FD:9 HTTP/1.1 200 OK Date: Wed, 29 Aug 2012 00:58:25 GMT Server: Apache/2.2.16 (Debian) Last-Modified: Tue, 28 Aug 2012 18:34:36 GMT ETag: "13e4735-136-4c857b1b54700" Accept-Ranges: bytes Keep-Alive: timeout=15, max=99 Content-Type: image/png Content-Length: 310 Connection: keep-alive �PNG
IHDR��o�|d�IT pHYs � �B�4� tEXtSoftwareMacromedia Fireworks MX��*$tEXtCreation Time12/06/04g�m�IDATx���1� E�u��r -�R)�]X֚��w�<Ѱ�1F���������� tX��!�Z��=:$TJ��{�3�CRgb:$v4v�Cb��(���B��!tH�L�[k�_wx8/:,@����� xQ�2]�|��IEND�B`� Your string is 306
Note: As you guys can observe from above output that the total HTTP response is 608 bytes initially. However, it becomes 306 after conversion using c_str() function.
Thank you and looking forward to someone reply. :D
Upvotes: 1
Views: 1961
Reputation: 31
Here is the solution after I modified my code, just want to share with others who may deal with same problem in future.
size_t SData_length;
void Client::send(string msg)
{
sData_length = msg.size();
// allocate memory for sData_source
sData_source_ = (char*)malloc(sData_length);
// copy data to sData_source
memcpy(sData_source_, msg.data(), sData_length);
// compare data after copy
if(memcmp(sData_source_, msg.data(), sData_length) == 0)
{
cout<<"Copy Data Succeed"<<endl;
}
}
Upvotes: 0
Reputation: 18848
Looks like you trying to treat a binary file as a string.
As soon as a \0
terminator is encountered, the string will end. Consider the following:
const char* foo = "abc\012345667";
int length = strlen(foo);
The length
is 3
, as the original string has been terminated after 3 characters.
Upvotes: 3