about summary refs log tree commit diff
path: root/pkgs/profpatsch/xdg-open/mini-url.rs
blob: b59d3a7501e26aa70419d6d401338191c26c12c2 (plain) (blame)
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
extern crate errno;
extern crate libc;

use std::ffi::{OsString};
use std::os::unix::ffi::OsStringExt;
use std::ffi::{CStr, CString};

// TODO: move the execline stuff into a different library

#[repr(C)]
pub struct Stralloc_C {
    s: *mut libc::c_char,
    len: libc::size_t,
    a: libc::size_t
}

fn stralloc_zero() -> Stralloc_C {
    Stralloc_C {
        s: std::ptr::null_mut(),
        len: 0,
        a: 0
    }
}

#[link(name = "skarnet")]
extern "C" {
    fn stralloc_free(sa: *mut Stralloc_C);
    fn stralloc_ready_tuned(
        sa: *mut Stralloc_C,
        n: libc::size_t,
        base: libc::size_t,
        a: libc::size_t,
        b: libc::size_t,
    ) -> libc::c_int;

    fn stralloc_catb(
        sa: *mut Stralloc_C,
        s: *const libc::c_char,
        len: libc::size_t
    ) -> libc::c_int;

    fn stralloc_copyb(
        sa: *mut Stralloc_C,
        s: *const libc::c_char,
        len: libc::size_t
    ) -> libc::c_int;

    fn xpathexec0(
        argv: *const *const libc::c_char
    );
    fn xpathexec_run(
        file: *const libc::c_char,
        argv: *const *const libc::c_char,
        envp: *const *const libc::c_char
    );
}

fn stralloc_ready(sa: *mut Stralloc_C, n: libc::size_t) {
    unsafe {
        if (stralloc_ready_tuned(sa, n, 8, 1, 8) == 0) {
            panic!("{}", errno::errno());
        }
    }
}

struct Stralloc(Stralloc_C);

impl Stralloc {
    fn new() -> Self {
        let mut sa = stralloc_zero();
        unsafe {
            stralloc_ready(&mut sa, 0);
        }
        Stralloc(sa)
    }
}

impl AsMut<Stralloc_C> for Stralloc {
    fn as_mut(&mut self) -> &mut Stralloc_C {
        match self {
            Stralloc(s) => s
        }
    }
}

impl AsRef<Stralloc_C> for Stralloc {
    fn as_ref(&self) -> &Stralloc_C {
        match self {
            Stralloc(s) => s
        }
    }
}

impl<'a> From<&mut [u8]> for Stralloc {
    fn from(s: &mut [u8]) -> Self {
        let mut sa = stralloc_zero();
        let ptr = s.as_mut_ptr() as *mut libc::c_char;
        unsafe {
            if stralloc_copyb(&mut sa, ptr, s.len()) == 0 {
                panic!("{}", errno::errno());
            }
        }
        Stralloc(sa)
    }
}

// TODO not sure if stralloc will always be a contiguous block?
// that’s the precondition for from_raw_parts
impl AsRef<[u8]> for Stralloc_C {
    fn as_ref(&self) -> &[u8] {
        let ptr = self.s as *const u8;
        unsafe {
            std::slice::from_raw_parts(ptr, self.len)
        }
    }
}

impl Drop for Stralloc {
    fn drop(&mut self) {
        match self {
            Stralloc(inner) => {
                unsafe {
                    stralloc_free(inner);
                }
            }
        }
    }
}


#[repr(C)]
#[derive(Debug)]
pub struct Elsubst_C
{
    var: libc::size_t,
    value: libc::size_t,
    // values are \0-separated strings,
    // and n is the amount of elements in one such string
    n: libc::c_uint
}

#[link(name = "execline")]
extern "C" {
    fn el_substitute(
        dst: *mut Stralloc_C,
        src: *const libc::c_char,
        len: libc::size_t,
        // length is given by nsubst
        vars: *const libc::c_char,
        // length is given by nsubst
        values: *const libc::c_char,

        substs: *const Elsubst_C,
        nsubst: libc::size_t
    ) -> libc::c_int;
}

struct Subst<'a> {
    var: &'a CStr,
    value: &'a CStr,
}

fn simple_substitute<'a, 'b>(subst: &[Subst<'a>], src: &CStr) -> CString {
    let len = src.to_bytes_with_nul().len() as libc::size_t;
    let src = src.as_ptr() as *const libc::c_char;
    let mut vars : Vec<u8> = Vec::new();
    let mut values : Vec<u8> = Vec::new();
    let mut substs : Vec<Elsubst_C> = Vec::new();
    let mut var_i = 0;
    let mut value_i = 0;
    for s in subst {
        let var = s.var.to_bytes_with_nul();
        let value = s.value.to_bytes_with_nul();
        vars.extend_from_slice(var);
        values.extend_from_slice(value);
        substs.push(Elsubst_C {
            // these index into the vars/values arrays given to el_substitute
            var: var_i,
            value: value_i,
            // we don’t deal with split values here
            n: 1
        });
        var_i += var.len();
        value_i += value.len();
    }
    let nsubst = subst.len();
    let mut dst = stralloc_zero();
    unsafe {
        if el_substitute(
            &mut dst,
            src,
            len,
            vars.as_ptr() as *const libc::c_char,
            values.as_ptr() as *const libc::c_char,
            substs.as_ptr() as *const Elsubst_C,
            nsubst
        ) == -1 {
            panic!("{}", errno::errno());
        }
        // el_substitute returns a \0-delim C string
        CStr::from_bytes_with_nul_unchecked(dst.as_ref()).to_owned()
    }
}

#[derive(Clone, Debug, PartialEq, Eq,)]
struct Parsed<'a> {
    protocol: Protocol,
    domain: &'a str,
    port: &'a str,
    path: &'a str,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Protocol {
    Http,
    Https,
}

