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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use crate::{
    structs::{Config, Response, Connection}
};
use native_tls::TlsConnector;
use std::{
    usize,
    time::{Instant, Duration},
    collections::HashMap,
    net::{ToSocketAddrs, TcpStream},
    error::Error,
    io::{self, Write, Read, ErrorKind},
};
use flate2::read::{GzDecoder};

pub fn make_connection(config: &Config) -> Result<Connection, Box<dyn Error>> {
    if config.https {
        connect_to_https(&config.host, config.port)
    } else {
        connect_to_http(&config.host, config.port)
    }
}

pub fn raw_request(config: &Config, mut stream: Connection, request: &str) -> Result<(Option<Connection>, Response), Box<dyn Error>> {

    if config.verbose > 1 {
        writeln!(io::stdout(), "{}", request).ok();
    }

    //send the actual request and reconnect in case of Broken Pipe error
    stream = match stream.write_all(request.as_bytes()) {
        Ok(()) => stream,
        Err(err) =>  match err.kind() {
            ErrorKind::BrokenPipe => {
                stream = make_connection(&config)?;
                stream.write_all(request.as_bytes())?;
                stream
            }
            _ => return Err(Box::new(err))
        }
    };

    let start = Instant::now();

    //read the first line and try to reconnect in case of closed connection
    let (stream, firstline) = match read_firstline(stream) {
        Ok(val) => match val.1.len() {
            1 => {
                stream = make_connection(&config)?;
                stream.write_all(request.as_bytes())?;
                match read_firstline(stream) {
                    Ok(val) => val,
                    Err(err) => {
                        writeln!(io::stderr(), "{}", err).ok();
                        return Ok((None,
                            Response {
                                time: start.elapsed().as_millis(),
                                code: 0,
                                body: String::new(),
                                http_version: String::from("HTTP/0.0"),
                                headers: HashMap::new()
                            }))
                    }
                }
            },
            _ => val

        },
        Err(err) => {
            writeln!(io::stderr(), "{}", err).ok();
            return Ok((None,
                Response {
                    time: start.elapsed().as_millis(),
                    code: 0,
                    body: String::new(),
                    http_version: String::from("HTTP/0.0"),
                    headers: HashMap::new()
                }))
        }
    };

    let firstline = String::from_utf8_lossy(&firstline[..]);

    let (stream, headers) = match read_headers(stream) {
        Ok(val) => val,
        Err(err) => {
            writeln!(io::stderr(), "{}", err).ok();
            return Ok((None,
                Response {
                    time: start.elapsed().as_millis(),
                    code: 0,
                    body: String::new(),
                    http_version: String::from("HTTP/0.0"),
                    headers: HashMap::new()
                }))
        }
    };

    let (body, stream, duration) = match headers.get("content-length") {
        Some(val) => {
            //get the value of content-length header
            let mut val = val.to_string();
            val.retain(|c| !c.is_whitespace());
            let content_length: usize = val.parse::<usize>()?;

            match read_body(stream, start, content_length) {
                Ok(val) => val,
                Err(err) => {
                    writeln!(io::stderr(), "{}", err).ok();
                    return Ok((None,
                        Response {
                            time: start.elapsed().as_millis(),
                            code: 0,
                            body: String::new(),
                            http_version: String::from("HTTP/0.0"),
                            headers: headers
                        }))
                }
            }
        }
        None => match headers.get("transfer-encoding") {
            Some(val) => {
                if val.contains("chunked") {
                   match read_chunked_body(stream, start) {
                       Ok(val) => val,
                       Err(err) => {
                            writeln!(io::stderr(), "{}", err).ok();
                            return Ok((None,
                                Response {
                                    time: start.elapsed().as_millis(),
                                    code: 0,
                                    body: String::new(),
                                    http_version: String::from("HTTP/0.0"),
                                    headers: headers
                            }))
                       }
                   }
                } else {
                    (Vec::new(), stream, start.elapsed())
                }
            },
            None => (Vec::new(), stream, start.elapsed()),
        }
    };

    if config.verbose > 0 {
        writeln!(io::stdout(), "{:?}, {}ms", firstline.trim(), duration.as_millis()).ok();
        if config.verbose > 1 {
            writeln!(io::stdout(), "------------").ok();
        }
    }

    let mut firstline = firstline.split(' ');
    let http_version = firstline.next().unwrap_or("HTTP/0.0").trim().to_string();
    let code = match firstline.next() {
        Some(val) => val.trim(),
        None => "0",
    };

    let body = match headers.get("content-encoding") {
        Some(val) => decode_body(val, body),
        None => String::from_utf8_lossy(&body).to_string()
    };

    Ok((Some(stream), Response {
        time: duration.as_millis(),
        code: match code.parse::<u16>() {
            Ok(val) => val,
            Err(_) => 0,
        },
        http_version,
        headers,
        body
    }))
}

