Increase the timeout for RPC responses from Bitcoin Core
[rust-lightning] / lightning-block-sync / src / http.rs
1 //! Simple HTTP implementation which supports both async and traditional execution environments
2 //! with minimal dependencies. This is used as the basis for REST and RPC clients.
3
4 use chunked_transfer;
5 use serde_json;
6
7 use std::convert::TryFrom;
8 #[cfg(not(feature = "tokio"))]
9 use std::io::Write;
10 use std::net::ToSocketAddrs;
11 use std::time::Duration;
12
13 #[cfg(feature = "tokio")]
14 use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
15 #[cfg(feature = "tokio")]
16 use tokio::net::TcpStream;
17
18 #[cfg(not(feature = "tokio"))]
19 use std::io::BufRead;
20 use std::io::Read;
21 #[cfg(not(feature = "tokio"))]
22 use std::net::TcpStream;
23
24 /// Timeout for operations on TCP streams.
25 const TCP_STREAM_TIMEOUT: Duration = Duration::from_secs(5);
26
27 /// Timeout for reading the first byte of a response. This is separate from the general read
28 /// timeout as it is not uncommon for Bitcoin Core to be blocked waiting on UTXO cache flushes for
29 /// upwards of a minute or more. Note that we always retry once when we time out, so the maximum
30 /// time we allow Bitcoin Core to block for is twice this value.
31 const TCP_STREAM_RESPONSE_TIMEOUT: Duration = Duration::from_secs(120);
32
33 /// Maximum HTTP message header size in bytes.
34 const MAX_HTTP_MESSAGE_HEADER_SIZE: usize = 8192;
35
36 /// Maximum HTTP message body size in bytes. Enough for a hex-encoded block in JSON format and any
37 /// overhead for HTTP chunked transfer encoding.
38 const MAX_HTTP_MESSAGE_BODY_SIZE: usize = 2 * 4_000_000 + 32_000;
39
40 /// Endpoint for interacting with an HTTP-based API.
41 #[derive(Debug)]
42 pub struct HttpEndpoint {
43         host: String,
44         port: Option<u16>,
45         path: String,
46 }
47
48 impl HttpEndpoint {
49         /// Creates an endpoint for the given host and default HTTP port.
50         pub fn for_host(host: String) -> Self {
51                 Self {
52                         host,
53                         port: None,
54                         path: String::from("/"),
55                 }
56         }
57
58         /// Specifies a port to use with the endpoint.
59         pub fn with_port(mut self, port: u16) -> Self {
60                 self.port = Some(port);
61                 self
62         }
63
64         /// Specifies a path to use with the endpoint.
65         pub fn with_path(mut self, path: String) -> Self {
66                 self.path = path;
67                 self
68         }
69
70         /// Returns the endpoint host.
71         pub fn host(&self) -> &str {
72                 &self.host
73         }
74
75         /// Returns the endpoint port.
76         pub fn port(&self) -> u16 {
77                 match self.port {
78                         None => 80,
79                         Some(port) => port,
80                 }
81         }
82
83         /// Returns the endpoint path.
84         pub fn path(&self) -> &str {
85                 &self.path
86         }
87 }
88
89 impl<'a> std::net::ToSocketAddrs for &'a HttpEndpoint {
90         type Iter = <(&'a str, u16) as std::net::ToSocketAddrs>::Iter;
91
92         fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
93                 (self.host(), self.port()).to_socket_addrs()
94         }
95 }
96
97 /// Client for making HTTP requests.
98 pub(crate) struct HttpClient {
99         stream: TcpStream,
100 }
101
102 impl HttpClient {
103         /// Opens a connection to an HTTP endpoint.
104         pub fn connect<E: ToSocketAddrs>(endpoint: E) -> std::io::Result<Self> {
105                 let address = match endpoint.to_socket_addrs()?.next() {
106                         None => {
107                                 return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "could not resolve to any addresses"));
108                         },
109                         Some(address) => address,
110                 };
111                 let stream = std::net::TcpStream::connect_timeout(&address, TCP_STREAM_TIMEOUT)?;
112                 stream.set_read_timeout(Some(TCP_STREAM_TIMEOUT))?;
113                 stream.set_write_timeout(Some(TCP_STREAM_TIMEOUT))?;
114
115                 #[cfg(feature = "tokio")]
116                 let stream = {
117                         stream.set_nonblocking(true)?;
118                         TcpStream::from_std(stream)?
119                 };
120
121                 Ok(Self { stream })
122         }
123
124         /// Sends a `GET` request for a resource identified by `uri` at the `host`.
125         ///
126         /// Returns the response body in `F` format.
127         #[allow(dead_code)]
128         pub async fn get<F>(&mut self, uri: &str, host: &str) -> std::io::Result<F>
129         where F: TryFrom<Vec<u8>, Error = std::io::Error> {
130                 let request = format!(
131                         "GET {} HTTP/1.1\r\n\
132                          Host: {}\r\n\
133                          Connection: keep-alive\r\n\
134                          \r\n", uri, host);
135                 let response_body = self.send_request_with_retry(&request).await?;
136                 F::try_from(response_body)
137         }
138
139         /// Sends a `POST` request for a resource identified by `uri` at the `host` using the given HTTP
140         /// authentication credentials.
141         ///
142         /// The request body consists of the provided JSON `content`. Returns the response body in `F`
143         /// format.
144         #[allow(dead_code)]
145         pub async fn post<F>(&mut self, uri: &str, host: &str, auth: &str, content: serde_json::Value) -> std::io::Result<F>
146         where F: TryFrom<Vec<u8>, Error = std::io::Error> {
147                 let content = content.to_string();
148                 let request = format!(
149                         "POST {} HTTP/1.1\r\n\
150                          Host: {}\r\n\
151                          Authorization: {}\r\n\
152                          Connection: keep-alive\r\n\
153                          Content-Type: application/json\r\n\
154                          Content-Length: {}\r\n\
155                          \r\n\
156                          {}", uri, host, auth, content.len(), content);
157                 let response_body = self.send_request_with_retry(&request).await?;
158                 F::try_from(response_body)
159         }
160
161         /// Sends an HTTP request message and reads the response, returning its body. Attempts to
162         /// reconnect and retry if the connection has been closed.
163         async fn send_request_with_retry(&mut self, request: &str) -> std::io::Result<Vec<u8>> {
164                 let endpoint = self.stream.peer_addr().unwrap();
165                 match self.send_request(request).await {
166                         Ok(bytes) => Ok(bytes),
167                         Err(_) => {
168                                 // Reconnect and retry on fail. This can happen if the connection was closed after
169                                 // the keep-alive limits are reached, or generally if the request timed out due to
170                                 // Bitcoin Core being stuck on a long-running operation or its RPC queue being
171                                 // full.
172                                 // Block 100ms before retrying the request as in many cases the source of the error
173                                 // may be persistent for some time.
174                                 #[cfg(feature = "tokio")]
175                                 tokio::time::sleep(Duration::from_millis(100)).await;
176                                 #[cfg(not(feature = "tokio"))]
177                                 std::thread::sleep(Duration::from_millis(100));
178                                 *self = Self::connect(endpoint)?;
179                                 self.send_request(request).await
180                         },
181                 }
182         }
183
184         /// Sends an HTTP request message and reads the response, returning its body.
185         async fn send_request(&mut self, request: &str) -> std::io::Result<Vec<u8>> {
186                 self.write_request(request).await?;
187                 self.read_response().await
188         }
189
190         /// Writes an HTTP request message.
191         async fn write_request(&mut self, request: &str) -> std::io::Result<()> {
192                 #[cfg(feature = "tokio")]
193                 {
194                         self.stream.write_all(request.as_bytes()).await?;
195                         self.stream.flush().await
196                 }
197                 #[cfg(not(feature = "tokio"))]
198                 {
199                         self.stream.write_all(request.as_bytes())?;
200                         self.stream.flush()
201                 }
202         }
203
204         /// Reads an HTTP response message.
205         async fn read_response(&mut self) -> std::io::Result<Vec<u8>> {
206                 #[cfg(feature = "tokio")]
207                 let stream = self.stream.split().0;
208                 #[cfg(not(feature = "tokio"))]
209                 let stream = std::io::Read::by_ref(&mut self.stream);
210
211                 let limited_stream = stream.take(MAX_HTTP_MESSAGE_HEADER_SIZE as u64);
212
213                 #[cfg(feature = "tokio")]
214                 let mut reader = tokio::io::BufReader::new(limited_stream);
215                 #[cfg(not(feature = "tokio"))]
216                 let mut reader = std::io::BufReader::new(limited_stream);
217
218                 macro_rules! read_line {
219                         () => { read_line!(0) };
220                         ($retry_count: expr) => { {
221                                 let mut line = String::new();
222                                 let mut timeout_count: u64 = 0;
223                                 let bytes_read = loop {
224                                         #[cfg(feature = "tokio")]
225                                         let read_res = reader.read_line(&mut line).await;
226                                         #[cfg(not(feature = "tokio"))]
227                                         let read_res = reader.read_line(&mut line);
228                                         match read_res {
229                                                 Ok(bytes_read) => break bytes_read,
230                                                 Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
231                                                         timeout_count += 1;
232                                                         if timeout_count > $retry_count {
233                                                                 return Err(e);
234                                                         } else {
235                                                                 continue;
236                                                         }
237                                                 }
238                                                 Err(e) => return Err(e),
239                                         }
240                                 };
241
242                                 match bytes_read {
243                                         0 => None,
244                                         _ => {
245                                                 // Remove trailing CRLF
246                                                 if line.ends_with('\n') { line.pop(); if line.ends_with('\r') { line.pop(); } }
247                                                 Some(line)
248                                         },
249                                 }
250                         } }
251                 }
252
253                 // Read and parse status line
254                 // Note that we allow retrying a few times to reach TCP_STREAM_RESPONSE_TIMEOUT.
255                 let status_line = read_line!(TCP_STREAM_RESPONSE_TIMEOUT.as_secs() / TCP_STREAM_TIMEOUT.as_secs())
256                         .ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no status line"))?;
257                 let status = HttpStatus::parse(&status_line)?;
258
259                 // Read and parse relevant headers
260                 let mut message_length = HttpMessageLength::Empty;
261                 loop {
262                         let line = read_line!()
263                                 .ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no headers"))?;
264                         if line.is_empty() { break; }
265
266                         let header = HttpHeader::parse(&line)?;
267                         if header.has_name("Content-Length") {
268                                 let length = header.value.parse()
269                                         .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
270                                 if let HttpMessageLength::Empty = message_length {
271                                         message_length = HttpMessageLength::ContentLength(length);
272                                 }
273                                 continue;
274                         }
275
276                         if header.has_name("Transfer-Encoding") {
277                                 message_length = HttpMessageLength::TransferEncoding(header.value.into());
278                                 continue;
279                         }
280                 }
281
282                 if !status.is_ok() {
283                         // TODO: Handle 3xx redirection responses.
284                         return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "not found"));
285                 }
286
287                 // Read message body
288                 let read_limit = MAX_HTTP_MESSAGE_BODY_SIZE - reader.buffer().len();
289                 reader.get_mut().set_limit(read_limit as u64);
290                 match message_length {
291                         HttpMessageLength::Empty => { Ok(Vec::new()) },
292                         HttpMessageLength::ContentLength(length) => {
293                                 if length == 0 || length > MAX_HTTP_MESSAGE_BODY_SIZE {
294                                         Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "out of range"))
295                                 } else {
296                                         let mut content = vec![0; length];
297                                         #[cfg(feature = "tokio")]
298                                         reader.read_exact(&mut content[..]).await?;
299                                         #[cfg(not(feature = "tokio"))]
300                                         reader.read_exact(&mut content[..])?;
301                                         Ok(content)
302                                 }
303                         },
304                         HttpMessageLength::TransferEncoding(coding) => {
305                                 if !coding.eq_ignore_ascii_case("chunked") {
306                                         Err(std::io::Error::new(
307                                                         std::io::ErrorKind::InvalidInput, "unsupported transfer coding"))
308                                 } else {
309                                         let mut content = Vec::new();
310                                         #[cfg(feature = "tokio")]
311                                         {
312                                                 // Since chunked_transfer doesn't have an async interface, only use it to
313                                                 // determine the size of each chunk to read.
314                                                 //
315                                                 // TODO: Replace with an async interface when available.
316                                                 // https://github.com/frewsxcv/rust-chunked-transfer/issues/7
317                                                 loop {
318                                                         // Read the chunk header which contains the chunk size.
319                                                         let mut chunk_header = String::new();
320                                                         reader.read_line(&mut chunk_header).await?;
321                                                         if chunk_header == "0\r\n" {
322                                                                 // Read the terminator chunk since the decoder consumes the CRLF
323                                                                 // immediately when this chunk is encountered.
324                                                                 reader.read_line(&mut chunk_header).await?;
325                                                         }
326
327                                                         // Decode the chunk header to obtain the chunk size.
328                                                         let mut buffer = Vec::new();
329                                                         let mut decoder = chunked_transfer::Decoder::new(chunk_header.as_bytes());
330                                                         decoder.read_to_end(&mut buffer)?;
331
332                                                         // Read the chunk body.
333                                                         let chunk_size = match decoder.remaining_chunks_size() {
334                                                                 None => break,
335                                                                 Some(chunk_size) => chunk_size,
336                                                         };
337                                                         let chunk_offset = content.len();
338                                                         content.resize(chunk_offset + chunk_size + "\r\n".len(), 0);
339                                                         reader.read_exact(&mut content[chunk_offset..]).await?;
340                                                         content.resize(chunk_offset + chunk_size, 0);
341                                                 }
342                                                 Ok(content)
343                                         }
344                                         #[cfg(not(feature = "tokio"))]
345                                         {
346                                                 let mut decoder = chunked_transfer::Decoder::new(reader);
347                                                 decoder.read_to_end(&mut content)?;
348                                                 Ok(content)
349                                         }
350                                 }
351                         },
352                 }
353         }
354 }
355
356 /// HTTP response status code as defined by [RFC 7231].
357 ///
358 /// [RFC 7231]: https://tools.ietf.org/html/rfc7231#section-6
359 struct HttpStatus<'a> {
360         code: &'a str,
361 }
362
363 impl<'a> HttpStatus<'a> {
364         /// Parses an HTTP status line as defined by [RFC 7230].
365         ///
366         /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.1.2
367         fn parse(line: &'a String) -> std::io::Result<HttpStatus<'a>> {
368                 let mut tokens = line.splitn(3, ' ');
369
370                 let http_version = tokens.next()
371                         .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no HTTP-Version"))?;
372                 if !http_version.eq_ignore_ascii_case("HTTP/1.1") &&
373                         !http_version.eq_ignore_ascii_case("HTTP/1.0") {
374                         return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid HTTP-Version"));
375                 }
376
377                 let code = tokens.next()
378                         .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Status-Code"))?;
379                 if code.len() != 3 || !code.chars().all(|c| c.is_ascii_digit()) {
380                         return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid Status-Code"));
381                 }
382
383                 let _reason = tokens.next()
384                         .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Reason-Phrase"))?;
385
386                 Ok(Self { code })
387         }
388
389         /// Returns whether the status is successful (i.e., 2xx status class).
390         fn is_ok(&self) -> bool {
391                 self.code.starts_with('2')
392         }
393 }
394
395 /// HTTP response header as defined by [RFC 7231].
396 ///
397 /// [RFC 7231]: https://tools.ietf.org/html/rfc7231#section-7
398 struct HttpHeader<'a> {
399         name: &'a str,
400         value: &'a str,
401 }
402
403 impl<'a> HttpHeader<'a> {
404         /// Parses an HTTP header field as defined by [RFC 7230].
405         ///
406         /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.2
407         fn parse(line: &'a String) -> std::io::Result<HttpHeader<'a>> {
408                 let mut tokens = line.splitn(2, ':');
409                 let name = tokens.next()
410                         .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header name"))?;
411                 let value = tokens.next()
412                         .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header value"))?
413                         .trim_start();
414                 Ok(Self { name, value })
415         }
416
417         /// Returns whether the header field has the given name.
418         fn has_name(&self, name: &str) -> bool {
419                 self.name.eq_ignore_ascii_case(name)
420         }
421 }
422
423 /// HTTP message body length as defined by [RFC 7230].
424 ///
425 /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.3.3
426 enum HttpMessageLength {
427         Empty,
428         ContentLength(usize),
429         TransferEncoding(String),
430 }
431
432 /// An HTTP response body in binary format.
433 pub struct BinaryResponse(pub Vec<u8>);
434
435 /// An HTTP response body in JSON format.
436 pub struct JsonResponse(pub serde_json::Value);
437
438 /// Interprets bytes from an HTTP response body as binary data.
439 impl TryFrom<Vec<u8>> for BinaryResponse {
440         type Error = std::io::Error;
441
442         fn try_from(bytes: Vec<u8>) -> std::io::Result<Self> {
443                 Ok(BinaryResponse(bytes))
444         }
445 }
446
447 /// Interprets bytes from an HTTP response body as a JSON value.
448 impl TryFrom<Vec<u8>> for JsonResponse {
449         type Error = std::io::Error;
450
451         fn try_from(bytes: Vec<u8>) -> std::io::Result<Self> {
452                 Ok(JsonResponse(serde_json::from_slice(&bytes)?))
453         }
454 }
455
456 #[cfg(test)]
457 mod endpoint_tests {
458         use super::HttpEndpoint;
459
460         #[test]
461         fn with_default_port() {
462                 let endpoint = HttpEndpoint::for_host("foo.com".into());
463                 assert_eq!(endpoint.host(), "foo.com");
464                 assert_eq!(endpoint.port(), 80);
465         }
466
467         #[test]
468         fn with_custom_port() {
469                 let endpoint = HttpEndpoint::for_host("foo.com".into()).with_port(8080);
470                 assert_eq!(endpoint.host(), "foo.com");
471                 assert_eq!(endpoint.port(), 8080);
472         }
473
474         #[test]
475         fn with_uri_path() {
476                 let endpoint = HttpEndpoint::for_host("foo.com".into()).with_path("/path".into());
477                 assert_eq!(endpoint.host(), "foo.com");
478                 assert_eq!(endpoint.path(), "/path");
479         }
480
481         #[test]
482         fn without_uri_path() {
483                 let endpoint = HttpEndpoint::for_host("foo.com".into());
484                 assert_eq!(endpoint.host(), "foo.com");
485                 assert_eq!(endpoint.path(), "/");
486         }
487
488         #[test]
489         fn convert_to_socket_addrs() {
490                 let endpoint = HttpEndpoint::for_host("foo.com".into());
491                 let host = endpoint.host();
492                 let port = endpoint.port();
493
494                 use std::net::ToSocketAddrs;
495                 match (&endpoint).to_socket_addrs() {
496                         Err(e) => panic!("Unexpected error: {:?}", e),
497                         Ok(mut socket_addrs) => {
498                                 match socket_addrs.next() {
499                                         None => panic!("Expected socket address"),
500                                         Some(addr) => {
501                                                 assert_eq!(addr, (host, port).to_socket_addrs().unwrap().next().unwrap());
502                                                 assert!(socket_addrs.next().is_none());
503                                         }
504                                 }
505                         }
506                 }
507         }
508 }
509
510 #[cfg(test)]
511 pub(crate) mod client_tests {
512         use super::*;
513         use std::io::BufRead;
514         use std::io::Write;
515
516         /// Server for handling HTTP client requests with a stock response.
517         pub struct HttpServer {
518                 address: std::net::SocketAddr,
519                 handler: std::thread::JoinHandle<()>,
520                 shutdown: std::sync::Arc<std::sync::atomic::AtomicBool>,
521         }
522
523         /// Body of HTTP response messages.
524         pub enum MessageBody<T: ToString> {
525                 Empty,
526                 Content(T),
527                 ChunkedContent(T),
528         }
529
530         impl HttpServer {
531                 pub fn responding_with_ok<T: ToString>(body: MessageBody<T>) -> Self {
532                         let response = match body {
533                                 MessageBody::Empty => "HTTP/1.1 200 OK\r\n\r\n".to_string(),
534                                 MessageBody::Content(body) => {
535                                         let body = body.to_string();
536                                         format!(
537                                                 "HTTP/1.1 200 OK\r\n\
538                                                  Content-Length: {}\r\n\
539                                                  \r\n\
540                                                  {}", body.len(), body)
541                                 },
542                                 MessageBody::ChunkedContent(body) => {
543                                         let mut chuncked_body = Vec::new();
544                                         {
545                                                 use chunked_transfer::Encoder;
546                                                 let mut encoder = Encoder::with_chunks_size(&mut chuncked_body, 8);
547                                                 encoder.write_all(body.to_string().as_bytes()).unwrap();
548                                         }
549                                         format!(
550                                                 "HTTP/1.1 200 OK\r\n\
551                                                  Transfer-Encoding: chunked\r\n\
552                                                  \r\n\
553                                                  {}", String::from_utf8(chuncked_body).unwrap())
554                                 },
555                         };
556                         HttpServer::responding_with(response)
557                 }
558
559                 pub fn responding_with_not_found() -> Self {
560                         let response = "HTTP/1.1 404 Not Found\r\n\r\n".to_string();
561                         HttpServer::responding_with(response)
562                 }
563
564                 fn responding_with(response: String) -> Self {
565                         let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
566                         let address = listener.local_addr().unwrap();
567
568                         let shutdown = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
569                         let shutdown_signaled = std::sync::Arc::clone(&shutdown);
570                         let handler = std::thread::spawn(move || {
571                                 for stream in listener.incoming() {
572                                         let mut stream = stream.unwrap();
573                                         stream.set_write_timeout(Some(TCP_STREAM_TIMEOUT)).unwrap();
574
575                                         let lines_read = std::io::BufReader::new(&stream)
576                                                 .lines()
577                                                 .take_while(|line| !line.as_ref().unwrap().is_empty())
578                                                 .count();
579                                         if lines_read == 0 { continue; }
580
581                                         for chunk in response.as_bytes().chunks(16) {
582                                                 if shutdown_signaled.load(std::sync::atomic::Ordering::SeqCst) {
583                                                         return;
584                                                 } else {
585                                                         if let Err(_) = stream.write(chunk) { break; }
586                                                         if let Err(_) = stream.flush() { break; }
587                                                 }
588                                         }
589                                 }
590                         });
591
592                         Self { address, handler, shutdown }
593                 }
594
595                 fn shutdown(self) {
596                         self.shutdown.store(true, std::sync::atomic::Ordering::SeqCst);
597                         self.handler.join().unwrap();
598                 }
599
600                 pub fn endpoint(&self) -> HttpEndpoint {
601                         HttpEndpoint::for_host(self.address.ip().to_string()).with_port(self.address.port())
602                 }
603         }
604
605         #[test]
606         fn connect_to_unresolvable_host() {
607                 match HttpClient::connect(("example.invalid", 80)) {
608                         Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other),
609                         Ok(_) => panic!("Expected error"),
610                 }
611         }
612
613         #[test]
614         fn connect_with_no_socket_address() {
615                 match HttpClient::connect(&vec![][..]) {
616                         Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput),
617                         Ok(_) => panic!("Expected error"),
618                 }
619         }
620
621         #[test]
622         fn connect_with_unknown_server() {
623                 match HttpClient::connect(("::", 80)) {
624                         #[cfg(target_os = "windows")]
625                         Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::AddrNotAvailable),
626                         #[cfg(not(target_os = "windows"))]
627                         Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::ConnectionRefused),
628                         Ok(_) => panic!("Expected error"),
629                 }
630         }
631
632         #[tokio::test]
633         async fn connect_with_valid_endpoint() {
634                 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
635
636                 match HttpClient::connect(&server.endpoint()) {
637                         Err(e) => panic!("Unexpected error: {:?}", e),
638                         Ok(_) => {},
639                 }
640         }
641
642         #[tokio::test]
643         async fn read_empty_message() {
644                 let server = HttpServer::responding_with("".to_string());
645
646                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
647                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
648                         Err(e) => {
649                                 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
650                                 assert_eq!(e.get_ref().unwrap().to_string(), "no status line");
651                         },
652                         Ok(_) => panic!("Expected error"),
653                 }
654         }
655
656         #[tokio::test]
657         async fn read_incomplete_message() {
658                 let server = HttpServer::responding_with("HTTP/1.1 200 OK".to_string());
659
660                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
661                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
662                         Err(e) => {
663                                 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
664                                 assert_eq!(e.get_ref().unwrap().to_string(), "no headers");
665                         },
666                         Ok(_) => panic!("Expected error"),
667                 }
668         }
669
670         #[tokio::test]
671         async fn read_too_large_message_headers() {
672                 let response = format!(
673                         "HTTP/1.1 302 Found\r\n\
674                          Location: {}\r\n\
675                          \r\n", "Z".repeat(MAX_HTTP_MESSAGE_HEADER_SIZE));
676                 let server = HttpServer::responding_with(response);
677
678                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
679                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
680                         Err(e) => {
681                                 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
682                                 assert_eq!(e.get_ref().unwrap().to_string(), "no headers");
683                         },
684                         Ok(_) => panic!("Expected error"),
685                 }
686         }
687
688         #[tokio::test]
689         async fn read_too_large_message_body() {
690                 let body = "Z".repeat(MAX_HTTP_MESSAGE_BODY_SIZE + 1);
691                 let server = HttpServer::responding_with_ok::<String>(MessageBody::Content(body));
692
693                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
694                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
695                         Err(e) => {
696                                 assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
697                                 assert_eq!(e.get_ref().unwrap().to_string(), "out of range");
698                         },
699                         Ok(_) => panic!("Expected error"),
700                 }
701                 server.shutdown();
702         }
703
704         #[tokio::test]
705         async fn read_message_with_unsupported_transfer_coding() {
706                 let response = String::from(
707                         "HTTP/1.1 200 OK\r\n\
708                          Transfer-Encoding: gzip\r\n\
709                          \r\n\
710                          foobar");
711                 let server = HttpServer::responding_with(response);
712
713                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
714                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
715                         Err(e) => {
716                                 assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
717                                 assert_eq!(e.get_ref().unwrap().to_string(), "unsupported transfer coding");
718                         },
719                         Ok(_) => panic!("Expected error"),
720                 }
721         }
722
723         #[tokio::test]
724         async fn read_empty_message_body() {
725                 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
726
727                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
728                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
729                         Err(e) => panic!("Unexpected error: {:?}", e),
730                         Ok(bytes) => assert_eq!(bytes.0, Vec::<u8>::new()),
731                 }
732         }
733
734         #[tokio::test]
735         async fn read_message_body_with_length() {
736                 let body = "foo bar baz qux".repeat(32);
737                 let content = MessageBody::Content(body.clone());
738                 let server = HttpServer::responding_with_ok::<String>(content);
739
740                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
741                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
742                         Err(e) => panic!("Unexpected error: {:?}", e),
743                         Ok(bytes) => assert_eq!(bytes.0, body.as_bytes()),
744                 }
745         }
746
747         #[tokio::test]
748         async fn read_chunked_message_body() {
749                 let body = "foo bar baz qux".repeat(32);
750                 let chunked_content = MessageBody::ChunkedContent(body.clone());
751                 let server = HttpServer::responding_with_ok::<String>(chunked_content);
752
753                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
754                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
755                         Err(e) => panic!("Unexpected error: {:?}", e),
756                         Ok(bytes) => assert_eq!(bytes.0, body.as_bytes()),
757                 }
758         }
759
760         #[tokio::test]
761         async fn reconnect_closed_connection() {
762                 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
763
764                 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
765                 assert!(client.get::<BinaryResponse>("/foo", "foo.com").await.is_ok());
766                 match client.get::<BinaryResponse>("/foo", "foo.com").await {
767                         Err(e) => panic!("Unexpected error: {:?}", e),
768                         Ok(bytes) => assert_eq!(bytes.0, Vec::<u8>::new()),
769                 }
770         }
771
772         #[test]
773         fn from_bytes_into_binary_response() {
774                 let bytes = b"foo";
775                 match BinaryResponse::try_from(bytes.to_vec()) {
776                         Err(e) => panic!("Unexpected error: {:?}", e),
777                         Ok(response) => assert_eq!(&response.0, bytes),
778                 }
779         }
780
781         #[test]
782         fn from_invalid_bytes_into_json_response() {
783                 let json = serde_json::json!({ "result": 42 });
784                 match JsonResponse::try_from(json.to_string().as_bytes()[..5].to_vec()) {
785                         Err(_) => {},
786                         Ok(_) => panic!("Expected error"),
787                 }
788         }
789
790         #[test]
791         fn from_valid_bytes_into_json_response() {
792                 let json = serde_json::json!({ "result": 42 });
793                 match JsonResponse::try_from(json.to_string().as_bytes().to_vec()) {
794                         Err(e) => panic!("Unexpected error: {:?}", e),
795                         Ok(response) => assert_eq!(response.0, json),
796                 }
797         }
798 }