Sunday, June 10, 2007

Fun with Sockets and Port 80.

So the problem is you want to connect to port 80 with a socket connection. Now you would think this would be simple, but of course it is not. To connect to port 80 or for that matter any port below port 1024 you need to first connect the xmlsocket:// protocol, which requires a different type of server than an http server such as apache. Before we get real deep into this I will briefly explain the rules. Or should I say describe them as I understand them.

1. To load any type of content from a domain, you must first load a crossdomain.xml file, which tells flash which sites the site it is connecting to allows. An example would be.

cross-domain-policy
allow-access-from domain="mysite.com" ports="80"
/allow-access-from
/cross-domain-policy


so this policy file allows flash content from mysite.com to load content and it also says it allows connections from there to connect to port 80 with to-ports

2. Socket connections can load a crossdomain.xml file over standard http through a socket connection on any port over 1024, but the to-ports property will only allow ports above 1024 if the port connected to is above 1024.

3. To connect to a port below 1024 you must load the crossdomain.xml file from a port below 1024, and to do this you must use the xmlsocket protocol

Example:

Security.loadPolicyFile(xmlsocket://mysite.com/crossdomain.xml);



This line is not necessary unless you would like to specify the crossdomain.xml file location. By default the socket connection will request it from the root.

4. After the connection is made the socket will load the crossdomain.xml file and it will see that it can now access port 80 or whichever port you are trying to allow below 1024.


Conclusions:
Connecting to a port below 1024 is way too complicated. I do not understand why it is necessary to load the policy file over a different protocol. This just results in having to support yet another service, when the only reason for it is just simply to load an xml file. I understand there are security precautions there and that it is the main reason that this exists. I just feel that this could have been handled in a much more graceful or possibly better documented way.

Friday, June 1, 2007

Dynamic Object Instantiation

So you would think this would be pretty simple. An eval here an eval there and you would be done right? Nope. The reason this is difficult is because flash does not import a class even though you have the import statement in the actionscript unless the class is actually used. So the solution is to instantiate a dummy object, which will then force flash to import the class that is necessary to perform the eval().


import test;

var dummy:test = new test(); //flash does not import the class unless it is instantiated, so the eval will fail without this line

var func = eval("test");
trace(typeof(func)); //func is equal to a function, or the test class constructor

var obj = new func;
trace(typeof(obj)); //obj is equal to the object type of test

Thursday, May 31, 2007

Hannibal Rising

Tonight we watched Hannibal Rising. The movie was one of the best movies i have seen in a while. The shots and artistic feel to it was awesome. So many movies today are such crap, but this one caught my attention. It is definitely worth renting. I'm pretty sure it's only available at Blockbuster. Renee and I decided that it is much better than Hannibal.

Garage Sale Tomorrow

Renee and I will be having a garage sale on Saturday. We are going to sell the pile of crap that has built up over the last 3 years of our lives. Its amazing how much junk or unusable things a person can build up over the years. Our friends Wyoming and Todd will be coming over to help as well. They brought a bunch of their stuff over last night. It should be pretty fun. I hope we get rid of all this stuff......

POST variables. Where do they go?

So the problem is that GET variables are sent through the header, which means there is a size limitation. This poses a problem when the length of the variables you are trying to send in is around 8000 characters long. So you have to use POST to make it work correctly. And if you need to also authenticate while sending post variables you will also have to worry about sending everything through a socket connection and writing your own headers once again. They should look like this:



POST /apis/script.php HTTP/1.0
Host: jordan.dev.com
Authorization: Basic MTE6Y2FybHNleBLAHWxscw==
Content-type: application/x-www-form-urlencoded
Content-Length: 8895

var1=something&var2=somethingelse


The Content-type and Content-Length lines are the important headers. While the var1=something&var2=somethingelse placement is also important. It is in the body of the request, not in the header.

HTTP Headers can be FUN!

So here is the dilemma. You want to authenticate against standard http authentication and you want to use flash and actionscript 3.0 to do it. So you go into the help and find out that this simple thing just is not possible. So you google around for about an hour and find out that one person seems to have written something that will get you moving in the direction you need to be going.

The simple explanation of how you get through http authentication using flash is that you open a socket connection and write the headers yourself. Something like this.


__socket = new Socket();
__headers = new Array();

var bArray:ByteArray = new ByteArray();
bArray.writeUTFBytes(username + ":" + password);
__auth64 = Base64.Encode(bArray);

//by default we are adding the authentication header
addHeader("Authorization","Basic " + __auth64);

__socket.connect(__httpServer, __httpPort);

So your headers will look something like this before you send them in using __socket.writeUTFBytes(header_var);


POST /apis/script.php HTTP/1.0
Host: jordan.dev.com
Authorization: Basic MTE6Y2FybHNleSMTYWxscw==



The (Authorization: Basic base64encodedstring) is the important part here. This is the part of the header that will allow the authorization to take place without presenting the ugly standard login box that would normally appear.

Web-Sniffer was so helpful in figuring out what the headers needed to look like. It shows the request and response headers in http 1.0 and 1.1.

Overall this experience was awesome, and there are just tons of things that can be done using headers.

Tuesday, April 10, 2007

Magic the Gathering

Ok so far this has to be the geekiest post I have yet. This is worse than the Killer Bunnies One. So, I got into playing Magic the Gathering again, which is good and bad. Its good, because it gives me something to do and think about, and allows me to have a hobby that doesn't involve a computer or moving at dangerously fast speeds. Its bad, because it costs quite a bit to play and probably some other random reasons that I cant think of right now. This Friday I plan on going to play "Friday Night Magic", which should be pretty fun. This will be the first time I have ever played with random people. I also have 2 boxes of the latest expansions, Planer Chaos and Time Spiral on order. After those get here I will get to start building a Type II deck and hopefully get to enter a tournament to play in. Well thats it for now. More after Friday I suppose.