Internet Middleware API Reference  Rev 1.0
HTTPD

This section describes the HTTP daemon (HTTPD) APIs. More...

Overview

This section describes the HTTP daemon (HTTPD) APIs.


HTTPD is an information technology that processes requests using HTTP protocol, the basic network protocol used to distribute information on the World Wide Web. The term can refer either to the entire computer system, an appliance, or specifically to the software that accepts and supervises the HTTP requests.

How to use this module.

Functions

HTTPD_RESULT httpd_init (void)
 This function initiates the HTTP server. More...
 
HTTPD_RESULT httpd_start (httpd_para *parameter)
 This function starts the HTTP server. More...
 
HTTPD_RESULT httpd_stop (void)
 This function stops the HTTP server. More...
 
HTTPD_STATUS httpd_get_status (void)
 This function queries the HTTP server's status. More...
 

Modules

 Enum
 
 Struct
 

Function Documentation

HTTPD_STATUS httpd_get_status ( void  )

This function queries the HTTP server's status.

User can poll the server status instead of receiving a feedback.

Returns
Provides the function call result.
If the return value is HTTPD_STATUS_UNINIT, the server did not initiate;
If the return value is HTTPD_STATUS_RUN, the server execution is in progress;
If the return value is HTTPD_STATUS_STOPPING, the server execution is stopping;
If the return value is HTTPD_STATUS_STOP, the server execution has stopped.
Example
1 // query HTTP server status
2 HTTPD_STATUS status;
3 httpd_para para;
4 status = HTTPD_STATUS();
5 switch (status) {
6  case HTTPD_STATUS_UNINIT:
7  printf("\n\n[HTTPDX] HTTPD GET STATUS - the server did not initiate\n\n");
8  break;
9  case HTTPD_STATUS_RUN:
10  printf("\n\n[HTTPDX] HTTPD GET STATUS - the server execution is in progress\n\n");
11  break;
12  case HTTPD_STATUS_STOP:
13  printf("\n\n[HTTPDX] HTTPD GET STATUS - the server execution has stopped.\n\n");
14  break;
15  case HTTPD_STATUS_STOPPING:
16  printf("\n\n[HTTPDX] HTTPD GET STATUS - the server execution is stopping. Please wait for a feedback or polling status update.\n\n");
17  break;
18 }
19 break;
HTTPD_RESULT httpd_init ( void  )

This function initiates the HTTP server.

Returns
Provides the function call result.
If the return value is HTTPD_RESULT_SUCCESS, the operation completed successfully.
No other result is expected.
Example
1 // initiate HTTP server
2 HTTPD_RESULT result;
3 result = httpd_init();
4 switch (result) {
5  case HTTPD_RESULT_SUCCESS: // The operation completed successfully.
6  printf("\n\nHTTPD INIT - Success\n\n");
7  break;
8  case HTTPD_RESULT_WAITING: // The user needs to wait for a feedback or polling status update.
9  case HTTPD_RESULT_UNINIT: // The server did not initiate.
10  case HTTPD_RESULT_QUEUE_FULL: // The operation cannot be completed, the server queue is full.
11  break;
12 }
13 break;
HTTPD_RESULT httpd_start ( httpd_para parameter)

This function starts the HTTP server.

