MPFS) J*&Oj&-j.
F*>gHd̃J
O&
/
W @ I `
I x
I I 0
I I cP
cP ) X cP * uP % . P 3 5 P 4 5 cP > H ( cP ? H G H H G I H 0I R rL ,cF [ TQ X-}G c hZ A YM n e cP w f s ~P g x ~P th 8I l 8I 4l 8I o 8I o = HI 1u H HI yu I I | ` I "} BI BI 0F 0F \I \I T tI
0 tI 9 pI ` pI auth.htm cookies.htm dynvars.htm footer.inc forms.htm header.inc I2C Demos.htm index.htm leds.cgi mchp.css mchp.gif mchp.js olimex.gif snmp.bib status.xml UART.htm upload.htm snmp/snmpconfig.htm protect/config.htm protect/index.htm protect/reboot.cgi protect/reboot.htm email/index.htm dyndns/index.htm ~inc:header.inc~
Authentication
Many applications need to restrict access to authorized users. The HTTP
server supports Basic HTTP Authentication to provide this functionality.
Two functions are involved with authentication, both of which store a
return value in curHTTP.isAuthorized
. The MSB of this value
indicates whether or not access is allowed. The first callback,
HTTPNeedsAuth
, determines if the requested page requires
valid credentials to proceed. Return a value of 0x80
or greater
to allow access unconditionally. Return 0x79
or lower to require
authorization.
The second,
HTTPVerifyAuth
determines if the supplied credentials
are acceptable for the requested page. Your application should return a value
greater than
0x80
to permit access, or a value less than
0x79
to reject the password supplied.
As an example, access this restricted page:
~inc:footer.inc~ 0 ~inc:header.inc~
Cookies
Cookies are small text strings that are stored in the browser. These strings
are set by the server, and returned as parameters for all future requests until
the session ends. They can be used to store session state information, and are
valid until the browser is closed.
Name: | ~cookiename~ |
Favorite: | not implemented |
There are numerous uses for cookies. In this example, the user's name is
stored as a cookie. All future page loads will show this name in the top left
corner, as well as in the box above.
Cookies are set by storing null-terminated name/value pairs in curHTTP.data
and indicating how many cookies are to be set in curHTTP.hasArgs
. They
can be set in either HTTPExecuteGet
or HTTPExecutePost
.
Cookies are retrieved in the same manner as GET form arguments. They are stored
in curHTTP.data
and can be located using the HTTPGetArg
functions. Since they are stored in the same array as GET arguments, the total
length of cookies and variable arguments cannot exceed the 80 byte buffer limit.
Exercise: Set a second cookie named fav
with the
value of the favorite
field and have it displayed in the gray box above.
~inc:footer.inc~ j ~inc:header.inc~
Dynamic Variables
One of the most basic needs is to provide status information back to the user of
your web application. The HTTP server provides for this using dynamic
variable substitution callbacks. These commands in your HTML code will
alert the server to execute a callback function at that point.
To insert a dynamic variable, place the name of the variable inside of the tilde
(~~
) character, like this: ~~myVariable~~
. When that
sequence is found, the server will call the function HTTPPrint_myVariable()
.
For example, here's the build date of the HEX file programmed in your part:
~builddate~
You can also pass parameters to dynamic variables by placing numeric values inside
of parenthesis after the variable name. ~~led(2)~~
will print the value
of the second LED. The numeric values are passed as WORD values to your callback function.
You can pass as many parameters as you wish to these functions, and if your C code has
constants defined, those will be parsed as well.
As an example, here is a binary representation which LEDs are on and off on the board:
~led(7)~ ~led(6)~ ~led(5)~ ~led(4)~ ~led(3)~ ~led(2)~ ~led(1)~ ?
Exercise: Try to add the last (LSB) LED to the output above.
For short outputs (less than 16 bytes) you need only to call the appropriate
TCPPut
function and return. For longer outputs the output state must be
managed through successive calls, which prevents the limited buffer space from being
overrun.
As an example, here is the current LCD display (which uses 32 bytes):
~lcdtext~
You can also use dynamic variables to include files. This is useful to save
storage space for your pages, since a portion of most pages (header, menu, footer)
do not change. By placing ~~inc:filename.inc~~
in your HTML code,
the file filename.inc
will be read and inserted at this location.
Exercise: Look at code for this page and find how
header.inc
and footer.inc
are used. These files provide
a template for the pages to include the same menu, layout, and design elements.
(At this time, dynamic variables are non-recursive, so variables located inside
files
included in this manner are not parsed.)
~inc:footer.inc~ A J S \ e
n w h
C Z