TEST ( ModMatchTest, TestExecuteWithArgsOptional ) {
    Match< std::string, int > match( "/foo/(\\w+)/?(\\d+)", "name", "user-id" );
    Request _request( "/foo/bar/" );
    Response _response;
    EXPECT_EQ( http::http_status::NOT_FOUND, match.execute( _request, _response ) );
    EXPECT_EQ( 0U, _request.attribute_names().size() );
}
Exemple #2
0
Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {

	ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
	if (requesting) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	method = p_method;

	Error err = _parse_url(p_url);
	if (err)
		return err;

	validate_ssl = p_ssl_validate_domain;

	bool has_user_agent = false;
	bool has_accept = false;
	headers = p_custom_headers;

	request_data = p_request_data;

	for (int i = 0; i < headers.size(); i++) {

		if (headers[i].findn("user-agent:") == 0)
			has_user_agent = true;
		if (headers[i].findn("Accept:") == 0)
			has_accept = true;
	}

	if (!has_user_agent) {
		headers.push_back("User-Agent: GodotEngine/" + String(VERSION_MKSTRING) + " (" + OS::get_singleton()->get_name() + ")");
	}

	if (!has_accept) {
		headers.push_back("Accept: */*");
	}

	requesting = true;

	if (use_threads) {

		thread_done = false;
		thread_request_quit = false;
		client->set_blocking_mode(true);
		thread = Thread::create(_thread_func, this);
	} else {
		client->set_blocking_mode(false);
		err = _request();
		if (err != OK) {
			call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
			return ERR_CANT_CONNECT;
		}

		set_process_internal(true);
	}

	return OK;
}
bool NotifyrClient::_connect() {
	if (_client.connect("api.notifyr.io", 80)) {
		_request();
		return true;
	} else {
		return false;
	}
}
TEST ( ModMatchTest, TestExecuteWithArgs ) {
    Match< std::string, int > match( "/foo/(\\w+)/(\\d+)", "name", "user-id" );
    Request _request( "/foo/alice/42" );
    Response _response;
    EXPECT_EQ( http::http_status::OK, match.execute( _request, _response ) );
    EXPECT_EQ( 2U, _request.attribute_names().size() );
    EXPECT_EQ( "alice", _request.attribute( "name" ) );
    EXPECT_EQ( "42", _request.attribute( "user-id" ) );
}
Exemple #5
0
Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_headers, bool p_ssl_validate_domain) {

	ERR_FAIL_COND_V(!is_inside_tree(),ERR_UNCONFIGURED);
	if ( requesting ) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	Error err = _parse_url(p_url);
	validate_ssl=p_ssl_validate_domain;

	bool has_user_agent=false;
	bool has_accept=false;
	headers=p_custom_headers;

	for(int i=0;i<headers.size();i++) {

		if (headers[i].findn("user-agent:")==0)
			has_user_agent=true;
		if (headers[i].findn("Accept:")==0)
			has_accept=true;
	}

	if (!has_user_agent) {
		headers.push_back("User-Agent: GodotEngine/"+String(VERSION_MKSTRING)+" ("+OS::get_singleton()->get_name()+")");
	}

	if (!has_accept) {
		headers.push_back("Accept: */*");
	}


	err = _request();

	if (err==OK) {
		set_process(true);
		requesting=true;
	}


	return err;
}
Exemple #6
0
Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {

	ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
	if (requesting) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	method = p_method;

	Error err = _parse_url(p_url);
	if (err)
		return err;

	validate_ssl = p_ssl_validate_domain;

	headers = p_custom_headers;

	request_data = p_request_data;

	requesting = true;

	if (use_threads) {

		thread_done = false;
		thread_request_quit = false;
		client->set_blocking_mode(true);
		thread = Thread::create(_thread_func, this);
	} else {
		client->set_blocking_mode(false);
		err = _request();
		if (err != OK) {
			call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
			return ERR_CANT_CONNECT;
		}

		set_process_internal(true);
	}

	return OK;
}
Exemple #7
0
		http_request_header(char *request) {
			std::string _request(request);
			// split request by lines.
			int i;
			while((i = _request.find_first_of("\r\n")) != std::string::npos) {
				r_line.push_back(_request.substr(0, i));
				_request = _request.substr(i + 1);
			}

			// split first request line to method, uri and version
			std::cout << "Request : " << r_line[0] << std::endl;
			i = r_line[0].find_first_of(" ");
			this->method = r_line[0].substr(0, i);
			r_line[0] = r_line[0].substr(i + 1);
			i = r_line[0].find_first_of(" ");
			this->uri = r_line[0].substr(0, i);
			this->version = r_line[0].substr(i + 1);

			// add index.html if dir
			if((this->uri[this->uri.size() - 1]) == '/') uri += "index.html"; 
		};
