OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [httpd/] [current/] [doc/] [httpd.sgml] - Blame information for rev 844

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
 
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
Embedded HTTP Server
29
30
Embedded HTTP Server
31
 
32
33
Intrduction
34
35
The eCos HTTPD package provides a simple HTTP
36
server for use with applications in eCos. This server is specifically
37
aimed at the remote control and monitoring requirements of embedded
38
applications. For this reason the emphasis is on dynamically generated
39
content, simple forms handling and a basic CGI interface. It is
40
not intended to be a general purpose server for
41
delivering arbitrary web content. For these purposes a port of the
42
GoAhead web server is available from 
43
url="http://www.goahead.com">www.goahead.com.
44
45
This server is also capable of serving content using IPv6 when
46
the eCos configuration contains IPv6.
47
48
49
 
50
51
Server Organization
52
53
The server consists of one or more threads running in parallel to any
54
application threads and which serve web pages to clients. Apart from
55
defining content, the application does not need to do anything to
56
start the HTTP server.
57
58
59
The HTTP server is, by default, started by a static constructor. This
60
simply creates an initial thread and sets it running. Since this is
61
called before the scheduler is started, nothing will happen until the
62
application calls cyg_scheduler_start(). The
63
server thread can also be started explicitly by the application, see
64
the CYGNUM_HTTPD_SERVER_AUTO_START option for
65
details.
66
67
68
When the thread gets to run it first optionally delays for some period
69
of time. This is to allow the application to perform any
70
initialization free of any interference from the HTTP server. When the
71
thread does finally run it creates a socket, binds it to the HTTP
72
server port, and puts it into listen mode. It will then create any
73
additional HTTPD server threads that have been configured before
74
becoming a server thread itself.
75
76
77
Each HTTPD server thread simply waits for a connection to be made to
78
the server port. When the connection is made it reads the HTTP request
79
and extracts the filename being accessed. If the request also contains
80
form data, this is also preserved. The filename is then looked up in a
81
table.
82
83
84
Each table entry contains a filename pattern string, a
85
pointer to a handler function, and a user defined argument for the
86
function. Table entries are defined using the same link-time table
87
building mechanism used to generate device tables. This is all handled
88
by the CYG_HTTPD_TABLE_ENTRY() macro which has the
89
following format:
90
91
92
 
93
#include <cyg/httpd/httpd.h>
94
 
95
CYG_HTTPD_TABLE_ENTRY( __name, __pattern, __handler, __arg )
96
 
97
98
99
The __name argument is a variable name for the
100
table entry since C does not allow us to define anonymous data
101
structures. This name should be chosen so that it is unique and does
102
not pollute the name space. The __pattern
103
argument is the match pattern. The __handler
104
argument is a pointer to the handler function and
105
__arg the user defined value.
106
107
108
The link-time table building means that several different pieces of
109
code can define server table entries, and so long as the patterns do
110
not clash they can be totally oblivious of each other. However, note
111
also that this mechanism does not guarantee the order in which entries
112
appear, this depends on the order of object files in the link, which
113
could vary from one build to the next. So any tricky pattern matching
114
that relies on this may not always work.
115
116
117
A request filename matches an entry in the table if either it exactly
118
matches the pattern string, or if the pattern ends in an asterisk, and
119
it matches everything up to that point. So for example the pattern
120
"/monitor/threads.html" will only match that exact filename,
121
but the pattern "/monitor/thread-*" will match
122
"/monitor/thread-0040.html",
123
"/monitor/thread-0100.html" and any other filename starting
124
with "/monitor/thread-".
125
126
127
When a pattern is matched, the hander function is called. It has the
128
following prototype:
129
130
131
cyg_bool cyg_httpd_handler(FILE *client,
132
                           char *filename,
133
                           char *formdata,
134
                           void *arg);
135
136
137
The client argument is the TCP connection to
138
the client: anything output through this stream will be returned to
139
the browser. The filename argument is the
140
filename from the HTTP request and the formdata
141
argument is any form response data, or NULL if none was sent. The
142
arg argument is the user defined value from the
143
table entry.
144
145
146
The handler is entirely responsible for generating the response to the
147
client, both HTTP header and content. If the handler decides that it
148
does not want to generate a response it can return
149
false, in which case the table scan is resumed for
150
another match. If no match is found, or no handler returns true, then
151
a default response page is generated indicating that the requested
152
page cannot be found.
153
154
155
Finally, the server thread closes the connection to the client and
156
loops back to accept a new connection.
157
158
159
 
