Tuesday, May 24, 2011

XHR Over SPDY

‹prev | My Chain | next›

I continue today working through some unexpected behavior observed when looking at SPDY requests and SPDY responses in detail. With the SETTINGS frame more-or-less answered (Chrome is broke), I move onto two homepage requests for the node-spdy sample app:



When I first looked into it, I also noted a "bizarre" plain text server response immediately after a second home page request
request »»»
70 7.122965 127.0.0.1 127.0.0.1 HTTP Continuation or non-HTTP traffic
0000 00 00 00 05 01 00 00 12 68 65 6c 6c 6f 20 66 72 ........ hello fr
0010 6f 6d 20 73 65 72 76 65 72 21 om serve r!
This is a non-compressed data frame from the client. It says "hello from server!" in the packet, which shows up on the displayed page, but this is definitely going from the client to the server (port 8081):



Before digging deeper, I check another conversation to find:



Another request from the client to the server, but... I find just a little later:



That really is odd. In this case the server really did respond with "hello from server!"

In the first sample conversation, there was one packet which, for some unknown reason, Wireshark was not able to decrypt. It would seem that the packet in question was the server responding with "hello from server!" That aside, I need to see what is going on here, so I ack through the node-spdy code for "hello from server" to find it is defined in pub/index.html:
      var xhr = new XMLHttpRequest();

xhr.open('POST', '/', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
document.getElementById('content').innerHTML = xhr.responseText;
}
};
xhr.send("hello from server!");
Nice. Old school XHR.

That snippet opens the root resource on the node-spdy test server for an asynchronous (the true param) POST. When the XHR request is complete, the response is put inside the element with ID of "content". Lastly, it sends the XHR off to the server. I would guess that the server simply echos POSTs back, but why guess?

From the node-spdy examples/spdy-server.js:
var server = spdy.createServer(options, function(req, res) {
if (req.method == 'POST') {
res.writeHead(200);
req.pipe(res);
return;
}

static(req, res, function() {
res.writeHead(404);
res.end();
});
});
Yup, for a POST, respond with a 200/OK and write the request back to the client.

Ooh! Nice pipe() in there. Rather than read the request payload and then explicitly writing it back out to the response stream, the example server pipes the request stream into the response stream.

Good stuff and another minor mystery solved. Up tomorrow I will either investigate server push or compressing html/js/css.


Day #30

No comments:

Post a Comment