tiny_http_client 0.1.1

Tiny synchronous HTTP/HTTPS GET and POST client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use std::io::{Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
use std::sync::{Arc, Once};

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
use rustls::pki_types::ServerName;

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned};

#[cfg(any(
    windows,
    target_os = "macos",
    all(target_os = "linux", feature = "linux-native-tls")
))]
use native_tls::TlsConnector;

const MAX_REDIRECTS: usize = 5;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
static RUSTLS_INIT: Once = Once::new();

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
fn init_rustls() {
    RUSTLS_INIT.call_once(|| {
        rustls::crypto::ring::default_provider()
            .install_default()
            .expect("Failed to install rustls crypto provider");
    });
}

#[cfg(all(
    feature = "linux-own-cert-list",
    target_os = "linux",
    not(feature = "linux-native-tls")
))]
mod own_certs {
    include!(env!("TINY_HTTP_CLIENT_OWN_CERTS"));
}

pub struct Response {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: Vec<u8>,
}

impl Response {
    pub fn as_bytes(&self) -> &[u8] {
        &self.body
    }

    pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
        std::str::from_utf8(&self.body)
    }

    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .map(|(_, value)| value.as_str())
    }
}

/// Simple HTTP GET request.
#[cfg(feature = "http-get")]
pub fn get(url: &str) -> Result<Response, Box<dyn std::error::Error>> {
    get_with_headers(url, &[])
}

/// HTTP GET request with custom headers.
///
/// Example:
///
/// get_with_headers(
///     "https://example.com/",
///     &[("User-Agent", "my-client")],
/// )?;
#[cfg(feature = "http-get")]
pub fn get_with_headers(
    url: &str,
    headers: &[(&str, &str)],
) -> Result<Response, Box<dyn std::error::Error>> {
    #[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
    init_rustls();

    request_redirect("GET", url, None, headers, 0)
}

/// Simple HTTP POST request.
///
/// The request body is sent as-is.
///
/// If a Content-Type is required, it can be supplied using
/// `post_with_headers()`.
///
/// Example:
///
/// post(
///     "https://example.com/api",
///     br#"{"hello":"world"}"#,
/// )?;
#[cfg(feature = "http-post")]
pub fn post(url: &str, body: &[u8]) -> Result<Response, Box<dyn std::error::Error>> {
    post_with_headers(url, body, &[])
}

/// HTTP POST request with custom headers.
///
/// Example:
///
/// post_with_headers(
///     "https://example.com/api",
///     br#"{"hello":"world"}"#,
///     &[("Content-Type", "application/json")],
/// )?;
#[cfg(feature = "http-post")]
pub fn post_with_headers(
    url: &str,
    body: &[u8],
    headers: &[(&str, &str)],
) -> Result<Response, Box<dyn std::error::Error>> {
    #[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
    init_rustls();

    request_redirect("POST", url, Some(body), headers, 0)
}

fn request_redirect(
    method: &str,
    url: &str,
    body: Option<&[u8]>,
    headers: &[(&str, &str)],
    redirect_count: usize,
) -> Result<Response, Box<dyn std::error::Error>> {
    if redirect_count > MAX_REDIRECTS {
        return Err("too many HTTP redirects".into());
    }

    let parsed_url = parse_url(url)?;

    let response = if parsed_url.https {
        request_https(&parsed_url, method, body, headers)?
    } else {
        request_http(&parsed_url, method, body, headers)?
    };

    /*
     * We follow all standard HTTP redirects:
     *
     * 301 Moved Permanently
     * 302 Found
     * 303 See Other
     * 307 Temporary Redirect
     * 308 Permanent Redirect
     *
     * For POST requests:
     *
     * 301 / 302 / 303 are converted to GET.
     *
     * 307 / 308 preserve the original method and body.
     */
    match response.status {
        301 | 302 | 303 | 307 | 308 => {
            if let Some(location) = response.header("Location") {
                let next_url = resolve_redirect(&parsed_url, location)?;

                let preserve_method =
                    method.eq_ignore_ascii_case("GET") || matches!(response.status, 307 | 308);

                if preserve_method {
                    return request_redirect(method, &next_url, body, headers, redirect_count + 1);
                }

                return request_redirect("GET", &next_url, None, headers, redirect_count + 1);
            }
        }
        _ => {}
    }

    if !(200..300).contains(&response.status) {
        return Err(format!("HTTP request failed: {}", response.status).into());
    }

    Ok(response)
}