160
161
 
162
163
Server Configuration
164
165
The HTTP server has a number of configuration options:
166
167
 
168
169
<literal>CYGNUM_HTTPD_SERVER_PORT</literal>
170
171
This option defines the TCP port that the server will listen on. It
172
defaults to the standard HTTP port number 80. It may be changed to a
173
different number if, for example, another HTTP server is using the
174
main HTTP port.
175
176
177
 
178
179
<literal>CYGDAT_HTTPD_SERVER_ID</literal>
180
181
This is the string that is reported to the client in the
182
"Server:" field of the HTTP header.
183
184
185
 
186
187
<literal>CYGNUM_HTTPD_THREAD_COUNT</literal>
188
189
The HTTP server can be configured to use more than one thread to
190
service HTTP requests. If you expect to serve complex pages with many
191
images or other components that are fetched separately, or if any
192
pages may take a long time to send, then it may be useful to increase
193
the number of server threads. For most uses, however, the connection
194
queuing in the TCP/IP stack and the speed with which each page is
195
generated, means that a single thread is usually adequate.
196
197
198
 
199
200
<literal>CYGNUM_HTTPD_THREAD_PRIORITY</literal>
201
202
The HTTP server threads can be run at any priority. The exact priority
203
depends on the importance of the server relative to the rest of the
204
system. The default is to put them in the middle of the priority range
205
to provide reasonable response without impacting genuine high priority
206
threads.
207
208
209
 
210
211
<literal>CYGNUM_HTTPD_THREAD_STACK_SIZE</literal>
212
213
This is the amount of stack to be allocated for each of the HTTPD
214
threads. The actual stack size allocated will be this value plus the
215
values of CYGNUM_HAL_STACK_SIZE_MINIMUM and
216
CYGNUM_HTTPD_SERVER_BUFFER_SIZE.
217
218
219
 
220
221
<literal>CYGNUM_HTTPD_SERVER_BUFFER_SIZE</literal>
222
223
This defines the size of the buffer used to receive the first line of
224
each HTTP request. If you expect to use particularly long URLs or have
225
very complex forms, this should be increased.
226
227
228
 
229
230
<literal>CYGNUM_HTTPD_SERVER_AUTO_START</literal>
231
232
This option causes the HTTP Daemon to be started automatically during
233
system initialization. If this option is not set then the application
234
must start the daemon explicitly by calling
235
cyg_httpd_startup(). This option is set by
236
default.
237
238
239
 
240
241
<literal>CYGNUM_HTTPD_SERVER_DELAY</literal>
242
243
This defines the number of system clock ticks that the HTTP server
244
will wait before initializing itself and spawning any extra server
245
threads. This is to give the application a chance to initialize
246
properly without any interference from the HTTPD.
247
248
249
 
250
251
 
252
253
 
254
255
Support Functions and Macros
256
257
The emphasis of this server is on dynamically generated content,
258
rather than fetching it from a filesystem. To do this the handler
259
functions make calls to fprintf() and
260
fputs(). Such handler functions would end up a
261
mass of print calls, with the actual structure of the HTML page hidden
262
in the format strings and arguments, making maintenance and debugging
263
very difficult. Such an approach would also result in the definition
264
of many, often only slightly different, format strings, leading to
265
unnecessary bloat.
266
267
268
In an effort to expose the structure of the HTML in the structure of
269
the C code, and to maximize the sharing of string constants, the
270
cyg/httpd/httpd.h header file defines a set of
271
helper functions and macros. Most of these are wrappers for predefined
272
print calls on the client stream passed to the
273
hander function. For examples of their use, see the System Monitor
274
example.
275
276
277
278
All arguments to macros are pointers to strings, unless otherwise
279
stated. In general, wherever a function or macro has an
280
attr or __attr
281
parameter, then the contents of this string will be inserted into the
282
tag being defined as HTML attributes. If it is a NULL or empty string
283
it will be ignored.
284
285
286
 
