예제 #1
0
파일: HTTPUrl.cpp 프로젝트: Smi1le/OOP
Protocol CHttpUrl::CheckProtocol(boost::string_ref const &url, size_t &index)
{
	Protocol protocol;
	auto pos = url.find("://");
	if (pos != url.size())
	{
		if (url.substr(0, pos) == "https")
		{	
			protocol = Protocol::HTTPS;
		}
		else if (url.substr(0, pos) == "http")
		{
			protocol = Protocol::HTTP;
		}
		else
		{
			throw std::invalid_argument("Protocol uncorrect.");
		}
		if (index == url.size())
		{
			throw std::invalid_argument("Invalid url was introduced");
		}
	}
	index = pos + 3;
	return protocol;
}
예제 #2
0
unsigned short CHttpUrl::ParsePort(boost::string_ref & str)
{
	if (str.front() == ':')
	{
		auto portPos = str.find('/');
		string port;
		if (portPos == boost::string_ref::npos)
		{
			port = str.substr(1, str.size()).to_string();
		}
		else
		{
			port = str.substr(1, portPos - 1).to_string();
		}
		str = str.substr(port.size() + 1, str.size());
		bool portOk = !port.empty();
		if (portOk)
		{
			try
			{
				return boost::lexical_cast<unsigned short>(port);
			}
			catch (...)
			{
				portOk = false;
			}
		}
		if (!portOk)
		{
			throw CUrlParsingError("Port parsing error");
		}
	}
	return  0;
}
예제 #3
0
std::string CHttpUrl::ParseDomain(boost::string_ref & url)
{
	auto domainPos = url.find(':');
	if (domainPos == boost::string_ref::npos)
	{
		domainPos = url.find("/");
		domainPos = (domainPos == boost::string_ref::npos ? url.size() : domainPos);
	}
	auto domain = url.substr(0, domainPos).to_string();
	url = url.substr(domainPos, url.size());
	return domain;
}
예제 #4
0
Protocol CHttpUrl::ParseProtocol(boost::string_ref & str)
{
	const string schemeDelimiter = "://";
	auto schemePos = str.find(schemeDelimiter);
	if (schemePos == boost::string_ref::npos)
	{
		throw CUrlParsingError("Protocol parsing error");
	}
	string protocol = str.substr(0, schemePos).to_string();

	str = str.substr(schemePos + schemeDelimiter.size() , str.size() - 1);

	return ToProtocol(protocol);
}
예제 #5
0
파일: HTTPUrl.cpp 프로젝트: Smi1le/OOP
std::string CHttpUrl::ParseDomainName(boost::string_ref const &url, size_t &index)
{
	for (size_t i = 0; i != url.size(); ++i)
	{
		if (url[i] == '/')
		{
			index += i;
			return std::string(url.substr(0, i));
		}
		if (url[i] == ':')
		{
			m_port = ParsePort(url, i + 1);
			return std::string(url.substr(0, i));
		}
	}
	index += url.size();
	return std::string(url);
}
예제 #6
0
boost::iterator_range<
	boost::filter_iterator<glob_matcher, directory_iterator>
