Functions

Functions

int32_t http_server_init (user_type *user, mmc_function_type *(*d8900_init_mmc)(void), int32_t max_connections)
 Initialize HTTP server. More...
 
void http_define_callback (int32_t(*callback_func)(httpserv_type *))
 Define a user callback function for HTTP requests. More...
 
int32_t http_interpreter (httpserv_type *http_server)
 User defined callback function. More...
 
void websocket_define_callback (int32_t(*callback_func)(ws_socket_type *))
 Define a user callback function for websocket requests. More...
 
void http_set_authentication (char *auth_name, char *auth_key, direntry_type *err401, direntry_type *dir_list,...)
 Initialize a secure web page. More...
 
int32_t http_get_uri (httpserv_type *http_server)
 Search user defined Uniform Resource Identifiers (URI) list for requested uri. More...
 
int32_t http_send_user_page (httpserv_type *http_info, user_type *puser, int32_t uri)
 Send user defined webpage. More...
 
int32_t http_send_uri (httpserv_type *http_server, direntry_type *uri)
 Send URI using the specified URI without looking up in user list. More...
 
http_con_typehttp_set_up_page (httpserv_type *http_info, user_type *puser, int32_t uri)
 set up a page for later transmission More...
 
int32_t http_send_page (http_con_type *con)
 start page transmission More...
 
int32_t http_server (void)
 The main polling function for concurrent HTTP server. More...
 
void http_set_index_page (uint32_t page)
 Define index page. More...
 
int32_t http_add_page (direntry_type *uri)
 Dynamically add a webpage during runtime. More...
 
int32_t http_send_websocket (ws_socket_type *ws, char *buffer, uint32_t length)
 send a message over connected websocket More...
 

Detailed Description

HTTP Server

Function Documentation

int32_t http_server_init ( user_type user,
mmc_function_type *(*)(void)  d8900_init_mmc,
int32_t  max_connections 
)

This function is the main HTTP server initialization function. It must be called once after net_init() to install the HTTP service. The user list must contain the keyword "webserver" as user name for a valid service name. Any other or additional user names are not allowed. d8900_init_mmc() is an old and obsolete MMC initialization function for the D.Module.CS8900. Any webpage access can lead to a number of parallel opened connections. All sockets and all temporary information for each connection is taken from heap. If you want to limit the heap consumption, use parameter max_connection.

Parameters
*user- used for authorize users for HTTP access
*d8900_init_mmc- not available on DSK
max_connections- specifies the maximum concurrent connections
Returns
  • TRUE => HTTP server was successfully installed
  • FALSE => out of memory
Library:
http.lib
Prototype:
httplib.h
Examples:
HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.
void http_define_callback ( int32_t(*)(httpserv_type *)  callback_func)

This function installs a user callback function. It must be called at least once or any time the callback function has to be changed.

Parameters
(*callback_func)()- pointer to user callback function
Returns
-
Library:
http.lib
Prototype:
httplib.h
See also
http_interpreter()
Examples:
HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.
int32_t http_interpreter ( httpserv_type http_server)

This callback function must be implemented by user. It is called each time a web page is requested or data is received.

Parameters
http_server- current http server connection
Returns
  • _HTTP_CLOSE_CONNECTION => close connection
  • _HTTP_KEEP_ALIVE => keep alive
Library:
http.lib
Prototype:
httplib.h
Note
Parameter *http_server is only valid within the context of this function. Don't use it for later transmission!
See also
httpserv_type
1 "http_define_callback ( http_interpreter);"

Skeleton function:

