Represent URLs as url objects, not strings

This commit is contained in:
Brian Anderson 2012-07-27 17:24:44 -07:00
parent 1d7e37e299
commit 633c013567
6 changed files with 42 additions and 34 deletions

View file

@ -38,11 +38,15 @@ import js::rust::compartment;
import resource::resource_task; import resource::resource_task;
import resource_task::{ResourceTask}; import resource_task::{ResourceTask};
import std::net::url::url;
import url_to_str = std::net::url::to_str;
import util::url::make_url;
type Content = chan<ControlMsg>; type Content = chan<ControlMsg>;
enum ControlMsg { enum ControlMsg {
ParseMsg(~str), ParseMsg(url),
ExecuteMsg(~str), ExecuteMsg(url),
ExitMsg ExitMsg
} }
@ -121,12 +125,12 @@ class Content<S:Sink send copy> {
fn handle_control_msg(control_msg: ControlMsg) -> bool { fn handle_control_msg(control_msg: ControlMsg) -> bool {
alt control_msg { alt control_msg {
ParseMsg(filename) { ParseMsg(url) {
#debug["content: Received filename `%s` to parse", filename]; #debug["content: Received url `%s` to parse", url_to_str(url)];
// Note: we can parse the next document in parallel // Note: we can parse the next document in parallel
// with any previous documents. // with any previous documents.
let stream = spawn_html_lexer_task(copy filename, self.resource_task); let stream = spawn_html_lexer_task(copy url, self.resource_task);
let (root, style_port, js_port) = build_dom(self.scope, stream); let (root, style_port, js_port) = build_dom(self.scope, stream);
let css_rules = style_port.recv(); let css_rules = style_port.recv();
let js_scripts = js_port.recv(); let js_scripts = js_port.recv();
@ -156,12 +160,12 @@ class Content<S:Sink send copy> {
ret true; ret true;
} }
ExecuteMsg(filename) { ExecuteMsg(url) {
#debug["content: Received filename `%s` to execute", filename]; #debug["content: Received url `%s` to execute", url_to_str(url)];
alt read_whole_file(filename) { alt read_whole_file(url.path) {
err(msg) { err(msg) {
println(#fmt["Error opening %s: %s", filename, msg]); println(#fmt["Error opening %s: %s", url_to_str(url), msg]);
} }
ok(bytes) { ok(bytes) {
let cx = self.jsrt.cx(); let cx = self.jsrt.cx();
@ -169,7 +173,7 @@ class Content<S:Sink send copy> {
cx.set_logging_error_reporter(); cx.set_logging_error_reporter();
cx.new_compartment(global_class).chain(|compartment| { cx.new_compartment(global_class).chain(|compartment| {
compartment.define_functions(debug_fns); compartment.define_functions(debug_fns);
cx.evaluate_script(compartment.global_obj, bytes, filename, 1u) cx.evaluate_script(compartment.global_obj, bytes, url.path, 1u)
}); });
} }
} }

View file

@ -6,6 +6,7 @@ import layout_task::Layout;
import content::{Content, ExecuteMsg, ParseMsg, ExitMsg, create_content}; import content::{Content, ExecuteMsg, ParseMsg, ExitMsg, create_content};
import resource::resource_task; import resource::resource_task;
import resource::resource_task::{ResourceTask}; import resource::resource_task::{ResourceTask};
import std::net::url::url;
import pipes::{port, chan}; import pipes::{port, chan};
@ -42,8 +43,7 @@ class Engine<S:Sink send copy> {
fn handle_request(request: Msg) -> bool { fn handle_request(request: Msg) -> bool {
alt request { alt request {
LoadURLMsg(url) { LoadURLMsg(url) {
let url = copy url; if url.path.ends_with(".js") {
if url.ends_with(".js") {
self.content.send(ExecuteMsg(url)) self.content.send(ExecuteMsg(url))
} else { } else {
self.content.send(ParseMsg(url)) self.content.send(ParseMsg(url))
@ -70,7 +70,7 @@ class Engine<S:Sink send copy> {
} }
enum Msg { enum Msg {
LoadURLMsg(~str), LoadURLMsg(url),
ExitMsg(chan<()>) ExitMsg(chan<()>)
} }

View file

@ -9,6 +9,8 @@ import pipes::{port, chan};
import lexer_util::*; import lexer_util::*;
import std::net::url::url;
enum ParserState { enum ParserState {
CssElement, CssElement,
CssRelation, CssRelation,
@ -252,21 +254,21 @@ fn spawn_css_lexer_from_string(-content : ~str) -> port<Token> {
} }
#[warn(no_non_implicitly_copyable_typarams)] #[warn(no_non_implicitly_copyable_typarams)]
fn spawn_css_lexer_task(-filename: ~str) -> pipes::port<Token> { fn spawn_css_lexer_task(-url: url) -> pipes::port<Token> {
let (result_chan, result_port) = pipes::stream(); let (result_chan, result_port) = pipes::stream();
task::spawn(|| { task::spawn(|| {
assert filename.ends_with(".css"); assert url.path.ends_with(".css");
let file_try = io::read_whole_file(filename); let file_try = io::read_whole_file(url.path);
// Check if the given css file existed, if it does, parse it, // Check if the given css file existed, if it does, parse it,
// otherwise just send an eof. // otherwise just send an eof.
if file_try.is_ok() { if file_try.is_ok() {
#debug["Lexing css sheet %?", filename]; #debug["Lexing css sheet %?", url.path];
let file_data = file_try.get(); let file_data = file_try.get();
lex_css_from_bytes(file_data, result_chan); lex_css_from_bytes(file_data, result_chan);
} else { } else {
#debug["Failed to open css sheet %?", filename]; #debug["Failed to open css sheet %?", url.path];
result_chan.send(Eof); result_chan.send(Eof);
} }
}); });

View file

@ -12,11 +12,12 @@ import parser = parser::html_lexer;
import parser::Token; import parser::Token;
import dom::style::Stylesheet; import dom::style::Stylesheet;
import vec::{push, push_all_move, flat_map}; import vec::{push, push_all_move, flat_map};
import std::net::url::url;
import dvec::extensions; import dvec::extensions;
enum CSSMessage { enum CSSMessage {
File(~str), File(url),
Exit Exit
} }
@ -99,14 +100,11 @@ fn css_link_listener(to_parent : comm::chan<Stylesheet>, from_parent : comm::por
loop { loop {
alt from_parent.recv() { alt from_parent.recv() {
File(filename) { File(url) {
let result_port = comm::port(); let result_port = comm::port();
let result_chan = comm::chan(result_port); let result_chan = comm::chan(result_port);
let filename = copy filename;
task::spawn(|| { task::spawn(|| {
//TODO: deal with extraneous copies let css_stream = css_lexer::spawn_css_lexer_task(copy url);
let filename <- copy filename;
let css_stream = css_lexer::spawn_css_lexer_task(filename);
let mut css_rules = css_builder::build_stylesheet(css_stream); let mut css_rules = css_builder::build_stylesheet(css_stream);
result_chan.send(css_rules); result_chan.send(css_rules);
}); });
@ -205,7 +203,9 @@ fn build_dom(scope: NodeScope, stream: comm::port<Token>) -> (Node, comm::port<S
alt elmt.get_attr(~"href") { alt elmt.get_attr(~"href") {
some(filename) { some(filename) {
#debug["Linking to a css sheet named: %s", filename]; #debug["Linking to a css sheet named: %s", filename];
style_chan.send(File(copy filename)); // FIXME: Need to base the new url on the current url
let url = make_url(filename, none);
style_chan.send(File(url));
} }
none { /* fall through*/ } none { /* fall through*/ }
} }

View file

@ -6,6 +6,7 @@ import vec::push;
import lexer_util::*; import lexer_util::*;
import resource::resource_task; import resource::resource_task;
import resource_task::{ResourceTask}; import resource_task::{ResourceTask};
import std::net::url::url;
enum Token { enum Token {
StartOpeningTag(~str), StartOpeningTag(~str),
@ -170,15 +171,13 @@ fn lexer(reader: io::reader, state : ParseState) -> HtmlLexer {
} }
#[warn(no_non_implicitly_copyable_typarams)] #[warn(no_non_implicitly_copyable_typarams)]
fn spawn_html_lexer_task(-filename: ~str, resource_task: ResourceTask) -> port<Token> { fn spawn_html_lexer_task(-url: url, resource_task: ResourceTask) -> port<Token> {
let html_port = port(); let html_port = port();
let html_chan = chan(html_port); let html_chan = chan(html_port);
let html_file = copy filename;
task::spawn(|| { task::spawn(|| {
let filename = copy html_file; assert url.path.ends_with(~".html");
assert (copy filename).ends_with(~".html"); let file_data = io::read_whole_file(url.path).get();
let file_data = io::read_whole_file(filename).get();
let reader = io::bytes_reader(file_data); let reader = io::bytes_reader(file_data);
let lexer = lexer(reader, NormalHtml); let lexer = lexer(reader, NormalHtml);

View file

@ -5,6 +5,9 @@ import osmain::{OSMain, AddKeyHandler};
import opts::{Opts, Screen, Png}; import opts::{Opts, Screen, Png};
import engine::{Engine, LoadURLMsg}; import engine::{Engine, LoadURLMsg};
import url_to_str = std::net::url::to_str;
import util::url::make_url;
import pipes::{port, chan}; import pipes::{port, chan};
fn main(args: ~[~str]) { fn main(args: ~[~str]) {
@ -41,8 +44,9 @@ fn run_pipeline_screen(urls: ~[~str]) {
let engine_chan = engine.start(); let engine_chan = engine.start();
for urls.each |filename| { for urls.each |filename| {
#debug["master: Sending filename `%s`", filename]; let url = make_url(filename, none);
engine_chan.send(LoadURLMsg(copy filename)); #debug["master: Sending url `%s`", url_to_str(url)];
engine_chan.send(LoadURLMsg(url));
#debug["master: Waiting for keypress"]; #debug["master: Waiting for keypress"];
alt keypress_from_osmain.try_recv() { alt keypress_from_osmain.try_recv() {
some(*) { } some(*) { }
@ -71,8 +75,7 @@ fn run_pipeline_png(-url: ~str, outfile: ~str) {
let sink = PngSink(pngdata_from_sink); let sink = PngSink(pngdata_from_sink);
let engine = Engine(sink); let engine = Engine(sink);
let engine_chan = engine.start(); let engine_chan = engine.start();
let url = copy url; engine_chan.send(LoadURLMsg(make_url(url, none)));
engine_chan.send(LoadURLMsg(url));
alt buffered_file_writer(outfile) { alt buffered_file_writer(outfile) {
ok(writer) { ok(writer) {
writer.write(pngdata_from_sink.recv()) writer.write(pngdata_from_sink.recv())