How to retrieve the http response headers in a non-AIR application - actionscript-3

Access HTTP response headers in for flash.net.URLLoader object?
I found the above question that seemed to have a solution for an AIR application but not for my non-AIR flex application? Is it even possible?

If it is not possible in native as3 you could parse it yourself. Open a socket connection using the Socket class and connect to the url/domain using port 80.
After you connected you can read out the socket buffer and parse the string to get the response headers.
You can follow the Telnet example on http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b0-181c51321220efd9d1c-8000.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7cf7 and change the port numbers to 80.

I believe I was reaching a little beyond the abilities of a non-air flash application. I wound up retrieving the headers server side and then using the external interface callback functionality to use what I knew about the headers flex-side.

Related

How to send json data over http

I am new to http protocol. When we are sending json message over http to server, How we need to send ?
we need to send the data from different port each time
OR
we can send data form a single port in each time.
If I want to use existing connection to send data in future then whether it is possible or not ?
There is no reason why you would create a TCP socket for each piece of data you want to send — and this has nothing to do with HTTP — and particularly not through a different port each time. In fact, once you hace the socket created and you have connected to the server you should in principle always talk to the server through that socket.
Also, the HTTP protocol uses the port 80, and HTTPS uses 443. That number does not change on demand. Of course you can send HTTP requests through any available port you want and some services even run on special ports using HTTP as the communication protocol but normaly HTTP is 80. See the /etc/services file on linux and read about getaddrinfo().

Why do we need to mention the ws protocol in WebSocket constructor?

I have just started learning about WebSocket recently. As from http://www.websocket.org/ it is mentioned that A WebSocket connection is established by upgrading from the HTTP protocol to the WebSockets protocol during the initial handshake between the client and the server. Again, To connect to an end-point, just create a new WebSocket instance, providing the new object with a URL that represents the end-point to which you wish to connect, as shown in the following example. Note that a ws:// and wss:// prefix are proposed to indicate a WebSocket and a secure WebSocket connection, respectively.
var myWebSocket = new WebSocket("ws://www.websockets.org");
My question is why do I need to include ws or wss in the url when the HTTP server knows that it should upgrade the protocol ? I have gone through few tutorials. In all of those the url argument to the WebSocket constructor is the same as the HTTP server url. Is it because, We first establish connection to that url and then the WebSocket is bound to that connection ? I am a newbie on this. Some clear explanation will be highly appreciated regarding this.
Just like you need to specify http:// or https:// to tell the browser whether to use SSL with HTTP, you need ws:// or wss:// to let it know whether to use SSL with WebSockets.

Connecting to HTML5 Websocket

I'm a bit confused about HTML5 Websockets. I've looked at numerous tutorials out there and a lot of them have different variations of connecting using different ports. What do these ports mean?
Adobe for instance, uses this:
new WebSocket('ws://localhost:1740');
Then another tutorial has this where no ports are required:
new WebSocket("ws://www.websockets.org");
And finally a third tutorial has a port, but it's completely different:
new WebSocket("ws://localhost:8080/echo");
My question would be, why do these vary? How do I know which ports to connect to? Also, I've attempted to do my own connection:
var ws = new WebSocket("ws://test.ontarget-network.com/");
But I get the following error: Unexpected response code: 200
I've tested around and tried connecting to various other "ports" (not knowing what I'm doing obviously, typing in random numbers) and this error would disappear, however, my code
ws.onopen = function(){
alert("Connection Established");
};
would not execute.
I'm trying to fully understand HTML5's Websockets API so I can experiment and create more dynamic applications. Thanks for the help.
The server should have an endpoint that accepts WebSocket connections. So, if that endpoint is /echo you would want to connect to:
ws://localhost:8080/echo/websocket
You will get the Unexpected response code: 200 error if you exclude the /websocket suffix after the endpoint. I was having the same confusion and this link cleared things up a bit for me.
The following comes from the latest WebSocket draft:
By default the WebSocket protocol uses port 80 for regular WebSocket
connections and port 443 for WebSocket connections tunneled over TLS
[RFC2818].
Really though, you should be able to use any valid port not in use. As long as clients are trying to connect to the same port that the server-side script opens for the socket connection, you should be fine.
A quick note on ports:
Port 80 is the HTTP port.
Port 8080 is the alternate HTTP port.
Port 443 is the HTTPS (i.e., HTTP with TLS) port.
Port 1740 in the Adobe code seems like some random port not already in use by other services.
For a full list of preset ports, please see the following:
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
As for your "Unexpected response code: 200" error, I'm guessing that the WebSocket URL you're using on the client side is not pointing to a valid server-side script, but that's hard to comment on without more info.
I had the same issue, But to survive with
Unexpected response code: 200
You need to have either server-side script to handle the web socket, or you can use Node.js to build a you server script.
for the sake of education you can try to biuld your own websocket sever script.
Actually there is something else... You can not open a connection to every port since there is a list of blocked ports in every browser. I remember seeing the full list of ports in 'The tangled Web' from Michal Zalewski; however, I think a quick google will show this also.

