Skip to content

Commit ddcff25

Browse files
authored
Merge pull request #6271 from hugocaillard/chore/upgrade-time-crate
chore: upgrade time crate
2 parents b30a09e + 5e10cfa commit ddcff25

File tree

8 files changed

+39
-40
lines changed

8 files changed

+39
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
77

88
## Unreleased
99

10+
- The HTTP `Date` header in responses now strictly follows RFC7231.
11+
1012
### Changed
1113

1214
- When a previous block commit is unable to be RBFed, the miner will now just wait for it to be confirmed instead of submitting a new block commit which breaks the miner's UTXO chain.

Cargo.lock

Lines changed: 18 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clarity/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ rusqlite = { workspace = true, optional = true }
3737
version = "1.0"
3838
features = ["arbitrary_precision", "unbounded_depth"]
3939

40-
[dependencies.time]
41-
version = "0.2.23"
42-
features = ["std"]
43-
4440
[dev-dependencies]
4541
assert-json-diff = "1.0.0"
4642
mutants = "0.0.3"

stacks-common/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ workspace = true
6868
version = "4.1.3"
6969
features = ["serde"]
7070

71-
[dependencies.time]
72-
version = "0.2.23"
73-
features = ["std"]
74-
7571
[target.'cfg(not(target_family = "wasm"))'.dependencies]
7672
secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }
7773

stacks-common/src/util/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub mod vrf;
3434
use std::fs::File;
3535
use std::io::{BufReader, BufWriter, Write};
3636
use std::path::Path;
37-
use std::time::{SystemTime, UNIX_EPOCH};
38-
use std::{error, fmt, thread, time};
37+
use std::time::{self, SystemTime, UNIX_EPOCH};
38+
use std::{error, fmt, thread};
3939

4040
/// Given a relative path inside the Cargo workspace, return the absolute path
4141
#[cfg(any(test, feature = "testing"))]

stackslib/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ libstackerdb = { path = "../libstackerdb" }
5252
siphasher = "0.3.7"
5353
hashbrown = { workspace = true }
5454
rusqlite = { workspace = true }
55+
time = "0.3.41"
5556
toml = { workspace = true }
5657

5758
[target.'cfg(not(any(target_os = "macos",target_os="windows", target_arch = "arm" )))'.dependencies]
@@ -77,10 +78,6 @@ features = ["serde", "recovery"]
7778
[dependencies.ed25519-dalek]
7879
workspace = true
7980

80-
[dependencies.time]
81-
version = "0.2.23"
82-
features = ["std"]
83-
8481
[dev-dependencies]
8582
assert-json-diff = "1.0.0"
8683
stdext = "0.3.1"

stackslib/src/net/http/response.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use std::collections::{BTreeMap, HashSet};
1818
use std::fmt;
1919
use std::io::{Read, Write};
20-
use std::time::SystemTime;
2120

2221
use stacks_common::codec::{Error as CodecError, StacksMessageCodec};
2322
use stacks_common::deps_common::httparse;
@@ -347,9 +346,11 @@ impl HttpResponsePreamble {
347346
}
348347

349348
/// Get an RFC 7231 date that represents the current time
350-
fn rfc7231_now() -> String {
351-
let now = time::PrimitiveDateTime::from(SystemTime::now());
352-
now.format("%a, %b %-d %-Y %-H:%M:%S GMT")
349+
fn rfc7231_now() -> Result<String, CodecError> {
350+
time::OffsetDateTime::now_utc()
351+
.format(&time::format_description::well_known::Rfc2822)
352+
.map(|date| date.replace("+0000", "GMT"))
353+
.map_err(|e| CodecError::GenericError(format!("Failed to format RFC 7231 date: {:?}", e)))
353354
}
354355

355356
/// Read from a stream until we see '\r\n\r\n', with the purpose of reading an HTTP preamble.
@@ -387,7 +388,7 @@ impl StacksMessageCodec for HttpResponsePreamble {
387388
if !self.headers.contains_key("date") {
388389
fd.write_all("Date: ".as_bytes())
389390
.map_err(CodecError::WriteError)?;
390-
fd.write_all(rfc7231_now().as_bytes())
391+
fd.write_all(rfc7231_now()?.as_bytes())
391392
.map_err(CodecError::WriteError)?;
392393
fd.write_all("\r\n".as_bytes())
393394
.map_err(CodecError::WriteError)?;

stackslib/src/net/http/tests.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use std::collections::BTreeMap;
1818

19+
use regex;
1920
use stacks_common::codec::{Error as CodecError, StacksMessageCodec};
2021
use stacks_common::types::net::{PeerAddress, PeerHost};
2122

@@ -146,7 +147,7 @@ fn test_parse_http_request_preamble_ok() {
146147
("POST asdf HTTP/1.1\r\nHost: core.blockstack.org\r\nConnection: close\r\nFoo: Bar\r\n\r\n",
147148
HttpRequestPreamble::from_headers(HttpVersion::Http11, "POST".to_string(), "asdf".to_string(), "core.blockstack.org".to_string(), 80, false, vec!["foo".to_string()], vec!["Bar".to_string()])),
148149
("POST asdf HTTP/1.1\r\nHost: core.blockstack.org\r\nFoo: Bar\r\nConnection: close\r\n\r\n",
149-
HttpRequestPreamble::from_headers(HttpVersion::Http11, "POST".to_string(), "asdf".to_string(), "core.blockstack.org".to_string(), 80, false, vec!["foo".to_string()], vec!["Bar".to_string()]))
150+
HttpRequestPreamble::from_headers(HttpVersion::Http11, "POST".to_string(), "asdf".to_string(), "core.blockstack.org".to_string(), 80, false, vec!["foo".to_string()], vec!["Bar".to_string()]))
150151
];
151152

152153
for (data, request) in tests.iter() {
@@ -391,6 +392,14 @@ fn test_http_response_preamble_headers() {
391392
"Content-Type is missing"
392393
);
393394
assert!(txt.find("Date: ").is_some(), "Date header is missing");
395+
396+
let rfc7231_date_regex =
397+
regex::Regex::new(r"Date: [A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT\r\n")
398+
.unwrap();
399+
assert!(
400+
rfc7231_date_regex.is_match(&txt),
401+
"Date header format is incorrect"
402+
);
394403
assert!(txt.find("foo: bar\r\n").is_some(), "foo header is missing");
395404
assert!(
396405
txt.find("Access-Control-Allow-Origin: *\r\n").is_some(),

0 commit comments

Comments
 (0)