idevice 0.1.68

A Rust library to interact with services on iOS devices.
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
//! Minimal async HTTP/1.1 client.
//!
//! The crate only needs a handful of one-shot HTTP requests: the tunneld device
//! list, the TSS controller, and the restore data-service proxy. Rather
//! than pull in every single dependency on planet earth for that,
//! this module speaks HTTP/1.1 directly.
//!
//! It is deliberately small and not a general-purpose client.

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::debug;

use crate::{IdeviceError, ReadWrite};

/// Maximum number of redirects to follow before giving up
const MAX_REDIRECTS: usize = 10;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
    Get,
    Post,
}

impl Method {
    fn as_str(self) -> &'static str {
        match self {
            Method::Get => "GET",
            Method::Post => "POST",
        }
    }
}

#[derive(Debug, Clone)]
pub struct HttpRequest {
    method: Method,
    url: String,
    headers: Vec<(String, String)>,
    body: Vec<u8>,
}

impl HttpRequest {
    pub fn get(url: impl Into<String>) -> Self {
        Self::new(Method::Get, url)
    }

    pub fn post(url: impl Into<String>) -> Self {
        Self::new(Method::Post, url)
    }

    fn new(method: Method, url: impl Into<String>) -> Self {
        Self {
            method,
            url: url.into(),
            headers: Vec::new(),
            body: Vec::new(),
        }
    }

    /// Adds a request header. Duplicate names are preserved and sent in order.
    #[must_use]
    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    #[must_use]
    pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = body.into();
        self
    }

    /// Sends the request, following redirects, and returns the final response.
    pub async fn send(self) -> Result<HttpResponse, IdeviceError> {
        let mut method = self.method;
        let mut url = self.url;
        let mut headers = self.headers;
        let mut body = self.body;

        for _ in 0..=MAX_REDIRECTS {
            let resp = send_once(method, &url, &headers, &body).await?;
            match resp.status {
                301 | 302 | 303 | 307 | 308 => {
                    let Some(location) = resp.header("location") else {
                        return Ok(resp);
                    };
                    url = resolve_redirect(&url, location)?;
                    debug!("following {} redirect to {url}", resp.status);
                    // 307/308 preserve the method and body; 301/302/303 degrade to
                    // a bodyless GET, matching browser and `reqwest` behavior.
                    if !matches!(resp.status, 307 | 308) {
                        method = Method::Get;
                        body = Vec::new();
                        headers.retain(|(k, _)| {
                            !k.eq_ignore_ascii_case("content-length")
                                && !k.eq_ignore_ascii_case("content-type")
                        });
                    }
                }
                _ => return Ok(resp),
            }
        }
        Err(IdeviceError::Http(format!(
            "too many redirects (>{MAX_REDIRECTS})"
        )))
    }
}

pub async fn get(url: impl Into<String>) -> Result<HttpResponse, IdeviceError> {
    HttpRequest::get(url).send().await
}

#[derive(Debug, Clone)]
pub struct HttpResponse {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: Vec<u8>,
}

impl HttpResponse {
    /// Returns the first header matching `name` (case-insensitive).
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.as_str())
    }

    pub fn text(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }

    #[cfg(feature = "_serde_json")]
    pub fn json(&self) -> Result<serde_json::Value, IdeviceError> {
        Ok(serde_json::from_slice(&self.body)?)
    }
}

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

