From 78a87f207c9b4cddf21306ed096e00725b0c8bd5 Mon Sep 17 00:00:00 2001
From: Myzel394 <50424412+Myzel394@users.noreply.github.com>
Date: Sat, 3 Feb 2024 21:35:50 +0100
Subject: [PATCH] feat: Add first wip for http server
---
Cargo.lock | 27 ++++++
Cargo.toml | 4 +-
src/engines/duckduckgo.rs | 173 ++++++++++++++++++++++++------------
src/engines/engine_base.rs | 15 +++-
src/main.rs | 177 ++++++++++++++++++++++++-------------
5 files changed, 273 insertions(+), 123 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 36377d6..729a834 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -310,6 +310,7 @@ checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
dependencies = [
"futures-channel",
"futures-core",
+ "futures-executor",
"futures-io",
"futures-sink",
"futures-task",
@@ -332,12 +333,34 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
+[[package]]
+name = "futures-executor"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
+dependencies = [
+ "futures-core",
+ "futures-task",
+ "futures-util",
+]
+
[[package]]
name = "futures-io"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
+[[package]]
+name = "futures-macro"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "futures-sink"
version = "0.3.30"
@@ -359,6 +382,7 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-io",
+ "futures-macro",
"futures-sink",
"futures-task",
"memchr",
@@ -1416,6 +1440,8 @@ dependencies = [
name = "tcp_test"
version = "0.1.0"
dependencies = [
+ "async-trait",
+ "futures",
"lazy-regex",
"lazy_static",
"regex",
@@ -1505,6 +1531,7 @@ dependencies = [
"libc",
"mio",
"num_cpus",
+ "parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
diff --git a/Cargo.toml b/Cargo.toml
index 34c315a..3ef482e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,12 +6,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+async-trait = "0.1.77"
+futures = "0.3.30"
lazy-regex = "3.1.0"
lazy_static = "1.4.0"
regex = "1.10.3"
reqwest = "0.11.23"
rocket = "0.5.0"
rustls = "0.22.2"
-tokio = "1.35.1"
+tokio = {version = "1.35.1", features = ["full"]}
urlencoding = "2.1.3"
webpki-roots = "0.26.0"
diff --git a/src/engines/duckduckgo.rs b/src/engines/duckduckgo.rs
index 1023895..99eb11d 100644
--- a/src/engines/duckduckgo.rs
+++ b/src/engines/duckduckgo.rs
@@ -1,46 +1,23 @@
// Search engine parser for DuckDuckGo
pub mod duckduckgo {
- use lazy_regex::regex_replace_all;
- // Results start at:
- //
- // Example for a result:
- //
+ use std::{
+ cmp::min,
+ collections::VecDeque,
+ pin::Pin,
+ str::Bytes,
+ task::{Context, Poll},
+ };
+
+ use async_trait::async_trait;
+ use futures::Stream;
use lazy_static::lazy_static;
use regex::Regex;
use urlencoding::decode;
use crate::{
- engines::engine_base::engine_base::{EngineBase, SearchResult},
- utils::utils::{decode_html_text, replace_html_unicode},
+ client::client::{Client, PACKET_SIZE},
+ engines::engine_base::engine_base::{EngineBase, SearchEngine, SearchResult},
+ utils::utils::decode_html_text,
};
lazy_static! {
@@ -50,10 +27,15 @@ pub mod duckduckgo {
static ref STRIP_HTML_TAGS: Regex = Regex::new(r#"<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>"#).unwrap();
}
+ pub type CallbackType = Box
() + Send + Sync>;
+
pub struct DuckDuckGo {
- pub search_results: Vec,
+ callback: CallbackType,
+ pub completed: bool,
results_started: bool,
previous_block: String,
+ // Holds all results until consumed by iterator
+ pub results: VecDeque,
}
impl DuckDuckGo {
@@ -65,14 +47,86 @@ pub mod duckduckgo {
self.previous_block.clear();
self.previous_block.push_str(&remaining_text);
}
- }
- impl EngineBase for DuckDuckGo {
- fn get_search_results(&self) -> &Vec {
- &self.search_results
+ pub fn new() -> Self {
+ Self {
+ callback: Box::new(|_: SearchResult| {}),
+ results_started: false,
+ previous_block: String::new(),
+ results: VecDeque::new(),
+ completed: false,
+ }
}
- fn parse_packet<'a>(&mut self, packet: impl Iterator- ) -> String {
+ pub fn set_callback(&mut self, callback: CallbackType) {
+ self.callback = callback;
+ }
+ }
+
+ // impl Stream for DuckDuckGo {
+ // type Item = String;
+ //
+ // fn poll_next(
+ // self: Pin<&mut Self>,
+ // cx: &mut Context<'_>,
+ // ) -> std::task::Poll