Example WEB‐client multirequests - anyks/awh GitHub Wiki

#include <awh/client/awh.hpp>

using namespace awh;
using namespace placeholders;

class WebClient {
	private:
		uint8_t _count;
	private:
		const fmk_t * _fmk;
		const log_t * _log;
	public:
		
		void message(const int32_t sid, [[maybe_unused]] const uint64_t rid, const uint32_t code, const string & message){
			if(code >= 300)
				this->_log->print("Request failed: %u %s stream=%i", log_t::flag_t::WARNING, code, message.c_str(), sid);
		}
		
		void active(const client::web_t::mode_t mode, client::awh_t * awh){
			this->_log->print("%s client", log_t::flag_t::INFO, (mode == client::web_t::mode_t::CONNECT ? "Connect" : "Disconnect"));
			
			if(mode == client::web_t::mode_t::CONNECT){
				uri_t uri(this->_fmk, this->_log);

				client::web_t::request_t req1(this->_fmk, this->_log),
				                         req2(this->_fmk, this->_log);

				req1.method = web_t::method_t::GET;
				req2.method = web_t::method_t::GET;

				req1.url = uri.parse("/mac/");
				req2.url = uri.parse("/iphone/");

				awh->send(std::move(req1));
				awh->send(std::move(req2));
			}
		}

		void entity([[maybe_unused]] const int32_t sid, [[maybe_unused]] const uint64_t rid, [[maybe_unused]] const uint32_t code, const string & message, const vector <char> & entity, client::awh_t * awh){
			this->_count++;

			cout << "RESPONSE: " << string(entity.begin(), entity.end()) << endl;

			if(this->_count == 2)
				awh->stop();
		}

		void headers([[maybe_unused]] const int32_t sid, [[maybe_unused]] const uint64_t rid, [[maybe_unused]] const uint32_t code, [[maybe_unused]] const string & message, const unordered_multimap <string, string> & headers){
			for(auto & header : headers)
				cout << "HEADER: " << header.first << ": " << header.second << endl;

			cout << endl;
		}

		void complete([[maybe_unused]] const int32_t sid, [[maybe_unused]] const uint64_t rid, [[maybe_unused]] const uint32_t code, [[maybe_unused]] const string & message, const vector <char> & entity, const unordered_multimap <string, string> & headers, client::awh_t * awh){
			this->_count++;

			for(auto & header : headers)
				cout << "HEADER: " << header.first << ": " << header.second << endl;
			
			cout << endl;

			cout << "RESPONSE: " << string(entity.begin(), entity.end()) << endl;

			if(this->_count == 2)
				awh->stop();
		}
	public:
		WebClient(const fmk_t * fmk, const log_t * log) : _fmk(fmk), _log(log) {}
};

int32_t main(int32_t argc, char * argv[]){
	fmk_t fmk{};
	log_t log(&fmk);

	client::core_t core(&fmk, &log);
	client::awh_t awh(&core, &fmk, &log);

	WebClient executor(&fmk, &log);

	core.proto(awh::engine_t::proto_t::HTTP2);
	// core.proto(awh::engine_t::proto_t::HTTP1_1);

	log.name("WEB Client");
	log.format("%H:%M:%S %d.%m.%Y");

	awh.mode({
		client::web_t::flag_t::NOT_INFO,
		client::web_t::flag_t::REDIRECTS,
		client::web_t::flag_t::CONNECT_METHOD_ENABLE
	});

	node_t::ssl_t ssl;
	ssl.verify = true;
	ssl.ca     = "./certs/ca.pem";
	// ssl.key  = "./certs/certificates/client-key.pem";
	// ssl.cert = "./certs/certificates/client-cert.pem";
	core.ssl(ssl);

	// awh.user("user", "password");
	// awh.authType(auth_t::type_t::BASIC);
	// awh.authType(auth_t::type_t::DIGEST, auth_t::hash_t::MD5);

	// awh.proxy("http://user:[email protected]:port");
	// awh.proxy("https://user:[email protected]:port");
	// awh.proxy("socks5://user:[email protected]:port");

	// awh.proxy(client::scheme_t::work_t::ALLOW);
	// awh.proxy(client::scheme_t::work_t::DISALLOW);

	// awh.authTypeProxy(auth_t::type_t::BASIC);
	// awh.authTypeProxy(auth_t::type_t::DIGEST, auth_t::hash_t::MD5);

	/*
	awh.compressors({
		http_t::compressor_t::ZSTD,
		http_t::compressor_t::BROTLI,
		http_t::compressor_t::GZIP,
		http_t::compressor_t::DEFLATE
	});
	*/

	awh.on <void (const client::web_t::mode_t)> ("active", &WebClient::active, &executor, _1, &awh);
	awh.on <void (const int32_t, const uint64_t, const uint32_t, const string &)> ("response", &WebClient::message, &executor, _1, _2, _3, _4);
	// awh.on <void (const int32_t, const uint64_t, const uint32_t, const string &, const unordered_multimap <string, string> &)> ("headers", &WebClient::headers, &executor, _1, _2, _3, _4, _5);
	// awh.on <void (const int32_t, const uint64_t, const uint32_t, const string &, const vector <char> &)> ("entity", &WebClient::entity, &executor, _1, _2, _3, _4, _5, &awh);
	awh.on <void (const int32_t, const uint64_t, const uint32_t, const string &, const vector <char> &, const unordered_multimap <string, string> &)> ("complete", &WebClient::complete, &executor, _1, _2, _3, _4, _5, _6, &awh);
	
	awh.init("https://apple.com");
	awh.start();

	return EXIT_SUCCESS;
}
⚠️ **GitHub.com Fallback** ⚠️