Exemple #8
0
bool HTTPRequest::_handle_response(bool *ret_value) {

	if (!client->has_response()) {
		call_deferred("emit_signal","request_completed",RESULT_NO_RESPONSE,0,StringArray(),ByteArray());
		*ret_value=true;
		return true;
	}

	got_response=true;
	response_code=client->get_response_code();
	List<String> rheaders;
	client->get_response_headers(&rheaders);
	response_headers.resize(0);
	downloaded=0;
	for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
		print_line("HEADER: "+E->get());
		response_headers.push_back(E->get());
	}

	if (response_code==301 || response_code==302) {
		//redirect
		if (max_redirects>=0 && redirections>=max_redirects) {

			call_deferred("emit_signal","request_completed",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray());
			*ret_value=true;
			return true;
		}

		String new_request;

		for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
			if (E->get().findn("Location: ")!=-1) {
				new_request=E->get().substr(9,E->get().length()).strip_edges();
			}
		}

		print_line("NEW LOCATION: "+new_request);

		if (new_request!="") {
			//process redirect
			client->close();
			int new_redirs=redirections+1; //because _request() will clear it
			Error err;
			if (new_request.begins_with("http")) {
				//new url, request all again
				err=_parse_url(new_request);
			} else {
				request_string=new_request;
			}

			err = _request();

			print_line("new connection: "+itos(err));
			if (err==OK) {
				request_sent=false;
				got_response=false;
				body_len=-1;
				body.resize(0);
				downloaded=0;
				redirections=new_redirs;
				*ret_value=false;
				return true;

			}
		}
	}

	return false;
}
TEST ( ModMatchTest, TestNotFound ) {
    Match<> match( "/foo" );
    Request _request( "/bar" );
    Response _response;
    EXPECT_EQ( http::http_status::NOT_FOUND, match.execute( _request, _response ) );
}
TEST ( ModMatchTest, TestExecute ) {
    Match<> match( "/foo" );
    Request _request( "/foo" );
    Response _response;
    EXPECT_EQ( http::http_status::OK, match.execute ( _request, _response ) );
}
Exemple #11
0
g_spawn_status g_spawn_poi(const char* path, const char* args, const char* workdir, g_security_level securityLevel, g_pid* pid, g_fd out_stdio[3],
		g_fd in_stdio[3]) {

	g_spawn_status res = G_SPAWN_STATUS_UNKNOWN;
	uint32_t tid = g_get_tid();

	// get spawner task identifier
	g_tid spawner_tid = g_task_get_id(G_SPAWNER_IDENTIFIER);
	if (spawner_tid == -1) {
		return res;
	}

	// create transaction
	g_message_transaction tx = g_get_message_tx_id();

	// create request
	size_t path_bytes = __g_strlen(path) + 1;
	size_t args_bytes = __g_strlen(args) + 1;
	size_t workdir_bytes = __g_strlen(workdir) + 1;
	size_t requestlen = sizeof(g_spawn_command_spawn_request) + path_bytes + args_bytes + workdir_bytes;
	g_local<uint8_t> _request(new uint8_t[requestlen]);
	uint8_t* request = _request();

	// copy request contents
	g_spawn_command_spawn_request* req = (g_spawn_command_spawn_request*) request;
	req->header.command = G_SPAWN_COMMAND_SPAWN_REQUEST;
	req->security_level = securityLevel;
	req->path_bytes = path_bytes;
	req->args_bytes = args_bytes;
	req->workdir_bytes = workdir_bytes;

	if (in_stdio != nullptr) {
		req->stdin = in_stdio[0];
		req->stdout = in_stdio[1];
		req->stderr = in_stdio[2];
	} else {
		req->stdin = G_FD_NONE;
		req->stdout = G_FD_NONE;
		req->stderr = G_FD_NONE;
	}

	uint8_t* insert = request;
	insert += sizeof(g_spawn_command_spawn_request);
	__g_memcpy(insert, path, path_bytes);
	insert += path_bytes;
	__g_memcpy(insert, args, args_bytes);
	insert += args_bytes;
	__g_memcpy(insert, workdir, workdir_bytes);

	// send request to spawner
	g_send_message_t(spawner_tid, request, requestlen, tx);

	// receive response
	size_t resp_len = sizeof(g_message_header) + sizeof(g_spawn_command_spawn_response);
	g_local<uint8_t> resp_buf(new uint8_t[resp_len]);
	g_receive_message_t(resp_buf(), resp_len, tx);

	g_spawn_command_spawn_response* response = (g_spawn_command_spawn_response*) G_MESSAGE_CONTENT(resp_buf());

	// if successful, take response parameters
	if (response->status == G_SPAWN_STATUS_SUCCESSFUL) {

		if (pid != nullptr) {
			*pid = response->spawned_process_id;
		}

		if (out_stdio != nullptr) {
			out_stdio[0] = response->stdin_write;
			out_stdio[1] = response->stdout_read;
			out_stdio[2] = response->stderr_read;
		}
	}

	return response->status;
}