Archive for the ‘Uncategorized’ Category

Introducing Quizmash

Back in the mists of time I was studying for my Adobe Flash certification, and struggled to find many example questions on the web.

There were a couple of sites that had a few, but they were always limited to the questions the person who made them had written. I thought it would be cool if someone built a site allow anyone to post questions on a subject, so that the pool of available questions would grow over time.

Flash forward a couple of years and behold, Quizmash! It lets users not only post questions, but subjects too, so if you have some knowledge on any subject, technical or trivial, post it here, and hopefully someone will find it useful in their hunt for example questions.

It’s only got a couple of example questions in there to start with, and there’s plenty of features to add, and no doubt bugs to squash, so right now I’m looking for any of you flash geniuses out there to come up with some questions about Actionscript 3, or indeed any other technical subject you can think of to get the ball rolling.

Ok, get mashing!

4 Comments


Building a RESTful webservice client in flash, and fighting all the way.

I’ve recently finished struggling with writing a client for a RESTful webservice in flash, and let me tell you it’s been a long painful process, one which i thought i’d like to share. First a (hopefully not too inaccurate) primer on what REST is all about.

RESTful web services use the full http specification in order to send an recieve data. That is, instead of just using POST and GET like most web applications, they also make use of PUT and DELETE, amongst others. Unlike SOAP webservices in which the body of the http message contains contextual information about a request or response, they use HTTP response codes and headers to convey this information in a much more simple lightweight way. I thoughroughly reccommend RESTful webservices by O’Reilly if you want to learn all about them.

They’re becoming increasingly popular, and so I decided I was going to build one for a current job. After scraping the rust off my php skills I managed to get a fully restful webservice together, and feeling pretty pleased with myself I turned to the easy part. Building a client for said webservice in flash.

Well that turned out not to be the easy part.

Problem 1 – No PUT or DELETE

First up I dicovered that the URLLoader class does not support any http methods other than GET and POST. I suspect that this has something to do with the way flash routes requests through the browser, however REST webbservices can accommodate this limitation (as it’s apparently quite common) by using something called overloaded POST, where the request is always sent as a POST with the real request method embedded as a url variable or in the message header. And here we hit problem two.

Problem 2 – No empty POSTs

When sending a POST request from flash, if there is no body in the request, then it is always sent as a GET. Ouch! Although it is sloppy, I figured I could get around this by putting junk vars in the body to stop this. Now because this webservice is out on the public web, it uses HTTP basic authentication as a security layer. To do this an encoded username/password string is sent in the headers of the request. If the correct key is not sent, the browser issues a 401 Unauthorised http code, to challenge the client and inform it what kind of authentication method is being used. You’ll see this in use when your browser pops up a login dialog for some web pages. Flash 9 and above does not allow some blacklisted headers to be sent to the server. Including, you guessed it…

Problem 3 – No authentication headers allowed

Thats right folks. Flash player 9 does no allow the Authentication:Basic header to be sent. 9.0.115 Allows it but only if the flash cross domain policy file on the server explicitly says so. Even if the swf is on the same server. Now in this case I could modify the cross domain file as it was my own webservice, but what about the thousands of webservices that I don’t control? Anyway I couldnt be sure on which domain the client would sit, so I decided to hack my webservice to look for an alternative custom header if Authentication:Basic wasn’t present. This was a horrible way to work around this, and I had no idea what I would do if I needed to talk to a third party webservice that used basic authentication. The next step was to recieve the response from the server to see if the login was correct, and…..

Problem 4 – No support for http status codes

The http spec defines a whole raft of http status codes for use in communicating the status of a request. 200-ok means success, and i’m sure you’re familiar with 404-not found. The problem is even though the URLLoader class has a status event, with a status code property, it doesnt actually work. First up is the fact that in some browsers all codes other than success are returned to the flash player as a 0, which isn’t actually a status code. Then there’s the fact that if anything other than 200 is recieved, flash fires the status event and stop the data from being recieved. This is even if the status code is not an error, but something such as a 201-created which is a subtype of the 200 success code.

So where next?

Well after banging my head against flashes broken HTTP support, i came across a potential solution in as3httpclientlib. An open source library that uses the AS3 socket classes to emulate the entire http specification. This is an awesome library and an absolute joy to use, in no time I had built my client and all was well with the world. Until I uploaded it.

Problem 5 -Socket policy files

This is a dark realm to be dealing with indeed. The current flash player does not allow socket connections to any server, unless a socket policy file is available, even if the client is in the same domain. Ok I thought i’ll just drop one next to my cross domain file. unfortunately its not that simple. The policy file needs to be served by a socket policy server running on port 843 of the same domain, or via the same socket the request was made to in response to a specific socket call. So basically I was screwed. I didnt have enough access to the server to set this up, and even if I could you need to be a sysadmin wiz to do so. I really dont think Adobe could have made this any more dfficult, and though there are articles on how to do thisĀ  on their site, they seem not be written for the people most likely to need them, flash developers. This means that the beautiful as3httpclient is esentially only useful for AIR development.

Picking myself up.

In the midst of all this a freind of mine had suggested just giving up and writing an rpc style proxy on the client side to talk to. I told him I refused to be beaten (though I later was, curse my foolish pride) but this planted the seed for my solution. What i have basically written is an extension to as3httpclientlib, that altough behaves the same from the users view, actually interprets full HTTP requests into text based POST meta data, that is sent to a php proxy file. This proxy then make the real request to the server, encodes the response in a similar meta format, then returns this to the flash player via URLloader behind the scenes. This is then deserialised into what appears to be a seamless as3httpclientlib response, et voila.

This is all in early alpha of course, but it seems to be working for me so far. The beauty of it being that this should enable flash to talk to ANY restful webservice, without all the pain you can see i’ve been through. Please have a play with it and let me know what you think. And so i humbly present to you, the snappily titled as3httpproxyclientlib. Its up on google code, under the same MIT licence as its parent library. It requires you have the parent library in your classpath, but otherwise, short of uploading the proxy file to a server of your choosing, and passing this url to the client instance, should behave in the same way. It does require you have CURLlib installed on your php server, but i believe thats pretty common, and I’m open to people contributing proxies in other languages and for other server configurations. In fact any contributions at all are welcomed as i’m no php developer and it’s probably leaves huge room for improvement.

Let me know how you get on, and don’t let flash get you down.

2 Comments


Blog rescued

After a lot of painful SQL madness i’ve finally recovered my blog. Hopefully i can manage not to kill it again

No Comments


Blog upgraded

I’ve just upgraded the blog to use the latest release of WordPress. It was a pretty painless experience, and the tutorials on the wordpress site are great.

The release includes a shiny new admin interface, and a load of technical things which i’m assured fix a lot of security issues. The best thing has to be the new preview pane, that lets you view your entry as it will appear in your wordpress template, rather than just formatted as the rest of the admin interface.

No Comments



  • SetPageWidth