private function getOpenGrokSearchResults(string $branch, array $params) : \Generator { $branch = $branch === self::DEFAULT_BRANCH ? $branch : ($branch = 'PHP-' . $branch); $url = self::BASE_URL . '?project=' . $branch . '&n=10000&' . http_build_query($params); try { $request = (new HttpRequest())->setMethod('GET')->setUri($url); $this->cookieJar->store(new Cookie('OpenGrokProject', $branch, null, null, self::BASE_URL)); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($request)); /** @var \DOMDocument $doc */ $doc = domdocument_load_html($response->getBody()); } catch (\Throwable $e) { throw new OpenGrokSearchFailureException("Totally failed to get a valid [results page]({$url})", 1); } if (!($resultsDiv = $doc->getElementById('results'))) { throw new OpenGrokSearchFailureException("The [results page]({$url}) is not in the format I expected it to be", 1); } try { $resultsTable = xpath_get_element($resultsDiv, './table'); } catch (ElementNotFoundException $e) { throw new OpenGrokSearchFailureException("There were no [results]({$url}) for that search", 0); } $dir = null; $tests = false; $trim = strlen('/xref/' . $branch); $results = ['url' => $url, 'count' => 0, 'code' => [], 'tests' => []]; $baseUrl = new Uri($url); foreach ($resultsTable->getElementsByTagName('tr') as $row) { /** @var \DOMElement $row */ if (preg_match('#\\bdir\\b#', $row->getAttribute('class'))) { $tests = (bool) preg_match('#/tests/#i', xpath_get_element($row, './td/a')->textContent); continue; } foreach (xpath_get_elements($row, "./td/tt/a[@class='s']") as $resultAnchor) { $hrefAttr = $resultAnchor->getAttribute('href'); $path = substr($hrefAttr, $trim); $href = (string) $baseUrl->resolve($hrefAttr); $el = xpath_get_element($resultAnchor, "./span[@class='l']"); $line = $el->textContent; $code = ''; while ($el = $el->nextSibling) { $code .= $el->textContent; } $results[$tests ? 'tests' : 'code'][] = ['href' => $href, 'path' => $path, 'line' => $line, 'code' => trim(preg_replace('#\\s+#', ' ', $code))]; $results['count']++; } } return $results; }
private function getRedirectUri(RequestCycle $cycle) { $request = $cycle->request; $response = $cycle->response; if (!$cycle->options[self::OP_FOLLOW_LOCATION]) { return null; } if (!$response->hasHeader('Location')) { return null; } $status = $response->getStatus(); $method = $request->getMethod(); if ($status < 200 || $status > 399 || $method === 'HEAD') { return null; } $requestUri = new Uri($request->getUri()); $redirectLocation = current($response->getHeader('Location')); if (!$requestUri->canResolve($redirectLocation)) { return null; } $newUri = $requestUri->resolve($redirectLocation); $cycle->redirectHistory[] = $request->getUri(); return $newUri; }
/** * @dataProvider provideResolvables */ public function testResolve($baseUri, $toResolve, $expectedResult) { $baseUri = new Uri($baseUri); $this->assertEquals($expectedResult, $baseUri->resolve($toResolve)); }