<commit_before>//! A not-quite-trivial HTTP server which responds to requests by showing the request and response
//! headers and any other information it has.

extern mod extra;
extern mod http;

use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
use std::rt::io::Writer;
use extra::time;

use http::server::{Config, Server, ServerUtil, Request, ResponseWriter};
use http::headers::HeaderEnum;
use http::headers::test_utils::to_stream_into_str;

#[deriving(Clone)]
struct InfoServer;

impl Server for InfoServer {
    fn get_config(&self) -> Config {
        Config { bind_address: SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 } }
    }

    fn handle_request(&self, r: &Request, w: &mut ResponseWriter) {
        w.headers.insert(~"Date", to_stream_into_str(&time::now_utc()));
        w.headers.insert(~"Content-Type", ~"text/html");
        w.headers.insert(~"Server", ~"Rust Thingummy/0.0-pre");
        w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>"));

        w.write(bytes!("<h1>Request</h1>"));
        let s = fmt!("<dl>
            <dt>Method</dt><dd>%s</dd>
            <dt>Host</dt><dd>%?</dd>
            <dt>Request URI</dt><dd>%?</dd>
            <dt>HTTP version</dt><dd>%?</dd>
            <dt>Close connection</dt><dd>%?</dd></dl>",
            r.method.to_str(),
            r.headers.host,
            r.request_uri,
            r.version,
            r.close_connection);
        w.write(s.as_bytes().to_owned());
        w.write(bytes!("<h2>Extension headers</h2>"));
        w.write(bytes!("<table><thead><tr><th>Name</th><th>Value</th></thead><tbody>"));
        for header in r.headers.iter() {
            let line = fmt!("<tr><td><code>%s</code></td><td><code>%s</code></td></tr>",
                            header.header_name(),
                            header.header_value());
            w.write(line.as_bytes().to_owned());
        }
        w.write(bytes!("</tbody></table>"));
        w.write(bytes!("<h2>Body</h2><pre>"));
        w.write(r.body.as_bytes().to_owned());
        w.write(bytes!("</pre>"));

        w.write(bytes!("<h1>Response</h1>"));
        let s = fmt!("<dl><dt>Status</dt><dd>%s</dd></dl>", w.status.to_str());
        w.write(s.as_bytes().to_owned());
        w.write(bytes!("<h2>Headers</h2>"));
        w.write(bytes!("<table><thead><tr><th>Name</th><th>Value</th></thead><tbody>"));
        {
            let h = w.headers.clone();
            for (k, v) in h.iter() {
                let line = fmt!("<tr><td><code>%s</code></td><td><code>%s</code></td></tr>", *k, *v);
                w.write(line.as_bytes().to_owned());
            }
        }
        w.write(bytes!("</tbody></table>"));
        // Server doesn't yet support the chunked transfer coding, nor was Content-Length specified,
        // so we'd better close the connection to ensure the page finishes loading.
        // DON'T COPY THIS WAY OF DOING THINGS. EVER. OR ELSE.
        unsafe { ::std::cast::transmute::<&Request, &mut Request>(r).close_connection = true; }
    }
}

fn main() {
    InfoServer.serve_forever();
}<commit_msg>Remove unnecessary allocation.<commit_after>//! A not-quite-trivial HTTP server which responds to requests by showing the request and response
//! headers and any other information it has.

extern mod extra;
extern mod http;

use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
use std::rt::io::Writer;
use extra::time;

use http::server::{Config, Server, ServerUtil, Request, ResponseWriter};
use http::headers::HeaderEnum;
use http::headers::test_utils::to_stream_into_str;

#[deriving(Clone)]
struct InfoServer;

impl Server for InfoServer {
    fn get_config(&self) -> Config {
        Config { bind_address: SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 } }
    }

    fn handle_request(&self, r: &Request, w: &mut ResponseWriter) {
        w.headers.insert(~"Date", to_stream_into_str(&time::now_utc()));
        w.headers.insert(~"Content-Type", ~"text/html");
        w.headers.insert(~"Server", ~"Rust Thingummy/0.0-pre");
        w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>"));

        w.write(bytes!("<h1>Request</h1>"));
        let s = fmt!("<dl>
            <dt>Method</dt><dd>%s</dd>
            <dt>Host</dt><dd>%?</dd>
            <dt>Request URI</dt><dd>%?</dd>
            <dt>HTTP version</dt><dd>%?</dd>
            <dt>Close connection</dt><dd>%?</dd></dl>",
            r.method.to_str(),
            r.headers.host,
            r.request_uri,
            r.version,
            r.close_connection);
        w.write(s.as_bytes());
        w.write(bytes!("<h2>Extension headers</h2>"));
        w.write(bytes!("<table><thead><tr><th>Name</th><th>Value</th></thead><tbody>"));
        for header in r.headers.iter() {
            let line = fmt!("<tr><td><code>%s</code></td><td><code>%s</code></td></tr>",
                            header.header_name(),
                            header.header_value());
            w.write(line.as_bytes());
        }
        w.write(bytes!("</tbody></table>"));
        w.write(bytes!("<h2>Body</h2><pre>"));
        w.write(r.body.as_bytes());
        w.write(bytes!("</pre>"));

        w.write(bytes!("<h1>Response</h1>"));
        let s = fmt!("<dl><dt>Status</dt><dd>%s</dd></dl>", w.status.to_str());
        w.write(s.as_bytes());
        w.write(bytes!("<h2>Headers</h2>"));
        w.write(bytes!("<table><thead><tr><th>Name</th><th>Value</th></thead><tbody>"));
        {
            let h = w.headers.clone();
            for (k, v) in h.iter() {
                let line = fmt!("<tr><td><code>%s</code></td><td><code>%s</code></td></tr>", *k, *v);
                w.write(line.as_bytes());
            }
        }
        w.write(bytes!("</tbody></table>"));
        // Server doesn't yet support the chunked transfer coding, nor was Content-Length specified,
        // so we'd better close the connection to ensure the page finishes loading.
        // DON'T COPY THIS WAY OF DOING THINGS. EVER. OR ELSE.
        unsafe { ::std::cast::transmute::<&Request, &mut Request>(r).close_connection = true; }
    }
}

fn main() {
    InfoServer.serve_forever();
}
{'fraction_non_alphanumeric': 0.2079401236576635, 'fraction_numerical': 0.0078099576960624795, 'mean_word_length': 2.6180105944673335, 'pattern_counts': {'":': 0, '<': 175, '<?xml version=': 0, '>': 177, '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': '7420911', 'n_tokens_mistral': 2061, 'n_tokens_neox': 2031, 'n_words': 368}