Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How HTTP servers accepts Telnet

Tags:

telnet

I understand that Telnet is a protocol as much as HTTP. I have this notion that after the initial TCP connection is made the Telnet client would send some telnet specific code over to the server on the other side in this case a HTTP server. But since HTTP server doesn't understand Telnet specific codes it should throw an error or drop the connection etc. But in reality we can telnet to a HTTP server and fetch pages if we type in correct HTTP headers and send them. How can it be like that? Wikipedia entry really didn't help me to understand this specific point. (http://en.wikipedia.org/wiki/Telnet#Telnet_data)

like image 799
chamibuddhika Avatar asked Jan 19 '11 03:01

chamibuddhika


1 Answers

Telnet is just an easy interactive way to open a TCP connection to a listening socket. Because the telnet client blindly sends what you type to that socket, it can theoretically emulate any other protocol on top of TCP. Actually the fact that non printable chars are interpreted by the keyboard driver is the only limit.

HTTP does not use non printable chars except to delimit between the HTTP header and the body with two consecutive "line breaks" (i.e. a "blank line").
Please note that I'm not talking about the HTML body tag here, but the payload (e.g SOAP body).

No magic here basically.

Let's see the dynamic of things.
HTTP supports a number of commands like GET, POST, PUT etc... Each command has its syntax and there is an associated response with an agreed upon syntax and well defined error codes. When you connect to an HTTP server using telnet, you open the socket connection and the server forks a thread to manage the dialog with your client. You can then mimic a browser by typing the command that the browser would send. Each time you strike the CR key, the client submits the line to the server. If a command contains several lines, you can enter several lines, each of them corresponding to a line of the command header. Once you strike two CR in a row (i.e. an empty line), the command header is deemed complete by the server and the response is put together and sent back to your client. Because a telnet client's life goal is to echo received characters (unless told otherwise), then you can see the response header and body on your terminal window. Telnet stops there. A browser would render the HTML (if the response is an HTML page).
I hope that clarifies it all.

like image 119
Alain Pannetier Avatar answered Sep 22 '22 23:09

Alain Pannetier