summaryrefslogtreecommitdiff
path: root/third_party/uip-1.0/apps/httpd/httpd.c
blob: f28c6e73aefe12f90108e2fedec47c93c5d162bf (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//*****************************************************************************
// HTTP server.
// Adam Dunkels <adam@dunkels.com>
//
// Copyright (c) 2001, Adam Dunkels.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote
//    products derived from this software without specific prior
//    written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This file is part of the uIP TCP/IP stack.
//
//*****************************************************************************

#include "uip.h"
#include "httpd.h"

//*****************************************************************************
//
// Macro for easy access to buffer data
//
//*****************************************************************************
#define BUF_APPDATA ((u8_t *)uip_appdata)

//*****************************************************************************
//
// Definitions of HTTP Server States
//
//*****************************************************************************
#define HTTP_NOGET      0
#define HTTP_FILE       1
#define HTTP_TEXT       2
#define HTTP_FUNC       3
#define HTTP_END        4

//*****************************************************************************
//
// Global for keeping up with web server state.
//
//*****************************************************************************
struct httpd_state *hs;

//*****************************************************************************
//
// Default Web Page - allocated in three segments to allow easy update of a
// counter that is incremented each time the page is sent.
//
//*****************************************************************************
static const char page_not_found[] =
"HTTP/1.0 404 OK\r\n"
"Server: UIP/1.0 (http://www.sics.se/~adam/uip/)\r\n"
"Content-type: text/html\r\n\r\n"
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd\">"
"<html>"
  "<head>"
    "<title>Page Not Found!</title>"
  "</head>"
  "<body>"
    "Page Not Found!"
  "</body>"
"</html>";

//*****************************************************************************
//
// Default Web Page - allocated in three segments to allow easy update of a
// counter that is incremented each time the page is sent.
//
//*****************************************************************************
static const char default_page_buf1of3[] =
"HTTP/1.0 200 OK\r\n"
"Server: UIP/1.0 (http://www.sics.se/~adam/uip/)\r\n"
"Content-type: text/html\r\n\r\n"
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd\">"
"<html>"
  "<head>"
    "<title>Welcome to the uIP web server!</title>"
  "</head>"
  "<body>"
    "<center>"
    "<h1>&micro;IP Web Server</h1>"
    "<p>This web page is served by a small web server running on top of "
    "the <a href=\"http://www.sics.se/~adam/uip/\">&micro;IP embedded TCP/IP "
    "stack</a>."
    "<hr width=\"75%\">"
    "<p>The &micro;IP stack is running on a "
    "<a href=\"http://www.ti.com\">Texas Instruments</a> "
    "<a href=\"http://www.ti.com/tm4c129x\">Tiva<small><sup>&#8482;</sup></small> C Series</a>"
    "<hr width=\"75%\">"
    "<p>This page has been sent ";
static char default_page_buf2of3[] =
                             "00001";
static const char default_page_buf3of3[] =
    " times since reset!"
    "</center>"
  "</body>"
"</html>";

//*****************************************************************************
//
// Increment the view count.  This routine will increment the 5-digit ascii
// counter that is sent with the web page.
//
//*****************************************************************************
static void
httpd_inc_page_count(void)
{
    //
    // Increment the 'ones' digit.  If it wraps, then increment the 'tens'
    // digit.
    //
    default_page_buf2of3[4]++;
    if(default_page_buf2of3[4] == 0x3a)
    {
        default_page_buf2of3[4] = 0x30;
        default_page_buf2of3[3]++;
    }

    //
    // If the 'tens' digit wraps, increment the 'hundreds' digit.
    //
    if(default_page_buf2of3[3] == 0x3a)
    {
        default_page_buf2of3[3] = 0x30;
        default_page_buf2of3[2]++;
    }

    //
    // If the 'hundreds' digit wraps, increment the 'thousands' digit.
    //
    if(default_page_buf2of3[2] == 0x3a)
    {
        default_page_buf2of3[2] = 0x30;
        default_page_buf2of3[1]++;
    }

    //
    // If the 'thousands' digit wraps, increment the 'ten-thousands' digit.
    //
    if(default_page_buf2of3[1] == 0x3a)
    {
        default_page_buf2of3[1] = 0x30;
        default_page_buf2of3[0]++;
    }

    //
    // If the 'ten-thousands' digit wrapped, start over.
    //
    if(default_page_buf2of3[0] == 0x3a)
    {
        default_page_buf2of3[0] = 0x30;
    }
}

//*****************************************************************************
//
// Initialize the web server.
//
// Starts to listen for incoming connection requests on TCP port 80.
//
//*****************************************************************************
void
httpd_init(void)
{
    //
    // Listen to port 80.
    //
    uip_listen(HTONS(80));
}

//*****************************************************************************
//
// HTTP Application Callback Function
//
//*****************************************************************************
void
httpd_appcall(void)
{
    switch(uip_conn->lport)
    {
        //
        // This is the web server:
        //
        case HTONS(80):
        {
            //
            // Pick out the application state from the uip_conn structure.
            //
            hs = (struct httpd_state *)&(uip_conn->appstate);

            //
            // We use the uip_ test functions to deduce why we were
            // called. If uip_connected() is non-zero, we were called
            // because a remote host has connected to us. If
            // uip_newdata() is non-zero, we were called because the
            // remote host has sent us new data, and if uip_acked() is
            // non-zero, the remote host has acknowledged the data we
            // previously sent to it. */
            if(uip_connected())
            {
                //
                // Since we have just been connected with the remote host, we
                // reset the state for this connection. The ->count variable
                // contains the amount of data that is yet to be sent to the
                // remote host, and the ->state is set to HTTP_NOGET to signal
                // that we haven't received any HTTP GET request for this
                // connection yet.
                //
                hs->state = HTTP_NOGET;
                hs->count = 0;
                hs->bufcount = 0;
                return;
            }
            else if(uip_poll())
            {
                //
                // If we are polled ten times, we abort the connection. This is
                // because we don't want connections lingering indefinately in
                // the system.
                //
                if(hs->count++ >= 10)
                {
                    uip_abort();
                }
                return;
            }
            else if(uip_newdata() && hs->state == HTTP_NOGET)
            {
                //
                // This is the first data we receive, and it should contain a
                // GET.
                //
                // Check for GET.
                //
                if(BUF_APPDATA[0] != 'G' ||
                   BUF_APPDATA[1] != 'E' ||
                   BUF_APPDATA[2] != 'T' ||
                   BUF_APPDATA[3] != ' ')
                {
                    //
                    // If it isn't a GET, we abort the connection.
                    //
                    uip_abort();
                    return;
                }

                //
                // Check to see what we should send.
                //
                if((BUF_APPDATA[4] == '/') &&
                   (BUF_APPDATA[5] == ' '))
                {
                    //
                    // Send buffer 1
                    //
                    uip_send(default_page_buf1of3,
                            sizeof(default_page_buf1of3) - 1);
                }
                else
                {
                    uip_send(page_not_found,
                            sizeof(page_not_found) - 1);
                    hs->bufcount = 3;
                }
            }
            else if(uip_acked())
            {
                hs->bufcount++;
                if(hs->bufcount == 1)
                {
                    uip_send(default_page_buf2of3,
                            sizeof(default_page_buf2of3) - 1);
                }
                else if(hs->bufcount == 2)
                {
                    uip_send(default_page_buf3of3,
                            sizeof(default_page_buf3of3) - 1);
                }
                else if(hs->bufcount == 3)
                {
                    httpd_inc_page_count();
                    uip_close();
                }
                else
                {
                    uip_close();
                }
            }
            //
            // Finally, return to uIP. Our outgoing packet will soon be on its
            // way...
            //
            return;
        }

        default:
        {
            //
            // Should never happen.
            //
            uip_abort();
            break;
        }
    }
}