Process any received curl multi messages
182006
private function processMessages()
    {
        while ($done = curl_multi_info_read($this->multiHandle)) {
            $request = $this->resourceHash[(int) $done['handle']];
            try {
                $this->processResponse($request, $this->handles[$request], $done);
                $this->successful[] = $request;
            } catch (\Exception $e) {
                $this->removeErroredRequest($request, $e);
            }
        }
    }
649918
def recv(self):
        """"""
        try:
            items = self.poller.poll(self.timeout)
        except KeyboardInterrupt:
            return  # interrupted

        if items:
            # if we got a reply, process it
            msg = self.client.recv_multipart()
            self.close()
            if self.verbose:
                logging.info("I: received reply:")
                dump(msg)

            # Don't try to handle errors, just assert noisily
            assert len(msg) >= 4

            #first drop will be drop (cause empty)
            header = msg.pop(0)
            header = msg.pop(0)
            assert MDP.C_CLIENT == header

            #this one contains servicename
            #TODO: exploit this
            header = msg.pop(0)

            return msg
        else:
            logging.warn("W: permanent error, abandoning request")
48371
def handle_collect(self, msg):
        """"""
        (success, sequence_number, comment) = self._handle_collect(msg)
        self.collector.send_multipart(msgs.MessageWriter().bool(success).uint64(sequence_number).string(comment).get())
580070
private void doBatchMessage(final JsonObject message) {
    String batchID = message.getString("batch");
    if (currentBatch != null && currentBatch.id().equals(batchID)) {
      Object value = deserializer.deserialize(message);
      if (value != null) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("%s - Batch received: Batch[batch=%s, id=%d, message=%s]", this, batchID, message.getLong("id"), value));
        }
        currentBatch.handleMessage(value);
      }
    }
  }
404369
def _process_message(self, message: amqp.Message) -> None:
        """"""
        if self.shutdown_pending.is_set():
            return

        try:
            if isinstance(message.body, bytes):
                message.body = message.body.decode()
            description = json.loads(message.body)
        except Exception:
            logger.error("Cannot decode message. Dropping. Message: %r", message.body)
            traceback.print_exc()
            message.channel.basic_reject(message.delivery_tag, requeue=False)
        else:
            logger.info("Processing task: %r", description)
            self._process_description(message, description)
