Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function get(path, data)
- local host, path = path:match('(.-)(/.*)')
- local req = [[
- GET %s HTTP/1.1
- Host: %s
- User-Agent: Unknown agent
- Accept: plain/text
- Connection: close
- ]]
- local http, err = require'socket'.connect(host, 80)
- if not http then return nil, err end
- http:settimeout(5)
- if data then
- local post = {}
- for key, value in pairs(data) do table.insert(post, ('%s=%s'):format(key,value)) end
- path = '?'..table.concat(post, '&')
- end
- http:send(req:format(path, host))
- local headers, response = {}, ''
- local data = http:receive('*a')
- print(data)
- local splitter = data:find('\r\n\r\n')
- local head = data:sub(1, splitter+2)
- response = data:sub(splitter+4)
- for line in head:gmatch('(.-)\n') do
- key, value = line:match('(.-):(.*)')
- headers[key] = value
- end
- return response, headers
- end
- local function post(site, data, mime)
- local host, path
- if site:find('/') then
- host, path = site:match('(.*)(/.*)')
- else
- host, path = site, '/'
- end
- local req = [[
- POST %s HTTP/1.1
- Host: %s
- User-Agent: Unknown agent
- Content-Type: %s
- Content-Length: %d
- Connection: close
- %s]]
- local http, err = require'socket'.connect(host, 80)
- if not http then return nil, err end
- http:settimeout(5)
- local post = {}
- for key, value in pairs(data) do table.insert(post, ('%s=%s'):format(key,value)) end
- post = table.concat(post, '&')
- req = req:format(path, host, (mime or 'application/x-www-form-urlencoded'), #post, post)
- http:send(req)
- local headers, response = {}, ''
- local data = http:receive('*a')
- local splitter = data:find('\r\n\r\n')
- local head = data:sub(1, splitter+2)
- response = data:sub(splitter+4)
- for line in head:gmatch('(.-)\n') do
- key, value = line:match('^(.-):(.*)$')
- if key and value then
- headers[key] = value
- else
- headers.code, headers.status = line:match('(%d%d%d) (.*)$')
- headers.code = headers.code and tonumber(headers.code)
- end
- end
- return response, headers
- end
Advertisement
Add Comment
Please, Sign In to add comment