<commit_before>extern crate iron;
extern crate rand;
extern crate urlencoded;

use iron::headers;
use iron::prelude::*;
use iron::method::Method;
use iron::modifiers::{Redirect, Header};
use iron::{Handler, Url, status};

use rand::Rng;

use std::collections::HashMap;
use std::sync::RwLock;

use urlencoded::UrlEncodedBody;

struct UrlShortenerHandler {
    shortened_urls: RwLock<HashMap<String, Url>>,
}

impl UrlShortenerHandler {
    fn new() -> Self {
        UrlShortenerHandler { shortened_urls: RwLock::new(HashMap::new()) }
    }
}

impl Handler for UrlShortenerHandler {
    fn handle(&self, req: &mut Request) -> IronResult<Response> {
        match &req.method {
            &Method::Post => {
                let long_url = Url::parse(req.get_ref::<UrlEncodedBody>()
                        .unwrap()
                        .get("url")
                        .unwrap()
                        .get(0)
                        .unwrap())
                    .unwrap();

                let random_key: String = rand::thread_rng().gen_ascii_chars().take(5).collect();
                let short_url = format!("http://localhost:3000/{}", random_key);

                self.shortened_urls.write().unwrap().insert(random_key, long_url);

                Ok(Response::with((status::Created, Header(headers::Location(short_url)))))
            }
            &Method::Get | &Method::Head => {
                let request_path = &req.url.path().join("/");
                match self.shortened_urls.read().unwrap().get(request_path) {
                    Some(url) => Ok(Response::with((status::Found, Redirect(url.clone())))),
                    None => Ok(Response::with(status::NotFound)),
                }
            }
            _ => Ok(Response::with(status::NotFound)),
        }
    }
}

fn main() {
    println!("Starting URL Shortener on port 3000...");

    let handler = UrlShortenerHandler::new();
    match Iron::new(handler).http("localhost:3000") {<commit_msg>Return textual error on incorrect client request<commit_after>extern crate iron;
extern crate rand;
extern crate urlencoded;

use iron::headers;
use iron::prelude::*;
use iron::method::Method;
use iron::modifiers::{Redirect, Header};
use iron::{Handler, Url, status};

use rand::Rng;

use std::collections::HashMap;
use std::sync::RwLock;

use urlencoded::UrlEncodedBody;

struct UrlShortenerHandler {
    shortened_urls: RwLock<HashMap<String, Url>>,
}

impl UrlShortenerHandler {
    fn new() -> Self {
        UrlShortenerHandler { shortened_urls: RwLock::new(HashMap::new()) }
    }
}

impl Handler for UrlShortenerHandler {
    fn handle(&self, req: &mut Request) -> IronResult<Response> {
        match &req.method {
            &Method::Post => {
                let long_url = req.get_ref::<UrlEncodedBody>()
                    .map_err(|_| "URL encoded body missing")
                    .and_then(|data| match data.get("url") {
                        Some(urls) => Ok(urls.first().unwrap()),
                        None => Err("Parameter 'url' missing from the URL encoded body"),
                    })
                    .and_then(|url| Url::parse(url).map_err(|_| "Parameter 'url' is invalid"));

                match long_url {
                    Ok(long_url) => {
                        let random_key: String =
                            rand::thread_rng().gen_ascii_chars().take(5).collect();
                        let short_url = format!("http://localhost:3000/{}", random_key);

                        self.shortened_urls.write().unwrap().insert(random_key, long_url);

                        Ok(Response::with((status::Created, Header(headers::Location(short_url)))))
                    }
                    Err(e) => Ok(Response::with((status::BadRequest, e))),
                }
            }
            &Method::Get | &Method::Head => {
                let request_path = &req.url.path().join("/");
                match self.shortened_urls.read().unwrap().get(request_path) {
                    Some(url) => Ok(Response::with((status::Found, Redirect(url.clone())))),
                    None => Ok(Response::with(status::NotFound)),
                }
            }
            _ => Ok(Response::with(status::NotFound)),
        }
    }
}

fn main() {
    println!("Starting URL Shortener on port 3000...");

    let handler = UrlShortenerHandler::new();
    match Iron::new(handler).http("localhost:3000") {
{'fraction_non_alphanumeric': 0.1509476534296029, 'fraction_numerical': 0.006092057761732852, 'mean_word_length': 2.173228346456693, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1716171', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1246, 'n_words': 237}