Esempio n. 1
0
bool KDirectory::createDir(const KData& dir)
{
	int pos = 0;
	while (true)
	{
		if ((pos = dir.find("/", pos)) == -1)
			pos = dir.length();
		KData dtDir = dir.substr(0, pos);
		pos++;
		if (!dtDir.isEmpty())
		{
			if (!isDirectoryExist(dtDir))
			{
				/*
				 if ( -1 == mkdir(dtDir.getData(),S_IRWXU|S_IRWXG|S_IRWXO) )
				 {
				 return false;
				 }
				 */
				if (MKDIR(dtDir.getData()) != 0)
				{
					return false;
				}

			}
		}
		if (pos >= (int) dir.length())
			break;
	}
	return true;
}
Esempio n. 2
0
bool KFile::setFile(KData fullFilePath, int mode)
{
	if (m_bIsOpen)
	{
		fclose (m_hFile);
		m_bIsOpen = false;
	}

#ifndef WIN32
	replaceAll(fullFilePath);
#endif

	if (fullFilePath.find("/") == -1)			//斜杠……
	{
		m_kFilePath = "./";
		m_kFileName = fullFilePath;
	}
	else
	{
		KData tStr = fullFilePath;
		if (tStr[tStr.length() - 1] == '/')
		{
			return false;
		}
		else
		{
			int pos = tStr.findlast("/");
			m_kFilePath = tStr.substr(0, pos + 1);
			m_kFileName = tStr.substr(pos + 1, tStr.length() - pos - 1);
		}
	}
	return open(mode);
}
Esempio n. 3
0
/*
 bool KDirectory::isDirEmpty( const KData& fullDir )
 {
 KData dtDir = fullDir;
 dtDir.makePath( true );
 DIR* dir;
 if ( (dir=opendir(dtDir.getData())) == NULL )
 return false;
 struct dirent *pDirent;
 while( ( pDirent = readdir(dir) ) != NULL  )
 {
 struct stat statbuf;
 KData dtPath = dtDir;
 dtPath += pDirent->d_name;
 if ( stat(dtPath.getData(),&statbuf) == -1 )
 continue;
 if ( S_ISDIR(statbuf.st_mode) )
 {
 if ( pDirent->d_name && strcmp(pDirent->d_name,".") && strcmp(pDirent->d_name,"..") )
 {
 if ( !isDirEmpty(dtPath) )
 {
 closedir( dir );
 return false;
 }
 }
 }
 else
 {
 closedir( dir );
 return false;
 }
 }
 closedir( dir );
 return true;

 }
 */
