Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

rustpkg support (and a few language updates) #3

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
http_client
*~
*.so
*.dylib
*.dylib
*.a
*.dummy
*.o
Makefile
38 changes: 19 additions & 19 deletions http_client.rc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::net::ip::{
IpAddr, IpGetAddrErr, Ipv4, Ipv6
};
use std::net::tcp::{connect, TcpErrData, TcpSocket};
use std::net::url::Url;
use std::net::url;
use std::uv_global_loop;
use connection::{
Connection, ConnectionFactory, UvConnectionFactory,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum RequestError {
ErrorMisc
}

/// Request
/// Request
#[deriving(Eq)]
pub enum RequestEvent {
Status(StatusCode),
Expand All @@ -71,22 +71,22 @@ pub fn uv_dns_resolver() -> DnsResolver {
return r;
}

pub fn uv_http_request(url: Url) -> HttpRequest<TcpSocket, UvConnectionFactory> {
pub fn uv_http_request(url: url::Url) -> HttpRequest<TcpSocket, UvConnectionFactory> {
HttpRequest(uv_dns_resolver(), UvConnectionFactory, url)
}

#[allow(non_implicitly_copyable_typarams)]
pub struct HttpRequest<C, CF> {
resolve_ip_addr: DnsResolver,
connection_factory: CF,
url: Url,
url: url::Url,
parser: Parser,
cb: @fn(+ev: RequestEvent)
}

pub fn HttpRequest<C: Connection, CF: ConnectionFactory<C>>(resolver: DnsResolver,
connection_factory: CF,
url: Url) ->
url: url::Url) ->
HttpRequest<C,CF> {
HttpRequest {
resolve_ip_addr: resolver,
Expand Down Expand Up @@ -265,9 +265,9 @@ pub impl<C: Connection, CF: ConnectionFactory<C>> HttpRequest<C, CF> {
}

#[allow(non_implicitly_copyable_typarams)]
pub fn sequence<C: Connection, CF: ConnectionFactory<C>>(request: &mut HttpRequest<C, CF>) ->
pub fn sequence<C: Connection, CF: ConnectionFactory<C>>(request: &mut HttpRequest<C, CF>) ->
~[RequestEvent] {

let events = @mut ~[];
do request.begin |event| {
vec::push(&mut *events, event)
Expand All @@ -279,10 +279,10 @@ pub fn sequence<C: Connection, CF: ConnectionFactory<C>>(request: &mut HttpReque
#[allow(non_implicitly_copyable_typarams)]
pub fn test_resolve_error() {
let url = url::from_str(~"http://example.com_not_real/").get();
let request = uv_http_request(url);
let events = sequence(request);
let mut request = uv_http_request(url);
let events = sequence(&mut request);

fail_unless!(events == ~[
assert!(events == ~[
Error(ErrorDnsResolution),
]);
}
Expand All @@ -293,10 +293,10 @@ pub fn test_connect_error() {
// This address is invalid because the first octet
// of a class A address cannot be 0
let url = url::from_str(~"http://0.42.42.42/").get();
let request = uv_http_request(url);
let events = sequence(request);
let mut request = uv_http_request(url);
let events = sequence(&mut request);

fail_unless!(events == ~[
assert!(events == ~[
Error(ErrorConnect),
]);
}
Expand All @@ -305,8 +305,8 @@ pub fn test_connect_error() {
#[allow(non_implicitly_copyable_typarams)]
pub fn test_connect_success() {
let url = url::from_str(~"http://example.com/").get();
let request = uv_http_request(url);
let events = sequence(request);
let mut request = uv_http_request(url);
let events = sequence(&mut request);

for events.each |ev| {
match *ev {
Expand All @@ -321,23 +321,23 @@ pub fn test_connect_success() {
#[allow(non_implicitly_copyable_typarams)]
pub fn test_simple_body() {
let url = url::from_str(~"http://www.iana.org/").get();
let request = uv_http_request(url);
let events = sequence(request);
let mut request = uv_http_request(url);
let events = sequence(&mut request);

let mut found = false;

for events.each |ev| {
match ev {
&Payload(ref value) => {
if str::from_bytes((copy value).get()).to_lower().contains(~"doctype html") {
if str::from_bytes((copy value).take()).to_lower().contains(~"doctype html") {
found = true
}
}
_ => { }
}
}

fail_unless!(found);
assert!(found);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions http_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ struct struct_unnamed1 {
len: uint16_t,
}

#[nolink]
#[link_args = "-L. -lhttp_parser"]
#[link_name = "http_parser"]
#[link_args = "-L."]
pub extern mod bindgen {

fn http_parser_init(++parser: *http_parser, ++_type: enum_http_parser_type);
Expand Down
2 changes: 1 addition & 1 deletion parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub impl Parser {

fn callbacks(http_parser: *http_parser::http_parser) -> *ParserCallbacks {
unsafe {
fail_unless!((*http_parser).data.is_not_null());
assert!((*http_parser).data.is_not_null());
return (*http_parser).data as *ParserCallbacks;
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[pkg(id = "http_client",
vers = "0.1.0")];

extern mod rustpkg;
use core::run;

#[pkg_do(build)]
fn main() {
run::run_program("./configure", []);
run::run_program("make", []);
let crate = rustpkg::Crate(~"http_client.rc");
rustpkg::build(~[crate]);
}
8 changes: 4 additions & 4 deletions request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::net::url::Url;
use std::net::url;

pub fn build_request(url: Url) -> ~str {
pub fn build_request(url: url::Url) -> ~str {

let host = copy url.host;
let mut path = if url.path.len() > 0 { copy url.path } else { ~"/" };
Expand All @@ -24,7 +24,7 @@ pub fn build_request(url: Url) -> ~str {
#[allow(non_implicitly_copyable_typarams)]
fn should_request_slash_when_path_is_empty() {
let url = url::from_str(~"http://host").get();
fail_unless!(url.path.is_empty());
assert!(url.path.is_empty());
let headers = build_request(url);
fail_unless!(headers.contains(~"GET / "));
assert!(headers.contains(~"GET / "));
}