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
use crate::{panic, Any, Arc, BTreeMap, Data, Rc, Transaction, Value};

use serde::{Deserialize, Serialize};

/// General Query.
#[derive(Serialize, Deserialize, Debug)]
#[non_exhaustive]
pub struct GenQuery {
    /// The SQL query string.
    pub sql: Arc<String>,
    /// The path argument for the query.
    pub path: String,
    /// Query parameters.
    pub params: BTreeMap<String, String>,
    /// Query form.
    pub form: BTreeMap<String, String>,
    /// Query cookies.
    pub cookies: BTreeMap<String, String>,
    /// Querey parts ( files ).
    pub parts: Vec<Part>,
    /// Micro-seconds since January 1, 1970 0:00:00 UTC
    pub now: i64,
}

/// General Response.
#[non_exhaustive]
pub struct GenResponse {
    /// Error string.
    pub err: String,
    /// Response status code.
    pub status_code: u16,
    /// Response headers.
    pub headers: Vec<(String, String)>,
    /// Reponse body.
    pub output: Vec<u8>,
}

/// Query + Response, implements Transaction.
#[non_exhaustive]
pub struct GenTransaction {
    /// Transaction Query.
    pub qy: GenQuery,
    /// Transaction Response.
    pub rp: GenResponse,
    /// Transacation extension data.
    pub ext: Box<dyn Any + Send + Sync>,
}

/// Part of multipart data ( uploaded files ).
#[derive(Serialize, Deserialize, Debug, Default)]
#[non_exhaustive]
pub struct Part {
    /// Part name.
    pub name: String,
    /// Part filename.
    pub file_name: String,
    /// Part contenttype.
    pub content_type: String,
    ///
    pub text: String,
    ///
    pub data: Data,
}

impl GenTransaction {
    ///
    pub fn new() -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::SystemTime::UNIX_EPOCH)
            .unwrap();
        let now = now.as_micros() as i64;
        let output = Vec::with_capacity(10000);
        let headers = Vec::new();
        let status_code = 200;
        Self {
            qy: GenQuery {
                sql: Arc::new("EXEC web.Main()".to_string()),
                path: String::new(),
                params: BTreeMap::new(),
                form: BTreeMap::new(),
                cookies: BTreeMap::new(),
                parts: Vec::new(),
                now,
            },
            rp: GenResponse {
                err: String::new(),
                output,
                status_code,
                headers,
            },
            ext: Box::new(()),
        }
    }

    /// Append string to output.
    fn push_str(&mut self, s: &str) {
        self.rp.output.extend_from_slice(s.as_bytes());
    }
}

impl Transaction for GenTransaction {
    fn arg(&mut self, kind: i64, s: &str) -> Rc<String> {
        let s = match kind {
            0 => Some(&self.qy.path),
            1 => self.qy.params.get(s),
            2 => self.qy.form.get(s),
            3 => self.qy.cookies.get(s),
            _ => None,
        };
        let s = if let Some(s) = s { s } else { "" };
        Rc::new(s.to_string())
    }

    fn status_code(&mut self, code: i64) {
        self.rp.status_code = code as u16;
    }

    fn header(&mut self, name: &str, value: &str) {
        self.rp.headers.push((name.to_string(), value.to_string()));
    }

    fn global(&self, kind: i64) -> i64 {
        match kind {
            0 => self.qy.now,
            _ => panic!(),
        }
    }

    fn selected(&mut self, values: &[Value]) {
        for v in values {
            match v {
                Value::RcBinary(x) => {
                    self.rp.output.extend_from_slice(x);
                }
                Value::ArcBinary(x) => {
                    self.rp.output.extend_from_slice(x);
                }
                _ => {
                    self.push_str(&v.str());
                }
            }
        }
    }

    fn set_error(&mut self, err: String) {
        self.rp.err = err;
    }

    fn get_error(&mut self) -> String {
        let result = self.rp.err.to_string();
        self.rp.err = String::new();
        result
    }

    fn file_attr(&mut self, k: i64, x: i64) -> Rc<String> {
        let k = k as usize;
        let result: &str = {
            if k >= self.qy.parts.len() {
                ""
            } else {
                let p = &self.qy.parts[k];
                match x {
                    0 => &p.name,
                    1 => &p.content_type,
                    2 => &p.file_name,
                    3 => &p.text,
                    _ => panic!(),
                }
            }
        };
        Rc::new(result.to_string())
    }

    fn file_content(&mut self, k: i64) -> Data {
        self.qy.parts[k as usize].data.clone()
    }

    fn set_extension(&mut self, ext: Box<dyn Any + Send + Sync>) {
        self.ext = ext;
    }

    fn get_extension(&mut self) -> Box<dyn Any + Send + Sync> {
        std::mem::replace(&mut self.ext, Box::new(()))
    }
}

impl Default for GenTransaction {
    fn default() -> Self {
        Self::new()
    }
}