Lua socket client:send function

1.7k Views Asked by At

I am making an effort to understand sockets in lua. I am a bit stuck in client:send(data [, i [, j]]) as http://w3.impa.br/~diego/software/luasocket/tcp.html#send provides but I can't quite understand what it actually does and this manual doesn't explain much. For example, in order to send a file request, we use c:send("GET " .. file .. " HTTP/1.0\r\n\r\n"). Why should we use "GET" at the start and "HTTP/1.0\r\n\r\n" at the end? I've searched for other sites but none seem to be informative enough...

1

There are 1 best solutions below

0
On BEST ANSWER

It is all explained in the HTTP 1.0 protocol specifications.

Read specially the request section of the specs:

The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by SP characters. No CR or LF are allowed except in the final CRLF sequence.

Request-Line   = Method SP Request-URI SP HTTP-Version CRLF

There are the following methods supported:

  • GET
  • POST
  • HEAD

SP is separator. CRLF is CR (carriage return) followed by LF (newline feed) characters. The constants are listed here.

So, in a request formed like below:

GET some/path/to/file.lua HTTP/1.0\r\n\r\n

You have:

  • Method = GET
  • URI = some/path/to/file.lua
  • HTTP version = HTTP/1.0
  • CR = \r
  • LF = \n

The characters \r and \n respectively represent CR and LF in several programming languages. The are actually the same characters as: string.char(13) and string.char(10) respectively.