aboutsummaryrefslogtreecommitdiff
path: root/htmlua-parser/src/htmlua_stdlib.rs
blob: 3c27f26c733856ef8c0dd7abdbf785dc25f8762e (plain)
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
use std::{cell::{LazyCell, RefCell}, collections::HashMap, fmt::Write, rc::Rc};

use anyhow::anyhow;
use mlua::{prelude::*, Error, Lua, Table};
use reqwest::{blocking::{Client, Response}, header::HeaderMap};
use serde::{Deserialize, Serialize};
use serde_json::to_value;


pub fn create_htmlua_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
    let t = l.create_table()?;

    // This cannot be the best way to do this

    let stdout_println = stdout.clone();
    t.set(
        "println",
        l.create_function(move |_, text: String| {
            let mut stdout_ref = stdout_println.borrow_mut();
            writeln!(stdout_ref, "{text}").map_err(mlua::Error::external)
        })?,
    )?;

    let stdout_print = stdout.clone();
    t.set(
        "print",
        l.create_function(move |_, text: String| {
            let mut stdout_ref = stdout_print.borrow_mut();
            write!(stdout_ref, "{text}").map_err(mlua::Error::external)
        })?,
    )?;

    t.set("http", create_http_lib(l)?)?;
    Ok(t)
}

fn create_http_lib(l: &Lua) -> mlua::Result<Table> {
    let t = l.create_table()?;
    let http_client: Rc<LazyCell<Client>> = Rc::new(LazyCell::new(|| Client::builder()
            .timeout(std::time::Duration::from_secs(3))
            .user_agent("htmlua/0.1.0")
            .build().unwrap()
        ));


    let client = http_client.clone();
    t.set(
        "get",
        l.create_function(move |_, url: String| {
            let res = client.get(url).send().map_err(|e| Error::RuntimeError(e.to_string()))?;
            let lua_res: LuaHttpResponse = TryFrom::try_from(res)?;
            Ok(lua_res)
        })?
    )?;

    let client = http_client.clone();
    t.set(
        "post",
        l.create_function(move |_, url: String| {
            let res = client.post(url).send().map_err(|e| Error::RuntimeError(e.to_string()))?;
            let lua_res: LuaHttpResponse = TryFrom::try_from(res)?;
            Ok(lua_res)
        })?
    )?;

    let client = http_client.clone();
    t.set(
        "get_with_data",
        l.create_function(move |_, (url, data): (String, HashMap<String, String>)| {
            let res = client.get(url).query(&data).send().map_err(|e| Error::RuntimeError(e.to_string()))?;
            let lua_res: LuaHttpResponse = TryFrom::try_from(res)?;
            Ok(lua_res)
        })?
    )?;

    let client = http_client.clone();
    t.set(
        "post_with_data_form",
        l.create_function(move |_, (url, data): (String, HashMap<String, String>)| {
            let res = client.post(url).form(&data).send().map_err(|e| Error::RuntimeError(e.to_string()))?;
            let lua_res: LuaHttpResponse = TryFrom::try_from(res)?;
            Ok(lua_res)
        })?
    )?;

    let client = http_client.clone();
    t.set(
        "post_with_data_json",
        l.create_function(move |_, (url, data): (String, HashMap<String, String>)| {
            let res = client.post(url).json(&data).send().map_err(|e| Error::RuntimeError(e.to_string()))?;
            let lua_res: LuaHttpResponse = TryFrom::try_from(res)?;
            Ok(lua_res)
        })?
    )?;
    //TODO: all http

    t.set(
        "decode_json",
        l.create_function(move |l, text: String| {
            let table: serde_json::Value = serde_json::from_str(&text).map_err(|e| Error::RuntimeError(e.to_string()))?;
            Ok(l.to_value(&table))
        })?
    )?;

    Ok(t)
}


#[derive(Serialize, Deserialize)]
struct LuaHttpResponse {
    headers: HashMap<String, String>,
    body: String,
    status: u16,
}

impl TryFrom<Response> for LuaHttpResponse {
    fn try_from(value: Response) -> Result<Self, Self::Error> {
        Ok(LuaHttpResponse { headers: headermap_to_hashmap(value.headers()), status: value.status().as_u16(), body: value.text().map_err(|e| Error::RuntimeError(e.to_string()))?, })
    }

    type Error = Error;
}

fn headermap_to_hashmap(headers: &HeaderMap) -> HashMap<String, String> {
    let mut map = HashMap::new();

    for (name, value) in headers {
        if let Ok(value_str) = value.to_str() {
            map.insert(name.as_str().to_string(), value_str.to_string());
        }
    }

    map
}

impl IntoLua for LuaHttpResponse {
    fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
        let table = lua.create_table()?;

        table.set("status", self.status)?;
        table.set("body", self.body)?;

        let headers_table = lua.create_table()?;
        for (key, value) in self.headers {
            headers_table.set(key, value)?;
        }
        table.set("headers", headers_table)?;

        Ok(LuaValue::Table(table))
    }
}