Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP protocol #469

Merged
merged 17 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"src/net",
"src/protocol/admin",
"src/protocol/common",
"src/protocol/http",
"src/protocol/memcache",
"src/protocol/ping",
"src/protocol/resp",
Expand All @@ -38,22 +39,26 @@ members = [

[workspace.dependencies]
ahash = "0.8.0"
arrayvec = "0.7.2"
backtrace = "0.3.66"
bitvec = "1.0.1"
blake3 = "1.3.1"
boring = "2.1.0"
boring-sys = "2.1.0"
bstr = "1.0.1"
bytes = "1.2.1"
clap = "2.33.3"
crossbeam-channel = "0.5.6"
crossbeam-queue = "0.3.5"
foreign-types-shared = "0.3.1"
httparse = "1.8.0"
libc = "0.2.134"
log = "0.4.17"
memmap2 = "0.2.2"
metrohash = "1.0.6"
mio = "0.8.4"
nom = "5.1.2"
phf = "0.11.1"
proc-macro2 = "1.0.46"
quote = "1.0.21"
rand = "0.8.5"
Expand All @@ -70,6 +75,7 @@ thiserror = "1.0.24"
tiny_http = "0.11.0"
toml = "0.5.9"
twox-hash = { version = "1.6.3", default-features = false }
urlencoding = "2.1.2"
zookeeper = "0.6.1"

[profile.release]
Expand Down
22 changes: 22 additions & 0 deletions src/protocol/http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "protocol-http"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

swlynch99 marked this conversation as resolved.
Show resolved Hide resolved
[dependencies]
arrayvec = { workspace = true }
bytes = { workspace = true }
bstr = { workspace = true }
httparse = { workspace = true }
phf = { workspace = true, features = ["macros"] }
thiserror = { workspace = true }
urlencoding = { workspace = true }

protocol-common = { path = "../common" }
logger = { path = "../../logger" }

[dev-dependencies]
assert_matches = "1.5.0"
62 changes: 62 additions & 0 deletions src/protocol/http/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 Twitter, Inc.
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

use crate::Response;

#[derive(Debug, Error)]
pub enum Error {
#[error("unable to parse request")]
Unparseable(#[from] httparse::Error),
#[error("Content-Length header was invalid")]
BadContentLength,
#[error("Content-Length header was missing")]
MissingContentLength,
#[error("method was unsupported")]
BadRequestMethod,

/// Contains the number of additional bytes needed to parse the rest of the
/// request, if known.
#[error("not enough data present to parse the whole request")]
PartialRequest(Option<usize>),

#[error("an internal error occurred: {0}")]
InternalError(&'static str),
}

impl Error {
pub fn to_response(&self) -> Response {
match self {
Self::Unparseable(e) => Response::builder(400)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(format!("Unable to parse request: {}", e).as_bytes()),
Self::BadRequestMethod => Response::builder(405)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(
format!("Unsupported method, only GET, PUT, and DELETE are supported")
.as_bytes(),
),
Self::BadContentLength => Response::builder(400)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(format!("Content-Length header was invalid").as_bytes()),
Self::MissingContentLength => Response::builder(411)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(
format!("A Content-Length header is required for all PUT requests").as_bytes(),
),
Self::InternalError(message) => Response::builder(500)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(message.as_bytes()),

Self::PartialRequest(_) => Response::builder(500)
.should_close(true)
.header("Content-Type", b"text/plain")
.body(b"internal server error"),
}
}
}
38 changes: 38 additions & 0 deletions src/protocol/http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 Twitter, Inc.
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

//! HTTP protocol for pelikan.
//!
swlynch99 marked this conversation as resolved.
Show resolved Hide resolved
//! This crate contains definitions for a basic REST protocol for interacting
//! with a cache. It supports just 3 operations:
//! - `GET` - get the value associated with the provided key, if present
//! - `PUT` - set the value associated with the provided key
//! - `DELETE` - remove a key from the cache
//!
//! In all cases the key is passed in as the request path in the request
//! and the value is passed in as the request body. The protocol supports
//! reusing the HTTP connection for multiple requests. The only length
//! specification supported by pelikan is setting the Content-Length header.

#[macro_use]
extern crate thiserror;

mod error;
pub mod request;
pub mod response;
mod util;

pub use crate::error::Error;
pub use crate::request::Headers;
pub use crate::request::{ParseData, Request, RequestData, RequestParser};
pub use crate::response::Response;

pub type Result<T> = std::result::Result<T, Error>;
pub type ParseResult = Result<Request>;

pub trait Storage {
fn get(&mut self, key: &[u8], headers: &Headers) -> Response;
fn put(&mut self, key: &[u8], value: &[u8], headers: &Headers) -> Response;
fn delete(&mut self, key: &[u8], headers: &Headers) -> Response;
}
Loading