
---@class copas_http_request*
---@field url? string URL。
---@field method? string HTTP method.
---@field headers? table<string, string> Request header.
---@field source? ltn12_source Request body source.
---@field sink? ltn12_sink Response body sink.
---@field body? string Request body.
---@field proxy? string|table acting.
---@field redirect? boolean Whether redirection is allowed.
---@field create? function socket Create function.
---@field timeout? number|table Timeout configuration.
---@field target? table Response body list.

---@class copas_http_connection*
local copas_http_connection = {}

---@class copas_http*
---@field TIMEOUT number Default timeout.
---@field PORT integer HTTP port.
---@field USERAGENT string Default User-Agent.
---@field SSLPORT integer HTTPS port.
---@field SSLPROTOCOL string TLS protocol.
---@field SSLOPTIONS string TLS options。
---@field SSLVERIFY string TLS verify。
---@field SSLSNISTRICT boolean SNI strict or not.
---@field PROXY? string|table Agent configuration.
---@type copas_http*
local M = {}

M.TIMEOUT = 60
M.PORT = 80
M.USERAGENT = nil
M.SSLPORT = 443
M.SSLPROTOCOL = nil
M.SSLOPTIONS = nil
M.SSLVERIFY = nil
M.SSLSNISTRICT = false
M.PROXY = nil

--- Open an HTTP connection.
---@param request copas_http_request* Request form.
---@return copas_http_connection* connection Connection object.
function M.open(request) end

--- Create socket factory function.
---@param params copas_http_request* Request parameters.
---@return function create Create function.
function M.getcreatefunc(params) end

--- Parse HTTP requests.
---@param urlOrRequest string|copas_http_request* URL or request form.
---@param body? string Request body.
---@return copas_http_request* request Request form.
function M.parseRequest(urlOrRequest, body) end

--- Make HTTP/HTTPS requests.
---@param request string|copas_http_request* URL or request form.
---@param body? string Request body.
---@return string|integer|nil bodyOrOk Response body or LTN12 status.
---@return integer|string? statusCodeOrErr Status code or error message.
---@return table? headers response header.
---@return string? statusLine status line.
function M.request(request, body) end

--- Send request line.
---@param method string HTTP method.
---@param uri string URI。
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function copas_http_connection:sendrequestline(method, uri) end

--- Send request headers.
---@param headers table<string, string> Request header.
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function copas_http_connection:sendheaders(headers) end

--- Send the request body.
---@param headers table<string, string> Request header.
---@param source ltn12_source Request body source.
---@param step? ltn12_pump_step pump step。
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function copas_http_connection:sendbody(headers, source, step) end

--- Receive status line.
---@return integer|nil code status code.
---@return string? status Status line or error.
function copas_http_connection:receivestatusline() end

--- Receive response headers.
---@return table|nil headers response header.
---@return string? errMsg error message.
function copas_http_connection:receiveheaders() end

--- Receive response body.
---@param headers table response header.
---@param sink ltn12_sink Response body sink.
---@param step? ltn12_pump_step pump step。
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function copas_http_connection:receivebody(headers, sink, step) end

--- Receive HTTP/0.9 response body.
---@param status string Status text.
---@param sink ltn12_sink Response body sink.
---@param step? ltn12_pump_step pump step。
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function copas_http_connection:receive09body(status, sink, step) end

--- Close the connection.
---@return any result Close results.
function copas_http_connection:close() end

return M
