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

Serialize page token with MessagePack instead of as JSON string #924

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions dropshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ multer = "3.0.0"
paste = "1.0.14"
percent-encoding = "2.3.1"
proc-macro2 = "1.0.78"
rmp-serde = "1.1.2"
rustls = "0.22.2"
rustls-pemfile = "2.1.0"
serde_json = "1.0.114"
Expand Down
19 changes: 9 additions & 10 deletions dropshot/src/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,12 @@ fn serialize_page_token<PageSelector: Serialize>(
let serialized_token =
SerializedToken { v: PaginationVersion::V1, page_start };

let json_bytes =
serde_json::to_vec(&serialized_token).map_err(|e| {
HttpError::for_internal_error(format!(
"failed to serialize token: {}",
e
))
})?;
let json_bytes = rmp_serde::to_vec(&serialized_token).map_err(|e| {
HttpError::for_internal_error(format!(
"failed to serialize token: {}",
e
))
})?;

URL_SAFE.encode(json_bytes)
};
Expand Down Expand Up @@ -470,7 +469,7 @@ fn deserialize_page_token<PageSelector: DeserializeOwned>(
// output of the error anyway. It's not clear how else we could
// propagate this information out.
let deserialized: SerializedToken<PageSelector> =
serde_json::from_slice(&json_bytes).map_err(|_| {
rmp_serde::from_slice(&json_bytes).map_err(|_| {
String::from("failed to parse pagination token: corrupted token")
})?;

Expand Down Expand Up @@ -533,7 +532,7 @@ mod test {
s: String,
}
let input =
TokenWithStr { s: String::from_utf8(vec![b'e'; 352]).unwrap() };
TokenWithStr { s: String::from_utf8(vec![b'e'; 376]).unwrap() };
let serialized = serialize_page_token(&input).unwrap();
assert_eq!(serialized.len(), super::MAX_TOKEN_LENGTH);
let output: TokenWithStr = deserialize_page_token(&serialized).unwrap();
Expand All @@ -544,7 +543,7 @@ mod test {
// Start by attempting to serialize a token larger than the maximum
// allowed size.
let input =
TokenWithStr { s: String::from_utf8(vec![b'e'; 353]).unwrap() };
TokenWithStr { s: String::from_utf8(vec![b'e'; 377]).unwrap() };
let error = serialize_page_token(&input).unwrap_err();
assert_eq!(error.status_code, http::StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(error.external_message, "Internal Server Error");
Expand Down