1 int32_t http_interpreter (httpserv_type *http_server)
2 {
3  // ************************************************************************
4  // locals
5  // ************************************************************************
6  int32_t ret = _HTTP_CLOSE_CONNECTION;
7 
8  // ************************************************************************
9  // Execute specific method
10  // ************************************************************************
11  switch (http_server -> method)
12  {
13  case _HTTP_GET:
14  if ( http_server -> uri == -1 )
15  {
16  // ************************************************************
17  // uri not found: send user defined not-found-message
18  // ************************************************************
19 
20  http_send_uri (http_server, &e404);
21  }
22  else
23  {
24  // ************************************************************
25  // send the requested page
26  // ************************************************************
27  if ( http_send_user_page (http_server,
28  user,
29  http_server -> uri ) <= 0)
30  {
31  // *********************************************************
32  // page could not be sent immediately, keep connection alive
33  // and try again later
34  // *********************************************************
35  ret = _HTTP_KEEP_ALIVE;
36  }
37  }
38  break;
39 
40  case _HTTP_UNKNOWN : // unknown method
41  // ****************************************************************
42  // method not implemented or syntax error
43  // ****************************************************************
44  http_send_uri (http_server, &e501);
45  break;
46  }
47 
48  // ************************************************************************
49  // back to http server
50  // ************************************************************************
51  return (ret);
52 }
275 {
276  /***************************************************************************
277  locals
278  ***************************************************************************/
280  int i;
281 
282  /***************************************************************************
283  Execute specific method
284  ***************************************************************************/
285  switch (http_server-> method)
286  {
287  case _HTTP_GET:
288  /*******************************************************************
289  uri is the requested directory index
290  *******************************************************************/
291  if ( http_server-> uri == -1 )
292  {
293  /***************************************************************
294  uri not found: send user defined not-found-message
295  ***************************************************************/
296  http_send_uri (http_server, &e404);
297  }
298  else
299  {
300  if ( http_server-> uri == index.index)
301  {
302  /***************************************************************
303  If the requested page is the index page, parse form arguments
304  and toggle leds.
305  The form command in index.htm cause a request in the form:
306  GET index.htm?Led1=1&Led2=1&Led3=1&Led4=1 HTTP 1.1 ....
307  The http_server argument list holds all form parameters
308  including the requested form:
309  http_server-> argc = 9;
310  http_server-> argv[0] = "index.htm";
311  http_server-> argv[1] = "Led1";
312  http_server-> argv[2] = "1";
313  http_server-> argv[3] = "Led2";
314  http_server-> argv[4] = "1";
315  .
316  .
317  .
318  ***************************************************************/
319  for ( i = 1; i<http_server-> argc;i++)
320  {
321  if ( !strncmp (http_server-> argv[i], "Led1", 4) )
322  {
323  toggle_led (&led1, 0);
324  }
325  if ( !strncmp (http_server-> argv[i], "Led2", 4) )
326  {
327  toggle_led (&led2, 1);
328  }
329  if ( !strncmp (http_server-> argv[i], "Led3", 4) )
330  {
331  toggle_led (&led3, 2);
332  }
333  if ( !strncmp (http_server-> argv[i], "Led4", 4) )
334  {
335  toggle_led (&led4, 3);
336  }
337  }
338 
339  /***********************************************************
340  update led-status
341  ***********************************************************/
342  UpdateLedStates();
343  } // if
344 
345  /***************************************************************
346  send the requested page
347  ***************************************************************/
348  if ( http_send_user_page (http_server,
349  user,
350  http_server-> uri ) <= INT32_C(0))
351  {
352  /***********************************************************
353  page could not be sent immediately, keep connection alive
354  and try again later
355  ***********************************************************/
356  ret = _HTTP_KEEP_ALIVE;
357  }
358  }
359  break;
360 
361  case _HTTP_UNKNOWN : /* unknown method */
362  /*******************************************************************
363  method not implemented or syntax error
364  *******************************************************************/
365  http_send_uri (http_server, &e501);
366  break;
367  }
368 
369  /***************************************************************************
370  back to http server
371  ***************************************************************************/
372  return (ret);
373 }
#define _HTTP_KEEP_ALIVE
Definition: httplib.h:98
int32_t http_send_user_page(httpserv_type *http_info, user_type *puser, int32_t uri)
Send user defined webpage.
#define INT32_C(value)
Definition: stdint.h:209
direntry_type e404
Definition: webpage.c:63
int32_t index
Definition: net.h:813
direntry_type led1
Definition: HTTPdynamic.c:164
#define _HTTP_CLOSE_CONNECTION
Definition: httplib.h:99
direntry_type led3
Definition: HTTPdynamic.c:166
#define _HTTP_UNKNOWN
Definition: httplib.h:110
direntry_type index
Definition: webpage.c:132
direntry_type led4
Definition: HTTPdynamic.c:167
direntry_type e501
Definition: webpage.c:81
user_type user[]
Definition: FTPclient.c:168
void UpdateLedStates(void)
Definition: HTTPdynamic.c:184
direntry_type led2
Definition: HTTPdynamic.c:165
int32_t http_send_uri(httpserv_type *http_server, direntry_type *uri)
Send URI using the specified URI without looking up in user list.
void toggle_led(direntry_type *fp, unsigned int led_nr)
Definition: HTTPdynamic.c:233
int int32_t
Definition: stdint.h:46
#define _HTTP_GET
Definition: httplib.h:106
void websocket_define_callback ( int32_t(*)(ws_socket_type *)  callback_func)

