-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.corto
More file actions
194 lines (163 loc) · 5.66 KB
/
Copy pathmodel.corto
File metadata and controls
194 lines (163 loc) · 5.66 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
in corto.httpserver
random(uint16 n) string
typedescriptor(lang.type type) string
class Service
// Abstraction for server-side HTTP. Multiple services (like REST, DDP, custom)
// may use the same HTTP instance to avoid using multiple ports.
// There can only be one HTTP server active per port, per process. A maximum of
// 64 servers can be active per process.
class HTTP {
enum Method {
None,
Get,
Head,
Post,
Put,
Delete,
Trace,
Options,
Connect,
Patch
}
// Manages connection & user data
class Connection {
ctx: object
server: HTTP
conn: word, private|local
write(string msg) void
}
// Provides request data and reply interface. Use delegate interface so that
// a Request instance can live on the stack (overridable methods can only be
// used on objects when RTTI is available).
struct Request {
// Request members
uri: string
body: string
method: HTTP.Method
conn: word, private|local
file: bool, private|local
garbage: list[string], private|local // to clean when request is done
ctx: word, private|local
status: uint16
delegate
// Delegate types for Request
d_setHeader(Request r, string key, string val) void
d_setStatus(Request r, uint16 status) void
d_reply(Request r, string msg) void
d_sendFile(Request r, string file) void
d_getHeader(Request r, string key) string
d_getVar(Request r, string key) string
member
// Delegate members (copied from server)
m_setHeader: d_setHeader, private|local
m_setStatus: d_setStatus, private|local
m_reply: d_reply, private|local
m_sendFile: d_sendfile, private|local
m_getHeader: d_getHeader, private|local
m_getVar: d_getVar, private|local
// Method wrappers for delegate
method
setHeader(string key, string val)
setStatus(uint16 status)
getStatus() uint16
setCookie(string key, string value)
reply(string msg)
badRequest(string msg)
sendfile(string file)
getHeader(string key) string
getVar(string key) string
getCookie(string key) string
}
// Delegate members (copied to Request)
member
m_setHeader: Request.d_setHeader, private|local
m_setStatus: Request.d_setStatus, private|local
m_reply: Request.d_reply, private|local
m_sendFile: Request.d_sendfile, private|local
m_getHeader: Request.d_getHeader, private|local
m_getVar: Request.d_getVar, private|local
port: uint16
pollInterval: uint16
pollServiceRate: uint16
services: list[Service], private|local|not_null
infra_services: list[Service], private|local|not_null
connections: list[Connection], private|local|not_null
pollCount: uint16, private|local
// Register a service with the server
add_service(Service s) void
// Unregister a service with the server
remove_service(Service s) void
// Register a service with infrastructure hooks
add_infra_service(Service s) void
// Unregister a service with the server
remove_infra_service(Service s) void
// Write a message to all open connections
broadcast(string msg) void
// Private methods
do_open(HTTP.Connection c) void
do_close(HTTP.Connection c) void
do_request(HTTP.Connection c, HTTP.Request r) void
do_message(HTTP.Connection c, string msg) void
do_poll() void
construct() int16
destruct() void
function
// Acquire singleton server for specified port
get_server(uint16 port) HTTP
// Set a singleton server for a specific port. Returns true if success,
// false when the slot is already occupied. When NULL is provided for server
// the slot is reset, and the function will always return TRUE.
set_server(uint16 port, HTTP server) bool
overridable
// Write a websocket message to an open connection
write(HTTP.Connection c, string msg) void
}
// Standalone HTTP server
class StandaloneHTTP: HTTP {
enable_sendfile: bool // Use sendfile to speed up file transfer (disabled by default)
thread: word, private|local
server: word, private|local
exiting: bool, private|local
enable_ssl: bool, hidden
ssl_cert: string, hidden
ssl_pkey: string, hidden
construct() int16
destruct() void
write(HTTP.Connection c, string msg) void
}
class StandaloneHTTPS : StandaloneHTTP {
alias ssl_cert: StandaloneHTTP.ssl_cert
alias ssl_pkey: StandaloneHTTP.ssl_pkey
construct() int16
destruct() void
}
// Service built on top of an HTTP instance
class Service {
port: uint16
endpoint: string
redirectEndpointToPath: bool, readonly|local
server: HTTP, private|local
construct() int16
destruct() void
// HTTP handlers
overridable
on_open(HTTP.Connection c) void
on_close(HTTP.Connection c) void
on_request(HTTP.Connection c, HTTP.Request r, string uri) int16
on_get(HTTP.Connection c, HTTP.Request r, string uri) int16
on_post(HTTP.Connection c, HTTP.Request r, string uri) int16
on_put(HTTP.Connection c, HTTP.Request r, string uri) int16
on_delete(HTTP.Connection c, HTTP.Request r, string uri) int16
on_message(HTTP.Connection c, string msg) void
on_poll() void
// Infrastructure handlers
on_pre_request(HTTP.Connection c, HTTP.Request r) word
on_post_request(HTTP.Connection c, HTTP.Request r, word ctx) void
}
// Service that serves up static content
class Files: Service {
path: string
construct() int16
override
on_request(HTTP.Connection c, HTTP.Request r, string uri) int16
}