fn read_firstline(
    mut stream: Connection
) -> Result<(Connection, Vec<u8>), Box<dyn Error>> {
    let mut buffer = [0;1];
    let mut firstline: Vec<u8> = Vec::with_capacity(64);
    let mut crlf: usize = 0;

    loop {

        stream.read(&mut buffer)?;

        if (buffer[0] == 13 || buffer[0] == 10) && firstline.len() > 4  {
            crlf += 1
        } else if crlf != 0 {
            crlf = 0
        }

        firstline.push(buffer[0]);

        if buffer[0] == 0 || crlf == 2 || firstline.len() > 512 {
            break
        }

    }
    Ok((stream, firstline))
}

fn read_headers(
    mut stream: Connection,
) -> Result<(Connection, HashMap<String,String>), Box<dyn Error>> {
    let mut buffer = [0;1];
    let mut headers: Vec<u8> = Vec::new();
    let mut crlf: usize = 0;

    //read headers char by char
    loop {
        stream.read(&mut buffer)?;

        if buffer[0] == 13 || buffer[0] == 10 {
            crlf += 1
        } else if crlf != 0 {
            crlf = 0
        }

        headers.push(buffer[0]);

        if buffer[0] == 0 || crlf == 4 || headers.len() > 32768 {
            break
        }
    }

    let headers = std::str::from_utf8(&headers[..])?.split("\r\n");

    let mut headers_map: HashMap<String, String> = HashMap::new();

    for header in headers {
        //TODO fix Header: https(:)//something
        let header: Vec<&str> = header.split(':').collect();
        if header.len() > 1 {
            headers_map.insert(
                header[0].to_string().to_lowercase(),
                header[1].to_string()
            );
        }
    }
    Ok((stream, headers_map))
}

fn read_body(
    mut stream: Connection,
    start: Instant,
    content_length: usize
) -> Result<(Vec<u8>, Connection, Duration), Box<dyn Error>> {
    let duration: Duration;
    Ok((
        if content_length > 3 {
            //we need to read firstly one byte only in order to get the exact answer time
            let mut first_byte = vec![0;1];
            stream.read_exact(&mut first_byte)?;
            duration = start.elapsed();

            //read the rest of the body
            let mut buffer = vec![0;content_length-1];
            stream.read_exact(&mut buffer)?;
            first_byte.append(&mut buffer);

            first_byte
        } else {
            //content-length is small, read the whole body instead
            let mut buffer = vec![0;content_length];
            stream.read_exact(&mut buffer)?;
            duration = start.elapsed();

            buffer
        }, stream, duration))
}