fn parse_url(url: &str) -> Result<ParsedUrl, IdeviceError> {
    let bad = |m: &str| IdeviceError::Http(format!("invalid URL `{url}`: {m}"));

    let (https, rest) = if let Some(r) = url.strip_prefix("http://") {
        (false, r)
    } else if let Some(r) = url.strip_prefix("https://") {
        (true, r)
    } else {
        return Err(bad("scheme must be http or https"));
    };

    // Split authority from the path. A `?` or `#` before any `/` still ends the
    // authority (e.g. `http://h?q`).
    let auth_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
    let authority = &rest[..auth_end];
    let path_part = &rest[auth_end..];
    if authority.is_empty() {
        return Err(bad("missing host"));
    }
    // Drop any userinfo (`user:pass@host`); unused by our endpoints.
    let hostport = authority.rsplit('@').next().unwrap_or(authority);

    // Separate host and optional port, honoring IPv6 literals in brackets.
    let (host, port_str) = if let Some(after) = hostport.strip_prefix('[') {
        let close = after
            .find(']')
            .ok_or_else(|| bad("unterminated IPv6 literal"))?;
        let host = &after[..close];
        let tail = &after[close + 1..];
        let port = tail.strip_prefix(':');
        (host.to_string(), port)
    } else if let Some((h, p)) = hostport.rsplit_once(':') {
        (h.to_string(), Some(p))
    } else {
        (hostport.to_string(), None)
    };
    if host.is_empty() {
        return Err(bad("missing host"));
    }

    let port = match port_str {
        Some(p) => p.parse::<u16>().map_err(|_| bad("invalid port"))?,
        None => {
            if https {
                443
            } else {
                80
            }
        }
    };

    let path = if path_part.is_empty() || path_part.starts_with(['?', '#']) {
        format!("/{path_part}")
    } else {
        path_part.to_string()
    };

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

/// If the redirect is relative, resolve it
fn resolve_redirect(base: &str, location: &str) -> Result<String, IdeviceError> {
    if location.starts_with("http://") || location.starts_with("https://") {
        return Ok(location.to_string());
    }
    let parsed = parse_url(base)?;
    let scheme = if parsed.https { "https" } else { "http" };
    if let Some(abs_path) = location.strip_prefix('/') {
        // Absolute-path reference: keep scheme + authority, replace the path.
        Ok(format!("{scheme}://{}/{abs_path}", parsed.authority))
    } else {
        // Relative reference: resolve against the base's directory.
        let dir = match parsed.path.rfind('/') {
            Some(i) => &parsed.path[..=i],
            None => "/",
        };
        Ok(format!("{scheme}://{}{dir}{location}", parsed.authority))
    }
}

async fn send_once(
    method: Method,
    url: &str,
    headers: &[(String, String)],
    body: &[u8],
) -> Result<HttpResponse, IdeviceError> {
    let parsed = parse_url(url)?;
    debug!("{} {} ({} body bytes)", method.as_str(), url, body.len());

    let tcp = tokio::net::TcpStream::connect((parsed.host.as_str(), parsed.port)).await?;
    let mut stream: Box<dyn ReadWrite> = if parsed.https {
        tls_connect(tcp, &parsed.host).await?
    } else {
        Box::new(tcp)
    };

    // Build the request head. We always set Host and Connection: close, and set
    // Content-Length whenever there is a body (or the method conventionally
    // carries one). Caller-supplied headers follow and may add Content-Type etc.
    let mut req = Vec::new();
    req.extend_from_slice(method.as_str().as_bytes());
    req.extend_from_slice(b" ");
    req.extend_from_slice(parsed.path.as_bytes());
    req.extend_from_slice(b" HTTP/1.1\r\n");
    push_header(&mut req, "Host", &parsed.authority);
    push_header(&mut req, "Connection", "close");
    let caller_sets_len = headers
        .iter()
        .any(|(k, _)| k.eq_ignore_ascii_case("content-length"));
    if !caller_sets_len && (!body.is_empty() || method == Method::Post) {
        push_header(&mut req, "Content-Length", &body.len().to_string());
    }
    for (k, v) in headers {
        push_header(&mut req, k, v);
    }
    req.extend_from_slice(b"\r\n");
    req.extend_from_slice(body);

    stream.write_all(&req).await?;
    stream.flush().await?;

    read_response(&mut stream).await
}

fn push_header(buf: &mut Vec<u8>, name: &str, value: &str) {
    buf.extend_from_slice(name.as_bytes());
    buf.extend_from_slice(b": ");
    buf.extend_from_slice(value.as_bytes());
    buf.extend_from_slice(b"\r\n");
}

async fn read_response(stream: &mut Box<dyn ReadWrite>) -> Result<HttpResponse, IdeviceError> {
    let mut buf = Vec::new();

    // Read until the end of the header block.
    let head_end = loop {
        if let Some(pos) = find_subsequence(&buf, b"\r\n\r\n") {
            break pos;
        }
        if !read_more(stream, &mut buf).await? {
            return Err(IdeviceError::Http(
                "connection closed before HTTP headers were complete".into(),
            ));
        }
    };

    let (status, headers) = parse_head(&buf[..head_end])?;
    let body_start = head_end + 4;

    let chunked = headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("transfer-encoding"))
        .is_some_and(|(_, v)| v.to_ascii_lowercase().contains("chunked"));
    let content_length = headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("content-length"))
        .and_then(|(_, v)| v.trim().parse::<usize>().ok());

    let body = if chunked {
        // Connection: close means the peer closes after the final chunk, so read
        // to end-of-stream and decode the complete chunked body.
        while read_more(stream, &mut buf).await? {}
        decode_chunked(&buf[body_start..])?
    } else if let Some(len) = content_length {
        while buf.len() < body_start + len {
            if !read_more(stream, &mut buf).await? {
                break; // truncated; return what we have
            }
        }
        let end = (body_start + len).min(buf.len());
        buf[body_start..end].to_vec()
    } else {
        // No framing info: read until the peer closes.
        while read_more(stream, &mut buf).await? {}
        buf[body_start..].to_vec()
    };

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

