Skip to content

Commit 23a0f10

Browse files
committed
add comments
finish reqheader() function
1 parent 45d45a2 commit 23a0f10

File tree

1 file changed

+144
-19
lines changed

1 file changed

+144
-19
lines changed

stl/webserver.m

Lines changed: 144 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
11
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
226

327
methods(Static)
428

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+
636
% webserver Create a web server instance
737
port = int32(port);
838
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+
1046
end
1147

1248
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.
1355
coder.cinclude('httpd.h');
1456
coder.ceval('web_debug', d);
1557
end
1658

1759
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.
1864
coder.cinclude('httpd.h');
1965

2066
u = '';
@@ -34,23 +80,48 @@ function debug(d)
3480
end
3581

3682
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+
3789
coder.ceval('web_show_request_header');
3890
end
3991

4092
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.
4197
coder.ceval('web_error', errno, cstring(s));
4298
end
4399

44100
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.
46107

47108
coder.ceval('web_html', cstring(s));
48109
end
49110

50111
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.
52122

53123
coder.cinclude('httpd.h');
124+
54125

55126
if nargin == 2
56127
if ~isa(values, 'struct')
@@ -64,38 +135,71 @@ function template(filename, values)
64135
if size(v,1) > 1
65136
fprintf('numeric value must be scalar or row vector');
66137
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)));
68144
end
69145
end
70146
end
71147
coder.ceval('web_template', cstring(filename));
72148
end
73149

74150
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+
76158
coder.ceval('web_file', cstring(filename), cstring(type));
77159
end
78160

79161
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.
81171
coder.ceval('web_data', s, length(s), cstring(type));
82172
end
83173

84174
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.
86180
v = int32(0);
87181
v = coder.ceval('web_isPOST');
88182
end
89183

90184
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.
92190
v = int32(0);
93191
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...
95193
end
96194

97195
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.
99203
coder.cinclude('httpd.h');
100204
coder.varsize('s');
101205
s = '';
@@ -104,18 +208,30 @@ function data(s, type)
104208
buf = char(zeros(1,BUFSIZ)); % create a buffer to write into, all nulls
105209

106210
% 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
113220
end
114221
end
115222
end
116223

117224
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.
119235

120236
coder.cinclude('httpd.h');
121237
coder.varsize('s');
@@ -138,7 +254,16 @@ function data(s, type)
138254
end
139255

140256
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.
142267
coder.cinclude('httpd.h');
143268
coder.varsize('s');
144269
s = '';

0 commit comments

Comments
 (0)