/** * Modify the url and add headers appropriate to authenticate to Acquia Search. * * @return * The nonce used in the request. */ protected function prepareRequest(&$url, &$options, $use_data = TRUE) { // Add a unique request ID to the URL. $id = uniqid(); if (!stristr($url, '?')) { $url .= "?"; } else { $url .= "&"; } $url .= 'request_id=' . $id; // If we're hosted on Acquia, and have an Acquia request ID, // append it to the request so that we map Solr queries to Acquia search requests. if (isset($_ENV['HTTP_X_REQUEST_ID'])) { $xid = empty($_ENV['HTTP_X_REQUEST_ID']) ? '-' : $_ENV['HTTP_X_REQUEST_ID']; $url .= '&x-request-id=' . rawurlencode($xid); } if ($use_data && isset($options['data'])) { list($cookie, $nonce) = acquia_search_auth_cookie($url, $options['data'], NULL, $this->env_id); } else { list($cookie, $nonce) = acquia_search_auth_cookie($url, NULL, NULL, $this->env_id); } if (empty($cookie)) { throw new Exception('Invalid authentication string - subscription keys expired or missing.'); } $options['headers']['Cookie'] = $cookie; $options['headers'] += array('User-Agent' => 'acquia_search/' . variable_get('acquia_search_version', '7.x')); $options['context'] = acquia_agent_stream_context_create($url, 'acquia_search'); if (!$options['context']) { throw new Exception(t("Could not create stream context")); } return $nonce; }
/** * Modify the url and add headers appropriate to authenticate to Acquia Search. * * @return * The nonce used in the request. */ protected function prepareRequest(&$url, &$options, $use_data = TRUE) { $id = uniqid(); if (!stristr($url, '?')) { $url .= "?"; } else { $url .= "&"; } $url .= 'request_id=' . $id; if ($use_data && isset($options['data'])) { list($cookie, $nonce) = acquia_search_auth_cookie($url, $options['data'], NULL, $this->env_id); } else { list($cookie, $nonce) = acquia_search_auth_cookie($url, NULL, NULL, $this->env_id); } if (empty($cookie)) { throw new Exception('Invalid authentication string - subscription keys expired or missing.'); } $options['headers']['Cookie'] = $cookie; $options['headers'] += array('User-Agent' => 'acquia_search/' . variable_get('acquia_search_version', '7.x')); $options['context'] = acquia_agent_stream_context_create($url, 'acquia_search'); if (!$options['context']) { throw new Exception(t("Could not create stream context")); } return $nonce; }
/** * Central method for making a post operation against this Solr Server * * @see Drupal_Apache_Solr_Service::_sendRawGet() */ protected function _sendRawPost($url, $rawPost, $timeout = FALSE, $contentType = 'text/xml; charset=UTF-8') { $this->add_request_id($url); list($cookie, $nonce) = acquia_search_auth_cookie($url, $rawPost); $request_headers = array('Content-Type' => $contentType, 'Cookie' => $cookie); list($data, $headers) = $this->_makeHttpRequest($url, 'POST', $request_headers, $rawPost, $timeout); $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays); if ($response->getHttpStatus() != 200) { throw new Exception('"' . $response->getHttpStatus() . '" Status: ' . $response->getHttpStatusMessage() . "\n<br />request ID: {$id} <br />" . $url, $response->getHttpStatus()); } return $response; }
/** * Central method for making a post operation against this Solr Server * * @see Drupal_Apache_Solr_Service::_sendRawGet() */ protected function _sendRawPost($url, $rawPost, $timeout = FALSE, $contentType = 'text/xml; charset=UTF-8') { $id = $this->add_request_id($url); list($cookie, $nonce) = acquia_search_auth_cookie($url, $rawPost); $request_headers = array('Content-Type' => $contentType, 'Cookie' => $cookie, 'User-Agent' => 'acquia_search/' . ACQUIA_SEARCH_VERSION); list($data, $headers) = $this->_makeHttpRequest($url, 'POST', $request_headers, $rawPost, $timeout); $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays); $code = (int) $response->getHttpStatus(); if ($code != 200) { $message = $response->getHttpStatusMessage() . "\n request ID: {$id} \n"; if ($code >= 400 && $code != 403 && $code != 404) { // Add details, like Solr's exception message. $message .= $response->getRawResponse(); } throw new Exception('"' . $code . '" Status: ' . $message); } return $response; }
/** * Central method for making a post operation against this Solr Server * * @see Drupal_Apache_Solr_Service::_sendRawGet() */ protected function _sendRawPost($url, $rawPost, $timeout = FALSE, $contentType = 'text/xml; charset=UTF-8') { if (variable_get('apachesolr_read_only', 0)) { throw new Exception('Operating in read-only mode; updates are disabled.'); } $id = $this->add_request_id($url); list($cookie, $nonce) = acquia_search_auth_cookie($url, $rawPost); if (empty($cookie)) { throw new Exception('Invalid authentication string - subscription keys expired or missing.'); } $request_headers = array('Content-Type' => $contentType, 'Cookie' => $cookie, 'User-Agent' => 'acquia_search/' . ACQUIA_SEARCH_VERSION); list($data, $headers) = $this->_makeHttpRequest($url, 'POST', $request_headers, $rawPost, $timeout); $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays); $code = (int) $response->getHttpStatus(); if ($code != 200) { $message = $response->getHttpStatusMessage() . "\n request ID: {$id} \n"; if ($code >= 400 && $code != 403 && $code != 404) { // Add details, like Solr's exception message. $message .= $response->getRawResponse(); } throw new Exception('"' . $code . '" Status: ' . $message); } return $response; }
/** * Make a request to a servlet (a path) that's not a standard path. * * @param string $servlet * A path to be added to the base Solr path. e.g. 'extract/tika' * * @param array $params * Any request parameters when constructing the URL. * * @param string $method * 'GET', 'POST', 'PUT', or 'HEAD'. * * @param array $request_headers * Keyed array of header names and values. Should include 'Content-Type' * for POST or PUT. * * @param string $rawPost * Must be an empty string unless method is POST or PUT. * * @param float $timeout * Read timeout in seconds or FALSE. * * @return * Apache_Solr_Response object */ public function makeServletRequest($servlet, $params = array(), $method = 'GET', $request_headers = array(), $rawPost = '', $timeout = FALSE) { if ($method == 'GET' || $method == 'HEAD') { // Make sure we are not sending a request body. $rawPost = ''; } // Add default params. $params += array('wt' => self::SOLR_WRITER); $url = $this->_constructUrl($servlet, $params); $id = $this->add_request_id($url); // We assume we only authenticate the URL for other servlets. list($cookie, $nonce) = acquia_search_auth_cookie($url); if (empty($cookie)) { throw new Exception('Invalid authentication string - subscription keys expired or missing.'); } $request_headers += array('Cookie' => $cookie, 'User-Agent' => 'acquia_search/' . ACQUIA_SEARCH_VERSION); list($data, $headers) = $this->_makeHttpRequest($url, $method, $request_headers, $rawPost, $timeout); $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays); $hmac = acquia_search_extract_hmac($headers); $code = (int) $response->getHttpStatus(); if ($code != 200) { $message = $response->getHttpStatusMessage(); if ($code >= 400 && $code != 403 && $code != 404) { // Add details, like Solr's exception message. $message .= $response->getRawResponse(); } throw new Exception('"' . $code . '" Status: ' . $message); } elseif (!acquia_search_valid_response($hmac, $nonce, $data)) { throw new Exception('Authentication of search content failed url: ' . $url); } return $response; }