Skip to content

Commit 9985e7c

Browse files
committed
rustfmt: Run on lightning-block-sync/src/http.rs
1 parent 0ba4d40 commit 9985e7c

File tree

2 files changed

+111
-49
lines changed

2 files changed

+111
-49
lines changed

lightning-block-sync/src/http.rs

Lines changed: 111 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ pub struct HttpEndpoint {
5050
impl HttpEndpoint {
5151
/// Creates an endpoint for the given host and default HTTP port.
5252
pub fn for_host(host: String) -> Self {
53-
Self {
54-
host,
55-
port: None,
56-
path: String::from("/"),
57-
}
53+
Self { host, port: None, path: String::from("/") }
5854
}
5955

6056
/// Specifies a port to use with the endpoint.
@@ -107,7 +103,10 @@ impl HttpClient {
107103
pub fn connect<E: ToSocketAddrs>(endpoint: E) -> std::io::Result<Self> {
108104
let address = match endpoint.to_socket_addrs()?.next() {
109105
None => {
110-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "could not resolve to any addresses"));
106+
return Err(std::io::Error::new(
107+
std::io::ErrorKind::InvalidInput,
108+
"could not resolve to any addresses",
109+
));
111110
},
112111
Some(address) => address,
113112
};
@@ -129,12 +128,16 @@ impl HttpClient {
129128
/// Returns the response body in `F` format.
130129
#[allow(dead_code)]
131130
pub async fn get<F>(&mut self, uri: &str, host: &str) -> std::io::Result<F>
132-
where F: TryFrom<Vec<u8>, Error = std::io::Error> {
131+
where
132+
F: TryFrom<Vec<u8>, Error = std::io::Error>,
133+
{
133134
let request = format!(
134135
"GET {} HTTP/1.1\r\n\
135136
Host: {}\r\n\
136137
Connection: keep-alive\r\n\
137-
\r\n", uri, host);
138+
\r\n",
139+
uri, host
140+
);
138141
let response_body = self.send_request_with_retry(&request).await?;
139142
F::try_from(response_body)
140143
}
@@ -145,8 +148,12 @@ impl HttpClient {
145148
/// The request body consists of the provided JSON `content`. Returns the response body in `F`
146149
/// format.
147150
#[allow(dead_code)]
148-
pub async fn post<F>(&mut self, uri: &str, host: &str, auth: &str, content: serde_json::Value) -> std::io::Result<F>
149-
where F: TryFrom<Vec<u8>, Error = std::io::Error> {
151+
pub async fn post<F>(
152+
&mut self, uri: &str, host: &str, auth: &str, content: serde_json::Value,
153+
) -> std::io::Result<F>
154+
where
155+
F: TryFrom<Vec<u8>, Error = std::io::Error>,
156+
{
150157
let content = content.to_string();
151158
let request = format!(
152159
"POST {} HTTP/1.1\r\n\
@@ -156,7 +163,13 @@ impl HttpClient {
156163
Content-Type: application/json\r\n\
157164
Content-Length: {}\r\n\
158165
\r\n\
159-
{}", uri, host, auth, content.len(), content);
166+
{}",
167+
uri,
168+
host,
169+
auth,
170+
content.len(),
171+
content
172+
);
160173
let response_body = self.send_request_with_retry(&request).await?;
161174
F::try_from(response_body)
162175
}
@@ -218,8 +231,10 @@ impl HttpClient {
218231
let mut reader = std::io::BufReader::new(limited_stream);
219232

220233
macro_rules! read_line {
221-
() => { read_line!(0) };
222-
($retry_count: expr) => { {
234+
() => {
235+
read_line!(0)
236+
};
237+
($retry_count: expr) => {{
223238
let mut line = String::new();
224239
let mut timeout_count: u64 = 0;
225240
let bytes_read = loop {
@@ -236,7 +251,7 @@ impl HttpClient {
236251
} else {
237252
continue;
238253
}
239-
}
254+
},
240255
Err(e) => return Err(e),
241256
}
242257
};
@@ -245,29 +260,39 @@ impl HttpClient {
245260
0 => None,
246261
_ => {
247262
// Remove trailing CRLF
248-
if line.ends_with('\n') { line.pop(); if line.ends_with('\r') { line.pop(); } }
263+
if line.ends_with('\n') {
264+
line.pop();
265+
if line.ends_with('\r') {
266+
line.pop();
267+
}
268+
}
249269
Some(line)
250270
},
251271
}
252-
} }
272+
}};
253273
}
254274

255275
// Read and parse status line
256276
// Note that we allow retrying a few times to reach TCP_STREAM_RESPONSE_TIMEOUT.
257-
let status_line = read_line!(TCP_STREAM_RESPONSE_TIMEOUT.as_secs() / TCP_STREAM_TIMEOUT.as_secs())
258-
.ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no status line"))?;
277+
let status_line =
278+
read_line!(TCP_STREAM_RESPONSE_TIMEOUT.as_secs() / TCP_STREAM_TIMEOUT.as_secs())
279+
.ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no status line"))?;
259280
let status = HttpStatus::parse(&status_line)?;
260281

261282
// Read and parse relevant headers
262283
let mut message_length = HttpMessageLength::Empty;
263284
loop {
264285
let line = read_line!()
265286
.ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no headers"))?;
266-
if line.is_empty() { break; }
287+
if line.is_empty() {
288+
break;
289+
}
267290

268291
let header = HttpHeader::parse(&line)?;
269292
if header.has_name("Content-Length") {
270-
let length = header.value.parse()
293+
let length = header
294+
.value
295+
.parse()
271296
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
272297
if let HttpMessageLength::Empty = message_length {
273298
message_length = HttpMessageLength::ContentLength(length);
@@ -285,10 +310,13 @@ impl HttpClient {
285310
let read_limit = MAX_HTTP_MESSAGE_BODY_SIZE - reader.buffer().len();
286311
reader.get_mut().set_limit(read_limit as u64);
287312
let contents = match message_length {
288-
HttpMessageLength::Empty => { Vec::new() },
313+
HttpMessageLength::Empty => Vec::new(),
289314
HttpMessageLength::ContentLength(length) => {
290315
if length == 0 || length > MAX_HTTP_MESSAGE_BODY_SIZE {
291-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, format!("invalid response length: {} bytes", length)));
316+
return Err(std::io::Error::new(
317+
std::io::ErrorKind::InvalidData,
318+
format!("invalid response length: {} bytes", length),
319+
));
292320
} else {
293321
let mut content = vec![0; length];
294322
#[cfg(feature = "tokio")]
@@ -301,7 +329,9 @@ impl HttpClient {
301329
HttpMessageLength::TransferEncoding(coding) => {
302330
if !coding.eq_ignore_ascii_case("chunked") {
303331
return Err(std::io::Error::new(
304-
std::io::ErrorKind::InvalidInput, "unsupported transfer coding"))
332+
std::io::ErrorKind::InvalidInput,
333+
"unsupported transfer coding",
334+
));
305335
} else {
306336
let mut content = Vec::new();
307337
#[cfg(feature = "tokio")]
@@ -323,7 +353,8 @@ impl HttpClient {
323353

324354
// Decode the chunk header to obtain the chunk size.
325355
let mut buffer = Vec::new();
326-
let mut decoder = chunked_transfer::Decoder::new(chunk_header.as_bytes());
356+
let mut decoder =
357+
chunked_transfer::Decoder::new(chunk_header.as_bytes());
327358
decoder.read_to_end(&mut buffer)?;
328359

329360
// Read the chunk body.
@@ -350,10 +381,7 @@ impl HttpClient {
350381

351382
if !status.is_ok() {
352383
// TODO: Handle 3xx redirection responses.
353-
let error = HttpError {
354-
status_code: status.code.to_string(),
355-
contents,
356-
};
384+
let error = HttpError { status_code: status.code.to_string(), contents };
357385
return Err(std::io::Error::new(std::io::ErrorKind::Other, error));
358386
}
359387

@@ -391,20 +419,30 @@ impl<'a> HttpStatus<'a> {
391419
fn parse(line: &'a String) -> std::io::Result<HttpStatus<'a>> {
392420
let mut tokens = line.splitn(3, ' ');
393421

394-
let http_version = tokens.next()
422+
let http_version = tokens
423+
.next()
395424
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no HTTP-Version"))?;
396-
if !http_version.eq_ignore_ascii_case("HTTP/1.1") &&
397-
!http_version.eq_ignore_ascii_case("HTTP/1.0") {
398-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid HTTP-Version"));
425+
if !http_version.eq_ignore_ascii_case("HTTP/1.1")
426+
&& !http_version.eq_ignore_ascii_case("HTTP/1.0")
427+
{
428+
return Err(std::io::Error::new(
429+
std::io::ErrorKind::InvalidData,
430+
"invalid HTTP-Version",
431+
));
399432
}
400433

401-
let code = tokens.next()
434+
let code = tokens
435+
.next()
402436
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Status-Code"))?;
403437
if code.len() != 3 || !code.chars().all(|c| c.is_ascii_digit()) {
404-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid Status-Code"));
438+
return Err(std::io::Error::new(
439+
std::io::ErrorKind::InvalidData,
440+
"invalid Status-Code",
441+
));
405442
}
406443

407-
let _reason = tokens.next()
444+
let _reason = tokens
445+
.next()
408446
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Reason-Phrase"))?;
409447

410448
Ok(Self { code })
@@ -430,9 +468,11 @@ impl<'a> HttpHeader<'a> {
430468
/// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.2
431469
fn parse(line: &'a String) -> std::io::Result<HttpHeader<'a>> {
432470
let mut tokens = line.splitn(2, ':');
433-
let name = tokens.next()
471+
let name = tokens
472+
.next()
434473
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header name"))?;
435-
let value = tokens.next()
474+
let value = tokens
475+
.next()
436476
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header value"))?
437477
.trim_start();
438478
Ok(Self { name, value })
@@ -524,7 +564,7 @@ mod endpoint_tests {
524564
assert_eq!(addr, std_addrs.next().unwrap());
525565
}
526566
assert!(std_addrs.next().is_none());
527-
}
567+
},
528568
}
529569
}
530570
}
@@ -559,7 +599,11 @@ pub(crate) mod client_tests {
559599
"{}\r\n\
560600
Content-Length: {}\r\n\
561601
\r\n\
562-
{}", status, body.len(), body)
602+
{}",
603+
status,
604+
body.len(),
605+
body
606+
)
563607
},
564608
MessageBody::ChunkedContent(body) => {
565609
let mut chuncked_body = Vec::new();
@@ -572,7 +616,10 @@ pub(crate) mod client_tests {
572616
"{}\r\n\
573617
Transfer-Encoding: chunked\r\n\
574618
\r\n\
575-
{}", status, String::from_utf8(chuncked_body).unwrap())
619+
{}",
620+
status,
621+
String::from_utf8(chuncked_body).unwrap()
622+
)
576623
},
577624
};
578625
HttpServer::responding_with(response)
@@ -606,14 +653,20 @@ pub(crate) mod client_tests {
606653
.lines()
607654
.take_while(|line| !line.as_ref().unwrap().is_empty())
608655
.count();
609-
if lines_read == 0 { continue; }
656+
if lines_read == 0 {
657+
continue;
658+
}
610659

611660
for chunk in response.as_bytes().chunks(16) {
612661
if shutdown_signaled.load(std::sync::atomic::Ordering::SeqCst) {
613662
return;
614663
} else {
615-
if let Err(_) = stream.write(chunk) { break; }
616-
if let Err(_) = stream.flush() { break; }
664+
if let Err(_) = stream.write(chunk) {
665+
break;
666+
}
667+
if let Err(_) = stream.flush() {
668+
break;
669+
}
617670
}
618671
}
619672
}
@@ -636,8 +689,12 @@ pub(crate) mod client_tests {
636689
fn connect_to_unresolvable_host() {
637690
match HttpClient::connect(("example.invalid", 80)) {
638691
Err(e) => {
639-
assert!(e.to_string().contains("failed to lookup address information") ||
640-
e.to_string().contains("No such host"), "{:?}", e);
692+
assert!(
693+
e.to_string().contains("failed to lookup address information")
694+
|| e.to_string().contains("No such host"),
695+
"{:?}",
696+
e
697+
);
641698
},
642699
Ok(_) => panic!("Expected error"),
643700
}
@@ -705,7 +762,9 @@ pub(crate) mod client_tests {
705762
let response = format!(
706763
"HTTP/1.1 302 Found\r\n\
707764
Location: {}\r\n\
708-
\r\n", "Z".repeat(MAX_HTTP_MESSAGE_HEADER_SIZE));
765+
\r\n",
766+
"Z".repeat(MAX_HTTP_MESSAGE_HEADER_SIZE)
767+
);
709768
let server = HttpServer::responding_with(response);
710769

711770
let mut client = HttpClient::connect(&server.endpoint()).unwrap();
@@ -727,7 +786,10 @@ pub(crate) mod client_tests {
727786
match client.get::<BinaryResponse>("/foo", "foo.com").await {
728787
Err(e) => {
729788
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
730-
assert_eq!(e.get_ref().unwrap().to_string(), "invalid response length: 8032001 bytes");
789+
assert_eq!(
790+
e.get_ref().unwrap().to_string(),
791+
"invalid response length: 8032001 bytes"
792+
);
731793
},
732794
Ok(_) => panic!("Expected error"),
733795
}
@@ -740,7 +802,8 @@ pub(crate) mod client_tests {
740802
"HTTP/1.1 200 OK\r\n\
741803
Transfer-Encoding: gzip\r\n\
742804
\r\n\
743-
foobar");
805+
foobar",
806+
);
744807
let server = HttpServer::responding_with(response);
745808

746809
let mut client = HttpClient::connect(&server.endpoint()).unwrap();

rustfmt_excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
./lightning-background-processor/src/lib.rs
2-
./lightning-block-sync/src/http.rs
32
./lightning-block-sync/src/init.rs
43
./lightning-block-sync/src/lib.rs
54
./lightning-block-sync/src/poll.rs

0 commit comments

Comments
 (0)