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
use std::fs;
use std::io::BufReader;

use regex::Regex;

use std::rc::Rc;

use std::error;
use std::fmt;

mod constants {
    pub const MAGIC_SAZ: &[u8; 4] = b"\x50\x4B\x03\x04";
    pub const MAGIC_SAZ_EMPTY: &[u8; 4] = b"\x50\x4B\x05\x06";
    pub const MAGIC_SAZ_SPANNED: &[u8; 4] = b"\x50\x4B\x07\x08";
}

type Result<T> = std::result::Result<T, SazError>;

#[derive(Debug)]
/// Library Error
pub enum SazError {
    /// SAZ/ZIP file is empty
    Empty,
    /// SAZ/ZIP file is spanned
    Spanned,
    /// SAZ/ZIP file is invalid
    Invalid,
    /// Failure in reading file
    Error,
}

impl fmt::Display for SazError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SazError::Empty => write!(f, "SAZ file is empty"),
            SazError::Spanned => write!(f, "SAZ file is spanned"),
            SazError::Invalid => write!(f, "SAZ file is invalid"),
            SazError::Error => write!(f, "Error reading file"),
        }
    }
}

impl error::Error for SazError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            SazError::Empty => None,
            SazError::Spanned => None,
            SazError::Invalid => None,
            SazError::Error => None,
        }
    }
}

#[derive(Clone, Debug)]
struct SazFile {
    path: String,
    size: u64,
    contents: Rc<String>,
}

impl SazFile {
    fn new(path: &String, size: u64, contents: Rc<String>) -> SazFile {
        SazFile {
            path: path.to_string(),
            size: size,
            contents: contents.clone(),
        }
    }
}

#[derive(Clone, Debug)]
/// This struct represent a single SAZ session
pub struct SazSession {
    /// Identifier
    pub index: u32,
    /// Http Status Code
    pub result: u32,
    /// Request URL
    pub url: String,
    /// HTTP Body length in bytes
    pub body: u32,
    /// File with Request information
    pub file_request: String,
    /// File with Response information
    pub file_response: String,
    /// Contents of Request file
    pub file_request_contents: Rc<String>,
    /// Contents of Response file
    pub file_response_contents: Rc<String>,
}

impl SazSession {
    fn new(
        idx: u32,
        httpres: u32,
        httpurl: &str,
        httpbody: u32,
        frequest: &String,
        fresponse: &String,
        frequest_contents: &Rc<String>,
        fresponse_contents: &Rc<String>,
    ) -> SazSession {
        SazSession {
            index: idx,
            result: httpres,
            url: httpurl.to_string(),
            body: httpbody,
            file_request: frequest.clone(),
            file_response: fresponse.clone(),
            file_request_contents: frequest_contents.clone(),
            file_response_contents: fresponse_contents.clone(),
        }
    }
}

fn check_file_validity(filename: &str) -> Result<()> {
    let data = fs::read(filename);

    if let Ok(d) = data {
        let slice = &d[..4];
        if slice == constants::MAGIC_SAZ {
            return Ok(());
        } else if slice == constants::MAGIC_SAZ_EMPTY {
            return Err(SazError::Empty);
        } else if slice == constants::MAGIC_SAZ_SPANNED {
            return Err(SazError::Spanned);
        } else {
            return Err(SazError::Invalid);
        }
    } else {
        return Err(SazError::Error);
    }
}

fn zip_contents(filename: &str) -> Result<Vec<SazFile>> {
    check_file_validity(filename)?;

    let mut raw_folder: bool = false;
    let mut list: Vec<SazFile> = vec![];

    let file = fs::File::open(filename).unwrap();
    let reader = BufReader::new(file);

    let mut archive = zip::ZipArchive::new(reader).unwrap();

    for i in 0..archive.len() {
        let mut zipped_file = archive.by_index(i).unwrap();

        let outpath = match zipped_file.enclosed_name() {
            Some(path) => path,
            None => continue,
        };

        if !zipped_file.name().ends_with('/') {
            let file_path = outpath.to_str().unwrap().to_string();
            let file_size = zipped_file.size();

            let mut writer: Vec<u8> = vec![];
            let _ = std::io::copy(&mut zipped_file, &mut writer);
            let file_contents = unsafe { std::str::from_utf8_unchecked(&writer).to_string() };

            let zippedfile = SazFile::new(&file_path, file_size, Rc::new(file_contents));
            list.push(zippedfile);
        } else {
            if zipped_file.name() == "raw/" {
                raw_folder = true;
            }
        }
    }

    if !raw_folder {
        return Err(SazError::Invalid);
    }

    Ok(list)
}

