$env.log = function(msg, level){ debug(' '+ (level?level:'LOG') + ':\t['+ new Date()+"] {ENVJS} "+msg); }; $env.location = function(path, base){ var debug = false; debug && print("loc",path,base); if ( path == "about:blank" ) { return path; } var protocol = new RegExp('(^file\:|^http\:|^https\:|data:)'); var m = protocol.exec(path); if(m&&m.length>1){ var url = Ruby.URI.parse(path); var s = url.toString(); if ( s.substring(0,6) == "file:/" && s[6] != "/" ) { s = "file://" + s.substring(5,s.length); } debug && print("YY",s); return s; }else if(base){ base = Ruby.URI.parse(base); path = Ruby.URI.parse(path); var b = Ruby.eval("lambda { |a,b| a+b; }"); base = b(base,path); base = base + ""; var result = base; // print("ZZ",result); // ? This path only used for files? if ( result.substring(0,6) == "file:/" && result[6] != "/" ) { result = "file://" + result.substring(5,result.length); } if ( result.substring(0,7) == "file://" ) { result = result.substring(7,result.length); } debug && print("ZZ",result); return result; }else{ //return an absolute url from a url relative to the window location // print("hi", $master.first_script_window, $master.first_script_window && $master.first_script_window.location ); // if( ( base = window.location ) && if( ( base = ( ( $master.first_script_window && $master.first_script_window.location ) || window.location ) ) && ( base != "about:blank" ) && base.href && (base.href.length > 0) ) { base_uri = Ruby.URI.parse(base.href); new_uri = Ruby.URI.parse(path); result = Ruby.eval("lambda { |a,b| a+b; }")(base_uri,new_uri)+""; debug && print("IIII",result); return result; base = base.href.substring(0, base.href.lastIndexOf('/')); var result; // print("XXXXX",base); if ( base[base.length-1] == "/" || path[0] == "/" ) { result = base + path; } else { result = base + '/' + path; } if ( result.substring(0,6) == "file:/" && result[6] != "/" ) { result = "file://" + result.substring(5,result.length); } debug && print("****",result); return result; } else { // print("RRR",result); return "file://"+Ruby.File.expand_path(path); } } }; $env.connection = $master.connection || function(xhr, responseHandler, data){ var url = Ruby.URI.parse(xhr.url); var connection; var resp; // print("xhr",xhr.url); // print("xhr",url); if ( /^file\:/.test(url) ) { // experimental hack try { Ruby.eval("require 'envjs/net/cgi'"); resp = connection = new Ruby.Envjs.Net.CGI( xhr, data ); } catch(e) { try{ if ( xhr.method == "PUT" ) { var text = data || "" ; $env.writeToFile(text, url); } else if ( xhr.method == "DELETE" ) { $env.deleteFile(url); } else { Ruby.eval("require 'envjs/net/file'"); var request = new Ruby.Envjs.Net.File.Get( url.path ); connection = Ruby.Envjs.Net.File.start( url.host, url.port ); resp = connection.request( request ); //try to add some canned headers that make sense try{ if(xhr.url.match(/html$/)){ xhr.responseHeaders["Content-Type"] = 'text/html'; }else if(xhr.url.match(/.xml$/)){ xhr.responseHeaders["Content-Type"] = 'text/xml'; }else if(xhr.url.match(/.js$/)){ xhr.responseHeaders["Content-Type"] = 'text/javascript'; }else if(xhr.url.match(/.json$/)){ xhr.responseHeaders["Content-Type"] = 'application/json'; }else{ xhr.responseHeaders["Content-Type"] = 'text/plain'; } //xhr.responseHeaders['Last-Modified'] = connection.getLastModified(); //xhr.responseHeaders['Content-Length'] = headerValue+''; //xhr.responseHeaders['Date'] = new Date()+'';*/ }catch(e){ $env.warn('failed to load response headers',e); } } } catch (e) { connection = null; xhr.readyState = 4; if(e.toString().match(/Errno::ENOENT/)) { xhr.status = "404"; xhr.statusText = "Not Found"; xhr.responseText = undefined; } else { xhr.status = "500"; xhr.statusText = "Local File Protocol Error"; xhr.responseText = "
"+ e+ "
"; } } } } else { Ruby.eval("require 'net/http'"); var req; var path; try { path = url.request_uri(); } catch(e) { path = url.path; } if ( xhr.method == "GET" ) { req = new Ruby.Net.HTTP.Get( path ); } else if ( xhr.method == "POST" ) { req = new Ruby.Net.HTTP.Post( path ); } else if ( xhr.method == "PUT" ) { req = new Ruby.Net.HTTP.Put( path ); } for (var header in xhr.headers){ $master.add_req_field( req, header, xhr.headers[header] ); } //write data to output stream if required if(data&&data.length&&data.length>0){ if ( xhr.method == "PUT" || xhr.method == "POST" ) { Ruby.eval("lambda { |req,data| req.body = data}").call(req,data); // req.body = data; } } try { connection = Ruby.Net.HTTP.start( url.host, url.port ); resp = connection.request(req); } catch(e) { $env.warn("XHR net request failed: "+e); // FIX: do the on error stuff ... throw e; } } if(connection){ try{ if (false) { var respheadlength = connection.getHeaderFields().size(); // Stick the response headers into responseHeaders for (var i = 0; i < respheadlength; i++) { var headerName = connection.getHeaderFieldKey(i); var headerValue = connection.getHeaderField(i); if (headerName) xhr.responseHeaders[headerName+''] = headerValue+''; } } resp.each(function(k,v){ xhr.responseHeaders[k] = v; }); }catch(e){ $env.error('failed to load response headers: '+e); throw e; } xhr.readyState = 4; xhr.status = parseInt(resp.code,10) || 0; xhr.statusText = connection.responseMessage || ""; var contentEncoding = resp["Content-Encoding"] || "utf-8", baos = new Ruby.StringIO, length, stream = null, responseXML = null; try{ var lower = contentEncoding.toLowerCase(); stream = ( lower == "gzip" || lower == "decompress" ) ? ( Ruby.raise("java") && new java.util.zip.GZIPInputStream(resp.getInputStream()) ) : resp; }catch(e){ if (resp.code == "404") $env.info('failed to open connection stream \n' + e.toString(), e); else $env.error('failed to open connection stream \n' + e.toString(), e); stream = resp; } baos.write(resp.body); baos.close(); connection.finish(); xhr.responseText = baos.string(); } if(responseHandler){ $env.debug('calling ajax response handler'); responseHandler(); } }; var extract_line = Ruby.eval( "lambda { |e| \ begin; \ e.stack.to_s.split(%(\n))[1].match(/:([^:]*)$/)[1]; \ rescue; %(unknown); end; \ }"); var get_exception = window.get_exception = Ruby.eval(" \ lambda { |e| \ estr = e.to_s; \ estr.gsub!(/(