This function installs a user callback function.

Parameters
(*callback_func)()- pointer to user callback function
Returns
-
Library:
http.lib
Prototype:
httplib.h
void http_set_authentication ( char *  auth_name,
char *  auth_key,
direntry_type err401,
direntry_type dir_list,
  ... 
)

If one or more webpages should be authenticated before the content is transmitted, use http_set_authentication(). The authentication name and key are case sensitive. The first page must be the user defined authentication failed message err401.

Parameters
auth_name- web page name
auth_key- authentication username and password
err401- user defined 'Authentication required' message
dir_list- list of secure web pages
Returns
nothing
Library:
http.lib
Prototype:
httplib.h
Note
The dir list must be terminated with a double zero:
See also
direntry_type
1 http_set_authentication ("Secure Webpage", authentication_key, &e401,
2  &secure_page, 0, 0 );
Examples:
HTTPsimple.c.
int32_t http_get_uri ( httpserv_type http_server)
Parameters
http_server- current http connection referenced by user callback function
Returns
  • (-1) the requested URI was not found
  • (0..n) index of web page specified in urilist
Library:
http.lib
Prototype:
httplib.h
Note
The URI is index of the specified webpage in user list:
See also
httpserv_type
1 user_type user[] =
2 {
3  {"webserver", "*" , &URI0, &URI1, &URI2, &URI3, .... , 0},
4  // index 0 1 2 3 ...
5 };
int32_t http_send_user_page ( httpserv_type http_info,
user_type puser,
int32_t  uri 
)

This function starts the transmission of a requested webpage or element referenced by index. The index is used to access the requested webpage from the user list.

Parameters
http_info- current http connection referenced by user callback function
puser- is a pointer to current user directory
uri- specifies the URI (referenced by index) to send
Returns
  • (-1) if out of memory
  • ( 0) the URI is invalid
  • ( 1) on success