fn get_file_from_list<'a>(list: &'a [SazFile], filename: &str) -> &'a SazFile {
    let result = list.iter().find(|&f| f.path == filename).unwrap();
    result
}

fn get_sessions_total(list: &[SazFile]) -> (u32, usize) {
    let mut leading_zeroes: usize = 0;
    let mut sessions_total: u32 = 0;

    for zipped_file in list {
        let mut splitted = zipped_file.path.split('_');
        let splitted2 = splitted.nth(0);
        match splitted2 {
            Some(inner) => {
                let number = inner.split('/').nth(1);
                match number {
                    Some(inner2) => {
                        leading_zeroes = inner2.len();
                        let parsed = inner2.parse::<u32>().unwrap();
                        if sessions_total < parsed {
                            sessions_total = parsed;
                        }
                    }
                    None => continue,
                }
            }
            None => continue,
        }
    }

    (sessions_total, leading_zeroes)
}

fn regex_get_url(contents: &str) -> &str {
    let re = Regex::new(r"(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE) (.*) HTTP/1.1").unwrap();
    let capture = re.captures(contents).unwrap();
    let value = capture.get(2).unwrap().as_str();

    value
}

fn regex_get_http_status(contents: &str) -> u32 {
    let re = Regex::new(r"HTTP.*([0-9][0-9][0-9])").unwrap();

    let capture = re.captures(contents);
    match capture {
        Some(captured) => {
            return captured.get(1).unwrap().as_str().parse::<u32>().unwrap();
        }
        None => return 0,
    }
}

fn regex_get_content_length(contents: &str) -> u32 {
    let re = Regex::new(r"Content-Length:\s(\d+)").unwrap();
    let capture = re.captures(contents);

    match capture {
        Some(captured) => {
            return captured.get(1).unwrap().as_str().parse::<u32>().unwrap();
        }
        None => return 0,
    }
}

///
/// Parses given file.
/// Returns Result<Vec<SazSession>>
///
/// # Arguments
///
/// * `fname` - File name that represents the SAZ file
///
/// # Errors
///
/// Errors out if not possible to read file.
///
/// # Example
///
/// ``` rust
/// use std::env;
///
/// use sazparser;
///
/// fn main() {
///     let args: Vec<String> = env::args().collect();
///
///     // args[1] will be the file to parse
///     let saz = sazparser::parse(&*args[1]);
///
///     match saz {
///         Ok(v) => {
///             // use parsed information
///             println!("{:?}", v);
///         },
///         Err(e) => {
///             panic!("{}", e);
///         },
///     }
/// }
///```
///
pub fn parse(fname: &str) -> Result<Vec<SazSession>> {
    let mut entries: Vec<SazSession> = vec![];

    let list = zip_contents(&fname)?;
    let (total_sessions, leading_zeroes) = get_sessions_total(&list);

    for n in 1..=total_sessions {
        let request_file_name = format!("raw/{:0fill$}_c.txt", n, fill = leading_zeroes);
        let response_file_name = format!("raw/{:0fill$}_s.txt", n, fill = leading_zeroes);

        let request_file = get_file_from_list(&list, &*request_file_name);
        let response_file = get_file_from_list(&list, &*response_file_name);

        let url = regex_get_url(&*request_file.contents);
        let httpstatus = regex_get_http_status(&*response_file.contents);
        let contentlength = regex_get_content_length(&*response_file.contents);

        let entry = SazSession::new(
            n,
            httpstatus,
            url,
            contentlength,
            &request_file_name,
            &response_file_name,
            &request_file.contents,
            &response_file.contents,
        );

        entries.push(entry);
    }

    Ok(entries)
}