-
Notifications
You must be signed in to change notification settings - Fork 168
Implement HTTP protocol #469
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6611cf4
Write HTTP protocol
swlynch99 07f1f69
Fix license headers
swlynch99 889934e
Fix bad assert
swlynch99 278fb53
Remove debug logging from util.rs
swlynch99 505fb48
Implement Default for RequestParser
swlynch99 51a6980
Add debug implementations for request types
swlynch99 d11c67f
Add request parsing tests
swlynch99 ee170e3
formatting
swlynch99 3e829e6
Update Cargo.toml to use workspace dependencies
swlynch99 c54745b
Add response tests
swlynch99 cb2f5ab
Add support for klog with http requests
swlynch99 cfaa3a1
Refactor klog impls somewhat
swlynch99 6b2c7f5
Move dependency versions up to workspace Cargo.toml
swlynch99 a723905
Expand klog format to also include response status
swlynch99 486fbe1
Add missing license headers
swlynch99 f92e23a
Document protocol-http
swlynch99 bd1e073
formatting
swlynch99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
|
|
||
| [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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
|
||
| //! 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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.