287
288
HTTP Support
289
290
void cyg_http_start( FILE *client, char *content_type, int content_length );
291
void cyg_http_finish( FILE *client );
292
#define html_begin(__client)
293
#define html_end( __client )
294
295
296
The function cyg_http_start() generates a simple
297
HTTP response header containing the value of
298
CYGDAT_HTTPD_SERVER_ID in the "Server" field, and the
299
values of content_type and
300
content_length in the "Content-type"
301
and "Content-length" field respectively. The function
302
cyg_http_finish() just adds an extra newline to
303
the end of the output and then flushes it to force the data out to the
304
client.
305
306
307
The macro html_begin() generates an HTTP header
308
with a "text/html" content type followed by an opening
309
"<html>" tag. html_end() generates
310
a closing "</html>" tag and calls
311
cyg_http_finish().
312
313
314
 
315
316
General HTML Support
317
318
void cyg_html_tag_begin( FILE *client, char *tag, char *attr );
319
void cyg_html_tag_end( FILE *client, char *tag );
320
#define html_tag_begin( __client, __tag, __attr )
321
#define html_tag_end( __client, __tag )
322
#define html_head( __client, __title, __meta )
323
#define html_body_begin( __client, __attr )
324
#define html_body_end( __client )
325
#define html_heading( __client, __level, __heading )
326
#define html_para_begin( __client, __attr )
327
#define html_url( __client, __text, __link )
328
#define html_image( __client, __source, __alt, __attr )
329
330
331
The function cyg_html_tag_begin() generates an
332
opening tag with the given name. The function
333
cyg_html_tag_end() generates a closing tag with
334
the given name. The macros html_tag_begin() and
335
html_tag_end are just wrappers for these functions.
336
337
338
The macro html_head() generates an HTML header
339
section with __title as the title. The
340
__meta argument defines any meta tags that will
341
be inserted into the header. html_body_begin() and
342
html_body_end generate HTML body begin and end
343
tags.
344
345
346
html_heading() generates a complete HTML header
347
where __level is a numerical level, between 1
348
and 6, and __heading is the heading
349
text. html_para_begin() generates a paragraph
350
break.
351
352
353
html_url() inserts a URL where
354
__text is the displayed text and
355
__link is the URL of the linked
356
page. html_image() inserts an image tag where
357
__source is the URL of the image to be
358
included and __alt is the alternative text for
359
when the image is not displayed.
360
361
362
 
363
364
Table Support
365
366
#define html_table_begin( __client, __attr )
367
#define html_table_end( __client )
368
#define html_table_header( __client, __content, __attr )
369
#define html_table_row_begin( __client, __attr )
370
#define html_table_row_end( __client )
371
#define html_table_data_begin( __client, __attr )
372
#define html_table_data_end( __client )
373
374
375
html_table_begin() starts a table and
376
html_table_end() end
377
it. html_table_header() generates a simple table
378
column header containg the string __content.
379
380
381
html_table_row_begin() and
382
html_table_row_end() begin and end a table row,
383
and similarly html_table_data_begin() and
384
html_table_data_end() begin and end a table
385
entry.
386
387
388
 
389
390
Forms Support
391
392
#define html_form_begin( __client, __url, __attr )
393
#define html_form_end( __client )
394
#define html_form_input( __client, __type, __name, __value, __attr )
395
#define html_form_input_radio( __client, __name, __value, __checked )
396
#define html_form_input_checkbox( __client, __name, __value, __checked )
397
#define html_form_input_hidden( __client, __name, __value )
398
#define html_form_select_begin( __client, __name, __attr )
399
#define html_form_option( __client, __value, __label, __selected )
400
#define html_form_select_end( __client )
401
void cyg_formdata_parse( char *data, char *list[], int size );
402
char *cyg_formlist_find( char *list[], char *name );
403
404
405
html_form_begin() begins a form, the
406
__url argument is the value for the
407
action
408
attribute. html_form_end() ends the form.
409
410
411
html_form_input() defines a general form input
412
element with the given type, name and
413
value. html_form_input_radio creates a radio button
414
with the given name and value; the __checked
415
argument is a boolean expression that is used to determine whether the
416
checked attribute is added to the tag. Similarly
417
html_form_input_checkbox() defines a checkbox
418
element. html_form_input_hidden() defines a hidden
419
form element with the given name and value.
420
421
422
html_form_select_begin() begins a multiple choice
423
menu with the given name. html_form_select_end()
424
end it. html_form_option() defines a menu entry
425
with the given value and label; the __selected
426
argument is a boolean expression controlling whether the
427
selected attribute is added to the tag.
428
429
430
cyg_formdata_parse() converts a form response
431
string into an NULL-terminated array of
432
"name=value" entries. The data
433
argument is the string as passed to the handler function; note that
434
this string is not copied and will be updated in place to form the
435
list entries.  list is a pointer to an array of
436
character pointers, and is size elements long.
437
cyg_formlist_find() searches a list generated by
438
cyg_formdata_parse() and returns a pointer to the
439
value part of the string whose name part matches
440
name; if there is no match it will return
441
NULL.
442
443
444
 