Library:
http.lib
Prototype:
httplib.h
Note
uri is the index of the specified webpage in user list:
use this function only in context of your http_interpreter
See also
user_type, httpserv_type
http_send_uri(), http_set_up_page()
1 user_type user[] =
2 {
3  {"webserver", "*" , &URI0, &URI1, &URI2, &URI3, .... , 0},
4  // index 0 1 2 3 ...
5 };
6 ...
7  int32_t http_interpreter (httpserv_type *http_server)
8  {
9  ....
10  if ( http_server -> uri == -1 )
11  {
12  // **************************************************************
13  // uri not found: send user defined not-found-message
14  // **************************************************************
15  http_send_uri (http_server, &e404);
16  }
17  else
18  {
19  // **************************************************************
20  // send the requested page
21  // **************************************************************
22  if ( http_send_user_page (http_server,
23  user,
24  http_server -> uri ) <= 0)
25  {
26  // **********************************************************
27  // page could not be sent immediately, keep connection alive
28  // and try again later
29  // **********************************************************
30  ret = _HTTP_KEEP_ALIVE;
31  }
32  }
Examples:
HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.
int32_t http_send_uri ( httpserv_type http_server,
direntry_type uri 
)

Use this function to send a webpage for a given direntry. It is also useful to send pages not included with the user list (e.g. error messages).

Parameters
http_server- current http connection referenced by user callback function
*uri- specifies the URI to send
Returns
  • (-1) if out of memory
  • ( 0) the URI is invalid
  • ( 1) on success
Library:
http.lib
Prototype:
httplib.h
Note
Useful for sending error messages.
See also
direntry_type, httpserv_type
1 direntry_type E404 = {"e404" , "e404.htm" , sizeof(e404_c) ,
2  FTP_RW | FTP_RAM, 'A',
3  (uint32_t)e404_c };
4 .
5 .
6 http_send_uri (http_info, &E404);
Examples:
HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.
http_con_type* http_set_up_page ( httpserv_type http_info,
user_type puser,
int32_t  uri 
)

On success this function returns a pointer to a new http_con structure. The memory for this structure is taken from heap and must be freed later by function http_send_page(). The HTTP interpreter callback function should return _HTTP_KEEP_ALIVE to deactivate the shutdown timer. Use function http_send_page() with parameter http_con later in your program to start transmission over this socket.

Parameters
http_info- the current http connection referenced by user callback function
puser- a pointer to current user directory
uri- specifies the URI to send
Returns
  • NULL - invalid page or out of memory
  • !NULL - pointer to new http_con_type structure containing destination port and address
Note
use this function if the requested data is not yet available but should be transmitted later (e.g. after a DMA is ready)
Warning
this function requires a later call to http_send_page() to free memory
Library:
http.lib
Prototype:
httplib.h
See also
user_type, direntry_type, httpserv_type
http_send_page()
1 http_con = http_set_up_page (http_server,
2  user,
3  http_server-> uri );
4 
5 if ( http_con == NULL )
6 {
7  // *********************************************************************
8  // out of memory
9  // *********************************************************************
10 
11 } // if
12 else
13 {
14  // *********************************************************************
15  // valid connection, use http_send_page(http_con) later to send data
16  // *********************************************************************
17 }
int32_t http_send_page ( http_con_type con)

This function starts a page transmission. The transmission must have been set up before with function http_set_up_page(). All temporary allocated memory by function http_set_up_page() is freed.

Parameters
con- the current http connection
Returns
  • (1) connection invalid or not found
  • (0) on success
Library:
http.lib
Prototype:
httplib.h
See also
http_send_page()
int32_t http_server ( void  )
Parameters
-
Returns
  • (< 0) server out of memory
  • (>= 0) number of parallel connections
Library:
http.lib
Prototype:
httplib.h
Note
Must be called regularly with net_isq() in the main loop.
1  int main ( void )
2  {
3  ...
4 
5  // *************************************************************************
6  // main program loop: set main_loop to 0 to exit loop
7  // *************************************************************************
8  while ( main_loop )
9  {
10  // *********************************************************************
11  // call net_isq() and http_server()
12  // *********************************************************************
13  net_isq ();
14  http_server ();
15  }
16 
17  // *************************************************************************
18  // exit program, shut down peripherals
19  // *************************************************************************
20  return(0);
21 }
Examples:
HTTPdynamic.c, HTTPjava.c, and HTTPsimple.c.
void http_set_index_page ( uint32_t  page)

Since revision 2.79 the index page is determined automatically. If another uri should be the default index page call http_set_index_page().

Parameters
page- index page
Returns
nothing
Library:
http.lib
Prototype:
httplib.h
1 user_type user[] =
2 {
3  {"webserver", "*" , &URI0, &URI1, &URI2, &URI3, .... , 0},
4  // index 0 1 2 3 ...
5 };
6 ...
7  // set URI2 as index page
8  http_set_index_page(2);
int32_t http_add_page ( direntry_type uri)
Parameters
uri- webpage or element to add
Returns
  • (0) success
  • (-1) error, no webserver available or max directory limit reached
Library:
http.lib
Prototype:
httplib.h
See also
direntry_type
1 direntry_type E404 = {"e404" , "e404.htm" , sizeof(e404_c) ,
2  FTP_RW | FTP_RAM, 'A',
3  (uint32_t)e404_c };
4 .
5 .
6 http_add_page (&E404);
Examples:
HTTPdynamic.c, and HTTPjava.c.
int32_t http_send_websocket ( ws_socket_type ws,
char *  buffer,
uint32_t  length 
)
Parameters
ws- connected websocket
buffer- buffer to data to send
length- size of data to send
Returns
  • NET_SEND_SUCCESS (>0) - message was send immediately
  • NET_SEND_PENDING (=0) - message could not be delivered immediately, it was passed to protocol stack for delivery, read socket_struct ->error_code for details
  • NET_SEND_ERROR (<0) unrecoverable error
Library:
http.lib
Prototype:
httplib.h