Skip to content

Commit

Permalink
added some convenient user agents to Envjs.userAgents so you can easi…
Browse files Browse the repository at this point in the history
…ly override navigator.userAgent. Corrected bug related to redirects where document.location became out of sync with document.baseURI. found bug where redirects might end up parsing a document twice.
  • Loading branch information
thatcher committed Aug 23, 2010
1 parent 8691181 commit 90b16de
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.properties
Expand Up @@ -2,7 +2,7 @@
PROJECT: env-js
BUILD_MAJOR: 1
BUILD_MINOR: 2
BUILD_ID: 33
BUILD_ID: 34
BUILD_VERSION: ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_ID}
BUILD: ${PROJECT}.${BUILD_VERSION}
VERSION: ${BUILD} ${DSTAMP}
Expand Down
File renamed without changes
3 changes: 2 additions & 1 deletion src/platform/core/event.js
Expand Up @@ -14,7 +14,7 @@ Envjs.defaultEventBehaviors = {
if (target && target.nodeName === 'FORM') {
serialized = Envjs.serializeForm(target);
//console.log('serialized %s', serialized);
method = target.method !== ""?target.method.toUpperCase():"GET";
method = target.method?target.method.toUpperCase():"GET";

action = Envjs.uri(
target.action !== ""?target.action:target.ownerDocument.baseURI,
Expand All @@ -23,6 +23,7 @@ Envjs.defaultEventBehaviors = {
if(method=='GET' && !action.match(/^file:/)){
action = action + "?" + serialized;
}
//console.log('replacing document with form submission %s', action);
target.ownerDocument.location.replace(
action, method, serialized
);
Expand Down
4 changes: 4 additions & 0 deletions src/platform/core/window.js
Expand Up @@ -17,6 +17,10 @@ Envjs.os_version = '';
Envjs.lang = '';
Envjs.platform = '';

//some common user agents as constants so you can emulate them
Envjs.userAgents = {
firefox3: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
}

var __windows__ = {};

Expand Down
7 changes: 5 additions & 2 deletions src/window/navigator.js
Expand Up @@ -5,7 +5,7 @@
* Browser Navigator
*/
Navigator = function(){

var $userAgent;
return {
get appCodeName(){
return Envjs.appCodeName;
Expand Down Expand Up @@ -35,8 +35,11 @@ Navigator = function(){
return [];
},
get userAgent(){
return this.appCodeName + "/" + this.appVersion + " Resig/20070309 PilotFish/@BUILD_VERSION@";
return $userAgent||(this.appCodeName + "/" + this.appVersion + " Resig/20070309 PilotFish/@BUILD_VERSION@");
},
set userAgent(agent){
$userAgent = agent;
},
javaEnabled : function(){
return Envjs.javaEnabled;
}
Expand Down
1 change: 1 addition & 0 deletions src/window/window.js
Expand Up @@ -163,6 +163,7 @@ Window = function(scope, parent, opener){
set location(url){
//very important or you will go into an infinite
//loop when creating a xml document
//console.log('setting window location %s', url);
if(url) {
$location.assign(Envjs.uri(url));
}
Expand Down
26 changes: 19 additions & 7 deletions src/xhr/location.js
Expand Up @@ -171,10 +171,6 @@ Location = function(url, doc, history) {
data = data||null;
//console.log('assigning %s',url);

// update closure upvars
$url = url;
parts = Envjs.urlsplit($url);

//we can only assign if this Location is associated with a document
if ($document) {
//console.log('fetching %s (async? %s)', url, $document.async);
Expand All @@ -192,14 +188,29 @@ Location = function(url, doc, history) {
xhr.onreadystatechange = function() {
//console.log('readyState %s', xhr.readyState);
if (xhr.readyState === 4) {
//console.log('new document baseURI %s', xhr.url);
Envjs.exchangeHTMLDocument($document, xhr.responseText, xhr.url);
switch(xhr.status){
case 301:
case 302:
case 303:
case 305:
case 307:
//console.log('status is not good for assignment %s', xhr.status);
break;
default:
//console.log('status is good for assignment %s', xhr.status);
if (xhr.readyState === 4) {// update closure upvars
$url = xhr.url;
parts = Envjs.urlsplit($url);
//console.log('new document baseURI %s', xhr.url);
Envjs.exchangeHTMLDocument($document, xhr.responseText, xhr.url);
}
}
}
};
try{
xhr.send(data, false);//dont parse html
}catch(e){
//failed to load content
console.log('failed to load content %s', e);
Envjs.exchangeHTMLDocument($document, "\
<html><head><title>Error Loading</title></head><body>"+e+"</body></html>\
", xhr.url);
Expand All @@ -208,6 +219,7 @@ Location = function(url, doc, history) {
//Treat as an XMLDocument
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log('exchanging xml content %s', e);
$document = xhr.responseXML;
$document.baseURI = xhr.url;
if ($document.createEvent) {
Expand Down
21 changes: 17 additions & 4 deletions src/xhr/xmlhttprequest.js
Expand Up @@ -42,10 +42,13 @@ XMLHttpRequest.prototype = {
parsedoc = (parsedoc === undefined)?true:!!parsedoc;
redirect_count = (redirect_count === undefined) ? 0 : redirect_count;
function makeRequest(){
var cookie = Envjs.getCookies(_this.url);
var cookie = Envjs.getCookies(_this.url),
redirecting = false;
if(cookie){
_this.setRequestHeader('COOKIE', cookie);
}
if(window&&window.navigator&&window.navigator.userAgent)
_this.setRequestHeader('User-Agent', window.navigator.userAgent);
Envjs.connection(_this, function(){
if (!_this.aborted){
var doc = null,
Expand All @@ -69,7 +72,9 @@ XMLHttpRequest.prototype = {
case 307:
if(_this.getResponseHeader('Location') && redirect_count < 20){
//follow redirect and copy headers
//console.log('following redirect from %s url %s', _this.url, _this.getResponseHeader('Location'));
redirecting = true;
//console.log('following %s redirect %s from %s url %s',
// redirect_count, _this.status, _this.url, _this.getResponseHeader('Location'));
_this.url = Envjs.uri(_this.getResponseHeader('Location'));
//remove current cookie headers to allow the redirect to determine
//the currect cookie based on the new location
Expand All @@ -79,7 +84,14 @@ XMLHttpRequest.prototype = {
if('Cookie2' in _this.headers ){
delete _this.headers.Cookie2;
}
_this.send(data, parsedoc, redirect_count++);
redirect_count++;
if (_this.async){
//TODO: see TODO notes below
Envjs.runAsync(makeRequest);
}else{
makeRequest();
}
return;
}break;
default:
// try to parse the document if we havent explicitly set a
Expand Down Expand Up @@ -110,7 +122,8 @@ XMLHttpRequest.prototype = {
}
}, data);

if (!_this.aborted){
if (!_this.aborted && !redirecting){
//console.log('did not abort so call onreadystatechange');
_this.onreadystatechange();
}
}
Expand Down

0 comments on commit 90b16de

Please sign in to comment.