bool KDirectory::open(const KData& directory, bool create)
{
	if (directory.isEmpty())
	{
		return false;
	}

	KData dir = directory;
	int pos;
	KData dtKData = "\\";
	while ((pos = dir.find(dtKData)) != -1)
	{
		dir.replace(pos, 1, "/");
	}
	_directory.erase();

	bool bDirExist = isDirectoryExist(dir);
	if (!bDirExist)
	{
		if (create)
		{
			if (!createDir(dir))
				return false;
		}
		else
		{
			return false;
		}
	}
	_directory = dir;
	_directory.makePath();
	return true;
}
Esempio n. 4
0
int KHttp::getHttpFile(const KData& fullUrl, const KData& savefile,
		int startpos)
{
	string strPath = fullUrl;
	Trim(strPath);
	KData dtServer;
	KData dtFile;
	KData kDTFullUrl = strPath;

	LOGD("kDTFullUrl is %s",kDTFullUrl.getDataBuf());

	if (isEqualNoCase(kDTFullUrl.substr(0, 7), "http://"))
	{
		kDTFullUrl = kDTFullUrl.substr(7);
	}

	int nPos = kDTFullUrl.find("/");

	if (nPos == -1)
	{
		LOGERROR("nPos = -1");
		return 0;
	}
	else
	{
		dtServer = kDTFullUrl.substr(0, nPos);
		dtFile = kDTFullUrl.substr(nPos);
	}
	return getHttpFile(dtServer, dtFile, savefile, startpos);
}
Esempio n. 5
0
void KFile::replaceAll(KData& str)
{
	int pos;
	KData dtKData = "\\";
	while ((pos = str.find(dtKData)) != -1)
	{
		str.replace(pos, 1, "/");
	}
}
Esempio n. 6
0
bool KDirectory::createFileDir(const KData& dir)
{
	int pos = 0;
	while (true)
	{
		if ((pos = dir.find("/", pos)) == -1)
			break;
		KData dtDir = dir.substr(0, pos);
		pos++;
		if (!dtDir.isEmpty())
		{
			if (!isDirectoryExist(dtDir))
			{
				if (MKDIR(dtDir.getData()) != 0)
				{
					return false;
				}
			}
		}
	}
	return true;
}
Esempio n. 7
0
int KHttp::getHttpFileLength(const KData & fullUrl)
{
	KData server;
	KData httpFile;
	KData dtFullUrl = fullUrl;

	if (isEqualNoCase(dtFullUrl.substr(0, 7), "http://"))
	{
		dtFullUrl = dtFullUrl.substr(7);
	}

	int pos = dtFullUrl.find("/");
	if (pos == -1)
	{
		return -1;
	}
	else
	{
		server = dtFullUrl.substr(0, pos);
		httpFile = dtFullUrl.substr(pos);
	}

	m_bChunked = false;
	m_iWriteLen = 0;
	m_iLength = 0;
	m_clientSock.close();
	m_kCnnect.close();

	_respHeadMap.clear();
	m_iNotifyPos = 0;
	m_iNotifyGap = 0;
	m_iContentLength = 0;
	m_iStatusCode = 0;
	bool bGet = (_paramMap.size() == 0);

	KFile file;
	KData httpRequest;
	KData dtPost;

	if (bGet)
	{
		httpRequest = "GET ";
	}
	else
	{
		httpRequest = "POST ";
		map<KData, KData>::iterator iter;
		for (iter = _paramMap.begin(); iter != _paramMap.end(); iter++)
		{
			if (iter != _paramMap.begin())
				dtPost += "&";
			dtPost += iter->first;
			dtPost += "=";
			dtPost += iter->second;
		}
	}

	httpRequest += httpFile;
	httpRequest += " HTTP/1.1";
	httpRequest += CRLF;
	httpRequest += "Host: ";
	httpRequest += server;
	httpRequest += CRLF;
	httpRequest += "Accept: */*";
	httpRequest += CRLF;
	if (!m_dtUserAgent.isEmpty())
	{
		httpRequest += "User-Agent: ";
		httpRequest += m_dtUserAgent;
		httpRequest += CRLF;
	}

	httpRequest += "Pragma: no-cache";
	httpRequest += CRLF;
	httpRequest += "Cache-Control: no-cache";
	httpRequest += CRLF;
	httpRequest += "Connection: close";
	httpRequest += CRLF;

	if (!bGet)
	{
		httpRequest += "Content-Type: application/x-www-form-urlencoded";
		httpRequest += CRLF;
		httpRequest += "Content-Length: ";
		httpRequest += KData((int) dtPost.length());
		httpRequest += CRLF;
	}

	httpRequest += CRLF;

	char buff[MTU] =
	{ 0 };

	m_clientSock.setServer(server, 80);
	if (!m_clientSock.connect())
	{
		return 0;
	}

	m_kCnnect = m_clientSock.getConn();
	m_kCnnect.setTimeout(m_timeout);

	if (m_kCnnect.writeData(httpRequest) != (int) httpRequest.length())
		return 0;
	m_iWritedBytes += httpRequest.length();

	if (!bGet)
	{
		if (m_kCnnect.writeData(dtPost) != (int) dtPost.length())
			return 0;
		m_iWritedBytes += dtPost.length();
	}

	int iRead;
	m_iStatusCode = 0;

	memset(buff, 0, MTU);

	if ((iRead = m_kCnnect.readLine(buff, MTU)) <= 0)
	{
		m_iReadedBytes += iRead;
		return 0;
	}

	KData dtKData;
	KData dtLine(buff, iRead);
	if (dtLine.match(SP, &dtKData, true) == NOT_FOUND)
	{
		return 0;
	}
	if (dtKData != "HTTP/1.1" && dtKData != "HTTP/1.0")
	{
		return 0;
	}
	if (dtLine.match(SP, &dtKData, true) == NOT_FOUND)
	{
		return 0;
	}
	m_iStatusCode = (int) dtKData;

	while ((iRead = m_kCnnect.readLine(buff, MTU)) > 0)
	{
		m_iReadedBytes += iRead;
		KData dtLine(buff, iRead);

		KData dtBefVal;
		if (FOUND == dtLine.match(":", &dtBefVal, true))
		{
			dtBefVal.removeSpaces();
			dtLine.removeSpaces();
			if (isEqualNoCase(dtBefVal, "Content-Length"))
			{
				return (int) dtLine;
			}
		}
	}

	return 0;
}