43351
public function push(): array
    {
        $responseCollection = [];

        if (!$this->curlMultiHandle) {
            $this->curlMultiHandle = curl_multi_init();

            if (!defined('CURLPIPE_MULTIPLEX')) {
                define('CURLPIPE_MULTIPLEX', 2);
            }

            curl_multi_setopt($this->curlMultiHandle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
            curl_multi_setopt($this->curlMultiHandle, CURLMOPT_MAX_HOST_CONNECTIONS, $this->maxConcurrentConnections);
        }

        $mh = $this->curlMultiHandle;

        $i = 0;
        while (!empty($this->notifications) && $i++ < $this->nbConcurrentRequests) {
            $notification = array_pop($this->notifications);
            curl_multi_add_handle($mh, $this->prepareHandle($notification));
        }

        // Clear out curl handle buffer
        do {
            $execrun = curl_multi_exec($mh, $running);
        } while ($execrun === CURLM_CALL_MULTI_PERFORM);

        // Continue processing while we have active curl handles
        while ($running > 0 && $execrun === CURLM_OK) {
            // Block until data is available
            $select_fd = curl_multi_select($mh);
            // If select returns -1 while running, wait 250 microseconds before continuing
            // Using curl_multi_timeout would be better but it isn't available in PHP yet
            // https://php.net/manual/en/function.curl-multi-select.php#115381
            if ($running && $select_fd === -1) {
                usleep(250);
            }

            // Continue to wait for more data if needed
            do {
                $execrun = curl_multi_exec($mh, $running);
            } while ($execrun === CURLM_CALL_MULTI_PERFORM);

            // Start reading results
            while ($done = curl_multi_info_read($mh)) {
                $handle = $done['handle'];

                $result = curl_multi_getcontent($handle);

                // find out which token the response is about
                $token = curl_getinfo($handle, CURLINFO_PRIVATE);

                $responseParts = explode("\r\n\r\n", $result, 2);
                $headers = '';
                $body = '';
                if (isset($responseParts[0])) {
                    $headers = $responseParts[0];
                }
                if (isset($responseParts[1])) {
                    $body = $responseParts[1];
                }

                $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
                $responseCollection[] = new Response($statusCode, $headers, (string)$body, $token);
                curl_multi_remove_handle($mh, $handle);
                curl_close($handle);

                if (!empty($this->notifications)) {
                    $notification = array_pop($this->notifications);
                    curl_multi_add_handle($mh, $this->prepareHandle($notification));
                }
            }
        }

        if ($this->autoCloseConnections) {
            curl_multi_close($mh);
            $this->curlMultiHandle = null;
        }

        return $responseCollection;
    }
298698
protected static function handleMultiResponse ($info, $mh, &$handles) {
    $ch = $info['handle'];
    $req = $handles[(string) $ch];

    $rawResp = null;
    if (CURLE_OK == $info['result']) {
      // read the response from the handle
      $rawResp = curl_multi_getcontent($ch);
    }
    $req->processCurlResponse(
        $ch, $info['result'], curl_error($ch), $rawResp);

    // remove this handle from the queue
    curl_multi_remove_handle($mh, $ch);
    unset($handles[(string) $ch]);
  }
604416
function processMultiBytePacketStop (o) {
  processMultiBytePacket(o);
  const str = o.multiPacketBuffer.toString();
  o.multiPacketBuffer = null;
  return {
    'message': str
  };
}
1056358
public function process(HttpRequest $curl)
    {
        $data = $curl->getData();
        $additionalData = $curl->getAdditionalData();
        $callback = $additionalData['callback'];
        unset($additionalData['callback']);

        $obj = new HttpResult($data['response'], $data['curlInfo'], $curl->getCurlError(), $additionalData);
        call_user_func($callback, $obj);
    }
890485
function CurlApi (ids, callback, errback, waitFor) {
		var then, ctx;
		ctx = core.createContext(userCfg, undef, [].concat(ids));
		this['then'] = then = function (resolved, rejected) {
			when(ctx,
				// return the dependencies as arguments, not an array
				function (deps) {
					if (resolved) resolved.apply(undef, deps);
				},
				// just throw if the dev didn't specify an error handler
				function (ex) {
					if (rejected) rejected(ex); else throw ex;
				}
			);
			return this;
		};
		this['next'] = function (ids, cb, eb) {
			// chain api
			return new CurlApi(ids, cb, eb, ctx);
		};
		this['config'] = _config;
		if (callback || errback) then(callback, errback);
		when(waitFor, function () { core.getDeps(ctx); });
	}
190168
protected function dispatch()
    {
        // Start processing the requests.
        list($mrc, $active) = $this->process();

        // Keep processing requests until we're done.
        while ($active and $mrc === CURLM_OK) {
            // Process the next request.
            list($mrc, $active) = $this->process();
        }

        // Throw an exception if something went wrong.
        if ($mrc !== CURLM_OK) {
            throw new CurlErrorException('cURL read error #'.$mrc);
        }
    }
151758
def build_grab_exception(ex, curl):
    
    # CURLE_WRITE_ERROR (23)
    # An error occurred when writing received data to a local file, or
    # an error was returned to libcurl from a write callback.
    # This exception should be ignored if grab_callback_interrupted
    # flag # is enabled (this happens when nohead or nobody options
    # enabled)
    #
    # Also this error is raised when curl receives KeyboardInterrupt
    # while it is processing some callback function
    # (WRITEFUNCTION, HEADERFUNCTIO, etc)
    # If you think WTF then see details here:
    # https://github.com/pycurl/pycurl/issues/413
    if ex.args[0] == 23:
        if getattr(curl, 'grab_callback_interrupted', None) is True:
            # If the execution of body_process callback is
            # interrupted (body_maxsize, nobody and other options)
            # then the pycurl raised exception with code 23
            # We should ignore it
            return None
        else:
            return error.GrabNetworkError(ex.args[1], ex)
    else:
        if ex.args[0] == 28:
            return error.GrabTimeoutError(ex.args[1], ex)
        elif ex.args[0] == 7:
            return error.GrabConnectionError(ex.args[1], ex)
        elif ex.args[0] == 67:
            return error.GrabAuthError(ex.args[1], ex)
        elif ex.args[0] == 47:
            return error.GrabTooManyRedirectsError(ex.args[1], ex)
        elif ex.args[0] == 6:
            return error.GrabCouldNotResolveHostError(ex.args[1], ex)
        elif ex.args[0] == 3:
            return error.GrabInvalidUrl(ex.args[1], ex)
        else:
            return error.GrabNetworkError(ex.args[1], ex)
491820
protected function processStack(){

		do{

			do {
				$status = curl_multi_exec($this->curl_multi, $active);
			}
			while($status === CURLM_CALL_MULTI_PERFORM);

			// welcome to callback hell.
			while($state = curl_multi_info_read($this->curl_multi)){
				$url = $this->multiResponseHandler->handleResponse(new MultiResponse($state['handle']));

				if($url instanceof URL){
					$this->stack[] = $url;
				}

				curl_multi_remove_handle($this->curl_multi, $state['handle']);
				curl_close($state['handle']);
				$this->createHandle();
			}

			if($active){
				curl_multi_select($this->curl_multi, $this->options->timeout);
			}

		}
		while($active && $status === CURLM_OK);

	}
633150
def data_received(self, data):
        
        _LOGGER.debug("Starting: data_received")
        _LOGGER.debug('Received %d bytes from PLM: %s',
                      len(data), binascii.hexlify(data))
        self._buffer.put_nowait(data)
        asyncio.ensure_future(self._peel_messages_from_buffer(),
                              loop=self._loop)

        _LOGGER.debug("Finishing: data_received")
157157
public function receiveMessages($max_count,
                                    $wait = null,
                                    $visibility_timeout = null,
                                    $metas = [],
                                    $message_attributes = [])
    {
        if ($max_count <= 0) {
            return [];
        }
        
        $buffer    = [];
        $one_batch = 10;
        while ($msgs = $this->receiveMessageBatch(
            $one_batch,
            $wait,
            $visibility_timeout,
            $metas,
            $message_attributes
        )) {
            $buffer = array_merge($buffer, $msgs);
            $this->dispatch(self::READ_PROGRESS, count($buffer) / $max_count);
            
            // commented out because sometimes a full queue will somehow return less than 10 messages in a batch
            //if (count($msgs) < 10) {
            //    break;
            //}
            
            $one_batch = min(10, $max_count - count($buffer));
            if ($one_batch <= 0) {
                break;
            }
        }
        
        return $buffer;
    }
561498
protected function curl_execute()
    {
        $ch = curl_init();

        curl_setopt_array($ch, $this->getOptions());

        $this->_content = curl_exec($ch);

        $this->_errorCode = curl_errno($ch);

        $this->_info = curl_getinfo($ch);

        if ($this->_errorCode) {
            $this->_errorMessage = curl_error($ch);
        }
        curl_close($ch);
        return $this->isHttpOK();
    }
450756
def _recv_loop(self):
        """"""
        self._completed_response_lines = []
        self._is_multiline = None
        lines_iterator = self._get_lines()
        while True:
            try:
                line = next(lines_iterator)
                if self._is_multiline is None:
                    self._is_multiline = line.startswith("!") or line == "."
                if line.startswith("ERR"):
                    self._respond(ValueError(line))
                elif self._is_multiline:
                    if line == ".":
                        self._respond(self._completed_response_lines)
                    else:
                        assert line[0] == "!", \
                            "Multiline response {} doesn't start with !" \
                            .format(repr(line))
                        self._completed_response_lines.append(line[1:])
                else:
                    self._respond(line)
            except StopIteration:
                return
            except Exception:
                log.exception("Exception receiving message")
                raise