/// Reads one chunk of bytes into `buf`. Returns `false` on clean EOF.
async fn read_more(
    stream: &mut Box<dyn ReadWrite>,
    buf: &mut Vec<u8>,
) -> Result<bool, IdeviceError> {
    let mut tmp = [0u8; 16384];
    let n = stream.read(&mut tmp).await?;
    if n == 0 {
        return Ok(false);
    }
    buf.extend_from_slice(&tmp[..n]);
    Ok(true)
}

fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).position(|w| w == needle)
}

/// Parses the status line and headers from the header block (excluding the
/// trailing `\r\n\r\n`).
fn parse_head(head: &[u8]) -> Result<(u16, Vec<(String, String)>), IdeviceError> {
    let text = std::str::from_utf8(head)
        .map_err(|_| IdeviceError::Http("non-UTF-8 HTTP header block".into()))?;
    let mut lines = text.split("\r\n");

    let status_line = lines
        .next()
        .ok_or_else(|| IdeviceError::Http("empty HTTP response".into()))?;
    // "HTTP/1.1 200 OK" -> 200
    let status = status_line
        .split_whitespace()
        .nth(1)
        .and_then(|s| s.parse::<u16>().ok())
        .ok_or_else(|| IdeviceError::Http(format!("malformed HTTP status line: {status_line}")))?;

    let mut headers = Vec::new();
    for line in lines {
        if line.is_empty() {
            continue;
        }
        if let Some((k, v)) = line.split_once(':') {
            headers.push((k.trim().to_string(), v.trim().to_string()));
        }
    }
    Ok((status, headers))
}

/// Decodes a complete chunked transfer-encoding body.
fn decode_chunked(mut data: &[u8]) -> Result<Vec<u8>, IdeviceError> {
    let err = || IdeviceError::Http("malformed chunked HTTP body".into());
    let mut out = Vec::new();
    loop {
        // <hex-size>[;ext]\r\n
        let line_end = find_subsequence(data, b"\r\n").ok_or_else(err)?;
        let size_line = std::str::from_utf8(&data[..line_end]).map_err(|_| err())?;
        let size_hex = size_line.split(';').next().unwrap_or("").trim();
        let size = usize::from_str_radix(size_hex, 16).map_err(|_| err())?;
        data = &data[line_end + 2..];
        if size == 0 {
            break; // last chunk; ignore any trailers
        }
        if data.len() < size + 2 {
            return Err(err());
        }
        out.extend_from_slice(&data[..size]);
        // Each chunk's data is followed by a CRLF.
        if &data[size..size + 2] != b"\r\n" {
            return Err(err());
        }
        data = &data[size + 2..];
    }
    Ok(out)
}

