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
|
Cactus Code Thorn httpd
Authors : Tom Goodale
CVS info : $Header$
--------------------------------------------------------------------------
Purpose of the thorn:
-------------------
This is a prototype C version of a web server thorn.
Usage:
-------------------
Interface:
---------
The current interface for this webserver is contained in
http_Request.h
(Note that this will change once we have an agreed common protocol for
other thorns to talk to web-serving thorns.)
The primary mechanism is to
HTTP_RegisterPage(path, function, data)
where 'path' is the URI you want to register, e.g.
/parameters
'function' is a function of the form
int foo(cGH *cctkGH, httpRequest *request, void *data)
which will be called when the page is accessed, with 'data' being the
same data pointer that was passed into the registration routine.
Note that a request of the form '/foo/bar/' will look for a page of
the called '/foo/var/index.html' first, followed by '/foo/bar',
followed by '/foo' followed by '/'.
The httpRequest structure contains various pieces of information -
request->filedes The file descriptor of the socket to write
to.
request->body The body of the http request. This is
empty for a GET.
request->body_length The length of the body.
request->method The http method.
request->uri The full URI of the request.
request->residual The URI remaining after the registered URI
is removed from it.
request->http_major_version The major version of the HTTP protocol
used.
request->http_minor_version The minor version of the HTTP protocol
used.
request->n_arguments The number of arguments passed to the
request.
All other members of the structure should be considered opaque.
To get details of arguments or HTTP header lines, the functions
const char *HTTP_ArgumentValue(const httpRequest *request,
const char *arg);
const char *HTTP_HeaderValue(const httpRequest *request,
const char *header);
may be used. Given the name of a header field or an argument, these
functions will return the corresponding value, or NULL if the argument
or header does not exist.
Writing to the browser:
----------------------
Currently this is done via the 'write' command. This takes three
arguments - the file-descripter, a buffer, and the length of the
buffer.
The first line sent to the browser should be
write(request->filedes, "HTTP/1.0 200 OK\r\n",19)
(or an error code, e.g. "404 Not Found"). This should be followed by
any HTTP headers, such as "Content-Type", followed by "\r\n\r\n" and
then the content of your page, or at least that's what the standard
says 8-).
Examples:
---------
For examples of how to use the interface, please look at the Content.c
file.
Structure of the thorn:
----------------------
The thorn is split into two parts - web serving and content provision.
Web serving:
-----------
The web server capabilities are held in
Sockets.c - basic socket utilities; these should be
replaced by or moved to the Socket thorn.
Server.c - Stuff to register and serve pages.
http.c - Parsing of HTTP requests.
Startup.c - normal scheduled stuff.
The above files should not need to be touched apart from debugging
purposes, to add more HTTP methods, or otherwise to enhance the
interaction with the network and protocols.
Content Provision:
-----------------
The content provision is currently all in
Content.c
This makes use of the interfaces in http_Request.h described above to
provide content. In principle this could be in another thorn, but
that should wait until we have an agreed set of interfaces to allow
web-server thorns to be interchangeable, at least at compile time, but
hopefully at run-time.
|