NodeJS + HTML5 + Telnet = isitpossible?

I have this project for my classes i'm currently workin' on. here it is:
WebPage client for Telnet not on standard ports, with ability to choose a port and connect
I have machines with telnet servers on them, just waiting for connection.
So my idea was to set up a nodeJS with express server on a dedicated machine. This would handle connections through telnet and host a page for clients, that would use socket.io to exchange information with server side.
But as i'm new to such technologies (telecommunications student) i wonder if it is possible. I spotted something like this - jsterm.com by Peter Nitsch, but i see there are some massive gaps in code and the demo does not really work so i don't know if it actually works. Did anyone try this?
My other problem is - when i send information to nodeJS server through websockets, which seems achievable for me, what do i do with this information? Do i just set up another websocket to pass the same data i got from client websocket directly to the telnet port?
Can sockets connect directly to specific port, without any websocket waiting on the other side?
If my idea is wrong, could anyone help me - maybe there exists some nice solution - i was thinking about Anyterm for example but i see that it requires an apache server and runs completely different technologies...
Just to be clear, WebSocket connections are not raw TCP socket connections. They have extra header information in each packet, browser to server data is masked using a running XOR, etc.
In order for the browser to communicate with a normal TCP server (e.g. a telnet server) you will need some sort of bridge service. It just so happens that such a thing already exists. websockify is a server that accepts WebSocket connections and bridges them to a raw TCP server.
In fact, the websockify project already includes a working telnet client as an example application. However, note that one limitation of websockify (for security reasons) is that the client cannot pick an arbitrary server address/port to connect to. The target address(es) must be predefined, either as a single target specified on the command line for websockify, or as multiple targets specified in a configuration file (and selected via a token in the WebSocket connect string).
There are multiple implementations of websockify in different languages (python, C, node, ruby, Clojure) however, only the python version currently supports multiple targets via a configuration file.
Disclaimer: I created websockify.

node.js - communicate with TCP server (data == JSON)

I'm new to node.js, dived into it during the last weekend and had fun with various examples and small tutorials.
Now I'd like to start a little project for my local area network and have a few questions to get myself into the right direction.
Setup:
I've got a server service running on my lan. It's possible to communicate with that service via TCP and/or HTTP (it's possible to enable or disable TCP or HTTP or both of them) on a specific port and sends and receives data via JSON on a request.
What I basically want to do is to create a webinterface based on node.js for that service, to receive and send JSON data with a webbrowser from and to that service.
Problem:
I already do know how to setup an http server based on node.js. But right now I'm stuck in finding an idea how to create a client based on node.js which stands between the service and the webbrowser client to pass through data from client to server and vise versa. Something like a router or proxy.
Here is a basic scheme based on the client's (webbrowser) point of view:
Send: Webbrowser requests -> node.js routes -> service receives
Receive: Webbrowser receives <- node.js routes <- service responds
Questions:
- Go for TCP or HTTP? (maybe disabling the HTTP Server would spare some ressources) - maybe already answered by this post
- Are there any node.js packages that would fit my needs?
- Go for a framework (expressions?) or would plain node.js be just enough?
- Any hints appreciated :)
edit:
- Is it possible to bind a network device like eth0 inside node.js instead of defining the ip address?
Thanks for your help && best regards
cefra
There's no reason why you can't have a REST HTTP service.
Use something like express to handle routing.
If I understand your problem correctly then you have a webservice written in "foobar" on a TCP port somewhere you can connect to with node.
So if your using express you would write something like
app.get("/resources/", function(req, res) {
var socket = new net.Socket();
socket.connect(port, host, function() {
socket.on("data", function(json) {
res.contentType("json");
res.send(json);
socket.end();
});
socket.write(...);
});
});
So basically you've written a http middleman that contacts your service over TCP then writes the data down the response of your HTTP request.