serverino.interfaces

Everything you need to communicate.

Types 8

structCookie

A cookie. Use Cookie("key", "value") to create a cookie. You can chain methods.

auto cookie = Cookie("name", "value").path("/").domain("example.com").secure().maxAge(1.days);
output.setCookie(cookie);

Methods
Cookie path(string path) @safe @nogc nothrow @property ref scope returnSet cookie path
Cookie domain(string domain) @safe @nogc nothrow @property ref scope returnSet cookie domain
Cookie secure(bool secure = true) @safe @nogc nothrow @property ref scope returnSet cookie secure flag. This cookie will be sent only thru https.
Cookie httpOnly(bool httpOnly = true) @safe @nogc nothrow @property ref scope returnSet cookie httpOnly flag. This cookie will not be accessible from javascript.
Cookie expire(SysTime expire) @safe @nogc nothrow @property ref scope returnSet cookie expire time. It overrides maxAge.
Cookie maxAge(Duration maxAge) @safe @nogc nothrow @property ref scope returnSet cookie max age. It overrides expire.
Cookie sameSite(SameSite sameSite) @safe @nogc nothrow @property ref scope returnSet cookie SameSite flag
Cookie invalidate() @safe @nogc nothrow @property ref scope returnInvalidate cookie. It will be deleted from browser on output.setCookie() request.
Constructors
this(string name, string value)Build a cookie with name and value
Nested Templates
SameSiteCookie SameSite flag

HTTP version used in request

HTTP10 = "HTTP/1.0"
HTTP11 = "HTTP/1.1"
structRequest

A request from user. Do not store ref to this struct anywhere.

void handler(Request request, Output output)
{
   info("You asked for ", request.path, " with method ", request.method, " and params ", request.get.data);
}

