use super::interception::setup_request_interception;
use super::{
acquire_fingerprinted_page, collect_and_build_response, navigate_and_wait, InterceptGuard,
PlaywrightEngine, XhrCaptureSession,
};
use crate::engines::client::playwright_pool::BrowserPool;
use crate::engines::engine_client::{EngineError, InternalScrapeRequest, InternalScrapeResponse};
use crate::engines::validators;
use std::time::Instant;
pub(super) async fn validate_scrape_request(
request: &InternalScrapeRequest,
) -> Result<(), EngineError> {
if request.method != crate::engines::engine_client::HttpMethod::Get {
return Err(EngineError::Other("Unsupported HTTP method".to_string()));
}
validators::validate_url(&request.url)
.await
.map_err(|e| EngineError::Other(format!("SSRF protection: {}", e)))?;
if !request.needs_js && !request.needs_screenshot {
return Err(EngineError::AllEnginesFailed(
"PlaywrightEngine only supports JS and screenshot requests".to_string(),
));
}
Ok(())
}
impl PlaywrightEngine {
pub(super) async fn scrape_session(
&self,
request: &InternalScrapeRequest,
pool: &BrowserPool,
start: Instant,
) -> Result<InternalScrapeResponse, EngineError> {
let (pooled_page, page) = acquire_fingerprinted_page(pool, &self.ua_pool, request).await?;
if !request.headers.is_empty() {
log::warn!("Custom headers are currently partially supported in PlaywrightEngine due to API constraints");
}
let mut intercept_guard = InterceptGuard::new();
setup_request_interception(&page, request, &mut intercept_guard).await?;
let xhr_session = match &self.xhr_capture {
Some(config) => XhrCaptureSession::start(&page, config).await,
None => None,
};
navigate_and_wait(&page, request).await?;
collect_and_build_response(&page, request, xhr_session.as_ref(), pooled_page, start).await
}
}