How to process requests on stdin socket? - sockets

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?

Related

cURL'ing a Phoenix socket

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) :)

Using lighttpd to send requests to TCP socket

This is my first set up of a webserver, so it's an experimental learning experience.
I had successfully got lighttpd using mod_fastcgi (https://redmine.lighttpd.net/projects/1/wiki/docs_modfastcgi) working to send a request to a separate process via a TCP socket. It sends the request using the FastCGI protocol.
But is there a module or way to send raw data to the TCP socket without the FastCGI protocol? Alternatively, if not lighttpd, is there another lightweight webserver that does allow sending basic requests to a TCP socket without any extra protocol wrapping around it?
I think you are looking for lighttpd mod_proxy.
https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy

Is it possible to make a relay agent (application layer) of a server (service)? Any example? C will be better

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.

How to bypass socket?

I have installed a streaming server "Lighttpd" (light-tpd) which runs on port 81.
I have a C program that listens to http requests on port 80 using a server socket created by socket api.
I want that as soon as I get a request on the port 80 from a client I forward that to the streaming server and the remaining conversation takes place b/w the Streaming Server and client & they bypass my C program completely.
The problem is client would be expecting msgs from socket at port 80 (i.e from the socket of my C program) since it had sent request to port 80 only rather than from the Streaming server which gives service on port 81.
can anyone help me out on this issue of bypassing the socket on port 80 for replying to the client.
Solution I think: my program can be a middle man...It will forward the request to port 81 of streaming server and when it get replies from there it forwards them to the client...but bypassing would be efficient and I don't know how to do that. Please help me out.
Thanks in advance
Why put your C program in front? Lighttpd is designed to act as a frontend proxy (among other uses), so you can put lighttpd in front and use its mod_proxy_core to pass requests to your C program. You can use X-Rewrite and/or X-Sendfile to pass requests back to Lighttpd after doing some processing inside your application.
I have recently implemented a similar technique where a single program accepts a TCP connection and then 'passes' that connection to another component and plays no further part in the socket conversation. It uses the technique of passing the file descriptor of the accepted socket over a UNIX socket to the server component which effectively does an inter-process dup() of the fd.
See here and here.
This works for me as I have control of both ends of the UNIX socket on the server-side, but to work for you, you'd need:
A UNIX socket between your dispatching component and server components.
Full control of the server component.
You might need to hack away at the lighttpd source code...
Sorry, not really an proper answer...

How do online port checkers work?

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.