445
446
Predefined Handlers
447
448
 
449
cyg_bool cyg_httpd_send_html( FILE *client, char *filename, char *request, void *arg );
450
 
451
typedef struct
452
{
453
    char        *content_type;
454
    cyg_uint32  content_length;
455
    cyg_uint8   *data;
456
} cyg_httpd_data;
457
#define CYG_HTTPD_DATA( __name, __type, __length, __data )
458
 
459
cyg_bool cyg_httpd_send_data( FILE *client, char *filename, char *request, void *arg );
460
 
461
462
463
The HTTP server defines a couple of predefined handers to make it
464
easier to deliver simple, static content.
465
466
467
cyg_httpd_send_html() takes a
468
NULL-terminated string as the argument and sends it
469
to the client with an HTTP header indicating that it is HTML. The
470
following is an example of its use:
471
472
473
 
474
char cyg_html_message[] = "<head><title>Welcome</title></head>\n"
475
                          "<body><h2>Welcome to my Web Page</h2></body>\n"
476
 
477
CYG_HTTPD_TABLE_ENTRY( cyg_html_message_entry,
478
                       "/message.html",
479
                       cyg_httpd_send_html,
480
                       cyg_html_message );
481
 
482
483
484
cyg_httpd_send_data() Sends arbitrary data to the
485
client. The argument is a pointer to a cyg_httpd_data
486
structure that defines the content type and length of the data, and a
487
pointer to the data itself. The CYG_HTTPD_DATA()
488
macro automates the definition of the structure. Here is a typical
489
example of its use:
490
491
492
 
493
static cyg_uint8 ecos_logo_gif[] = {
494
    ...
495
};
496
 
497
CYG_HTTPD_DATA( cyg_monitor_ecos_logo_data,
498
                "image/gif",
499
                sizeof(ecos_logo_gif),
500
                ecos_logo_gif );
501
 
502
CYG_HTTPD_TABLE_ENTRY( cyg_monitor_ecos_logo,
503
                       "/monitor/ecos.gif",
504
                       cyg_httpd_send_data,
505
                       &cyg_monitor_ecos_logo_data );
506
 
507
508
509
 
510
511
 
512
513
 
514
515
System Monitor
516
517
Included in the HTTPD package is a simple System Monitor that is
518
intended to act as a test and an example of how to produce servers.
519
It is also hoped that it might be of some use in and of itself.
520
521
522
The System Monitor is intended to work in the background of any
523
application. Adding the network stack and the HTTPD package to any
524
configuration will enable the monitor by default. It may be disabled
525
by disabling the CYGPKG_HTTPD_MONITOR option.
526
527
528
The monitor is intended to be simple and self-explanatory in use. It
529
consists of four main pages. The thread monitor page presents a table
530
of all current threads showing such things as id, state, priority,
531
name and stack dimensions. Clicking on the thread ID will link to a
532
thread edit page where the thread's state and priority may be
533
manipulated. The interrupt monitor just shows a table of the current
534
interrupts and indicates which are active. The memory monitor shows a
535
256 byte page of memory, with controls to change the base address and
536
display element size. Note: Accessing invalid memory locations can cause
537
memory exceptions and the program to crash. The network monitor page shows
538
information extracted from the active network interfaces and
539
protocols. Finally, if kernel instrumentation is enabled, the
540
instrumentation page provides some controls over the instrumentation
541
mechanism, and displays the instrumentation buffer.
542
543
544
 
545
546
 
547
548

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.