-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgi.cpp
83 lines (71 loc) · 2.25 KB
/
cgi.cpp
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
#include "lwip/apps/httpd.h"
#include "pico/cyw43_arch.h"
#include "lwipopts.h"
#include "cgi.h"
static const tCGI cgi_handlers[] = {
{
"/set_text.cgi", cgi_handler_set_text
},
};
void render_text(const char* text, uint32_t _ms_per_scroll_tick);
void urldecode2(char *dst, const char *src)
{
char a, b;
while (*src) {
if ((*src == '%') &&
((a = src[1]) && (b = src[2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
*dst++ = 16*a+b;
src+=3;
} else if (*src == '+') {
*dst++ = ' ';
src++;
} else {
*dst++ = *src++;
}
}
*dst++ = '\0';
}
const char* cgi_handler_set_text(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
int i=0;
/* We use this handler for one page request only: "/set_text.cgi"
* and it is at position 0 in the tCGI array (see above).
* So iIndex should be 0.
*/
printf("cgi_handler_set_text called with index %d\n", iIndex);
char* text = NULL;
int interval_ms = 0;
for (i = 0; i < iNumParams; i++){
/* check if parameter is "led" */
if (strcmp(pcParam[i] , "text") == 0) {
text = (char*) calloc(200+1, sizeof(char));
urldecode2(text, pcValue[i]);
} else if (strcmp(pcParam[i], "interval_ms") == 0) {
interval_ms = atoi(pcValue[i]);
}
}
if (text != NULL && interval_ms >= 0 && interval_ms < 0xFFFF) {
render_text(text, interval_ms);
}
free(text);
return "/set_text.html";
}
/* initialize the CGI handler */
void
cgi_init(void)
{
http_set_cgi_handlers(cgi_handlers, 1);
}