<commit_before><?php

/**
 * Copyright 2015-2016 Unicity International
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(strict_types = 1);

namespace Unicity\HTTP {

	use \Unicity\Core;
	use \Unicity\EVT;
	use \Unicity\HTTP;

	class RequestBroker extends Core\Object {

		/**
		 * This variable stores a reference to the dispatcher.
		 *
		 * @access protected
		 * @var EVT\IServer
		 */
		protected $server;

		/**
		 * This constructor initializes the class.
		 *
		 * @access public
		 */
		public function __construct() {
			$this->server = new EVT\Server();
		}

		/**
		 * This destructor ensures that any resources are properly disposed.
		 *
		 * @access public
		 */
		public function __destruct() {
			parent::__destruct();
			unset($this->server);
		}

		/**
		 * This method executes the given request.
		 *
		 * @access public
		 * @param HTTP\Request $request                             the request to be sent
		 * @return int                                              the response status
		 */
		public function execute(HTTP\Request $request) : int {
			return $this->executeAll([$request]);
		}

		/**
		 * This method executes the given requests.
		 *
		 * @access public
		 * @param array $requests                                   the requests to be sent
		 * @return int                                              the response status
		 */
		public function executeAll(array $requests) : int {
			$this->server->publish('requestOpened');

			$http_code = 200;

			$dispatcher = curl_multi_init();
			$resources = array();
			$count = count($requests);

			for ($i = 0; $i < $count; $i++) {
				$request = $requests[$i];

				$this->server->publish('requestInitiated', $request);

				$resource = curl_init();

				curl_setopt($resource, CURLOPT_HEADER, false);
				if (isset($request->headers) && !empty($request->headers)) {
					$headers = array();
					foreach ($request->headers as $name => $value) {
						$headers[] = "{$name}: {$value}";
					}
					curl_setopt($resource, CURLOPT_HTTPHEADER, $headers);
				}

				curl_setopt($resource, CURLOPT_FOLLOWLOCATION, 1);
				curl_setopt($resource, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
				curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);<commit_msg>Adding a 30 timeout to curl calls.<commit_after><?php

/**
 * Copyright 2015-2016 Unicity International
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(strict_types = 1);

namespace Unicity\HTTP {

	use \Unicity\Core;
	use \Unicity\EVT;
	use \Unicity\HTTP;

	class RequestBroker extends Core\Object {

		/**
		 * This variable stores a reference to the dispatcher.
		 *
		 * @access protected
		 * @var EVT\IServer
		 */
		protected $server;

		/**
		 * This constructor initializes the class.
		 *
		 * @access public
		 */
		public function __construct() {
			$this->server = new EVT\Server();
		}

		/**
		 * This destructor ensures that any resources are properly disposed.
		 *
		 * @access public
		 */
		public function __destruct() {
			parent::__destruct();
			unset($this->server);
		}

		/**
		 * This method executes the given request.
		 *
		 * @access public
		 * @param HTTP\Request $request                             the request to be sent
		 * @return int                                              the response status
		 */
		public function execute(HTTP\Request $request) : int {
			return $this->executeAll([$request]);
		}

		/**
		 * This method executes the given requests.
		 *
		 * @access public
		 * @param array $requests                                   the requests to be sent
		 * @return int                                              the response status
		 */
		public function executeAll(array $requests) : int {
			$this->server->publish('requestOpened');

			$http_code = 200;

			$dispatcher = curl_multi_init();
			$resources = array();
			$count = count($requests);

			for ($i = 0; $i < $count; $i++) {
				$request = $requests[$i];

				$this->server->publish('requestInitiated', $request);

				$resource = curl_init();

				curl_setopt($resource, CURLOPT_HEADER, false);
				if (isset($request->headers) && !empty($request->headers)) {
					$headers = array();
					foreach ($request->headers as $name => $value) {
						$headers[] = "{$name}: {$value}";
					}
					curl_setopt($resource, CURLOPT_HTTPHEADER, $headers);
				}

				curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, 30);
				curl_setopt($resource, CURLOPT_FOLLOWLOCATION, 1);
				curl_setopt($resource, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
				curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($resource, CURLOPT_TIMEOUT, 30);
{'fraction_non_alphanumeric': 0.11663716814159292, 'fraction_numerical': 0.008495575221238939, 'mean_word_length': 2.698298429319372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067164', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1526, 'n_words': 497}