Beispiel #1
0
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;
}
Beispiel #2
0
    std::shared_ptr<Response> request(const std::string& request_type, const std::string& path = "/", boost::string_ref content = "",
      const std::map<std::string, std::string>& header = std::map<std::string, std::string>()) {
      std::string corrected_path = path;
      if (corrected_path == "")
        corrected_path = "/";

      boost::asio::streambuf write_buffer;
      std::ostream write_stream(&write_buffer);
      write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
      write_stream << "Host: " << host << "\r\n";
      for (auto& h : header) {
        write_stream << h.first << ": " << h.second << "\r\n";
      }
      if (content.size()>0)
        write_stream << "Content-Length: " << content.size() << "\r\n";
      write_stream << "\r\n";

      try {
        connect();

        boost::asio::write(*socket, write_buffer);
        if (content.size()>0)
          boost::asio::write(*socket, boost::asio::buffer(content.data(), content.size()));

      }
      catch (const std::exception& e) {
        socket_error = true;
        throw std::invalid_argument(e.what());
      }

      return request_read();
    }
Beispiel #3
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;
}
Beispiel #4
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;
}
Beispiel #5
0
		bool operator()(const boost::string_ref& strRef1, const boost::string_ref& strRef2) const
		{
			auto oneSize = strRef1.size();
			auto twoSize = strRef2.size();

			if (oneSize != twoSize)
			{
				return false;
			}

			return std::memcmp(strRef1.begin(), strRef2.begin(), oneSize) == 0;
		}
Beispiel #6
0
 void
 on_request(
     boost::string_ref const& method_,
         boost::string_ref const& path_,
             int version_, error_code& ec)
 {
     method = std::string(
         method_.data(), method_.size());
     path = std::string(
         path_.data(), path_.size());
     version = version_;
     got_on_begin = true;
     if(fc_)
         fc_->fail(ec);
 }
Beispiel #7
0
 table_features(
       std::uint8_t const table_id
     , boost::string_ref const name
     , std::uint64_t const metadata_match
     , std::uint64_t const metadata_write
     , std::uint32_t const config
     , std::uint32_t const max_entries
     , properties_type properties)
   : table_features_{
         properties.calc_ofp_length(sizeof(ofp_type))
       , table_id
       , { 0, 0, 0, 0, 0 }
       , ""
       , metadata_match
       , metadata_write
       , config
       , max_entries
     }
   , properties_(std::move(properties))
 {
   auto const name_size
     = std::min(name.size(), sizeof(table_features_.name) - 1);
   using boost::adaptors::sliced;
   boost::copy(name | sliced(0, name_size), table_features_.name);
 }
void CInfixExpressionCalculator::SkipSpaces(boost::string_ref &ref)
{
    size_t i = 0;
    while (i < ref.size() && std::isspace(ref[i]))
        ++i;
    ref.remove_prefix(i);
}
Beispiel #9
0
			std::vector<std::pair<boost::string_ref, boost::string_ref>>::const_iterator find(const boost::string_ref key) const
			{
				return std::find_if(m_collection.begin(), m_collection.end(),
					[key](const std::pair<boost::string_ref, boost::string_ref>& str)-> bool
				{
					auto oneSize = str.first.size();
					auto twoSize = key.size();

					if (oneSize != twoSize)
					{
						return false;
					}

					if (oneSize >= 4)
					{
						if ((str.first[0] == key[0]) && 
							(str.first[1] == key[1]) && 
							(str.first[oneSize - 1] == key[oneSize - 1]) && 
							(str.first[oneSize - 2] == key[oneSize - 2]))
						{
							return std::memcmp(str.first.begin(), key.begin(), oneSize) == 0;
						}
					}
					else
					{
						return std::memcmp(str.first.begin(), key.begin(), oneSize) == 0;
					}

					return false;
				}
				);
			}
  inline boost::string_ref remove_leading_spaces(boost::string_ref string, const char* spaces = " \t\r\n")
  {
    auto pos = string.find_first_not_of(spaces);
    if (pos == std::string::npos) pos = string.size();

    string.remove_prefix(pos);
    return string;
  }