struct ParsedUrl {
    https: bool,
    host: String,
    port: u16,
    path: String,
}

fn parse_url(input: &str) -> Result<ParsedUrl, Box<dyn std::error::Error>> {
    let (https, rest) = if let Some(rest) = input.strip_prefix("https://") {
        (true, rest)
    } else if let Some(rest) = input.strip_prefix("http://") {
        (false, rest)
    } else {
        return Err(format!("unsupported URL: {}", input).into());
    };

    let (authority, path) = match rest.find('/') {
        Some(pos) => (&rest[..pos], &rest[pos..]),
        None => (rest, "/"),
    };

    let (host, port) = if let Some(host_part) = authority.strip_prefix('[') {
        let end = host_part.find(']').ok_or("invalid IPv6 address")?;

        let host = &host_part[..end];

        let port = host_part
            .get(end + 1..)
            .and_then(|s| s.strip_prefix(':'))
            .and_then(|s| s.parse::<u16>().ok())
            .unwrap_or(if https { 443 } else { 80 });

        (host.to_string(), port)
    } else {
        match authority.rfind(':') {
            Some(pos) if authority[pos + 1..].parse::<u16>().is_ok() => {
                (authority[..pos].to_string(), authority[pos + 1..].parse()?)
            }

            _ => (authority.to_string(), if https { 443 } else { 80 }),
        }
    };

    Ok(ParsedUrl {
        https,
        host,
        port,
        path: path.to_string(),
    })
}

fn request_http(
    url: &ParsedUrl,
    method: &str,
    body: Option<&[u8]>,
    headers: &[(&str, &str)],
) -> Result<Response, Box<dyn std::error::Error>> {
    let addr = format!("{}:{}", url.host, url.port);

    let socket_addr = addr
        .to_socket_addrs()?
        .next()
        .ok_or("failed to resolve host")?;

    let mut stream = TcpStream::connect_timeout(&socket_addr, CONNECT_TIMEOUT)?;

    stream.set_read_timeout(Some(CONNECT_TIMEOUT))?;
    stream.set_write_timeout(Some(CONNECT_TIMEOUT))?;

    write_request(&mut stream, method, &url.host, &url.path, headers, body)?;

    read_response(&mut stream)
}

#[cfg(any(
    windows,
    target_os = "macos",
    all(target_os = "linux", feature = "linux-native-tls")
))]
fn request_https(
    url: &ParsedUrl,
    method: &str,
    body: Option<&[u8]>,
    headers: &[(&str, &str)],
) -> Result<Response, Box<dyn std::error::Error>> {
    let addr = format!("{}:{}", url.host, url.port);

    let socket_addr = addr
        .to_socket_addrs()?
        .next()
        .ok_or("failed to resolve host")?;

    let tcp = TcpStream::connect_timeout(&socket_addr, CONNECT_TIMEOUT)?;

    tcp.set_read_timeout(Some(CONNECT_TIMEOUT))?;
    tcp.set_write_timeout(Some(CONNECT_TIMEOUT))?;

    let connector = TlsConnector::new()?;

    let mut stream = connector.connect(&url.host, tcp)?;

    write_request(&mut stream, method, &url.host, &url.path, headers, body)?;

    read_response(&mut stream)
}

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
fn load_root_certificates() -> Result<RootCertStore, Box<dyn std::error::Error>> {
    let mut root_store = RootCertStore::empty();

    #[cfg(feature = "linux-own-cert-list")]
    {
        for cert in own_certs::load() {
            root_store.add(cert)?;
        }
    }

    #[cfg(not(feature = "linux-own-cert-list"))]
    {
        root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    }

    Ok(root_store)
}

