Is there a way to curl (or something similar) a phoenix socket?
Let's say my server is running on localhost:4000, and my endpoint has:
defmodule MyApp.Endpoint do
use Phoenix.Endpoint, otp_app: :my_app
socket "/socket", MyApp.UserSocket
I have a UserSocket module with a connect method. What can I run from the command line to connect to this socket?
cURL supports by default long polling. You don't have to add any flags, just hit websocket url but you have to use http(s) not ws(s) protocol in url. Don't forget to add Accept and content type headers as application/json.
There is one drawback. You can't POST message to same connection :) so I guess it is better to use telnet instead and code by hand header to initiate polling so you can send (post) messages from same console. this way you can debug if "socket" is returning ok reply for your client pushes. and what is event better with this approach you can actually use HTTP 101 protocol upgrade to ws(s) :)
Related
I am using a web server called lighttpd and they have a command line option -1 to accept a request on the stdin socket. I am fairly new to sockets and such, and their documentation does not say much about the syntax or examples of this options. Would I be able to send a simple http request through this option? And how would I do this?
Breif Description of what I am trying to accomplish. So I am working with Crestrons Simpl+ software. My job is to create a module for a sound masking system called QT Pro. Now, QT Pro has an API where you can control it via HTTP. I need a way to establish a connection with the QT Pro via HTTP( I have everything I need, IP, Username, Password).
Whats the problem? I have just started working with this language. Unfortunately there isn't as much documentation as I would like, otherwise I wouldn't be here. I know I need to create a socket connection via TCP on port 80. I just don't know what I'm supposed to send through it.
Here is an example:
http://username:password#address/cmd.htm?cmd=setOneZoneData&ZN=Value&mD=Value
&mN=Value&auxA=Value&auxB=Value&autoR=Value
If I were to put this into the URL box, and fill it in correctly. then it would change the values that I specify. Am I supposed to send the entire thing? Or just after cmd.htm? Or is there some other way I'm supposed to send data? I'd like to stay away from the TCP/IP Module so I can keep this all within the same module.
Thanks.
You send
GET /cmd.htm?cmd=setOneZoneData&ZN=Value&mD=Value&mN=Value&auxA=Value&auxB=Value&autoR=Value HTTP/1.1
Host: address
Connection: close
(End with a couple of newlines.)
If you need to use HTTP basic authentication, then also include a header like
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
where the gibberish is the base64-encoded version of username:password.
But surely there is some mechanism for opening HTTP connections already there for you? Just blindly throwing out headers like this and hoping the response is what you expect is not robust, to say the least.
To see what is going on with your requests and responses, a great tool is netcat (or telnet, for that matter.)
Do nc address 80 to connect to server address on port 80, then paste your HTTP request:
GET /cmd.htm HTTP/1.1
Host: address
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Connection: close
and see what comes back. SOMETHING should come back. (Remember to terminate with two newlines.)
To see what requests your browser is sending when you do something that works, you can listen like this: nc -l -p 8080.
Then direct your browser to localhost:8080 with the rest of the URL as before, and you'll see the request that was sent. (Then you can type back to see how the browser handles the response.)
In the Java ME platform that I am using I have only SocketConnection class to connect to a distant server. For example I can create a connection to a server socket on the given ip using Connector.open("socket://"+ip+":"+port).
What I want is to implement an HttpConnection that will use http protocol, a url and port 80 as parameters. I imagine it will be something like Connector.open("http://www.google.com:80) to be able to send GET, POST, DELETE, PUT requests to the server afterwards.
Is there a way to get the ip via url address and then connect to this ip using http protocol? Or if it isn't supported by my platform, then I have to stick to using sockets? I know my question can seem a little abstract, so ask if you need any clarifications.
You have to stick to using sockets. If the Java ME platform that you are using have only SocketConnection, a call to Connector.open("http://www.google.com:80") will throw an exception.
I'm trying everything to NAT traversal to make a HTTP(or others) server be accessible from internet.
this is the previous question but with no luck.
HTTP Server behind NATs
So I'm trying to do the following
IE <--> agentC <---------NAT/Internet/.....----------->agentS<------->Apache Server
the scenario might be...
1.User input address in IE like "localhost:9999" (agentC)
2.agentC connect with agentS with Stun/TURN/ICE
3.agentS relay data to Apache Server and then reply to client.
I also refer to the following:
Is it possible to 'relay' a socket?
but the problem is:
1.the connection between agentC to agentS might be UDP, however the Http is on TCP, is it possible to "relay socket or packet"
2.I'm coding test code of agentS<---->Apache part,
((pp = popen("echo -e \"GET / HTTP/1.0\\n\\n\\n\"| nc localhost 80", "r")) == NULL)
.........
But the out put always "400 Bad Request".
(while type "echo -e "GET / HTTP/1.0\n\n\n"| nc localhost 80" in console will be successful)
3.I will modify a simple console chatroom to be agentS and agentC, is it possible to carry the http data (like pic,download...etc)?
Thank you for your patience
You don't really relay the socket, instead you relay the data. For example, "agentS" in your example opens a listening socket where it accepts connections from "agentC". When it gets a new connection from "agentC" then "agentS" connects to the web-server and enters a loop where where all it reads from either connection ("agentC" or web server) is sent to the other connection.
Since the two connections are independent it doesn't matter if one is TCP and the other UDP.
Also, if you need to do some processing on the data in "agentS" it's easy, as you actually have the data. The protocol between "agentC" and "agentS" doesn't even have to be HTTP, it can be whatever you want, as the programs can do protocol translation.
As a side note, when sending data to a web-server you end the lines with "\r\n", and the header is terminated by a single "\r\n" on its own line. So you only need to send "\r\n\r\n" after the GET request.
For example http://www.utorrent.com/testport?port=12345
How does this work? Can the server side script attempt to open a socket?
There are many ways of accomplishing this through server-side scripting. As #Oded mentioned, most server-side handlers are capable of initiating socket connections on arbitrary ports, and most of those even have dedicated port-scanning packages/libraries (PHP has one in the PEAR repository, Python's 'socket' module makes this type of tasks a breeze, etc...)
Keep in mind that on shared host platforms, socket connections are typically disabled for security purposes.
Another way that is also very easy to accomplish is to use a command-line port-scanner such as nmap from your server-side script. i.e in PHP, you would do echo ``nmap -p $port $ip\
The server side script will try to open a connection on the specified port to the originating IP.
If there is no response (the attempt will timeout), this would be an indication that the port is not open.
The server can try, as #Oded said. But that doesn't ensure the receiver will respond.
Typically, something like this happens:
The URL request contains instructions about which port to access. The headers that your browser sends include information about where the request is originating from.
Before responding to the request, the server tries to open a port and checks if this is successful. It waits a while before timing out.
The webpage is rendered dynamically based on the results of this test.
The response is returned to you containing the results.
Sometimes steps (2) and (3) will be replaced with an AJAX callback, which allows the response to return sooner.