forked from MaJerle/lwesp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.c
184 lines (166 loc) · 6.17 KB
/
http_server.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "http_server.h"
#include "lwesp/lwesp.h"
#include "lwesp/apps/lwesp_http_server.h"
#include "lwesp/apps/lwesp_http_server_fs.h"
static size_t http_ssi_cb(http_state_t* hs, const char* tag_name, size_t tag_len);
#if HTTP_SUPPORT_POST
static lwespr_t http_post_start_cb(http_state_t* hs, const char* uri, uint32_t content_len);
static lwespr_t http_post_data_cb(http_state_t* hs, lwesp_pbuf_p pbuf);
static lwespr_t http_post_end_cb(http_state_t* hs);
#endif /* HTTP_SUPPORT_POST */
static char* led_cgi_handler(http_param_t* params, size_t params_len);
static char* usart_cgi_handler(http_param_t* params, size_t params_len);
/**
* \brief List of CGI handlers
*/
const http_cgi_t
cgi_handlers[] = {
{ "/led.cgi", led_cgi_handler },
{ "/usart.cgi", usart_cgi_handler },
};
/**
* \brief HTTP init structure
*/
const http_init_t
http_init = {
#if HTTP_SUPPORT_POST
.post_start_fn = http_post_start_cb,
.post_data_fn = http_post_data_cb,
.post_end_fn = http_post_end_cb,
#endif /* HTTP_SUPPORT_POST */
.cgi = cgi_handlers,
.cgi_count = LWESP_ARRAYSIZE(cgi_handlers),
.ssi_fn = http_ssi_cb,
/*
* Use native WIN32 file system API
*/
#if WIN32
.fs_open = http_fs_open,
.fs_read = http_fs_read,
.fs_close = http_fs_close,
#endif /* WIN32 */
};
/**
* \brief Start HTTP server on port 80
* \return \ref lwespOK on success, member of \ref lwespr_t otherwise
*/
lwespr_t
http_server_start(void) {
lwespr_t res;
printf("Starting HTTP server on port 80...\r\n");
if ((res = lwesp_http_server_init(&http_init, 80)) == lwespOK) {
printf("HTTP server ready!\r\n");
} else {
printf("Cannot start HTTP server\r\n");
}
return res;
}
#if HTTP_SUPPORT_POST
/**
* \brief Callback function indicating post request method started
* \param[in] hs: HTTP state
* \param[in] uri: NULL-terminated uri string for POST request
* \param[in] content_len: Total content length received by "Content-Length" header
* \return \ref lwespOK on success, member of \ref lwespr_t otherwise
*/
static lwespr_t
http_post_start_cb(http_state_t* hs, const char* uri, uint32_t content_len) {
printf("POST started with %d length on URI: %s\r\n", (int)content_len, uri);
return lwespOK;
}
/**
* \brief Callback function indicating post request data received
* \param[in] hs: HTTP state
* \param[in] pbuf: New chunk of received data
* \return \ref lwespOK on success, member of \ref lwespr_t otherwise
*/
static lwespr_t
http_post_data_cb(http_state_t* hs, lwesp_pbuf_p pbuf) {
printf("POST data received: %d bytes\r\n", (int)lwesp_pbuf_length(pbuf, 1));
return lwespOK;
}
/**
* \brief Callback function indicating post request finished
* \param[in] hs: HTTP state
* \return \ref lwespOK on success, member of \ref lwespr_t otherwise
*/
static lwespr_t
http_post_end_cb(http_state_t* hs) {
printf("POST finished!\r\n");
return lwespOK;
}
#endif /* HTTP_SUPPORT_POST */
/**
* \brief Global SSI callback function
*
* Called in case SSI tag was found and ready to be replaced by custom data
*
* \param[in] hs: HTTP state
* \param[in] tag_name: Name of tag
* \param[in] tag_len: Length of tag
* \return 1 if more data to write on this tag or 0 if everything written for specific tag
*/
static size_t
http_ssi_cb(http_state_t* hs, const char* tag_name, size_t tag_len) {
static char ssi_buffer[32];
static uint32_t cnt;
cnt++;
LWESP_UNUSED(ssi_buffer);
if (!strncmp(tag_name, "title", tag_len)) {
lwesp_http_server_write_string(hs, "ESP8266 SSI TITLE");
return cnt % 3;
} else if (!strncmp(tag_name, "led_status", tag_len)) {
lwesp_http_server_write_string(hs, "Led is on");
} else if (!strncmp(tag_name, "wifi_list", tag_len)) {
size_t i = 0;
LWESP_UNUSED(i);
lwesp_http_server_write_string(hs, "<table class=\"table\">");
lwesp_http_server_write_string(hs, "<thead><tr><th>#</th><th>SSID</th><th>MAC</th><th>RSSI</th></tr></thead><tbody>");
//for (i = 0; i < apf; i++) {
// lwesp_http_server_write_string(hs, "<tr><td>");
// sprintf(ssi_buffer, "%d", (int)i);
// lwesp_http_server_write_string(hs, ssi_buffer);
// lwesp_http_server_write_string(hs, "</td><td>");
// lwesp_http_server_write_string(hs, aps[i].ssid);
// lwesp_http_server_write_string(hs, "</td><td>");
// sprintf(ssi_buffer, "%02X:%02X:%02X:%02X:%02X:%02X", aps[i].mac[0], aps[i].mac[1], aps[i].mac[2], aps[i].mac[3], aps[i].mac[4], aps[i].mac[5]);
// lwesp_http_server_write_string(hs, ssi_buffer);
// lwesp_http_server_write_string(hs, "</td><td>");
// sprintf(ssi_buffer, "%d", (int)aps[i].rssi);
// lwesp_http_server_write_string(hs, ssi_buffer);
// lwesp_http_server_write_string(hs, "</td></tr>");
//}
lwesp_http_server_write_string(hs, "</tbody></table>");
}
return 1;
}
/**
* \brief CGI handler function when user connects to "http://ip/led.cgi?param1=value1¶m2=value2"
* \param[in] params: Pointer to list of parameters with URI
* \param[in] params_len: Number of parameters
* \return URI string to return to user
*/
char*
led_cgi_handler(http_param_t* params, size_t params_len) {
printf("LED CGI HANDLER\r\n");
while (params_len--) {
printf("Param: name = %s, value = %s\r\n", params->name, params->value);
params++;
}
return "/index.shtml";
}
/**
* \brief CGI handler function when user connects to "http://ip/usart.cgi?param1=value1¶m2=value2"
* \param[in] params: Pointer to list of parameters with URI
* \param[in] params_len: Number of parameters
* \return URI string to return to user
*/
char*
usart_cgi_handler(http_param_t* params, size_t params_len) {
printf("USART CGI HANDLER!\r\n");
while (params_len--) {
printf("Param: name = %s, value = %s\r\n", params->name, params->value);
params++;
}
return "/index.html";
}