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...
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 theRequest-URI
and the protocol version, and ending withCRLF
. The elements are separated bySP
characters. NoCR
orLF
are allowed except in the finalCRLF
sequence.Request-Line = Method SP Request-URI SP HTTP-Version CRLF
There are the following methods supported:
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:
GET
some/path/to/file.lua
HTTP/1.0
\r
\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.