Merge pull request #531 from shiloa/master

Fix crash (segfault) on node.js if canvas loads images with secure url (https://...)
This commit is contained in:
Juriy Zaytsev 2013-04-04 04:55:14 -07:00
commit 9e10349558
3 changed files with 205 additions and 184 deletions

373
dist/all.js vendored
View file

@ -4486,7 +4486,7 @@ fabric.util.string = {
};
/**
* Takes url corresponding to an SVG document, and parses it into a set of fabric objects
* Takes url corresponding to an SVG document, and parses it into a set of fabric objects. Note that SVG is fetched via XMLHttpRequest, so it needs to conform to SOP (Same Origin Policy)
* @method loadSVGFromURL
* @memberof fabric
* @param {String} url
@ -5524,183 +5524,183 @@ fabric.Shadow = fabric.util.createClass(/** @scope fabric.Shadow.prototype */ {
})(typeof exports !== 'undefined' ? exports : this);
(function(global) {
"use strict";
/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
var fabric = global.fabric || (global.fabric = { });
if (fabric.Intersection) {
fabric.warn('fabric.Intersection is already defined');
return;
}
/**
* Intersection class
* @class Intersection
* @memberOf fabric
*/
function Intersection(status) {
if (arguments.length > 0) {
this.init(status);
}
}
fabric.Intersection = Intersection;
fabric.Intersection.prototype = /** @scope fabric.Intersection.prototype */ {
/**
* Constructor
* @method init
* @param {String} status
*/
init: function (status) {
this.status = status;
this.points = [];
},
/**
* Appends a point to intersection
* @method appendPoint
* @param {fabric.Point} point
*/
appendPoint: function (point) {
this.points.push(point);
},
/**
* Appends points to intersection
* @method appendPoints
* @param {Array} points
*/
appendPoints: function (points) {
this.points = this.points.concat(points);
}
};
/**
* Checks if one line intersects another
* @static
* @method intersectLineLine
* @param {fabric.Point} a1
* @param {fabric.Point} a2
* @param {fabric.Point} b1
* @param {fabric.Point} b2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectLineLine = function (a1, a2, b1, b2) {
var result,
ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x),
ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x),
u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if (u_b !== 0) {
var ua = ua_t / u_b,
ub = ub_t / u_b;
if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1) {
result = new Intersection("Intersection");
result.points.push(new fabric.Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y)));
}
else {
result = new Intersection("No Intersection");
}
}
else {
if (ua_t === 0 || ub_t === 0) {
result = new Intersection("Coincident");
}
else {
result = new Intersection("Parallel");
}
}
return result;
};
/**
* Checks if line intersects polygon
* @method intersectLinePolygon
* @static
* @param {fabric.Point} a1
* @param {fabric.Point} a2
* @param {Array} points
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectLinePolygon = function(a1,a2,points){
var result = new Intersection("No Intersection"),
length = points.length;
for (var i = 0; i < length; i++) {
var b1 = points[i],
b2 = points[(i+1) % length],
inter = Intersection.intersectLineLine(a1, a2, b1, b2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* Checks if polygon intersects another polygon
* @method intersectPolygonPolygon
* @static
* @param {Array} points1
* @param {Array} points2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectPolygonPolygon = function (points1, points2) {
var result = new Intersection("No Intersection"),
length = points1.length;
for (var i = 0; i < length; i++) {
var a1 = points1[i],
a2 = points1[(i+1) % length],
inter = Intersection.intersectLinePolygon(a1, a2, points2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* Checks if polygon intersects rectangle
* @method intersectPolygonRectangle
* @static
* @param {Array} points
* @param {Number} r1
* @param {Number} r2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectPolygonRectangle = function (points, r1, r2) {
var min = r1.min(r2),
max = r1.max(r2),
topRight = new fabric.Point(max.x, min.y),
bottomLeft = new fabric.Point(min.x, max.y),
inter1 = Intersection.intersectLinePolygon(min, topRight, points),
inter2 = Intersection.intersectLinePolygon(topRight, max, points),
inter3 = Intersection.intersectLinePolygon(max, bottomLeft, points),
inter4 = Intersection.intersectLinePolygon(bottomLeft, min, points),
result = new Intersection("No Intersection");
result.appendPoints(inter1.points);
result.appendPoints(inter2.points);
result.appendPoints(inter3.points);
result.appendPoints(inter4.points);
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
(function(global) {
"use strict";
/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
var fabric = global.fabric || (global.fabric = { });
if (fabric.Intersection) {
fabric.warn('fabric.Intersection is already defined');
return;
}
/**
* Intersection class
* @class Intersection
* @memberOf fabric
*/
function Intersection(status) {
if (arguments.length > 0) {
this.init(status);
}
}
fabric.Intersection = Intersection;
fabric.Intersection.prototype = /** @scope fabric.Intersection.prototype */ {
/**
* Constructor
* @method init
* @param {String} status
*/
init: function (status) {
this.status = status;
this.points = [];
},
/**
* Appends a point to intersection
* @method appendPoint
* @param {fabric.Point} point
*/
appendPoint: function (point) {
this.points.push(point);
},
/**
* Appends points to intersection
* @method appendPoints
* @param {Array} points
*/
appendPoints: function (points) {
this.points = this.points.concat(points);
}
};
/**
* Checks if one line intersects another
* @static
* @method intersectLineLine
* @param {fabric.Point} a1
* @param {fabric.Point} a2
* @param {fabric.Point} b1
* @param {fabric.Point} b2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectLineLine = function (a1, a2, b1, b2) {
var result,
ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x),
ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x),
u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if (u_b !== 0) {
var ua = ua_t / u_b,
ub = ub_t / u_b;
if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1) {
result = new Intersection("Intersection");
result.points.push(new fabric.Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y)));
}
else {
result = new Intersection("No Intersection");
}
}
else {
if (ua_t === 0 || ub_t === 0) {
result = new Intersection("Coincident");
}
else {
result = new Intersection("Parallel");
}
}
return result;
};
/**
* Checks if line intersects polygon
* @method intersectLinePolygon
* @static
* @param {fabric.Point} a1
* @param {fabric.Point} a2
* @param {Array} points
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectLinePolygon = function(a1,a2,points){
var result = new Intersection("No Intersection"),
length = points.length;
for (var i = 0; i < length; i++) {
var b1 = points[i],
b2 = points[(i+1) % length],
inter = Intersection.intersectLineLine(a1, a2, b1, b2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* Checks if polygon intersects another polygon
* @method intersectPolygonPolygon
* @static
* @param {Array} points1
* @param {Array} points2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectPolygonPolygon = function (points1, points2) {
var result = new Intersection("No Intersection"),
length = points1.length;
for (var i = 0; i < length; i++) {
var a1 = points1[i],
a2 = points1[(i+1) % length],
inter = Intersection.intersectLinePolygon(a1, a2, points2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* Checks if polygon intersects rectangle
* @method intersectPolygonRectangle
* @static
* @param {Array} points
* @param {Number} r1
* @param {Number} r2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectPolygonRectangle = function (points, r1, r2) {
var min = r1.min(r2),
max = r1.max(r2),
topRight = new fabric.Point(max.x, min.y),
bottomLeft = new fabric.Point(min.x, max.y),
inter1 = Intersection.intersectLinePolygon(min, topRight, points),
inter2 = Intersection.intersectLinePolygon(topRight, max, points),
inter3 = Intersection.intersectLinePolygon(max, bottomLeft, points),
inter4 = Intersection.intersectLinePolygon(bottomLeft, min, points),
result = new Intersection("No Intersection");
result.appendPoints(inter1.points);
result.appendPoints(inter2.points);
result.appendPoints(inter3.points);
result.appendPoints(inter4.points);
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
})(typeof exports !== 'undefined' ? exports : this);
(function(global) {
@ -6477,6 +6477,7 @@ fabric.Shadow = fabric.util.createClass(/** @scope fabric.Shadow.prototype */ {
*/
_onObjectRemoved: function(obj) {
this.fire('object:removed', { target: obj });
obj.fire('removed');
},
/**
@ -17201,16 +17202,26 @@ fabric.Image.filters.Pixelate.fromObject = function(object) {
var DOMParser = new require('xmldom').DOMParser,
URL = require('url'),
HTTP = require('http'),
HTTPS = require('https'),
Canvas = require('canvas'),
Image = require('canvas').Image;
/** @private */
function request(url, encoding, callback) {
var oURL = URL.parse(url),
req = HTTP.request({
var oURL = URL.parse(url);
// detect if http or https is used
if ( !oURL.port ) {
oURL.port = ( oURL.protocol.indexOf('https:') === 0 ) ? 443 : 80;
}
// assign request handler based on protocol
var req_handler = ( oURL.port === 443 ) ? HTTPS : HTTP;
var req = req_handler.request({
hostname: oURL.hostname,
port: oURL.port || 80,
port: oURL.port,
path: oURL.pathname,
method: 'GET'
}, function(response){

BIN
dist/all.min.js.gz vendored

Binary file not shown.

View file

@ -7,16 +7,26 @@
var DOMParser = new require('xmldom').DOMParser,
URL = require('url'),
HTTP = require('http'),
HTTPS = require('https'),
Canvas = require('canvas'),
Image = require('canvas').Image;
/** @private */
function request(url, encoding, callback) {
var oURL = URL.parse(url),
req = HTTP.request({
var oURL = URL.parse(url);
// detect if http or https is used
if ( !oURL.port ) {
oURL.port = ( oURL.protocol.indexOf('https:') === 0 ) ? 443 : 80;
}
// assign request handler based on protocol
var req_handler = ( oURL.port === 443 ) ? HTTPS : HTTP;
var req = req_handler.request({
hostname: oURL.hostname,
port: oURL.port || 80,
port: oURL.port,
path: oURL.pathname,
method: 'GET'
}, function(response){