impl Protocol {
    fn string(&self) -> &str {
        match self {
            Protocol::Http => "http",
            Protocol::Https => "https"
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Error {
    NotHttp,
    ColonButNoPort
}

fn mini_parse_http(url: &str) -> Result<Parsed, Error> {
    let (protocol, off) = {
        if url.starts_with("https://") {
            (Protocol::Https, 8)
        } else if url.starts_with("http://") {
            (Protocol::Http, 7)
        } else {
            return Err(Error::NotHttp)
        }
    };

    let (_, path) = url.split_at(off);

    let (domain_port, path) = match path.find('/') {
        Some(boundary) => path.split_at(boundary),
        None => (path, "/")
    };

    let (domain, port) = match domain_port.find(':') {
        Some(boundary) => match domain_port.split_at(boundary) {
            (d, ":") => return Err(Error::ColonButNoPort),
            (d, p) => unsafe { (d, p.get_unchecked(1..)) }
        },
        None => match protocol {
            Protocol::Http => (domain_port, "80"),
            Protocol::Https => (domain_port, "443"),
        }
    };

    Ok(Parsed {
        protocol,
        domain,
        port,
        path
    })
}


fn write_netstring<W: std::io::Write>(mut w: W, s: &str) -> std::io::Result<()> {
    write!(w, "{}:{},", s.len(), s)
}

fn to_cstrings(s: Vec<OsString>) -> Vec<CString> {
    s.into_iter()
        .map(|s: OsString| CString::new(s.into_vec()).unwrap())
        .collect::<Vec<CString>>()
}

fn to_c_argv(s: Vec<CString>) -> (Vec<CString>, Vec<*const libc::c_char>) {
    let mut c_ptr_args = s.iter()
        .map(|s| s.as_ptr())
        .collect::<Vec<*const libc::c_char>>();
    c_ptr_args.push(std::ptr::null());
    (s, c_ptr_args)
}


fn main() -> std::io::Result<()> {
    let args = std::env::args().collect::<Vec<_>>();
    let prog_index = 2;
    if args.len() < prog_index {
        eprintln!("1st arg is http/https url");
        std::process::exit(100);
    }

    let Parsed { domain, port, protocol, path } = mini_parse_http(&args[1]).unwrap_or_else(
        |err| {
            eprintln!("could not parse url: {:?}", err);
            std::process::exit(100);
        }
    );

    // println!("{:#?}", mini_parse_http(&args[1]).unwrap());

    let subst = |s: &CString| simple_substitute(&vec![
        Subst{
            var: &CString::new(b"host".to_vec()).unwrap(),
            value: &CString::new(domain).unwrap()
        },
        Subst{
            var: &CString::new(b"port".to_vec()).unwrap(),
            value: &CString::new(port).unwrap(),
        },
        Subst{
            var: &CString::new(b"protocol".to_vec()).unwrap(),
            value: &CString::new(protocol.string()).unwrap()
        },
        Subst{
            var: &CString::new(b"path".to_vec()).unwrap(),
            value: &CString::new(path).unwrap()
        },

    ], s);


    // TODO: replace this bit by xpathexec0 from el_exec.rs

    let cstring_argv = to_cstrings(std::env::args_os().collect::<Vec<_>>());
    let (c_strings, c_argv) = to_c_argv(cstring_argv.iter().map(subst).collect::<Vec<_>>());

    unsafe {
        xpathexec0(
            c_argv[prog_index..].as_ptr() as *const *const libc::c_char
        )
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_mini_parse() {
        assert_eq!(mini_parse_http("https://abc.bar:8080/blabla"),
                   Ok(Parsed {
                       protocol: Protocol::Https,
                       domain: "abc.bar",
                       port: "8080",
                       path: "/blabla"
                   }));
        assert_eq!(mini_parse_http("http://abc.bar:8080/blabla"),
                   Ok(Parsed {
                       protocol: Protocol::Http,
                       domain: "abc.bar",
                       port: "8080",
                       path: "/blabla"
                   }));
        assert_eq!(mini_parse_http("http://abc.bar/blabla"),
                   Ok(Parsed {
                       protocol: Protocol::Http,
                       domain: "abc.bar",
                       port: "80",
                       path: "/blabla"
                   }));
        assert_eq!(mini_parse_http("https://f"),
                   Ok(Parsed {
                       protocol: Protocol::Https,
                       domain: "f",
                       port: "443",
                       path: "/"
                   }));
        assert_eq!(mini_parse_http("foo.bar"),
                   Err(Error::NotHttp));
        assert_eq!(mini_parse_http("mirror://abc.bar:8080/blabla"),
                   Err(Error::NotHttp));
        assert_eq!(mini_parse_http("http://abc.bar:"),
                   Err(Error::ColonButNoPort));
    }


}