Skip to content

Commit

Permalink
Merge pull request chris-morgan#8 from larsbergstrom/rust_20140224
Browse files Browse the repository at this point in the history
Rust upgrade 20140224
  • Loading branch information
kmcallister committed Mar 18, 2014
2 parents 1a9a23b + 5dff177 commit e8c002b
Show file tree
Hide file tree
Showing 35 changed files with 742 additions and 684 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
env:
global:
- secure: SkhFtAUkRRrDYHr6z6YhWtGouw6jsys7dBKLojA+M3M68EM9U1VG9oNHMasCdFUt0a+TdPte9JccufbhvZABnlo7+yU6hQ94jhsyQ9qSL5y7V+2eyWBkQZDqyFt3jgLyrrSV2Nkk22xI4UAm2iGRKel6lXWi7i0i9hDT+3W0dK4=
before_install:
- yes | sudo add-apt-repository ppa:hansjorg/rust
- sudo apt-get update
install:
- sudo apt-get install rust-nightly
script:
- make check
- make docs
after_script:
- curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
RUSTC ?= rustc
RUSTDOC ?= rustdoc
RUSTPKG ?= rustpkg
RUSTFLAGS ?= -O -Z debug-info
RUSTLIBFLAGS ?= --dylib --rlib
RUSTFLAGS ?= -O
RUST_REPOSITORY ?= ../rust
RUST_CTAGS ?= $(RUST_REPOSITORY)/src/etc/ctags.rust
VERSION=0.1-pre
Expand Down Expand Up @@ -31,15 +30,18 @@ http: $(libhttp_so)

$(libhttp_so): $(http_files)
mkdir -p build/
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBFLAGS) src/http/lib.rs --out-dir=build
$(RUSTC) $(RUSTFLAGS) src/http/lib.rs --out-dir=build

all: http examples docs

build/codegen: $(codegen_files)
mkdir -p build/
$(RUSTC) $(RUSTFLAGS) src/codegen/main.rs --out-dir=build
$(RUSTC) src/codegen/main.rs --out-dir=build

src/http/generated/%.rs: build/codegen
src/http/generated:
mkdir -p src/http/generated

src/http/generated/%.rs: build/codegen src/http/generated
build/codegen $(patsubst src/http/generated/%,%,$@) src/http/generated/

