⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uriparser8.cpp

📁 一个HTTP协议的封装类
💻 CPP
字号:
#include "StdAfx.h"
#include "UriParser8.h"
#include <string>
using namespace std;
#include "StringTool.h"

UrlParser8::UrlParser8()
{
	iUri=NULL;
	iHost=NULL;
	iBody=NULL;
}

UrlParser8::~UrlParser8()
{
	delete[] iBody;
	delete[] iUri;
	delete[] iHost;
	
	iUri=NULL;
	iHost=NULL;
	iBody=NULL;
}

bool UrlParser8::Parse( const char* aUrl )
{
	int length=strlen(aUrl);

	if (length<=7)//h t t p : / /
	{
		return false;
	}

	if (StringTool::Find(aUrl,"http://",-1)==-1)
	{
		return false;
	}

	const char* pUrl=aUrl+7;
	length-=7;

	//端口号开始的位置
	int posPort=StringTool::Find(pUrl,':');
	//Uri开始的位置
	int posUri=StringTool::Find(pUrl,'/');
	//数据开始的位置
	int posData=StringTool::Find(pUrl,'?');

	if (posPort==0 || posUri==0 || posData==0)
	{
		return false;
	}

	if (posPort>0)
	{
		delete[] iHost;
		iHost=new char[posPort+1];
		strncpy(iHost,pUrl,posPort);
		iHost[posPort]='\0';
	}
	else if (posUri>0)
	{
		delete[] iHost;
		iHost=new char[posUri+1];
		strncpy(iHost,pUrl,posUri);
		iHost[posUri]='\0';
	}
	else
	{
		delete[] iHost;
		iHost=new char[length+1];
		strncpy(iHost,pUrl,length);
		iHost[length]='\0';
	}
	
	if (posPort<0)
	{
		iPort=80;
	}
	else if(posPort>0)
	{
		if (posPort<length-1)
		{
			//端口
			if (posUri<0)//url以端口结尾
			{
				char* port=new char[length-posPort];
				strncpy(port,pUrl+posPort,length-posPort-1);
				port[length-posPort-1]='\0';
				iPort=StringTool::Str2Num(port);
				delete[] port;
			}
			else
			{
				if (posUri-posPort>1)
				{
					char* port=new char[posUri-posPort];
					strncpy(port,pUrl+posPort+1,posUri-posPort-1);
					port[posUri-posPort-1]='\0';
					iPort=StringTool::Str2Num(port);
					delete[] port;
				}
				else
				{
					iPort=80;
				}
			}
		}
		else
		{
			iPort=80;
		}
	}

	if (posUri>0)
	{
		if (posUri<length-1)
		{
			if (posData<0)
			{
				delete[] iUri;
				iUri=new char[length-posUri+1];
				strncpy(iUri,pUrl+posUri,length-posUri);
				iUri[length-posUri]='\0';
			}
			else
			{
				delete[] iUri;
				iUri=new char[posData-posUri+1];
				strncpy(iUri,pUrl+posUri,posData-posUri);
				iUri[posData-posUri]='\0';
			}
		}
	}

	if (posData>0)
	{
		if (posData<length-1)
		{
			delete[] iBody;
			iBody=new char[length-posData];
			strncpy(iBody,pUrl+posData+1,length-posData-1);
			iBody[length-posData-1]='\0';
		}
	}

	return true;
}

const char* UrlParser8::Host() const
{
	return iHost;
}

const char* UrlParser8::Uri() const
{
	return iUri;
}

const char* UrlParser8::Body() const
{
	return iBody;
}

int UrlParser8::Port() const
{
	return iPort;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -