1
1
classdef webserver < handle % #codegen
2
+ % webserver Lightweight webserver library
3
+ %
4
+ % This class contains methods that implement a lightweight webserver
5
+ % for MATLAB Coder.
6
+ %
7
+ % Methods::
8
+ % webserver create a webserver instance
9
+ % debug enable debugging messages
10
+ % details display HTTP header
11
+ % -
12
+ % html send string to browser
13
+ % template send file with substitutions to browser
14
+ % file send file to browser
15
+ % data send data to browser
16
+ % error send error code to browser
17
+ % -
18
+ % url URL for current request
19
+ % isGET test for GET request
20
+ % isPOST test for POST request
21
+ % reqheader get element of HTTP header
22
+ % getarg get element of HTTP GET header
23
+ % postarg get element of HTTP POST header
24
+ %
25
+ % Copyright (C) 2018, by Peter I. Corke
2
26
3
27
methods (Static )
4
28
5
- function obj = webserver(port , callback )
29
+ function obj = webserver(port , callback , arg )
30
+ % webserver Create a webserver
31
+ %
32
+ % webserver(port, callback) creates a new webserver executing it a separate
33
+ % thread and listening on the specified port (int). The MATLAB entrypoint
34
+ % named callback is invoked on every GET and PUT request to the server.
35
+
6
36
% webserver Create a web server instance
7
37
port = int32(port );
8
38
coder .cinclude(' httpd.h' );
9
- coder .ceval(' web_start' , port , cstring(callback ));
39
+ coder .cinclude(' stl.h' );
40
+ if nargin == 3
41
+ coder .ceval(' web_start' , port , cstring(callback ), coder .ref(arg ));
42
+ else
43
+ coder .ceval(' web_start' , port , cstring(callback ), coder .opaque(' void *' , ' NULL' ));
44
+ end
45
+
10
46
end
11
47
12
48
function debug(d )
49
+ % webserver.debug Control debugging
50
+ %
51
+ % webserver.debug(D) controls the display of debug messages. If D is true messages
52
+ % are enabled.
53
+ %
54
+ % See also: stl.log.
13
55
coder .cinclude(' httpd.h' );
14
56
coder .ceval(' web_debug' , d );
15
57
end
16
58
17
59
function u = url()
60
+ % webserver.url Get the URL for the request
61
+ %
62
+ % url = webserver.url() is a character array representing the URL associated with
63
+ % this request.
18
64
coder .cinclude(' httpd.h' );
19
65
20
66
u = ' ' ;
@@ -34,23 +80,48 @@ function debug(d)
34
80
end
35
81
36
82
function details()
83
+ % webserver.details Show HTTP header details
84
+ %
85
+ % webserver.details() displays the HTTP header via the logging channel.
86
+ %
87
+ % See also: stl.log.
88
+
37
89
coder .ceval(' web_show_request_header' );
38
90
end
39
91
40
92
function error(errno , msg )
93
+ % webserver.error Send error code to browser
94
+ %
95
+ % websever.error(code, msg) send an error code (eg. 404) and message to the
96
+ % requesting browser.
41
97
coder .ceval(' web_error' , errno , cstring(s ));
42
98
end
43
99
44
100
function html(s )
45
- % webserver.html Send an HTML string to browser
101
+ % webserver.html Send an HTML string to browser
102
+ %
103
+ % weserver.html(str) the string str is sent to the requesting browser. The string
104
+ % can be plain text or HTML.
105
+ %
106
+ % See also: webserver.template, webserver.error.
46
107
47
108
coder .ceval(' web_html' , cstring(s ));
48
109
end
49
110
50
111
function template(filename , values )
51
- % webserver.template Send template file with substitution to browser
112
+ % webserver.template Send template file with substitution to browser
113
+ %
114
+ % webserver.template(filename, values) sends the contents of the specified file
115
+ % to the requesting browser with substitutions. Elements of the struct values
116
+ % are substituted for special HTML tags.
117
+ %
118
+ % For example the value of values.a is substitued by:
119
+ % <TMPL_VAR name="a">
120
+ %
121
+ % See also: webserver.html, webserver.error, CTemplate.
52
122
53
123
coder .cinclude(' httpd.h' );
124
+
54
125
55
126
if nargin == 2
56
127
if ~isa(values , ' struct' )
@@ -64,38 +135,71 @@ function template(filename, values)
64
135
if size(v ,1 ) > 1
65
136
fprintf(' numeric value must be scalar or row vector' );
66
137
else
67
- coder .ceval(' web_setvalue' , cstring(name ), cstring(num2str(v )));
138
+ if isinteger(v )
139
+ fmt = ' %d ' ;
140
+ else
141
+ fmt = ' %g ' ;
142
+ end
143
+ coder .ceval(' web_setvalue' , cstring(name ), cstring(sprintf(fmt , v )));
68
144
end
69
145
end
70
146
end
71
147
coder .ceval(' web_template' , cstring(filename ));
72
148
end
73
149
74
150
function file(filename , type )
75
- % webserver.file Send file and content type to browser
151
+ % webserver.file Send file and content type to browser
152
+ %
153
+ % webserver.file(filename, type) send the specified file to the requesting browser, with
154
+ % the specified MIME type.
155
+ %
156
+ % See also: webserver.template, webserver.html, webserver.error.
157
+
76
158
coder .ceval(' web_file' , cstring(filename ), cstring(type ));
77
159
end
78
160
79
161
function data(s , type )
80
- % webserver.file Send data and content type to browser
162
+ % webserver.file Send data and content type to browser
163
+ %
164
+ % webserver.data(data, type) send the character array data to the requesting browser, with
165
+ % the specified MIME type.
166
+ %
167
+ % Notes::
168
+ % - The data could be a binary string, eg. an image.
169
+ %
170
+ % See also: webserver.template, webserver.html, webserver.error.
81
171
coder .ceval(' web_data' , s , length(s ), cstring(type ));
82
172
end
83
173
84
174
function v = isPOST()
85
- % webserver.ispost Test if POST request
175
+ % webserver.isPOST Test for POST request
176
+ %
177
+ % v = webserver.isPOST() is true if this request is an HTTP POST.
178
+ %
179
+ % See also: webserver.isGET.
86
180
v = int32(0 );
87
181
v = coder .ceval(' web_isPOST' );
88
182
end
89
183
90
184
function v = isGET()
91
- % webserver.isget Test if GET request
185
+ % webserver.isGET Test for GET request
186
+ %
187
+ % v = webserver.isGET() is true if this request is an HTTP GET.
188
+ %
189
+ % See also: webserver.isPOST.
92
190
v = int32(0 );
93
191
v = coder .ceval(' web_isPOST' );
94
- v = ~v ; % codegen can't do this in the one line...
192
+ v = ~v ; % codegen cant do this in the one line...
95
193
end
96
194
97
195
function s = reqheader(name )
98
- % webserver.reqheader Return value of request header item
196
+ % webserver.reqheader Return value of request header item
197
+ %
198
+ % v = webserver.reqheader(key) is a character array representing the value of
199
+ % the specified key in the HTTP request header.
200
+ %
201
+ % Notes::
202
+ % - Returns empty string if the key is not found.
99
203
coder .cinclude(' httpd.h' );
100
204
coder .varsize(' s' );
101
205
s = ' ' ;
@@ -104,18 +208,30 @@ function data(s, type)
104
208
buf = char(zeros(1 ,BUFSIZ )); % create a buffer to write into, all nulls
105
209
106
210
% content
107
- % coder.ceval('web_url', coder.wref(buf), BUFSIZ); % evaluate the C function
108
-
109
- for i= 1 : BUFSIZ - 1 % find the end of the string, where the first unwritten null is
110
- if buf(i ) == 0
111
- s = buf(1 : i - 1 ); % found a null, return variable length array up to here
112
- return ;
211
+ found = int32(0 );
212
+ found = coder .ceval(' web_reqheader' , coder .wref(buf ), BUFSIZ ); % evaluate the C function
213
+
214
+ if found
215
+ for i= 1 : BUFSIZ - 1 % find the end of the string, where the first unwritten null is
216
+ if buf(i ) == 0
217
+ s = buf(1 : i - 1 ); % found a null, return variable length array up to here
218
+ return ;
219
+ end
113
220
end
114
221
end
115
222
end
116
223
117
224
function s = getarg(name )
118
- % webserver.getarg Return value of GET argument
225
+ % webserver.getarg Return value of GET argument
226
+ %
227
+ % v = webserver.getarg(key) is a character array representing the value of
228
+ % the specified key in the HTTP GET request header.
229
+ %
230
+ % Notes::
231
+ % - These parameters are on the end of the URL, eg. ?key1=val1&key2=val2
232
+ % - Returns empty string if the key is not found.
233
+ %
234
+ % See also: webserver.isGET, webserver.url.
119
235
120
236
coder .cinclude(' httpd.h' );
121
237
coder .varsize(' s' );
@@ -138,7 +254,16 @@ function data(s, type)
138
254
end
139
255
140
256
function s = postarg(name )
141
- % webserver.postarg Return value of POST argument
257
+ % webserver.postarg Return value of POST argument
258
+ %
259
+ % v = webserver.postarg(key) is a character array representing the value of
260
+ % the specified key in the HTTP POST request header.
261
+ %
262
+ % Notes::
263
+ % - POST data is typically sent from the browser using <form> and <input> tags.
264
+ % - Returns empty string if the key is not found.
265
+ %
266
+ % See also: webserver.isPOST, webserver.url.
142
267
coder .cinclude(' httpd.h' );
143
268
coder .varsize(' s' );
144
269
s = ' ' ;
0 commit comments