mirror of
https://github.com/danbulant/robotparser-rs
synced 2026-06-14 04:01:22 +00:00
Add method read_from_response
This commit is contained in:
parent
d0351ecd2a
commit
36c90ec169
1 changed files with 11 additions and 7 deletions
18
src/lib.rs
18
src/lib.rs
|
|
@ -44,6 +44,7 @@ use url::Url;
|
|||
use hyper::Client;
|
||||
use hyper::header::UserAgent;
|
||||
use hyper::status::StatusCode;
|
||||
use hyper::client::Response;
|
||||
|
||||
const USER_AGENT: &'static str = "robotparser-rs (https://crates.io/crates/robotparser)";
|
||||
|
||||
|
|
@ -247,7 +248,7 @@ impl<'a> RobotFileParser<'a> {
|
|||
let client = Client::new();
|
||||
let request = client.get(self.url.clone())
|
||||
.header(UserAgent(USER_AGENT.to_owned()));
|
||||
let mut res = match request.send() {
|
||||
let res = match request.send() {
|
||||
Ok(res) => res,
|
||||
Err(_) => {
|
||||
return;
|
||||
|
|
@ -260,16 +261,19 @@ impl<'a> RobotFileParser<'a> {
|
|||
status if status >= StatusCode::BadRequest && status < StatusCode::InternalServerError => {
|
||||
self.allow_all.set(true);
|
||||
}
|
||||
StatusCode::Ok => {
|
||||
let mut buf = String::new();
|
||||
res.read_to_string(&mut buf).unwrap();
|
||||
let lines: Vec<&str> = buf.split('\n').collect();
|
||||
self.parse(&lines);
|
||||
}
|
||||
StatusCode::Ok => self.from_response(res),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the HTTP response and feeds it to the parser.
|
||||
pub fn from_response(&self, mut response: Response) {
|
||||
let mut buf = String::new();
|
||||
response.read_to_string(&mut buf).unwrap();
|
||||
let lines: Vec<&str> = buf.split('\n').collect();
|
||||
self.parse(&lines);
|
||||
}
|
||||
|
||||
fn _add_entry(&self, entry: Entry<'a>) {
|
||||
if entry.has_useragent("*") {
|
||||
// the default entry is considered last
|
||||
|
|
|
|||
Loading…
Reference in a new issue