fn read_chunked_body(
    mut stream: Connection,
    start: Instant,
) -> Result<(Vec<u8>, Connection, Duration), Box<dyn Error>> {
    let mut duration: Option<Duration> = None;
    let mut body: Vec<u8> = Vec::new();

    loop {
        let mut chunk: String = String::new();
        //read crlf chars and the first byte at the start of a body
        loop {
            let mut one_byte = vec![0;1];
            stream.read_exact(&mut one_byte)?;
            if one_byte[0] != 10 && one_byte[0] != 13 {
                chunk.push(one_byte[0] as char);
                break
            }
        }

        match duration {
            Some(_) => (),
            None => duration = Some(start.elapsed()),
        }

        loop { //read the rest of the chunk length
            let mut one_byte = vec![0;1];
            stream.read_exact(&mut one_byte)?;
            if one_byte[0] == 10 || one_byte[0] == 13 { //read the first \r
                stream.read_exact(&mut one_byte)?; //read the next \n
                break
            } else {
                chunk.push(one_byte[0] as char)
            }
        }

        if chunk == "0" || chunk.is_empty() {
            break;
        }

        let chunk = usize::from_str_radix(&chunk, 16)?;
        let mut chunk_of_bytes = vec![0;chunk];
        stream.read_exact(&mut chunk_of_bytes)?;
        body.append(&mut chunk_of_bytes);
    }

    Ok((body, stream, duration.unwrap_or(start.elapsed())))
}

fn decode_body(encoding: &str, body: Vec<u8>) -> String {
    match encoding.trim() {
        "gzip" => {
            let mut d = GzDecoder::new(&body[..]);
            let mut decompressed_body = String::new();
            match d.read_to_string(&mut decompressed_body) {
                Ok(_) => decompressed_body,
                Err(err) => {
                    writeln!(io::stderr(), "{} ({:?}...)", err, &body[0..10]).ok();
                    String::from_utf8_lossy(&body).to_string()
                }
            }
        }
        /*"deflate" => {
            let mut d = DeflateDecoder::new(&body[..]);
            let mut decompressed_body = String::new();
            d.read_to_string(&mut decompressed_body).unwrap();
            decompressed_body
        }*/
        _ => String::from_utf8_lossy(&body).to_string()
    }
}

//for now for testing purposes only
fn _connect_via_proxy(proxy: &str, host: &str, port: usize) -> Result<std::net::TcpStream, Box<dyn Error>> {
    //make CONNECT request to the proxy
    let connect = format!(
        "CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\nProxy-Connection: Keep-Alive\r\n\r\n",
        host,
        port,
        host,
        port
    );

    //connect to the proxy
    let mut stream = TcpStream::connect(
        str::replace(
            proxy,
            "http://",
            "",
        )
    )?;

    stream.set_write_timeout(Some(Duration::from_secs(60)))?;
    stream.set_read_timeout(Some(Duration::from_secs(60)))?;

    //send request
    stream.write_all(connect.as_bytes())?;

    //read the answer from the proxy
    let mut buffer = [0;64];
    stream.read(&mut buffer)?;

    // check the answer from the proxy
    let answer = &String::from_utf8_lossy(&buffer);
    if !answer.contains("200") {
        writeln!(io::stderr(), "Got bad answer from the proxy").ok();
        std::process::exit(1)
    }

    Ok(stream)
}

fn connect_to_server(host: &str, port: usize) -> Result<std::net::TcpStream, Box<dyn Error>> {

    let stream = match TcpStream::connect_timeout(
        match &format!(
            "{}:{}",
            host,
            port
        )
        .to_socket_addrs()?
        .next() {
            Some(val) => val,
            None => {
                writeln!(io::stderr(), "Unable to connect to the server").ok();
                std::process::exit(1)
            }
        },
        Duration::from_secs(60)
    )  {
        Ok(val) => val,
        Err(_) => {
            writeln!(io::stderr(), "Unable to connect to the server").ok();
            std::process::exit(1)
        }
    };

    stream.set_read_timeout(Some(Duration::from_secs(60)))?;
    stream.set_write_timeout(Some(Duration::from_secs(60)))?;

    Ok(stream)
}

fn connect_to_http(
    host: &str,
    port: usize
) -> Result<Connection, Box<dyn Error>> {
    let stream = connect_to_server(host, port)?;

    Ok(Connection::Http{stream})
}

fn connect_to_https(
    host: &str,
    port: usize
) -> Result<Connection, Box<dyn Error>> {

    //connector to https
    let connector = TlsConnector::builder()
        .danger_accept_invalid_certs(true)
        .build()?;

    let stream = connect_to_server(host, port)?;

    let stream = connector.connect(host, stream)?;

    Ok(Connection::Https{stream})
}