Parameters
[in]parameteris configuration parameter to start the server. If it is NULL, the server starts with default setting.
Returns
Provides the function call result.
If the return value is HTTPD_RESULT_SUCCESS, the operation completed successfully;
If the return value is HTTPD_RESULT_WAITING, the user needs to wait for a feedback or polling status update;
If the return value is HTTPD_RESULT_UNINIT, the server did not initiate;
If the return value is HTTPD_RESULT_QUEUE_FULL, the server queue is full, the operation cannot be completed.
Example
1 #define CASE_SUCC 1
2 #define CASE_FAIL -1
3 
4 static xQueueHandle mbox = NULL;
5 static int httpd_fb_handle(void);
6 
7 int main(int argc, char* agrv[])
8 {
9  HTTPD_RESULT result = HTTPD_RESULT_UNINIT;
10  int case_result = CASE_FAIL;
11  httpd_para parameter;
12 
13  result = httpd_init();
14 
15  switch (result) {
16  case HTTPD_RESULT_SUCCESS: // The operation completed successfully.
17  break;
18  case HTTPD_RESULT_WAITING: // The user needs to wait for a feedback or polling status update.
19  case HTTPD_RESULT_UNINIT: // The server did not initiate.
20  case HTTPD_RESULT_QUEUE_FULL: // The operation cannot be completed, the server queue is full.
21  return case_result;
22  }
23 
24  mbox = xQueueCreate(4, sizeof(httpd_fb));
25  if (mbox == NULL) {
26  return case_result;
27  }
28 
29  memset(&parameter, 0, sizeof(httpd_para));
30  parameter.fb_queue = mbox;
31  result = httpd_start(&parameter);
32 
33  switch (result) {
34  case HTTPD_RESULT_SUCCESS: // The operation completed successfully.
35  case_result = CASE_SUCC;
36  break;
37  case HTTPD_RESULT_WAITING: // The user needs to wait for a feedback or polling status update.
38  case_result = httpd_fb_handle();
39  break;
40  case HTTPD_RESULT_UNINIT: // The server did not initiate.
41  case HTTPD_RESULT_QUEUE_FULL: // The operation cannot be completed, the server queue is full.
42  break;
43  }
44 
45  vQueueDelete(mbox);
46  mbox = NULL;
47 
48  return case_result;
49 }
50 
51 static int httpd_fb_handle(void) // Waiting for a feedback.
52 {
53  int delay_num = 15;
54  httpd_fb fb;
55 
56  for ( ; delay_num > 0; delay_num--) {
57  vTaskDelay(1000); // a delay of 1000ms.
58 
59  if (xQueueReceive(mbox, (void *)&fb, 0) == pdPASS) {
60  switch (fb.status) {
61  case HTTPD_STATUS_RUN:
62  return CASE_SUCC; // The server execution is in progress.
63  case HTTPD_STATUS_STOPPING:
64  case HTTPD_STATUS_STOP:
65  break; // Waiting for a feedback.
66  case HTTPD_STATUS_UNINIT:
67  return CASE_FAIL; // The server did not initiate.
68  }
69  }
70  }
71 
72  return CASE_FAIL;
73 }
HTTPD_RESULT httpd_stop ( void  )

This function stops the HTTP server.

Returns
Provides the function call result.
If the return value is HTTPD_RESULT_SUCCESS, the operation completed successfully;
If the return value is HTTPD_RESULT_WAITING, the user needs to wait for a feedback or polling status update;
If the return value is HTTPD_RESULT_UNINIT, the server did not initiate;
If the return value is HTTPD_RESULT_QUEUE_FULL, the server queue is full, the operation cannot be completed.
Example
1 #define CASE_SUCC 1
2 #define CASE_FAIL -1
3 
4 static int httpd_poll_status(void);
5 
6 int main(int argc, char* agrv[])
7 {
8  HTTPD_RESULT result = HTTPD_RESULT_UNINIT;
9  int case_result = CASE_FAIL;
10  httpd_para parameter;
11 
12  result = httpd_init();
13 
14  switch (result) {
15  case HTTPD_RESULT_SUCCESS: // The operation completed successfully.
16  break;
17  case HTTPD_RESULT_WAITING: // The user needs to wait for a feedback or poll the status.
18  case HTTPD_RESULT_UNINIT: // The server did not initiate.
19  case HTTPD_RESULT_QUEUE_FULL: // The operation cannot be completed, the server queue is full.
20  return case_result;
21  }
22 
23  result = httpd_stop();
24 
25  switch (result) {
26  case HTTPD_RESULT_SUCCESS: // The operation completed successfully.
27  case_result = CASE_SUCC;
28  break;
29  case HTTPD_RESULT_WAITING: // The user needs to wait for a feedback or polling status update.
30  case_result = httpd_poll_status();
31  break;
32  case HTTPD_RESULT_UNINIT: // The server did not initiate.
33  case HTTPD_RESULT_QUEUE_FULL: // The operation cannot be completed, the server queue is full.
34  break;
35  }
36 
37  return case_result;
38 }
39 
40 static int httpd_poll_status(void) // Polling status update.
41 {
42  int delay_num = 15;
43  HTTPD_STATUS status = HTTPD_STATUS_UNINIT;
44 
45  for ( ; delay_num > 0; delay_num--) {
46  vTaskDelay(1000); // a delay of 1000ms.
47 
48  status = httpd_get_status();
49 
50  switch (status) {
51  case HTTPD_STATUS_STOP:
52  return CASE_SUCC; // The server execution has stopped.
53  case HTTPD_STATUS_STOPPING:
54  case HTTPD_STATUS_RUN:
55  break; // Polling status is required.
56  case HTTPD_STATUS_UNINIT:
57  return CASE_FAIL; // The server did not initiate.
58  }
59  }
60 
61  return CASE_FAIL;
62 }