Article NWYZ CodeSOD: MacGyver's XMLHTTPRequest

CodeSOD: MacGyver's XMLHTTPRequest

by
Remy Porter
from The Daily WTF on (#NWYZ)

In these days of browser standards, it's easy to forget that once upon a time, simple tasks like an HTTP request from JavaScript were difficult or even impossible, and if you wanted it to work in every browser, you were going to have to write wrappers to try and create a consistent API.

Zeke inherited an application back from those bad old days. It needs to poll a server, and based on the response, it performs an action, but it does this in an "inventive" way.

First, they fashion a rudimentary enum:

 var Response = { nothing: 1, showInviteNewWin: 2, showInviteSameWin: 3, pushPageSamewin: 4, pushPageNewWin: 5, inviteTimeOut: 6, resendInfo: 7, neverInvite: 8, stop: 9, notCurrentPage: 10 };

They're going to use that in a moment to parse the server's response, but first- how are they going to send a request?

 var na_pro_img = null; function sendVisitInfo() { setCurrentPageId(); na_pro_img = null; na_pro_img = new Image(); na_pro_img.onload = checkResponse; /*imageLoaded;*/ var d = new Date(); na_pro_img.src = prefix + 'Visitor.aspx?Cmd=1&TITLE=' + escape((document.title != "") ? document.title : document.location) + '&REFERRER=' + escape(document.referrer) + '&LASTVISIT=' + escape(lastVisitCookie) + '&LASTINVITE=' + escape(lastInviteCookie) + '&RSND=' + resendCount + '&CUSTOMVARIABLES=' + escape(custProp) + '&LT=' + d.getTime() + ((neverCookie != null) ? ('&NEVERINVITE=' + escape(neverCookie)) : "") + '&cpId=' + curPageID; d = null; }

Well, that will generate a request, I suppose, with the SRC property passing a bunch of stuff in the URL parameters. But how on Earth are they parsing anything useful out of the response? And what does this have to do with their rudimentary enum?

 function checkResponse() { switch (na_pro_img.width) { case Response.nothing: break; case Response.showInviteNewWin: sessWin = 'n'; popInvite(); break; // more cases cut for brevity } }

They use the width. The width of the image is matched against their "enum". Through modern eyes, this is the sort of thing that makes you want to go WTF, but for those of us that had the misfortune to do web development back in the days when JavaScript was a novelty and autoplaying MIDI files was the coolest thing you could have on your page"

notevenmad.jpg

scout%2050x50.png[Advertisement] Scout is the best way to monitor your critical server infrastructure. With over 90 open source plugins, robust alerting, beautiful dashboards and a 5 minute install - Scout saves youvaluable engineering time. Try the server monitoring you'll today.Your first 30 days are free on us. Learn more at Scout. TheDailyWtf?d=yIl2AUoC8zA2BK1p4vYQUQ
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments