RSS
 

HTTP via Telnet

02 May

Okay, back to basics, the very low level basics. HTTP is text based, telnet enables a network connection via a text-based command line. All good. We can use telnet to talk to a web server by manually typing text to it, if you know the details of the content syntax to send HTTP via telnet.

Though this is not done, often, it is still good to know how it can be done. In fact, since these basics rarely change, I shouldn’t have even had to write this down, since it should be innate.

Sending HTTP Using Telnet

Once you get telnet running (see section, below), it is just a matter of typing HTTP to the server, manually.

There are four pieces the first three are shown, here:

GET / HTTP/1.1
Host: cachecrew.com
Connection: close
  1. This is the start of the HTTP commands you can enter. The first line must specify the HTTP method. There are three parts:
    1. HTTP method: GET, POST, PUT, DELETE, etc.
    2. The URI path; the path portion starting with ‘/’, which excludes the server portion.
    3. Protocol: HTTP/1.0 or HTTP/1.1. If you specify HTTP/1.1, you should probably specify Connection: close header otherwise the server will timeout and return an extraneous error (because HTTP version 1.1 defaults to a “persistent” connection).
  2. HTTP headers
  3. Empty line. This marks the end of the HTTP headers.
  4. If a Content-Length header was included, then content follows the empty-line marker as the fourth piece. (Not shown in the first example)

Important HTTP Header Notes

  1. Any errors made during input will often invalidate the entire entry. Typing and backspacing to correct will send the backspace characters to the server; this is not part of the HTTP syntax and will probably yield an error.
  2. A Host header should be sent. Some servers may host multiple domains on the same server-node and the host name is the only way those machines can differentiate between the target servers.
  3. As mentioned, if the protocol line (1) specifies the 1.1 version of HTTP, then the server will maintain an open connection, but time out with an error if further input is not sent. When specifying 1.1, it’s best to include a Connection: close header to tell the server not to keep the connection open.
  4. If you are sending “content”, you must specify a Content-Length header, specifying the number of bytes contained in the content. This value will truncate any content exceeding this length and it will cause the server to wait for additional input if the number is too big.

The server will only recognize “abcdefghijklmnopqrstuvwxyz”, even though a newline and “0123456789” were sent.

…
Content-Length: 26

abcdefghijklmnopqrstuvwxyz
0123456789

Notice that the line breaks are usually two-bytes, carriage-control/linefeed (0x0D, 0X0A), so

…
Content-Length: 28

abcdefghijklmnopqrstuvwxyz
0123456789

will be read as “abcdefghijklmnopqrstuvwxyz”[CR,LF].

…
Content-Length: 38

abcdefghijklmnopqrstuvwxyz
0123456789

will read the entire content “abcdefghijklmnopqrstuvwxyz”[CR,LF]”0123456789”.

The first lines of telnet are excluded, here, but shown in the next section.

The Telnet Command

This is one of the simplest network commands around. It connects the command like (that is the keyboard and screen) to a server, to send text (mostly) back and forth. You type, and the server responds. Its mechanism for communicating is dead-simple and can be re-purposed to communicate with other text-based services.

In the world of the Internet, in addition to knowing the name/address of a server, connecting to a service on the server is simply a matter of knowing the port to connect on. Telnet defaults to its port 23, which is not likely to work on a public server (or shouldn’t, if the server owner knows what is good for them). Web servers talk to web pages over port 80, by default. We can use telnet to talk with a web server by specifying port 80:

$ telnet cachecrew.com 80
Trying 208.113.163.125...
Connected to cachecrew.com.
Escape character is '^]'.
GET / HTTP/1.0
Host: cachecrew.com

The first and other highlighted lines are what you type.

  1. Use the telnet command to connect to the default web servers (port 80) at the cachecrew.com server.
  2. Telnet resolves the IP address from the server name and attempts to connect.
  3. Now it’s connected.
  4. You can type ctrl-] to get to telnet’s command mode. Then, if you want to quit, type “quit”. “Help” will list telnet commands you can use.
  5. This is the start of the HTTP commands you can enter. This is detailed in the section above.

On MacOS and Linux, telnet should already be installed and available from a terminal-command-line. On Windows, it is part of an installation feature which can be installed, explicitly, see How to Install Telnet in Windows 10. If you want Linux-like command-line features one Windows, you can install Cygwin. This can make telnet available to Windows’s regular cmd command-line, as well.

Telnet was originally used as a primitive way to communicate with a server. Because it lacks any decent security, servers rarely expose a default service for telnet to access. And, today, as most web servers enforce access via https, only, telnet is becoming less and less useful for accessing other commercial servers. For development, debugging, or testing a server which you can control, however, this can still be a useful tool.

 
 

Tags: ,