#[cfg(all(target_os = "linux", not(feature = "linux-native-tls")))]
fn request_https(
    url: &ParsedUrl,
    method: &str,
    body: Option<&[u8]>,
    headers: &[(&str, &str)],
) -> Result<Response, Box<dyn std::error::Error>> {
    let addr = format!("{}:{}", url.host, url.port);

    let socket_addr = addr
        .to_socket_addrs()?
        .next()
        .ok_or("failed to resolve host")?;

    let tcp = TcpStream::connect_timeout(&socket_addr, CONNECT_TIMEOUT)?;

    tcp.set_read_timeout(Some(CONNECT_TIMEOUT))?;
    tcp.set_write_timeout(Some(CONNECT_TIMEOUT))?;

    let root_store = load_root_certificates()?;

    let config = ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    /*
     * ServerName owns the hostname.
     *
     * rustls requires an owned value with a 'static lifetime
     * for ClientConnection.
     */
    let server_name = ServerName::try_from(url.host.clone())?;

    let connection = ClientConnection::new(Arc::new(config), server_name)?;

    let mut stream = StreamOwned::new(connection, tcp);

    write_request(&mut stream, method, &url.host, &url.path, headers, body)?;

    read_response(&mut stream)
}

fn write_request<S: Write>(
    stream: &mut S,
    method: &str,
    host: &str,
    path: &str,
    headers: &[(&str, &str)],
    body: Option<&[u8]>,
) -> Result<(), Box<dyn std::error::Error>> {
    /*
     * Calculate Content-Length before writing the request.
     *
     * GET requests don't have a request body.
     */
    let body_len = body.map_or(0, |body| body.len());

    write!(
        stream,
        "{} {} HTTP/1.1\r\n\
         Host: {}\r\n\
         User-Agent: peers_updater\r\n\
         Accept: */*\r\n\
         Connection: close\r\n",
        method, path, host
    )?;

    /*
     * POST requests need Content-Length so the server knows
     * exactly how many bytes belong to the request body.
     */
    if body.is_some() {
        write!(stream, "Content-Length: {}\r\n", body_len)?;
    }

    for (name, value) in headers {
        /*
         * Don't allow callers to accidentally inject CR/LF
         * into the HTTP request.
         */
        if name.contains('\r')
            || name.contains('\n')
            || value.contains('\r')
            || value.contains('\n')
        {
            return Err("invalid HTTP header".into());
        }

        write!(stream, "{}: {}\r\n", name, value)?;
    }

    write!(stream, "\r\n")?;

    if let Some(body) = body {
        stream.write_all(body)?;
    }

    stream.flush()?;

    Ok(())
}

fn read_response<R: Read>(stream: &mut R) -> Result<Response, Box<dyn std::error::Error>> {
    let mut data = Vec::new();

    stream.read_to_end(&mut data)?;

    parse_response(&data)
}

fn parse_response(data: &[u8]) -> Result<Response, Box<dyn std::error::Error>> {
    let header_end = find_header_end(data).ok_or("invalid HTTP response: headers not found")?;

    let header_bytes = &data[..header_end];

    let raw_body = &data[header_end + 4..];

    let header_text = std::str::from_utf8(header_bytes)?;

    let mut lines = header_text.split("\r\n");

    let status_line = lines
        .next()
        .ok_or("invalid HTTP response: status line missing")?;

    let mut status_parts = status_line.splitn(3, ' ');

    let _http_version = status_parts.next().ok_or("invalid HTTP status line")?;

    let status = status_parts
        .next()
        .ok_or("invalid HTTP status line")?
        .parse::<u16>()?;

    let mut headers = Vec::new();

    for line in lines {
        if line.is_empty() {
            continue;
        }

        if let Some(pos) = line.find(':') {
            let key = line[..pos].trim().to_string();
            let value = line[pos + 1..].trim().to_string();

            headers.push((key, value));
        }
    }

    /*
     * HTTP/1.1 chunked transfer encoding.
     *
     * Example:
     *
     * 4\r\n
     * Wiki\r\n
     * 5\r\n
     * pedia\r\n
     * 0\r\n
     * \r\n
     *
     * The chunk size is hexadecimal and is not part of the
     * actual response body.
     */
    let body = if headers.iter().any(|(key, value)| {
        key.eq_ignore_ascii_case("Transfer-Encoding")
            && value
                .split(',')
                .any(|encoding| encoding.trim().eq_ignore_ascii_case("chunked"))
    }) {
        decode_chunked(raw_body)?
    } else {
        raw_body.to_vec()
    };

    Ok(Response {
        status,
        headers,
        body,
    })
}

