Skip to content

Commit

Permalink
serialize page token with messagepack instead of as JSON string
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Feb 29, 2024
1 parent 164f2ed commit ab4e092
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
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 = "2.1.0"
paste = "1.0.14"
percent-encoding = "2.3.0"
proc-macro2 = "1.0.69"
rmp-serde = "1.1.2"
rustls = "0.21.7"
rustls-pemfile = "1.0.3"
serde_json = "1.0.108"
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

0 comments on commit ab4e092

Please sign in to comment.