build/%:: src/%/main.rs $(libhttp_so)
Expand Down
7 changes: 5 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ all: libhttp.dummy
codegen: $(wildcard $(VPATH)/src/codegen/*.rs)
$(RUSTC) $(HOST_RUSTFLAGS) $(VPATH)/src/codegen/main.rs -o codegen

$(VPATH)/src/http/generated/%.rs: codegen
$(VPATH)/src/http/generated:
mkdir -p $(VPATH)/src/http/generated

$(VPATH)/src/http/generated/%.rs: codegen $(VPATH)/src/http/generated
./codegen $(patsubst $(VPATH)/src/http/generated/%,%,$@) $(VPATH)/src/http/generated/

libhttp.dummy: $(libhttp_files)
$(RUSTC) $(RUSTFLAGS) $(VPATH)/src/http/lib.rs --out-dir . --lib
$(RUSTC) $(RUSTFLAGS) $(VPATH)/src/http/lib.rs --out-dir .
touch $@

check: tests
Expand Down
13 changes: 11 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ This project has two parts:
Both are in progress; both have basic, low-level implementations in place.
Neither is complete nor yet compliant in any way.

Rust versions
-------------

I urge you to track Rust master as rust-http does, but if you really are set on
using Rust 0.9, you can use the [`rust-0.9-compatible`
branch](https://github.com/chris-morgan/rust-http/commits/rust-0.9-compatible).
It is not maintained, however; it's just the last commit that *will* work on
Rust 0.9.

Goals
-----

Expand Down Expand Up @@ -48,11 +57,11 @@ Build everything::

Run one of the servers::

build/examples/apache_fake
build/examples/server/apache_fake

To run the client example, start one of the servers and run::

build/examples/client/client
build/examples/client http://127.0.0.1:8001/

At present, all of the example servers serve to http://127.0.0.1:8001/.

Expand Down
4 changes: 2 additions & 2 deletions doc/client-plan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ probably be a wrapper about a ``Reader``.
The initial API will be very simple, with ``Request::new(Method, Url)`` and the
use of string typing for headers::

extern mod http;
extern crate http;
use http::client::Request;
use http::method::Get;
use extra::url::Url;

let mut request = Request::new(Get, FromStr::from_str("http://rust-lang.org"));
let mut request = Request::new(Get, from_str("http://rust-lang.org"));
request.headers.insert(~"Connection", ~"close");
request.headers.insert(~"Referer", ~"https://google.com/");
let mut response = request.send();
Expand Down
92 changes: 49 additions & 43 deletions src/codegen/branchify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_escape];

use std::str::CharIterator;
use std::io::Writer;
use std::str::Chars;
use std::io::IoResult;

struct ParseBranch {
matches: ~[u8],
Expand All @@ -22,7 +22,7 @@ impl ParseBranch {
pub fn branchify(options: &[(&str, &str)], case_sensitive: bool) -> ~[ParseBranch] {
let mut root = ParseBranch::new();

fn go_down_moses(branch: &mut ParseBranch, mut chariter: CharIterator, result: &str, case_sensitive: bool) {
fn go_down_moses(branch: &mut ParseBranch, mut chariter: Chars, result: &str, case_sensitive: bool) {
match chariter.next() {
Some(c) => {
let first_case = if case_sensitive { c as u8 } else { c.to_ascii().to_upper().to_byte() };
Expand Down Expand Up @@ -85,60 +85,66 @@ pub fn generate_branchified_method(
end: &str,
max_len: &str,
valid: &str,
unknown: &str) {
unknown: &str) -> IoResult<()> {

fn r(writer: &mut Writer, branch: &ParseBranch, prefix: &str, indent: uint, read_call: &str,
end: &str, max_len: &str, valid: &str, unknown: &str) {
end: &str, max_len: &str, valid: &str, unknown: &str) -> IoResult<()> {
let indentstr = " ".repeat(indent * 4);
let w = |s: &str| {
writer.write(indentstr.as_bytes());
writer.write(s.as_bytes());
writer.write(bytes!("\n"));
};
macro_rules! w (
($s:expr) => {
try!(write!(writer, "{}{}\n", indentstr, $s))
}
)
for &c in branch.matches.iter() {
let next_prefix = format!("{}{}", prefix, c as char);
w(format!("Some(b) if b == '{}' as u8 => match {} \\{", c as char, read_call));
w!(format!("Ok(b) if b == '{}' as u8 => match {} \\{", c as char, read_call));
for b in branch.children.iter() {
r(writer, b, next_prefix, indent + 1, read_call, end, max_len, valid, unknown);
try!(r(writer, b, next_prefix, indent + 1, read_call, end, max_len, valid, unknown));
}
match branch.result {
Some(ref result) => w(format!(" Some(b) if b == SP => return Some({}),", *result)),
None => w(format!(" Some(b) if b == SP => return Some({}),",
Some(ref result) =>
w!(format!(" Ok(b) if b == SP => return Ok({}),", *result)),
None => w!(format!(" Ok(b) if b == SP => return Ok({}),",
unknown.replace("{}", format!("~\"{}\"", next_prefix)))),
}
w(format!(" Some(b) if {} => (\"{}\", b),", valid, next_prefix));
w(" _ => return None,");
w("},");
w!(format!(" Ok(b) if {} => (\"{}\", b),", valid, next_prefix));
w!(" Ok(_) => return Err(::std::io::IoError { kind: ::std::io::OtherIoError, desc: \"bad value\", detail: None }),");
w!(" Err(err) => return Err(err),");
w!("},");
}
Ok(())
}
let indentstr = " ".repeat(indent * 4);
let w = |s: &str| {
writer.write(indentstr.as_bytes());
writer.write(s.as_bytes());
writer.write(bytes!("\n"));
};
macro_rules! w (
($s:expr) => {
try!(write!(writer, "{}{}\n", indentstr, $s))
}
)

w(format!("let (s, next_byte) = match {} \\{", read_call));
w!(format!("let (s, next_byte) = match {} \\{", read_call));
for b in branches.iter() {
r(writer, b, "", indent + 1, read_call, end, max_len, valid, unknown);
try!(r(writer, b, "", indent + 1, read_call, end, max_len, valid, unknown));
}
w(format!(" Some(b) if {} => (\"\", b),", valid));
w( (" _ => return None,"));
w( ("};"));
w( ("// OK, that didn't pan out. Let's read the rest and see what we get."));
w( ("let mut s = s.to_owned();"));
w( ("s.push_char(next_byte as char);"));
w( ("loop {"));
w(format!(" match {} \\{", read_call));
w(format!(" Some(b) if b == {} => return Some({}),", end, unknown.replace("{}", "s")));
w(format!(" Some(b) if {} => \\{", valid));
w(format!(" if s.len() == {} \\{", max_len));
w( (" // Too long; bad request"));
w( (" return None;"));
w( (" }"));
w( (" s.push_char(b as char);"));
w( (" },"));
w( (" _ => return None,"));
w( (" }"));
w( ("}"));
w!(format!(" Ok(b) if {} => (\"\", b),", valid));
w!( (" Ok(_) => return Err(::std::io::IoError { kind: ::std::io::OtherIoError, desc: \"bad value\", detail: None }),"));
w!( (" Err(err) => return Err(err),"));
w!( ("};"));
w!( ("// OK, that didn't pan out. Let's read the rest and see what we get."));
w!( ("let mut s = s.to_owned();"));
w!( ("s.push_char(next_byte as char);"));
w!( ("loop {"));
w!(format!(" match {} \\{", read_call));
w!(format!(" Ok(b) if b == {} => return Ok({}),", end, unknown.replace("{}", "s")));
w!(format!(" Ok(b) if {} => \\{", valid));
w!(format!(" if s.len() == {} \\{", max_len));
w!( (" // Too long; bad request"));
w!( (" return Err(::std::io::IoError { kind: ::std::io::OtherIoError, desc: \"too long, bad request\", detail: None });"));
w!( (" }"));
w!( (" s.push_char(b as char);"));
w!( (" },"));
w!( (" Ok(_) => return Err(::std::io::IoError { kind: ::std::io::OtherIoError, desc: \"bad value\", detail: None }),"));
w!( (" Err(err) => return Err(err),"));
w!( (" }"));
w!( ("}"));
Ok(())
}
21 changes: 9 additions & 12 deletions src/codegen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

#[feature(macro_rules)];

use std::io;
use std::io::{File, Truncate, Write, fs};
extern crate collections;

use std::io::{File, Truncate, Write};
use std::os;

pub mod branchify;
Expand All @@ -14,27 +15,23 @@ fn main() {
let args = os::args();
match args.len() {
0 => {
println("usage: codegen [read_method|status].rs <output-dir>");
os::set_exit_status(1);
println!("usage: codegen [read_method|status].rs <output-dir>");
os::set_exit_status(1);
},
3 => {
let output_dir = Path::new(args[2].as_slice());
// TODO: maybe not 0777?
if !output_dir.exists() {
fs::mkdir(&output_dir, 0b111_111_111);
}

match args[1] {
~"read_method.rs" => read_method::generate(&output_dir),
~"status.rs" => status::generate(&output_dir),
~"read_method.rs" => read_method::generate(&output_dir).unwrap(),
~"status.rs" => status::generate(&output_dir).unwrap(),
s => {
println!("unknown thing-to-generate '{}'", s);
os::set_exit_status(1);
}
}
},
_ => {
println!("usage: {} [read_method|status].rs", args[0]);
println!("usage: {} [read_method|status].rs <output-dir>", args[0]);
os::set_exit_status(1);
}
}
Expand All @@ -43,7 +40,7 @@ fn main() {
pub fn get_writer(output_dir: &Path, filename: &str) -> ~Writer {
let mut output_file = output_dir.clone();
output_file.push(filename);
match io::result(|| File::open_mode(&output_file, Truncate, Write)) {
match File::open_mode(&output_file, Truncate, Write) {
Ok(writer) => ~writer as ~Writer,
Err(e) => fail!("Unable to write file: {}", e.desc),
}
Expand Down
14 changes: 7 additions & 7 deletions src/codegen/read_method.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::branchify::generate_branchified_method;
use super::get_writer;
use std::io::Writer;
use std::io::IoResult;

pub fn generate(output_dir: &Path) {
pub fn generate(output_dir: &Path) -> IoResult<()> {
let mut writer = get_writer(output_dir, "read_method.rs");
writer.write(bytes!("\
try!(writer.write(bytes!("\
// This automatically generated file is included in request.rs.
{
use method::{Connect, Delete, Get, Head, Options, Patch, Post, Put, Trace, ExtensionMethod};
use server::request::MAX_METHOD_LEN;
use rfc2616::{SP, is_token_item};
"));
")));

generate_branchified_method(
try!(generate_branchified_method(
writer,
branchify!(case sensitive,
"CONNECT" => Connect,
Expand All @@ -31,6 +31,6 @@ pub fn generate(output_dir: &Path) {
"SP",
"MAX_METHOD_LEN",
"is_token_item(b)",
"ExtensionMethod({})");
writer.write(bytes!("}\n"));
"ExtensionMethod({})"));
writer.write(bytes!("}\n"))
}
Loading

0 comments on commit e8c002b

Please sign in to comment.