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...
0
votes
1 Answers
0
votes
It is all explained in the HTTP 1.0 protocol specifications.
Read specially the request section of the specs:
The
Request-Linebegins with a method token, followed by theRequest-URIand the protocol version, and ending withCRLF. The elements are separated bySPcharacters. NoCRorLFare allowed except in the finalCRLFsequence.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.