#[cfg(feature = "rustls")]
async fn tls_connect(
    tcp: tokio::net::TcpStream,
    host: &str,
) -> Result<Box<dyn ReadWrite>, IdeviceError> {
    use std::sync::Arc;

    crate::ensure_default_crypto_provider();

    let roots = rustls::RootCertStore {
        roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
    };
    let config = rustls::ClientConfig::builder()
        .with_root_certificates(roots)
        .with_no_client_auth();

    let connector = tokio_rustls::TlsConnector::from(Arc::new(config));
    let server_name = rustls::pki_types::ServerName::try_from(host.to_string())
        .map_err(|_| IdeviceError::Http(format!("invalid TLS server name `{host}`")))?;
    let tls = connector.connect(server_name, tcp).await?;
    Ok(Box::new(tls))
}

#[cfg(all(feature = "openssl", not(feature = "rustls")))]
async fn tls_connect(
    tcp: tokio::net::TcpStream,
    host: &str,
) -> Result<Box<dyn ReadWrite>, IdeviceError> {
    let connector = openssl::ssl::SslConnector::builder(openssl::ssl::SslMethod::tls())?.build();
    let ssl = connector.configure()?.into_ssl(host)?;
    let mut stream = tokio_openssl::SslStream::new(ssl, tcp)?;
    std::pin::Pin::new(&mut stream).connect().await?;
    Ok(Box::new(stream))
}

#[cfg(not(any(feature = "rustls", feature = "openssl")))]
async fn tls_connect(
    _tcp: tokio::net::TcpStream,
    _host: &str,
) -> Result<Box<dyn ReadWrite>, IdeviceError> {
    Err(IdeviceError::Http(
        "an https URL was requested but no TLS backend is enabled (enable `rustls` or `openssl`)"
            .into(),
    ))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_plain_url() {
        let u = parse_url("http://127.0.0.1:5555/").unwrap();
        assert!(!u.https);
        assert_eq!(u.host, "127.0.0.1");
        assert_eq!(u.authority, "127.0.0.1:5555");
        assert_eq!(u.port, 5555);
        assert_eq!(u.path, "/");
    }

    #[test]
    fn default_ports_and_path() {
        let u = parse_url("http://gs.apple.com/TSS/controller?action=2").unwrap();
        assert_eq!(u.port, 80);
        assert_eq!(u.path, "/TSS/controller?action=2");

        let s = parse_url("https://example.com").unwrap();
        assert!(s.https);
        assert_eq!(s.port, 443);
        assert_eq!(s.path, "/");
    }

    #[test]
    fn ipv6_literal() {
        let u = parse_url("http://[::1]:8080/x").unwrap();
        assert_eq!(u.host, "::1");
        assert_eq!(u.authority, "[::1]:8080");
        assert_eq!(u.port, 8080);
        assert_eq!(u.path, "/x");
    }

    #[test]
    fn rejects_unknown_scheme() {
        assert!(parse_url("ftp://x/").is_err());
        assert!(parse_url("gs.apple.com").is_err());
    }

    #[test]
    fn redirect_resolution() {
        // Absolute
        assert_eq!(
            resolve_redirect("https://a.com/x", "https://b.com/y").unwrap(),
            "https://b.com/y"
        );
        // Absolute-path
        assert_eq!(
            resolve_redirect("https://a.com/x/y?q", "/z").unwrap(),
            "https://a.com/z"
        );
        // Relative
        assert_eq!(
            resolve_redirect("https://a.com/dir/page", "next").unwrap(),
            "https://a.com/dir/next"
        );
    }

    #[test]
    fn parses_head() {
        let head = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nContent-Type: text/plain";
        let (status, headers) = parse_head(head).unwrap();
        assert_eq!(status, 200);
        assert_eq!(headers.len(), 2);
        assert_eq!(headers[0], ("Content-Length".into(), "5".into()));
    }

    #[test]
    fn decodes_chunked() {
        let body = b"4\r\nWiki\r\n5\r\npedia\r\n0\r\n\r\n";
        assert_eq!(decode_chunked(body).unwrap(), b"Wikipedia");
    }

    #[test]
    fn decodes_chunked_with_extension() {
        let body = b"3;name=value\r\nabc\r\n0\r\n\r\n";
        assert_eq!(decode_chunked(body).unwrap(), b"abc");
    }
}