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