fn decode_chunked(data: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let mut body = Vec::new();
    let mut pos = 0;

    loop {
        /*
         * Find the end of the chunk-size line.
         */
        let line_end = find_crlf(&data[pos..]).ok_or(
            "invalid chunked response: \
                 chunk size not found",
        )?;

        let size_line = &data[pos..pos + line_end];

        /*
         * Ignore optional chunk extensions:
         *
         * 4;foo=bar
         *
         * Only the part before ';' is the hexadecimal size.
         */
        let size_text = match size_line.iter().position(|&b| b == b';') {
            Some(index) => &size_line[..index],
            None => size_line,
        };

        let size_text = std::str::from_utf8(size_text)?.trim();

        let chunk_size = usize::from_str_radix(size_text, 16).map_err(|_| "invalid chunk size")?;

        pos += line_end + 2;

        /*
         * Zero-sized chunk marks the end of the body.
         */
        if chunk_size == 0 {
            /*
             * A chunked response may contain trailer
             * after the final zero-sized chunk.
             *
             * We don't need them, so simply stop here.
             */
            return Ok(body);
        }

        /*
         * Make sure the complete chunk is available.
         */
        let chunk_end = pos.checked_add(chunk_size).ok_or("chunk size overflow")?;

        if chunk_end > data.len() {
            return Err("invalid chunked response: incomplete chunk".into());
        }

        body.extend_from_slice(&data[pos..chunk_end]);

        pos = chunk_end;

        /*
         * Every chunk-data section must be followed by CRLF.
         */
        if data.len() < pos + 2 || data[pos] != b'\r' || data[pos + 1] != b'\n' {
            return Err("invalid chunked response: missing CRLF".into());
        }

        pos += 2;
    }
}

fn find_crlf(data: &[u8]) -> Option<usize> {
    data.windows(2).position(|window| window == b"\r\n")
}

fn find_header_end(data: &[u8]) -> Option<usize> {
    data.windows(4).position(|window| window == b"\r\n\r\n")
}

fn resolve_redirect(
    current: &ParsedUrl,
    location: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    let location = location.trim();

    /*
     * Absolute URL:
     *
     * Location: https://objects.githubusercontent.com/...
     */
    if location.starts_with("http://") || location.starts_with("https://") {
        return Ok(location.to_string());
    }

    /*
     * Protocol-relative URL:
     *
     * Location: //objects.githubusercontent.com/...
     */
    if let Some(rest) = location.strip_prefix("//") {
        return Ok(format!(
            "{}://{}",
            if current.https { "https" } else { "http" },
            rest
        ));
    }

    /*
     * Absolute path:
     *
     * Location: /foo/bar
     */
    if location.starts_with('/') {
        return Ok(format!(
            "{}://{}:{}{}",
            if current.https { "https" } else { "http" },
            current.host,
            current.port,
            location
        ));
    }

    /*
     * Relative path:
     *
     * Location: foo/bar
     */
    let base_path = match current.path.rfind('/') {
        Some(pos) => &current.path[..pos + 1],
        None => "/",
    };

    Ok(format!(
        "{}://{}:{}{}{}",
        if current.https { "https" } else { "http" },
        current.host,
        current.port,
        base_path,
        location
    ))
}