Beispiel #11
0
	void DatabaseImpl::checkSimpleArgumentFor(const boost::string_ref& key, partNum_t partNum) const
	{
		RAISE_INVALID_ARGUMENT_IF(key.size() > MAX_KEY_SIZE, "key too long");
		RAISE_INVALID_ARGUMENT_IF(key.empty(), "empty key is not allowed");
		RAISE_INVALID_ARGUMENT_IF(partNum > MAX_PARTNUM, "partNum is too large");
		RAISE_INVALID_ARGUMENT_IF(partNum == ALL_PARTS, "partNum ALL_PARTS is not allowed");
		RAISE_INVALID_ARGUMENT_IF(partNum < 0, "negative partNum is not allowed");
	}
Beispiel #12
0
unsigned short CHttpUrl::ParsePort(boost::string_ref const &url, size_t index)
{
	std::string port;
	if (index == url.size() || (index < url.size() && !isdigit(url[index])))
	{
		throw CUrlParsingError("Unknown port in the url address");
	}
	for (; index != url.size(); ++index)
	{
		if (!isdigit(url[index]))
		{
			break;
		}
		port += url[index];
	}
	return static_cast<unsigned short>(atoi(port.c_str()));
}
Beispiel #13
0
SimpleTemplate::SimpleTemplate(boost::string_ref text)
{
    static char const rawMarker[] = "@@";
    static boost::string_ref const marker(rawMarker, sizeof(rawMarker) - 1);

    auto beg = text.find(marker);
    while (beg != boost::string_ref::npos) {
        m_literals.emplace_back(text.data(), beg);
        text.remove_prefix(beg + marker.size());
        auto end = text.find(marker);
        if (end == boost::string_ref::npos) {
            std::string& lit = m_literals.back();
            lit.reserve(lit.size() + marker.size() + text.size());
            lit.append(marker.data(), marker.size());
            lit.append(text.data(), text.size());
            assert(m_literals.size() == m_insertionKeys.size() + 1);
            return;
        }
        m_insertionKeys.emplace_back(text.data(), end);
        text.remove_prefix(end + marker.size());
        beg = text.find(marker);
    }
    m_literals.emplace_back(text.data(), text.size());
    assert(m_literals.size() == m_insertionKeys.size() + 1);
}
Beispiel #14
0
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);
}
  inline boost::string_ref extract_word(boost::string_ref string)
  {
    const char spaces[] = " \t\r\n";
    string = remove_leading_spaces(string, spaces);

    auto new_size = string.find_first_of(spaces);
    if (new_size == boost::string_ref::npos) new_size = string.size();

    return boost::string_ref(string.data(), new_size);
  }
Beispiel #16
0
 void
 on_response(int status_,
     boost::string_ref const& reason_,
         int version_, error_code& ec)
 {
     status = status_;
     reason = std::string(
         reason_.data(), reason_.size());
     version = version_;
     got_on_begin = true;
     if(fc_)
         fc_->fail(ec);
 }
Beispiel #17
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);
}
Beispiel #18
0
void url::
rebuild(const boost::string_ref &scheme,
        const boost::optional<boost::string_ref> &host,
        const boost::optional<uint16_t> &port,
        const boost::optional<boost::string_ref> &path,
        const boost::optional<boost::string_ref> &query,
        const boost::optional<boost::string_ref> &fragment,
        const boost::optional<boost::string_ref> &user_info)
{
    std::string str;

    str.append(scheme.data(), scheme.size());
    if (has_authority()) {
        str.append("://");

        if (user_info) {
            str.append(user_info->data(), user_info->size());
            str.append("@");
        }

        str.append(host->data(), host->size());

        if (port) {
            str.append(":");
            str.append(std::to_string(*port));
        }
    }
    else {
        str.append(":");
    }

    if (path) {
        str.append(path->data(), path->size());
    }

    if (query) {
        str.append("?");
        str.append(query->data(), query->size());
    }

    if (fragment) {
        str.append("#");
        str.append(fragment->data(), fragment->size());
    }

    url new_url { std::move(str) };
    swap(new_url);
}
Beispiel #19
0
	inline bool drive_letter_exists(Iterator itr, Iterator end)
	{
		boost::string_ref const str( ":\\" );

		if( std::distance( itr, end ) < static_cast< std::ptrdiff_t >( str.size() + 1 ) ) {
			return false;
		}

		for( int i = 'A'; i <= 'Z'; ++i ) {
			if( *itr == i ) {
				return std::equal( str.begin(), str.end(), itr + 1 );
			}
		}

		return false;
	}
