Example #1
0
std::string log_request_helper(const boost::asio::streambuf& buf)
{
    boost::asio::const_buffers_1 b = static_cast<boost::asio::const_buffers_1>(buf.data());
    boost::iterator_range<const char*> ib(buffer_cast<const char*>(b),
            buffer_cast<const char*>(b) + buffer_size(b));
    return string(ib.begin(), ib.end());
}
Example #2
0
File: main.cpp Project: CCJY/coliru
/// @brief Helper function that extracts a string from a streambuf.
std::string make_string(
  boost::asio::streambuf& streambuf,
  std::size_t n)
{
  return std::string(buffers_begin(streambuf.data()),
                     buffers_begin(streambuf.data()) + n);
}
Example #3
0
  // STEP 3: parse the body
  size_t Frame::parse_body(boost::asio::streambuf& _response)
  {
	  std::size_t _content_length = 0, bytecount = 0;
	  string _str;
	//debug_print("Frame parser phase 3");
	// special case: content-length
	if (m_headers.find("content-length") != m_headers.end()) {
		string& val = m_headers["content-length"];
		//debug_print(boost::format("phase 3: body content-length==%1%") % val);
		_content_length = lexical_cast<size_t>(val);
	}
	if (_content_length > 0) {
		bytecount += _content_length;
		// read back the body byte by byte
		const char* rawdata = boost::asio::buffer_cast<const char*>(_response.data());
		for (size_t i = 0; i < _content_length; i++ ) {
			m_body << rawdata[i];
		}
	} else {
		// read all bytes until the first NULL
		mygetline(_response, _str, '\0');
		bytecount += _str.size();
		m_body << _str;
	}
	bytecount += 1; // for the final frame-terminating NULL
	//debug_print(boost::format("phase 3: consumed %1% bytes, BODY(%2% bytes)==%3%") % bytecount % _str.size() % _str);
	_response.consume(bytecount);
	return(bytecount);
  }
Example #4
0
static std::string to_string(boost::asio::streambuf& buffer)
{
    boost::asio::streambuf::const_buffers_type bufs = buffer.data();
    std::string data(
        boost::asio::buffers_begin(bufs),
        boost::asio::buffers_begin(bufs) + buffer.size());
    return std::move(data);
}
Example #5
0
bool ConstByte::Fill(boost::asio::streambuf& buf)
{
	const char* cp = boost::asio::buffer_cast<const char*>(buf.data());
	if (static_cast<const char>(_expectedValue) != *cp) {
		return false;
	}
	return FixedSizeData<1>::Fill(buf);
}
Example #6
0
void webqq::async_fetch_cface_std_saver( boost::system::error_code ec, boost::asio::streambuf& buf, std::string cface, boost::filesystem::path parent_path)
{
	if (!fs::exists(parent_path)){
		fs::create_directories(parent_path);
	}

	if (!ec || ec == boost::asio::error::eof){
		std::string imgfilename = (parent_path / cface).string();
		std::ofstream cfaceimg(imgfilename.c_str(), std::ofstream::binary|std::ofstream::out);
		cfaceimg.write(boost::asio::buffer_cast<const char*>(buf.data()), boost::asio::buffer_size(buf.data()));
	}
}
Example #7
0
		void on_recv_(const boost::system::error_code& error, size_t bytes_transferred)
		{
			if(error && error != boost::asio::error::eof) {
				std::cout << "receive failed: " << error.message() << std::endl;
			} else {
				const char* data = asio::buffer_cast<const char*>(recv_.data());
				std::cout << "response(" << bytes_transferred << "): ";
				std::cout << data << std::endl;
				recv_.consume(recv_.size());
				read_trg_ = true;
			}
		}
Example #8
0
  // my own version of getline for an asio streambuf
inline void mygetline (boost::asio::streambuf& sb, string& _str, char delim = '\n') {
	const char* line = boost::asio::buffer_cast<const char*>(sb.data());
	char _c;
	size_t i;
	_str.clear();
	for( i = 0;
		((i < sb.size()) && ((_c = line[i]) != delim));
		i++
	) _str += _c;
	//debug_print( boost::format("mygetline: i=%1%, sb.size()==%2%") % i % sb.size() );
	//hexdump(_str.c_str(), _str.size());
}
Example #9
0
	void operator()( boost::system::error_code ec, boost::asio::streambuf & buf, std::string cface )
	{
 		using namespace boost::asio::detail;

		if( !ec || ec == boost::asio::error::eof )
		{
			std::ofstream cfaceimg( ( std::string( "images/" ) + cface ).c_str(), std::ofstream::openmode(std::ofstream::binary | std::ofstream::out) );
			cfaceimg.write( boost::asio::buffer_cast<const char*>( buf.data() ), boost::asio::buffer_size( buf.data() ) );
			ec = boost::system::error_code();
		}

		io_service.post( bind_handler( handler, ec));
	}
Example #10
0
    void handle_line(const boost::system::error_code& error, size_t bytes_received) {
        if(!error) {
            boost::asio::streambuf::const_buffers_type bufs = m_buffer.data();
            std::string line(boost::asio::buffers_begin(bufs),
                             boost::asio::buffers_begin(bufs) + bytes_received);

            std::cerr << "Sentence: " << line << std::endl;

            m_buffer.consume(bytes_received);

            read_next();
        } else {
            std::cerr << "Error: " << error << std::endl;
        }
    }
Example #11
0
boost::optional<rc_result> parse_rc_response(const boost::asio::streambuf& buf)
{
    typedef boost::asio::streambuf::const_buffers_type const_buffers_type;
    typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
    const_buffers_type buffers = buf.data();
    iterator begin = iterator::begin(buffers);
    iterator end = iterator::end(buffers);

    // Look for the start of the body of the response
    boost::iterator_range<const char*> delim = boost::as_literal("\r\n\r\n");
    std::pair<iterator, bool> result = boost::asio::detail::partial_search(
        begin, end, delim.begin(), delim.end());
    if (result.first != end && result.second)
    {
        iterator start = result.first + delim.size();

        // Skip a line
        delim = boost::as_literal("\r\n");
        result = boost::asio::detail::partial_search(start, end,
                delim.begin(), delim.end());
        if (result.first != end && result.second)
        {
            // todo: we can optimise parsing here
            std::string d;
            start = result.first + delim.size();
            std::copy(start, end, std::back_inserter(d));

            rc_result res;
            try
            {
                std::istringstream iss(d);
                iss >> res.ok;
                iss >> res.sum1;
                iss >> res.sum2;
                iss >> res.sum3;
                iss >> res.sum4;
            }
            catch (...)
            {
                return boost::optional<rc_result>();
            }

            return boost::optional<rc_result>(res);
        }
Example #12
0
File: main.cpp Project: CCJY/coliru
/// @brief Convert a streambuf to a string.
std::string make_string(boost::asio::streambuf& streambuf)
{
  return std::string(buffers_begin(streambuf.data()),
                     buffers_end(streambuf.data()));
}