feat: Add working example using reqwest

This commit is contained in:
Myzel394 2024-02-19 20:04:16 +01:00
parent e1bd09df44
commit 6c0751740c
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
3 changed files with 63 additions and 33 deletions

7
Cargo.lock generated
View File

@ -1245,16 +1245,17 @@ dependencies = [
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.17.7" version = "0.17.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74" checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [ dependencies = [
"cc", "cc",
"cfg-if",
"getrandom", "getrandom",
"libc", "libc",
"spin", "spin",
"untrusted", "untrusted",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]

5
README.md Normal file
View File

@ -0,0 +1,5 @@
## Ideas
* HTTP3 support
* First show all results, then sort them on server, hide old results and show new sorted ones
* Check if results have already been sent and don't send them again

View File

@ -20,7 +20,7 @@
// Found no other way to make this work // Found no other way to make this work
use crate::engines::engine_base::engine_base::EngineBase; use crate::engines::engine_base::engine_base::EngineBase;
use std::io::{Read, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream; use std::net::TcpStream;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
@ -28,7 +28,9 @@ use std::time::Instant;
use engines::brave::brave::Brave; use engines::brave::brave::Brave;
use futures::lock::Mutex; use futures::lock::Mutex;
use futures::StreamExt;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use reqwest::ClientBuilder;
use rocket::response::{ use rocket::response::{
content::{RawCss, RawHtml}, content::{RawCss, RawHtml},
stream::TextStream, stream::TextStream,
@ -98,7 +100,7 @@ async fn slowresponse() -> TextStream![String] {
#[get("/searchquery?<query>")] #[get("/searchquery?<query>")]
async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> { async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
let query_box = Box::new(query.to_string()); let query_box = query.to_string();
let now = Arc::new(Box::new(Instant::now())); let now = Arc::new(Box::new(Instant::now()));
let completed_ref = Arc::new(Mutex::new(false)); let completed_ref = Arc::new(Mutex::new(false));
@ -202,44 +204,66 @@ async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
let mut sock = TcpStream::connect("search.brave.com:443").unwrap(); let mut sock = TcpStream::connect("search.brave.com:443").unwrap();
let mut tls = rustls::Stream::new(&mut conn, &mut sock); let mut tls = rustls::Stream::new(&mut conn, &mut sock);
let now_sock = Instant::now();
tls.write_all( tls.write_all(
concat!( format!(
"GET /search?q=test&show_local=0&source=unlocalise HTTP/1.1\r\n", "GET /search?q={}&show_local=0&source=unlocalise HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\nAccept-Encoding: identity\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3\r\n\r\n",
"Host: search.brave.com\r\n", query_box,
"Connection: close\r\n",
"Accept-Encoding: identity\r\n",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3\r\n",
"\r\n",
) )
.as_bytes(), .as_bytes(),
) )
.unwrap(); .unwrap();
loop { let nw = Instant::now();
let mut buf = [0; 65535];
tls.conn.complete_io(tls.sock);
let n = tls.conn.reader().read(&mut buf);
// dbg!(&n); let client = ClientBuilder::new().build().unwrap();
let resposne = client
.get("https://www.bing.com/search?q=test")
.send()
.await
.unwrap();
if n.is_ok() { dbg!(nw.elapsed());
let n = n.unwrap();
if n == 0 {
break;
}
// println!("{}", String::from_utf8_lossy(&buf));
let mut brave = ddg_ref_writer.lock().await;
if let Some(result) = brave.parse_packet(buf.iter()) { let mut stream = resposne.bytes_stream();
println!("Brave: {}", now_ref.elapsed().as_millis()); while let Some(chunk) = stream.next().await {
brave.add_result(result); let chunk = chunk.unwrap();
drop(brave); println!("{}", "========");
tokio::task::yield_now().await; dbg!(nw.elapsed());
} println!("{}", String::from_utf8_lossy(&chunk));
}
} }
dbg!(nw.elapsed());
// loop {
// let mut buf = [0; 16384];
// let n = tls.conn.reader().read(&mut buf);
//
// // dbg!(&n);
//
// if n.is_ok() {
// let n = n.unwrap();
// if n == 0 {
// break;
// }
// // println!("{}", String::from_utf8_lossy(&buf));
// // let mut brave = ddg_ref_writer.lock().await;
//
// dbg!(nw.elapsed());
//
// // if let Some(result) = brave.parse_packet(buf.iter()) {
// // // println!("Brave: {}", now_ref.elapsed().as_millis());
// // brave.add_result(result);
// //
// // drop(brave);
// // tokio::task::yield_now().await;
// // }
// } else {
// tls.conn.complete_io(tls.sock);
// }
// }
let mut completed = completed_ref_writer.lock().await; let mut completed = completed_ref_writer.lock().await;
*completed = true; *completed = true;
}); });
@ -255,8 +279,8 @@ async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
let len = ddg.results.len(); let len = ddg.results.len();
if len == 0 { if len == 0 {
drop(ddg); drop(ddg);
tokio::task::yield_now().await; tokio::task::yield_now().await;
continue continue
} }