Beispiel #20
0
	size_type DataPage::addSingleRecord(const boost::string_ref& recordInlineData)
	{
		const size_type recordInlineSize = static_cast<size_type>(recordInlineData.size());
		const bool canAdd = freeSpace() >= sizeof(uint16_t) + recordInlineSize;

		if (canAdd) {
			// Compute offsets.
			const size_type endOfFreeArea = getEndOfFreeArea();
			const size_type recordOffset = endOfFreeArea - recordInlineSize;

			// Copy the record, add pointer to its start and adjust "end of free area".
			putBytes(recordOffset, recordInlineData);
			addRecordOffset(recordOffset);
			setEndOfFreeArea(recordOffset);
		}

		return (canAdd)? recordInlineSize : 0;
	}
Beispiel #21
0
    std::string encode_string(boost::string_ref str)
    {
        std::string result;
        result.reserve(str.size());

        for (boost::string_ref::const_iterator it = str.begin();
            it != str.end(); ++it)
        {
            switch (*it)
            {
                case '<': result += "&lt;";    break;
                case '>': result += "&gt;";    break;
                case '&': result += "&amp;";   break;
                case '"': result += "&quot;";  break;
                default:  result += *it;       break;
            }
        }

        return result;
    }
Beispiel #22
0
std::string
escaped_string(boost::string_ref const& s)
{
    std::string out;
    out.reserve(s.size());
    char const* p = s.data();
    while(p != s.end())
    {
        if(*p == '\r')
            out.append("\\r");
        else if(*p == '\n')
            out.append("\\n");
        else if(*p == '\t')
            out.append("\\t");
        else
            out.append(p, 1);
        ++p;
    }
    return out;
}
Beispiel #23
0
bool
server::p_raw(const boost::string_ref &raw)
{
	char tbuf[4096];
	if (!raw.empty())
	{
		if (raw.size() < sizeof (tbuf) - 3)
		{
			auto len = snprintf(tbuf, sizeof(tbuf), "%s\r\n", raw.data());
			if (len < 0)
			{
				PrintText(current_sess, _("Unable to send message to server, and error has occurred"));
				return false;
			}
			tcp_send_len(*this, boost::string_ref{ tbuf, static_cast<std::size_t>(len)});
		} else
		{
			tcp_send_len (*this, raw);
			tcp_send_len(*this, boost::string_ref{ "\r\n", 2 });
		}
		return true;
	}
	return false;
}
    void stringoutput (boost::string_ref const& bytes)
    {
        markstarted ();
        std::size_t position = 0, writtenuntil = 0;

        output_ ({&quote, 1});
        auto data = bytes.data();
        for (; position < bytes.size(); ++position)
        {
            auto i = jsonspecialcharacterescape.find (data[position]);
            if (i != jsonspecialcharacterescape.end ())
            {
                if (writtenuntil < position)
                {
                    output_ ({data + writtenuntil, position - writtenuntil});
                }
                output_ ({i->second, jsonescapelength});
                writtenuntil = position + 1;
            };
        }
        if (writtenuntil < position)
            output_ ({data + writtenuntil, position - writtenuntil});
        output_ ({&quote, 1});
    }
Beispiel #25
0
 explicit message(boost::string_ref str)
     : message(boost::asio::buffer(str.data(), str.size()))
 { }
Beispiel #26
0
 void on_body(boost::string_ref const& s, error_code& ec)
 {
     r_.write(s.data(), s.size(), ec);
 }
				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;
				}
Beispiel #28
0
	DataPage::AddedValueRef::AddedValueRef(const boost::string_ref& value) 
		: valueSizeOrTag_(static_cast<uint16_t>(value.size()))
		, valueReference_(value)
		, largeValueRef_(0LL)
	{ }
Beispiel #29
0
 void
 on_data(boost::string_ref const& s,
     error_code& ec)
 {
     body.append(s.data(), s.size());
 }
Beispiel #30
0
 void operator()(clmdep_msgpack::object& o, const boost::string_ref& v) const {
     uint32_t size = checked_get_container_size(v.size());
     o.type = clmdep_msgpack::type::STR;
     o.via.str.ptr = v.data();
     o.via.str.size = size;
 }