简体   繁体   中英

Lua socket client:send function

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM