serverino.interfaces
Everything you need to communicate.
class WebSocket
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 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 flagCookie invalidate() @safe @nogc nothrow @property ref scope returnInvalidate cookie. It will be deleted from browser on output.setCookie() request.Nested Templates
SameSiteCookie SameSite flagenumHttpVersion
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 * _internalMethods
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:...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!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!Nested Templates
MethodHTTP methodsParsingStatusSafeAccessParamA 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.RequestImplstructOutput
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 * _internalMethods
void setMaxRequestTime(Duration max) @safeOverride ServerinoConfig.setMaxRequestTime(Duration dur) for this requestvoid 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...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)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 sendData(const string data) @safevoid sendData(bool force = false)(const void[] data) @safeNested Templates
StatusKeyValueOutputImplstructWebSocketMessage
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
auto opcode() constConstructors
this(OpCode opcode, string payload)Build a WebSocket message.this(OpCode opcode, ubyte[] payload)Dittothis(OpCode opcode, T payload)Dittothis(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)Dittothis(string payload)Dittothis(ubyte[] payload)Nested Templates
OpCodeclassWebSocket
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 clientFields
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
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...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.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
RoleFlagsenumOnFileServed
Used by Output.serveFile
KeepFileKeep the file on disk
DeleteFileDelete the file after sending it
structServerinoProcess
Some useful functions to get information about the current serverino process
Methods
bool isDynamicComponent() staticReturns true if the current process is a serverino dynamic component (websocket or worker)Functions 4
fn
char[] uintToChars(size_t N)(return scope ref char[N] buffer, size_t value) @safe @nogc nothrow pure