-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttp.alusus
133 lines (110 loc) · 5.84 KB
/
Http.alusus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import "Srl/Memory.alusus";
import "Srl/Array.alusus";
import "Srl/String.alusus";
import "libcivetweb.so.1" or "libcivetweb.dylib";
module Http {
func getBuildDependencies(): Array[String] {
return Array[String]({ String(getThisSourceDirectory[]) + preprocess {
if String.isEqual(Process.platform, "macos") {
Spp.astMgr.insertAst(ast "libcivetweb.dylib");
} else {
Spp.astMgr.insertAst(ast "libcivetweb.so.1");
}
} });
}
class Context{
def stopFlag: int; // Should we stop event loop
};
def RequestCallback: alias ptr[func (connection: ptr[Connection]): Int];
class Callbacks{
def beginRequest: RequestCallback;
def endRequest: ptr[func (connection: ptr[Connection], replyStatusCode: Int)];
def logMessage: ptr[func (connection: ptr[Connection], message: CharsPtr): Int];
def logAccess: ptr[func (connection: ptr[Connection], message: CharsPtr): Int];
def initSsl: ptr[func (sslContext: ptr[Void], userData: ptr[Void]): Int];
def initSslDomain: ptr[func (serverDomain: ptr[array[Char]], sslContext: ptr[Void], userData: ptr[Void]): Int];
def externalSslCtx: ptr[func (sslCtx: ptr[ptr[Void]], userData: ptr[Void]): Int];
def externalSslCtxDomain: ptr[func (
serverDomain: ptr[array[Char]], sslCtx: ptr[ptr[Void]], userData: ptr[Void]
): Int];
def connectionClose: ptr[func (connection: ptr[Connection]): Void];
def connectionClosed: ptr[func (connection: ptr[Connection]): Void];
def initLua: ptr[func (connection: ptr[Connection], luaContext: ptr[Void]): Void];
def exitLua: ptr[func (connection: ptr[Connection], luaContext: ptr[Void]): Void];
def httpError: ptr[func (connection: ptr[Connection], status: Int, msg: ptr[array[Char]]): Int];
def initContext: ptr[func (context: ptr[Context]): Void];
def exitContext: ptr[func (context: ptr[Context]): Void];
def initThread: ptr[func (context: ptr[Context], threadType: Int): Void];
def exitThread: ptr[func (context: ptr[Context], threadType: Int, threadPtr: ptr[Void]): Void];
def initConnection: ptr[func (connection: ptr[Connection], connData: ptr[ptr[Void]]): Void];
};
class RequestInfo {
def requestMethod: CharsPtr; // "GET", "POST", etc
def requestUri: CharsPtr; // URL-decoded URI (absolute or relative, as in the request)
def localUriRaw: CharsPtr;
def localUri: CharsPtr; // URL-decoded URI (relative). Can be NULL if the request_uri does not address
// a resource at the server host.
def httpVersion: CharsPtr; // E.g. "1.0", "1.1"
def queryString: CharsPtr; // URL part after '?', not including '?', or NULL
def remoteUser: CharsPtr; // Authenticated user, or NULL if no auth used
def remoteAddr: array[Char, 48]; // Client's IP address as a string.
def contentLength: Int[64]; // Length (in bytes) of the request body, can be -1 if no length was given.
def remotePort: Int; // Client's port
def serverPort: Int;
def isSsl: Int; // 1 if SSL-ed, 0 if not
def userData: ptr[Void]; // User data pointer passed to mg_start()
def connData: ptr[Void]; // Connection-specific user data
def numberHeaders: Int; // Number of HTTP headers
def httpHeaders: array[Header, 64]; // Maximum 64 headers
};
class Header {
def name: CharsPtr; // HTTP header name
def value: CharsPtr; // HTTP header valuue
};
class Connection {
};
@expname[mg_start]
func startServer(callbacks: ptr[Callbacks], user_data: Int, options: ptr[CharsPtr]): ptr[Context];
func startServer (callback: RequestCallback, options: ref[Srl.Array[CharsPtr]]): ptr[Context] {
def callbacks: Callbacks;
Srl.Memory.set(callbacks~ptr, 0, Callbacks~size);
callbacks.beginRequest = callback;
return startServer(callbacks~ptr, 0, options.buf~ptr);
}
func startServer (callback: RequestCallback, optsCount: Int, opts: ...CharsPtr): ptr[Context] {
def options: Srl.Array[CharsPtr];
while optsCount-- > 0 options.add(opts~next_arg[CharsPtr]);
options.add(0);
return startServer(callback, options);
}
func startServer(callback: RequestCallback, port: CharsPtr): ptr[Context] {
return startServer(callback, { "listening_ports", port });
}
@expname[mg_stop]
func stopServer(context: ptr[Context]): Void;
// read data from client
@expname[mg_read]
func read(connection: ptr[Connection], buffer: ptr, bufferSize: Int): Int;
// send data to client
@expname[mg_write]
func write(connection: ptr[Connection], buffer: CharsPtr, bufferSize: Int): Int;
// work like write(), but allows to do message formatting
@expname[mg_printf]
func print(connection: ptr[Connection], format: CharsPtr, ...any): Int;
@expname[mg_send_file]
func sendFile(connection: ptr[Connection], fileName: CharsPtr): Void;
@expname[mg_send_mime_file]
func sendFile(connection: ptr[Connection], filename: CharsPtr, mime: CharsPtr): Void;
@expname[mg_get_cookie]
func getCookie(
cookiesString: CharsPtr, cookieName: CharsPtr, outCookieContent: CharsPtr, outCookieSize: Word[64]
): Int;
@expname[mg_get_header]
func getHeader(connection: ptr[Connection], headerName: CharsPtr): CharsPtr;
@expname[mg_get_request_info]
func getRequestInfo(connection: ptr[Connection]): ptr[RequestInfo];
@expname[mg_get_var]
func getVariable(
data: CharsPtr, dataSize: Int, variableName: CharsPtr, outVariable: CharsPtr, outVariableSize: Int
): Int;
};