1 /*
2  * Copyright (C) 2008-2010 Geometer Plus <contact@geometerplus.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include <cstdlib>
21 #include <algorithm>
22 
23 #include <ZLStringUtil.h>
24 
25 #include "ZLNetworkReadToStringRequest.h"
26 
27 
ZLNetworkReadToStringRequest(const std::string & url,const ZLNetworkSSLCertificate & sslCertificate,std::string & buffer)28 ZLNetworkReadToStringRequest::ZLNetworkReadToStringRequest(const std::string &url, const ZLNetworkSSLCertificate &sslCertificate, std::string &buffer) :
29 	ZLNetworkGetRequest(url, sslCertificate),
30 	myBuffer(buffer),
31 	myDataSize(0) {
32 }
33 
doBefore()34 bool ZLNetworkReadToStringRequest::doBefore() {
35 	return true;
36 }
37 
doAfter(bool)38 bool ZLNetworkReadToStringRequest::doAfter(bool) {
39 	return true;
40 }
41 
handleHeader(void * ptr,size_t size)42 bool ZLNetworkReadToStringRequest::handleHeader(void *ptr, size_t size) {
43 	static const std::string prefix = "Content-Length: ";
44 	std::string headerData((const char *) ptr, size);
45 	if (ZLStringUtil::stringStartsWith(headerData, prefix)) {
46 		int len = atoi(headerData.c_str() + prefix.length());
47 		if (len > 0) {
48 			myDataSize = len;
49 		}
50 	}
51 	setPercent(0, myDataSize);
52 	return true;
53 }
54 
handleContent(void * ptr,size_t size)55 bool ZLNetworkReadToStringRequest::handleContent(void *ptr, size_t size) {
56 	myBuffer.append((const char *) ptr, size);
57 	// TODO: call setPercent(...)
58 	return true;
59 }
60