-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPApp.c
271 lines (228 loc) · 7.99 KB
/
HTTPApp.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* **************************************************************************
* Software License Agreement
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License (version 2) as published by
* the Free Software Foundation AND MODIFIED BY OpenPicus team.
*
* ***NOTE*** The exception to the GPL is included to allow you to distribute
* a combined work that includes OpenPicus code without being obliged to
* provide the source code for proprietary components outside of the OpenPicus
* code.
* OpenPicus software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*
* Warranty
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
**************************************************************************/
/****************************************************************************
SECTION Include
****************************************************************************/
#include "TCPIP Stack/TCPIP.h"
#if defined(STACK_USE_HTTP2_SERVER)
#define __HTTPAPP_C
/****************************************************************************
FUNCTION HTTP_IO_RESULT HTTPExecuteGet(void)
This function processes every GET request from the pages. In the example,
it processes only the leds.cgi function, but you can add code to process
other GET requests.
*****************************************************************************/
HTTP_IO_RESULT HTTPExecuteGet(void)
{
BYTE *ptr;
BYTE filename[20];
// STEP #1:
// The function MPFSGetFilename retrieves the name of the requested cgi,
// in this case "leds.cgi" and puts it inside the filename variable.
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, 20);
// STEP #2:
// Handling of the cgi requests, in this case we have only "leds.cgi" but
// it would be possible to have any other cgi request, depending on the webpage
if(!memcmp(filename, "leds.cgi", 8)) // Is the requested file name "leds.cgi"?
{
// STEP #3:
// The complete request is contained inside the system variable curHTTP.data.
// Using the function HTTPGetArg is possible to read the arguments
// of the cgi request from curHTTP.data. In this case we are reading the
// argument "led" from the request "leds.cgi?led=x" and we assign it to ptr.
ptr = HTTPGetArg(curHTTP.data, (BYTE *)"led");
// The requested led is toggled
switch(*ptr)
{
case '0':
IOPut(p4, toggle);
break;
case '1':
IOPut(p6, toggle);
break;
case '2':
IOPut(p17, toggle);
break;
case '3':
IOPut(p19, toggle);
break;
case '4':
IOPut(p21, toggle);
break;
}
}
return HTTP_IO_DONE;
}
/****************************************************************************
Section:
POST Form Handlers
***************************************************************************/
#if defined(HTTP_USE_POST)
/*****************************************************************************
Function:
HTTP_IO_RESULT HTTPExecutePost(void)
This function processes every GET request from the pages.
***************************************************************************/
HTTP_IO_RESULT HTTPExecutePost(void)
{
// Resolve which function to use and pass along
BYTE filename[20];
// Load the file name
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, sizeof(filename));
return HTTP_IO_DONE;
}
#endif
/***************************************************************************
SECTION Dynamic Variable Callback Functions
In this section are managed the "dynamic variables" of the webserver.
Dynamic variables are contained in the status.xml file requested by the
webpage.
For each dynamic variable a callback function named HTTPPrint_varname
must be created.
****************************************************************************/
BYTE HTML_UP_ARROW[] = "up";
BYTE HTML_DOWN_ARROW[] = "dn";
// Callback function for the dynamic variable ~pot(num)~. Anytime the browser
// asks for ~pot(num)~, this function is executed.
void HTTPPrint_pot(WORD num)
{
BYTE AN0String[8];
WORD ADval;
// Analog values reading according to the webpage request.
switch(num)
{
case 0:
ADval = ADCVal(1);
uitoa(ADval, (BYTE*)AN0String);
break;
case 1:
ADval = ADCVal(2);
uitoa(ADval, (BYTE*)AN0String);
break;
}
// After the analog value is read, it is sent to the webpage
// using the function TCPPutString. sktHTTP is the standard
// socket for the HTTP communication.
TCPPutString(sktHTTP, AN0String);
}
// Callback function for the dynamic variable ~btn(num)~.
void HTTPPrint_btn(WORD num)
{
// Determine the button
switch(num)
{
case 0:
num = IOGet(p5);
break;
case 1:
num = IOGet(p7);
break;
case 2:
num = IOGet(p9);
break;
case 3:
num = IOGet(p11);
break;
case 4:
num = IOGet(p2);
break;
default:
num = 0;
}
// Sending back the button status to the webpage.
TCPPutString(sktHTTP, (num?HTML_UP_ARROW:HTML_DOWN_ARROW));
return;
}
// Callback function for the dynamic variable ~led(num)~.
void HTTPPrint_led(WORD num)
{
// Determine which LED
switch(num)
{
case 0:
num = IOGet(p4);
break;
case 1:
num = IOGet(p6);
break;
case 2:
num = IOGet(p17);
break;
case 3:
num = IOGet(p19);
break;
case 4:
num = IOGet(p21);
break;
default:
num = 0;
}
// Sending back the led status to the webpage.
TCPPut(sktHTTP, (num?'1':'0'));
return;
}
/****************************************************************************
SECTION Authorization Handlers
****************************************************************************/
/*****************************************************************************
FUNCTION BYTE HTTPNeedsAuth(BYTE* cFile)
This function is used by the stack to decide if a page is access protected.
If the function returns 0x00, the page is protected, if returns 0x80, no
authentication is required
*****************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPNeedsAuth(BYTE* cFile)
{
// If you want to restrict the access to some page, include it in the folder "protect"
// here you can change the folder, or add others
if(memcmp(cFile, (void*)"protect", 7) == 0)
return 0x00; // Authentication will be needed later
return 0x80; // No authentication required
}
#endif
/*****************************************************************************
FUNCTION BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
This function checks if username and password inserted are acceptable
***************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
{
if(strcmp((char *)cUser,(char *)"admin") == 0
&& strcmp((char *)cPass, (char *)"flyport") == 0)
return 0x80; // We accept this combination
return 0x00; // Provided user/pass is invalid
}
#endif
#endif