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
//! Web ARChive format parser
//!
//! Takes data and separates records in headers and content.
#[macro_use]
extern crate nom;
use std::str;
use std::collections::HashMap;
use std::fmt;
use nom::{Offset, space, Needed, IResult, Err};

/// The WArc `Record` struct
#[derive(Clone)]
pub struct Record {
    // lazy design should not use pub
    /// WArc headers
    pub headers: HashMap<String, String>,
    /// Content for call in a raw format
    pub content: Vec<u8>,
}

impl<'a> fmt::Debug for Record {
    fn fmt(&self, form: &mut fmt::Formatter) -> fmt::Result {
        write!(form, "\nHeaders:\n").unwrap();
        for (name, value) in &self.headers {
            write!(form, "{}", name).unwrap();
            write!(form, ": ").unwrap();
            write!(form, "{}", value).unwrap();
            write!(form, "\n").unwrap();
        }
        write!(form, "Content Length:{}\n", self.content.len()).unwrap();
        let s = match String::from_utf8(self.content.clone()) {
            Ok(s) => s,
            Err(_) => "Could not convert".to_string(),
        };
        write!(form, "Content :{:?}\n", s).unwrap();
        write!(form, "\n")
    }
}

fn version_number(input: &[u8]) -> IResult<&[u8], &[u8]> {
    for (idx, chr) in input.iter().enumerate() {
        match *chr {
            46 | 48...57 => continue,
            _ => return Ok((&input[idx..], &input[..idx])),
        }
    }
    Err(Err::Incomplete(Needed::Size(1)))
}

fn utf8_allowed(input: &[u8]) -> IResult<&[u8], &[u8]> {
    for (idx, chr) in input.iter().enumerate() {
        match *chr {
            0...31 => return Ok((&input[idx..], &input[..idx])),
            _ => continue,
        }
    }
    Err(Err::Incomplete(Needed::Size(1)))
}

fn token(input: &[u8]) -> IResult<&[u8], &[u8]> {
    for (idx, chr) in input.iter().enumerate() {
        match *chr {
            33 | 35...39 | 42 | 43 | 45 | 48...57 | 65...90 | 94...122 | 124 => continue,
            _ => return Ok((&input[idx..], &input[..idx])),
        }
    }
    Err(Err::Incomplete(Needed::Size(1)))
}

named!(init_line <&[u8], (&str, &str)>,
    do_parse!(
        opt!(tag!("\r"))            >>
        opt!(tag!("\n"))            >>
        tag!("WARC")                >>
        tag!("/")                   >>
        opt!(space)                 >>
        version: map_res!(version_number, str::from_utf8) >>
        opt!(tag!("\r"))            >>
        tag!("\n")                  >>
        (("WARCVERSION", version))
    )
);

named!(header_match <&[u8], (&str, &str)>,
    do_parse!(
        name: map_res!(token, str::from_utf8) >>
        opt!(space)                 >>
        tag!(":")                   >>
        opt!(space)                 >>
        value: map_res!(utf8_allowed, str::from_utf8) >>
        opt!(tag!("\r"))            >>
        tag!("\n")                  >>
        ((name, value))
    )
);

named!(header_aggregator<&[u8], Vec<(&str,&str)> >, many1!(header_match));

named!(warc_header<&[u8], ((&str, &str), Vec<(&str,&str)>) >,
    do_parse!(
        version: init_line          >>
        headers: header_aggregator  >>
        opt!(tag!("\r"))            >>
        tag!("\n")                  >>
        ((version, headers))
    )
);

/// Parses one record and returns an IResult from nom
///
/// IResult<&[u8], Record>
///
/// See records for processing more then one. The documentation is not displaying.
///
/// # Examples
/// ```ignore
///  extern crate warc_parser;
///  extern crate nom;
///  use nom::{IResult};
///  let parsed = warc_parser::record(&bbc);
///  match parsed{
///      IResult::Error(_) => assert!(false),
///      Err::Incomplete(_) => assert!(false),
///      Ok((i, entry)) => {
///          let empty: Vec<u8> =  Vec::new();
///          assert_eq!(empty, i);
///          assert_eq!(13, entry.headers.len());
///      }
///  }
/// ```
pub fn record(input: &[u8]) -> IResult<&[u8], Record> {
    let mut h: HashMap<String, String> = HashMap::new();
    // TODO if the stream parser does not get all the header it fails .
    // like a default size of 10 doesnt for for a producer
    warc_header(input).and_then(|(mut i, tuple_vec)| {
            let (name, version) = tuple_vec.0;
            h.insert(name.to_string(), version.to_string());
            let headers = tuple_vec.1; // not need figure it out
            for &(k, ref v) in headers.iter() {
                h.insert(k.to_string(), v.clone().to_string());
            }
            let mut content = None;
            let mut bytes_needed = 1;
            match h.get("Content-Length") {
                Some(length) => {
                    let length_number = length.parse::<usize>().unwrap();
                    if length_number <= i.len() {
                        content = Some(&i[0..length_number as usize]);
                        i = &i[length_number as usize..];
                        bytes_needed = 0;
                    } else {
                        bytes_needed = length_number - i.len();
                    }
                }
                _ => {
                    // TODO: Custom error type, this field is mandatory
                }
            }
            match content {
                Some(content) => {
                    let entry = Record {
                        headers: h,
                        content: content.to_vec(),
                    };
                    Ok((i, entry))
                }
                None => Err(Err::Incomplete(Needed::Size(bytes_needed))),
            }
    })
}

named!(record_complete <&[u8], Record >,
    complete!(
        do_parse!(
            entry: record              >>
            opt!(tag!("\r"))           >>
            tag!("\n")                 >>
            opt!(tag!("\r"))           >>
            tag!("\n")                 >>
            (entry)
        )
    )
);

/// Parses many record and returns an IResult with a Vec of Record
///
/// IResult<&[u8], Vec<Record>>
///
/// # Examples
/// ```ignore
///  extern crate warc_parser;
///  extern crate nom;
///  use nom::{IResult};
///  let parsed = warc_parser::records(&bbc);
///  match parsed{
///      IResult::Error(_) => assert!(false),
///      IResult::Incomplete(_) => assert!(false),
///      IResult::Done(i, records) => {
///          assert_eq!(8, records.len());
///      }
///  }
/// ```
named!(pub records<&[u8], Vec<Record> >, many1!(record_complete));