Fields
RequestImpl * _internal
Methods
string dump()(bool html = true) @safe constPrint request data
string toString()() @safe constditto
auto id() @safe @property constThe request ID. It is unique for each request.
auto get() @safe @nogc @property nothrow constParams from query string
auto post() @safe @nogc @property nothrow constParams from post if content-type is "application/x-www-form-urlencoded"
auto form() @safe @nogc @property nothrow const+ The fields from a form. Only if content-type is "multipart/form-data". + --- + FormData fd = request.form.read("form_id"); + if (fd.isFile) + { + // We have a file attached + info("File name:...
auto body() @safe @nogc @property nothrow constRaw posted data
auto header() @safe @nogc @property nothrow constHttp headers, always lowercase
auto cookie() @safe @nogc @property nothrow constCookies received from user
const(string) path() @safe @nogc @property nothrow constThe path requested by user.
string rawUri() @safe @nogc @property nothrow constThe raw, original Request-Target (URI) exactly as received from the client. Includes the query string. Attention: Do not use directly for file system paths without sanitization!
auto worker() @safe @nogc @property nothrow constWhich worker is processing this request?
auto host() @safe @nogc @property nothrow constThe host that received the request
auto requestLine() @safe @nogc @property nothrow constUse at your own risk! Raw data from user.
bool isSecure() @safe @nogc @property nothrow constIs this request coming from a TLS (https) listener? The flag is set by the daemon on its private channel to the worker, so a client can't forge it.
auto user() @safe @nogc @property nothrow constBasic http authentication user. Safe only if sent thru https!
auto password() @safe @nogc @property nothrow constBasic http authentication password. Safe only if sent thru https!
auto route() @safe @nogc @property nothrow constThe sequence of endpoints called so far
Method method() @safe @property @nogc nothrow constHTTP method
Nested Templates
MethodHTTP methods
ParsingStatus
SafeAccessParamA single key-value parameter inside SafeAccess.
SafeAccessReadAllRangeRange returned by readAll to iterate over all parameters with the same key.
SafeAccessSimple structure to safely access data from an associative array.
FormDataData sent through multipart/form-data.
RequestImpl
structOutput

A response to user. Default content-type is "text/html".

// Set status code to 404
output.status = 404

// Send a response. Same as: output.write("Sorry, page not found.");
output ~= "Sorry, page not found.";

Fields
OutputImpl * _internal
Methods
void setMaxRequestTime(Duration max) @safeOverride ServerinoConfig.setMaxRequestTime(Duration dur) for this request
void addHeader(in string key, in string value) @safeAdd a http header. You can't set `content-length`, `date`, `server`, `status` or `transfer-encoding` headers. They are managed by serverino internally. Throws if the name is not a valid header name...
void addHeader(in string key, in Duration dur) @safeDitto
void addHeader(in string key, in SysTime time) @safeDitto
void clearHeaders() @safeClear all headers.
bool serveFile(OnFileServed action = OnFileServed.KeepFile)(const string path, bool guessMime = true)Serve a file from disk. If you want to delete the file after serving it, use `serveFile!(OnFileServed.deleteFile)`.
void setCookie(Cookie c) @safeAdd or edit a cookie. To delete a cookie, use cookie.invalidate() and then setCookie(cookie)
ushort status() @safe @nogc @property nothrowRead status.
void status(ushort status) @safe @propertySet response status. 200 by default.
void opOpAssign(string op, T)(T data) if (op == "~")Syntax sugar. Easier way to write output. Example:
void opAssign(in bool v)Mute/unmute output. If false, serverino will not send any data to user.
void opAssign(in typeof(null) v)Clear the output buffer.
void write(string data = string.init) @safeWrite data to output. You can write as many times as you want.
void write(in void[] data) @safeDitto
void clear() @safeClear the output buffer.
void sendData(const string data) @safe
void sendData(bool force = false)(const void[] data) @safe
string toHTTPDate(SysTime t) @safe static
Nested Templates
Status
KeyValue
OutputImpl

A low-level representation of a WebSocket message. Probably you don't need to use this directly.

auto msg = WebSocketMessage("Hello world");
auto msg2 = WebSocketMessage(WebSocketMessage.OpCode.Text, "Hello world");
auto msg3 = WebSocketMessage(WebSocketMessage.OpCode.Ping, "Ping me back!");
auto msg4 = WebSocketMessage(WebSocketMessage.OpCode.Binary, [1, 2, 3, 4]);

Fields
bool isValidIs this message valid?
Methods
string asString() constReturn a string representation of the payload.
T as(T)() if (!isArray!T) constReturn the payload as a specific type.
T as(T)() if (isArray!T) constDitto
auto opcode() const
Constructors
this(OpCode opcode, string payload)Build a WebSocket message.
this(OpCode opcode, ubyte[] payload)Ditto
this(OpCode opcode, T payload)Ditto
this(OpCode opcode, T payload)
this(OpCode opcode, T payload)
this(OpCode opcode)Build an empty message with the given opcode, for the control frames that carry no payload.
this(T payload)Ditto
this(string payload)Ditto
this(ubyte[] payload)
Nested Templates
OpCode

A WebSocket. You can use this to send and receive WebSocket messages.

websocket.send("Hello world");      // Send a message to client
auto msg = websocket.receiveMessage();  // Receive a message from client

Fields
bool delegate(in WebSocketMessage msg) onMessageSet a callback to be called when a message is received. Return true to propagate the message to the next callback.
bool delegate(in WebSocketMessage msg) onCloseMessageSet a callback to be called when a close message is received. Return true to propagate the message to the next callback.
bool delegate(in string msg) onTextMessageSet a callback to be called when a text message is received. Return true to propagate the message to the next callback.
bool delegate(in ubyte[] msg) onBinaryMessageSet a callback to be called when a binary message is received. Return true to propagate the message to the next callback.
Methods
Socket socket()Return the underlying socket.
bool send()Try to send buffered data, if any. Returns false if there is still data to send. (only for non-blocking sockets)
auto send(T)(T data)Send a message. You can send a string, a basic type or an array of a basic type.
auto sendClose()Send a close message.
auto sendPing()Send a ping message. The peer should reply with a pong.
auto sendMessage(WebSocketMessage message, bool flagFIN = true)Send a custom message. flagFIN is true by default and should be true for most cases. You want to set it to false if you are sending a message in parts. The last part should have flagFIN set to true...
bool isSendBufferEmpty() constIf there is any data to send, it will be sent on next send() call *
WebSocketMessage receiveMessage()Receive a message from the WebSocket. There could be more than one message in the buffer so you should call this function in a loop until it returns an invalid message.
void kill(string reason) staticClose the WebSocket connection.
bool killRequested() staticIs the WebSocket connection closed?
string killReason() staticWhy the WebSocket connection was closed?
bool isDirty()Returns true if the WebSocket is dirty.
Constructors
this(Socket socket, WebSocket.Role role = WebSocket.Role.Server)Create a WebSocket. If you are using this as client, you should pass WebSocket.Role.Client as second parameter.
Nested Templates
Role
Flags

Used by Output.serveFile

KeepFileKeep the file on disk
DeleteFileDelete the file after sending it

Some useful functions to get information about the current serverino process

Methods
int daemonPID() staticReturns the PID of the serverino daemon
bool isDaemon() staticReturns true if the current process is the serverino daemon
bool isWebSocket() staticReturns true if the current process is a serverino websocket
bool isWorker() staticReturns true if the current process is a serverino worker
bool isDynamicComponent() staticReturns true if the current process is a serverino dynamic component (websocket or worker)

Functions 4

fnchar[] uintToChars(size_t N)(return scope ref char[N] buffer, size_t value) @safe @nogc nothrow pure
fnbool validHeader(in char[] key, in char[] value) @safe @nogc nothrow pure
fnbool validCookieDomain(in char[] domain) @safe @nogc nothrow pure
fnstring escapeControlChars(in char[] s) @safe pure