Skip to content

Commit

Permalink
Removed iframe.js inclusion.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Dec 13, 2010
1 parent 01aa09c commit 432073c
Showing 1 changed file with 261 additions and 25 deletions.
286 changes: 261 additions & 25 deletions src/files.c
Expand Up @@ -8,6 +8,8 @@
#include "socket.h"
#include "mem.h"

static char* iframe_get_buffer(size_t *len);

/**
* Return generic page for iframe inclusion
*/
Expand All @@ -17,33 +19,9 @@ file_send_iframe(struct connection *cx) {
static char *iframe_buffer = NULL;
static size_t iframe_buffer_len;

struct stat st;
int ret, fp;
size_t remain;
const char filename[] = "iframe.js";

/* read iframe file, the first time. */
if(iframe_buffer == NULL) {
ret = stat(filename, &st);
if(ret != 0) {
return;
}
iframe_buffer_len = st.st_size;
if(!(iframe_buffer = rcalloc(iframe_buffer_len, 1))) {
iframe_buffer_len = -1;
return;
}
remain = iframe_buffer_len;
fp = open(filename, O_RDONLY);
while(remain) {
int count = read(fp, iframe_buffer + iframe_buffer_len - remain, remain);
if(count <= 0) {
rfree(iframe_buffer);
iframe_buffer_len = -1;
return;
}
remain -= count;
}
iframe_buffer = iframe_get_buffer(&iframe_buffer_len);
}

char buffer_start[] = "<html><body><script type=\"text/javascript\">\ndocument.domain=\"";
Expand Down Expand Up @@ -143,3 +121,261 @@ file_send(struct connection *cx) {

return 0;
}

static char*
iframe_get_buffer(size_t *len) {

char *out = "\n\
if(typeof XMLHttpRequest == \"undefined\") {\n\
XMLHttpRequest = function () {\n\
try { return new ActiveXObject(\"Msxml2.XMLHTTP.6.0\"); }\n\
catch (e1) {}\n\
try { return new ActiveXObject(\"Msxml2.XMLHTTP.3.0\"); }\n\
catch (e2) {}\n\
try { return new ActiveXObject(\"Msxml2.XMLHTTP\"); }\n\
catch (e3) {}\n\
try { return new ActiveXObject(\"Microsoft.XMLHTTP\"); }\n\
catch (e4) {}\n\
throw new Error(\"This browser does not support XMLHttpRequest.\");\n\
};\n\
}\n\
\n\
/**\n\
* Cuts the first part of a message and returns it. What's after is discarded.\n\
* if msg is '[abc][def]', this function will return '[abc]' only.\n\
* if msg is '[\"abc]ef\"]gh', this function will return '[\"abc]ef\"]': characters between quotes are preserved.\n\
*/\n\
function cutMessage(msg) {\n\
\n\
if(msg.length < 2 || msg.charAt(0) != '[') {\n\
return \"\";\n\
}\n\
var pos;\n\
var depth = 0;\n\
var in_string = false;\n\
var len = msg.length;\n\
\n\
for(pos = 0; pos < len; pos++) {\n\
if(msg.charAt(pos) == '\"') {\n\
in_string = !in_string;\n\
continue;\n\
}\n\
if(in_string) { // do not consider characters that are part of a string.\n\
continue;\n\
}\n\
// opening and closing brackets, check for depth.\n\
if(msg.charAt(pos) == '[') {\n\
depth++;\n\
} else if(msg.charAt(pos) == ']') {\n\
depth--;\n\
}\n\
\n\
if(depth == 0) {\n\
return msg.substr(0, pos+1);\n\
}\n\
}\n\
\n\
return \"\";\n\
\n\
}\n\
\n\
function CometClient(host){\n\
\n\
this.host = host;\n\
this.pos = 0;\n\
this.seq = 0;\n\
\n\
this.hasWebSocket = (typeof(WebSocket) != \"undefined\");\n\
\n\
// detect if client can stream data using \"xhr.readyState=3\" or not\n\
this.canStream = 1;\n\
this.isIE = false;\n\
var noStream = new Array(\"Chrome\", \"WebKit\", \"KHTML\", \"MSIE\");\n\
for(i = 0; i < noStream.length; i++) {\n\
if(navigator.appVersion.indexOf(noStream[i]) != -1) {\n\
this.canStream = 0;\n\
break;\n\
}\n\
}\n\
if(navigator.appVersion.indexOf(\"MSIE\") != -1) {\n\
this.isIE = true;\n\
}\n\
\n\
this.disconnect = function() {\n\
if(this.hasWebSocket) {\n\
return this.wsDisconnect();\n\
}\n\
window.clearTimeout(this.reconnectionTimeout); // cancel reconnect.\n\
\n\
try {\n\
this.xhr.onreadystatechange = function(){};\n\
this.xhr.abort();\n\
} catch(e) {}\n\
}\n\
\n\
this.send = function(channel, msg) {\n\
\n\
var url = \"http://\" + this.host + \"/publish?name=\"+channel+\"&data=\"+msg;\n\
var xhr = new XMLHttpRequest;\n\
xhr.open(\"get\",url, true);\n\
xhr.send(null);\n\
}\n\
\n\
this.connect = function(channel, onMsg, seq) {\n\
\n\
if(typeof(seq) != \"undefined\") {\n\
this.seq = seq;\n\
}\n\
\n\
if(this.hasWebSocket) {\n\
return this.wsConnect(channel, onMsg);\n\
} else {\n\
return this.streamingConnect(channel, onMsg);\n\
}\n\
}\n\
\n\
this.wsConnect = function(channel, onMsg) {\n\
this.socket = new WebSocket('ws://'+this.host+'/websocket?name='+channel);\n\
this.socket.onopen = function () {\n\
};\n\
this.socket.onmessage = function (messageEvent) {\n\
try {\n\
var obj = JSON.parse(messageEvent.data);\n\
if(obj[0] == \"msg\") {\n\
onMsg(obj[1]);\n\
}\n\
} catch(e) {\n\
}\n\
};\n\
this.socket.onclose = function () {\n\
var comet = this;\n\
try {\n\
window.setTimeout(function() {\n\
comet.wsConnect(channel, onMsg);\n\
}, 1000);\n\
} catch(e) {}\n\
};\n\
}\n\
\n\
this.wsDisconnect = function() {\n\
this.socket.onclose = function() {}; // don't reconnect.\n\
this.socket.close();\n\
}\n\
\n\
this.streamingConnect = function(channel, onMsg) {\n\
\n\
var comet = this;\n\
\n\
if(comet.reconnectionTimer) {\n\
window.clearTimeout(comet.reconnectionTimer);\n\
}\n\
\n\
// prepare XHR\n\
var url = \"http://\"+this.host+\"/subscribe\";\n\
var params = \"name=\"+channel+\"&keep=\"+this.canStream;\n\
if(this.seq >= 0) {\n\
params += \"&seq=\"+this.seq;\n\
}\n\
\n\
comet.xhr = new XMLHttpRequest;\n\
comet.pos = 0; /* counts how much we've parsed so far */\n\
comet.xhr.open(\"POST\", url, true);\n\
\n\
comet.xhr.onreadystatechange = function() { /* callback for the reception of messages */\n\
\n\
if(comet.xhr.readyState != 4 && comet.canStream == 0) { /* not a full message yet. probably going through states 1 & 2 */\n\
return; // wait for state 4.\n\
}\n\
\n\
if(comet.xhr.readyState == 4 && comet.xhr.status != 200) { // error, reconnect\n\
try {\n\
comet.reconnectTimer = window.setTimeout(function() {\n\
comet.connect(channel, onMsg);\n\
}, 1000);\n\
} catch(e) {}\n\
return;\n\
}\n\
\n\
if(comet.xhr.readyState === 3 || comet.xhr.readyState == 4) { // got data!\n\
\n\
var data = comet.xhr.responseText;\n\
\n\
if(data.length == 0) { // empty response, reconnect.\n\
try {\n\
comet.reconnectTimer = window.setTimeout(function() {\n\
comet.connect(channel, onMsg);\n\
}, 1000); // reconnect soon.\n\
} catch(e) {}\n\
return;\n\
}\n\
\n\
// try parsing message\n\
do {\n\
// this might only be the first part of our current packet.\n\
var msg = cutMessage(data.substr(comet.pos));\n\
if(msg.length) try {\n\
var obj;\n\
if(comet.isIE) {\n\
obj = eval(\"(\"+msg+\")\");\n\
} else {\n\
obj = JSON.parse(msg);\n\
}\n\
comet.pos += msg.length; // add to what we've read so far.\n\
\n\
if(obj[1] && obj[1].seq) { // new timestamp\n\
comet.seq = obj[1].seq;\n\
if(comet.canStream == 0) {\n\
// console.log(\"new seq:\", comet.seq);\n\
}\n\
}\n\
\n\
switch(obj[0]) {\n\
case 'msg': // regular message\n\
if(obj[1].channel == channel) {\n\
if(onMsg) {\n\
try {\n\
onMsg(obj[1]);\n\
} catch(e) {} // ignore client errors\n\
}\n\
}\n\
break;\n\
}\n\
} catch(e) {\n\
comet.xhr.abort(); // avoid duplicates upon reconnection\n\
try{\n\
comet.reconnectTimer = window.setTimeout(function() {\n\
comet.connect(channel, onMsg);\n\
}, 1000); // reconnect soon.\n\
} catch(e) {}\n\
return;\n\
}\n\
\n\
if(comet.pos < data.length) {\n\
break;\n\
}\n\
} while(comet.pos < data.length);\n\
// there might be more in this event: consume the whole string.\n\
}\n\
if(comet.xhr.readyState == 4) { // reconnect\n\
// if no streaming capability, reconnect directly.\n\
if(this.canStream) {\n\
try {\n\
comet.reconnectTimer = window.setTimeout(function() {\n\
comet.connect(channel, onMsg);\n\
}, 1000);\n\
} catch(e) {}\n\
return;\n\
} else {\n\
comet.connect(channel, onMsg);\n\
}\n\
}\n\
\n\
}\n\
comet.xhr.send(params);\n\
}\n\
};\n\
";
*len = strlen(out);
return out;

}

0 comments on commit 432073c

Please sign in to comment.