>
match_files(const boost::string_ref glob)
{
	auto end = glob.rfind(PLATFORM_DIRECTORY_SEPARATOR);
	if (end == boost::string_ref::npos) {
		throw std::invalid_argument{"Invalid glob string."};
	}

	return {
		boost::make_filter_iterator(
			glob_matcher{glob.substr(end + 1)},
			directory_iterator{glob.substr(0, end)},
			directory_iterator{}
		),
		boost::make_filter_iterator(
			glob_matcher{},
			directory_iterator{},
			directory_iterator{}
		)
	};
}
				bool HttpAbpBaseFilter::IsMatch(boost::string_ref data, const HttpAbpFilterSettings dataSettings, boost::string_ref dataHost) const
				{
					if (!SettingsApply(dataSettings, m_settings))
					{
						return false;
					}

					size_t i = 0;

					auto len = m_ruleParts.size();

					size_t lastMatch = 0;

					for (i = 0; i < m_ruleParts.size(); ++i)
					{
						switch (m_rulePartTypes[i])
						{
							// Anchored address matching is basically a confusing way to say that we
							// must match against the host of the request, AFAIK.
							// 
							// However, we have a double wammy. If we match against the host, we
							// need to then find that same matched string in the full request and
							// substring the data from beyond our matched address string. This is a
							// PITA and a bit of a waste, but we check the dataHost member first
							// specifically, to avoid false positives, such as Google search results
							// that embed a URL we're trying to match against in GET parameters.
							case RulePartType::AnchoredAddress:
							{
								auto hostLen = dataHost.size();
								auto plen = m_ruleParts[i].size();
								if (plen <= hostLen)
								{
									auto res = dataHost.find(m_ruleParts[i]);

									if (res != boost::string_ref::npos)
									{
										auto hostInReqPos = data.find(dataHost);

										if (hostInReqPos != boost::string_ref::npos)
										{
											lastMatch = hostInReqPos + res + plen;
											continue;
										}
									}
								}

								return false;
							}
							break;

							case RulePartType::Wildcard:
							{
								// Wildcard, so as long as we have one additional character, we can move on.
								if (lastMatch + 1 <= data.size())
								{
									++lastMatch;
									continue;
								}

								return false;
							}
							break;

							case RulePartType::Separator:
							{
								if (lastMatch < data.size())
								{
									data = data.substr(lastMatch);

									auto sepPosition = data.find_first_of(SeparatorStrRef);

									if (sepPosition != boost::string_ref::npos)
									{
										lastMatch = sepPosition + 1;
										continue;
									}
								}	
								
								return false;
							}
							break;

							case RulePartType::StringLiteral:
							{
								if (lastMatch < data.size())
								{
									data = data.substr(lastMatch);
									size_t literalTextPosition = data.find(m_ruleParts[i]);

									if(literalTextPosition != boost::string_ref::npos)
									{
										lastMatch = literalTextPosition + m_ruleParts[i].size();
										continue;
									}
								}
								
								return false;
							}
							break;

							// Must be an exact match.
							case RulePartType::RequestLiteralMatch:
							{
								return util::string::Equal(data, m_ruleParts[i]);
							}
							break;

							// Basically just a substring match against the start of the request.
							case RulePartType::RequestLiteralPartialMatch:
							{								
								auto plen = m_ruleParts[i].size();
								auto reqSize = data.size();
								if (plen <= reqSize)
								{
									auto sub = data.substr(0, plen);

									if (util::string::Equal(m_ruleParts[i], sub))
									{
										lastMatch = plen;
										continue;
									}
								}

								return false;
							}
							break;
						}
					}

					// All matches were found successfully so, we matched
					return true;
				}
예제 #8
0
파일: proto-irc.cpp 프로젝트: sehe/hexchat
/* irc_inline() - 1 single line received from serv */
void
server::p_inline (const boost::string_ref& text)
{
	session *sess;
	char *type;
	char *word[PDIWORDS+1];
	char *word_eol[PDIWORDS+1];
	message_tags_data tags_data = message_tags_data();

	std::string pdibuf(text.size(), '\0');

	sess = this->front_session;

	/* Python relies on this */
	word[PDIWORDS] = NULL;
	word_eol[PDIWORDS] = NULL;
	std::string buf;
	if (text.starts_with('@'))
	{
		auto sep = text.find_first_of(' ');
		if (sep == boost::string_ref::npos)
			return;
		
		/* skip the '@' */
		auto tags = text.substr(1, sep - 1);
		
		buf = text.substr(sep + 1).to_string();

		handle_message_tags(*this, tags, tags_data);
	}
	else
	{
		buf = text.to_string();
	}

	url_check_line(buf.data(), buf.size());

	/* split line into words and words_to_end_of_line */
	process_data_init (&pdibuf[0], &buf[0], word, word_eol, false, false);

	if (buf[0] == ':')
	{
		/* find a context for this message */
		if (this->is_channel_name (word[3]))
		{
			auto tmp = find_channel (word[3]);
			if (tmp)
				sess = &(*tmp);
		}

		/* for server messages, the 2nd word is the "message type" */
		type = word[2];

		word[0] = type;
		word_eol[1] = &buf[0];	/* keep the ":" for plugins */

		if (plugin_emit_server(sess, type, word, word_eol,
			tags_data.timestamp))
		{
			return;
		}

		word[1]++;
		word_eol[1] = &buf[1];	/* but not for HexChat internally */

	} else
	{
		word[0] = type = word[1];

		if (plugin_emit_server(sess, type, word, word_eol,
			tags_data.timestamp))
		{
			return;
		}
	}

	if (buf[0] != ':')
	{
		process_named_servermsg (sess, &buf[0], word[0], word_eol, &tags_data);
		return;
	}

	/* see if the second word is a numeric */
	std::locale locale;
	if (std::isdigit (word[2][0], locale))
	{
		char* t = word_eol[4];
		if (*t == ':')
			t++;

		process_numeric (sess, atoi (word[2]), word, word_eol, t, &tags_data);
	} else
	{
		process_named_msg (sess, type, word, word_eol, &tags_data);
	}
}