Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Class abhttp.Socket

A HTTP client socket.

Constructor

function init(hostOrStream, port=null, keepAlive=300)
Create a new HTTP socket, either from a TCP/IP socket or a host/port pair.
Parameters:
hostOrStream – a host name, or a socket stream.
port – a port number, or null if creating from a socket. If port=443, the connection will be made with ssl.
keepAlive – the value for the keepAlive field. If null, a new connection will be created for each request. If keepAlive=null and port=null, only one request can be sent without re-init.

Fields

cookies The cookies, as an array indexed by the cookie name.
dumpin A stream (file) to write all incoming data to.
dumpout A stream (file) to write all outgoing data to.
fields The request fields, as an array indexed by the field name.
host The name of the host to connect to (if not null, will also be used as HTTP host name).
keepAlive The keep-alive mode: null for no keep alive, a timeout (in seconds) for keep alive.
port The port to connect to (may be null).
stream The underlying socket stream.

Functions

avail() Get the number of bytes which can be read without blocking.
close() Close the socket.
connect(stream=...) Connect this socket to a stream.
discardContent() Discard all remaining bytes of content.
field(name) Get a field from the response header.
flush() Flush the socket, sending all output.
handleResponse() Handle a response, i.e. parse a response header.
numfield(name) Get a numeric field from the response header.
read(len) Read a number of bytes from the socket.
readContent(len) Read up to a given number of bytes from the content.
readln(maxlen=...) Read a line up to a maximum length from the socket.
request(method, path, params=..., fields=...) Send a request.
write(s) Write a string to the socket.
writeln(s) Write a string and a CRLF pair to the socket.
function connect(stream=null)
Connect this socket to a stream.
Parameters:
stream – the stream to connect to. If null, a stream is created from host and port.
function write(s)
Write a string to the socket.
Parameters:
s – the string.
function writeln(s)
Write a string and a CRLF pair to the socket.
Parameters:
s – the string.
function flush()
Flush the socket, sending all output.
function close()
Close the socket.
function request(method, path, params=null, fields=null)
Send a request.
Parameters:
method – the request method (GET, HEAD, POST).
path – the request path.
params – the request parameters.
fields – the extra header fields, as an array indexed by name.
function read(len)
Read a number of bytes from the socket.
Parameters:
len – the number of bytes to read.
Returns:the string read.
function readln(maxlen=1024)
Read a line up to a maximum length from the socket.
Parameters:
maxlen – the maximum number of bytes to read.
Returns:the string read (without line end mark).
function avail()
Get the number of bytes which can be read without blocking.
Returns:the available bytes.
function handleResponse()
Handle a response, i.e. parse a response header. This sets up various internal fields, and allows to subsequently read the content via readContent().
Returns:the response code, e.g. 200 for OK. Numeric if it is a number.
function field(name)
Get a field from the response header.
Parameters:
name – the field name (e.g. "Content-Type"), not case sensitive.
Returns:the field contents, or null if there is no such field.
function numfield(name)
Get a numeric field from the response header.
Parameters:
name – the field name (e.g. "Content-Length"), not case sensitive.
Returns:the field value, or -1 if there is no such field or it does not contain a valid decimal number.
function readContent(len)
Read up to a given number of bytes from the content.
Parameters:
len – the number of bytes to read.
Returns:the content chunk, or null if there is no more content to read.
function discardContent()
Discard all remaining bytes of content.
mShell Home  > Documentation  > mdoc