OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [ARM9_STR91X_IAR/] [lwip/] [lwipWebServer/] [httpd.c] - Blame information for rev 750

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 577 jeremybenn
/*
2
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. The name of the author may not be used to endorse or promote products
14
 *    derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
 * OF SUCH DAMAGE.
26
 *
27
 * This file is part of the lwIP TCP/IP stack.
28
 *
29
 * Author: Adam Dunkels <adam@sics.se>
30
 *
31
 */
32
 
33
#include "lwip/debug.h"
34
#include "lwip/stats.h"
35
#include "httpd.h"
36
#include "lwip/tcp.h"
37
#include "fs.h"
38
 
39
#include <string.h>
40
 
41
#pragma pack(2)
42
struct http_state {
43
  char *file;
44
  u32_t left;
45
  u8_t retries;
46
};
47
#pragma pack()
48
 
49
/*-----------------------------------------------------------------------------------*/
50
void conn_err(void *arg, err_t err)
51
{
52
  struct http_state *hs;
53
 
54
  hs = arg;
55
  mem_free(hs);
56
}
57
/*-----------------------------------------------------------------------------------*/
58
static void
59
close_conn(struct tcp_pcb *pcb, struct http_state *hs)
60
{
61
  tcp_arg(pcb, NULL);
62
  tcp_sent(pcb, NULL);
63
  tcp_recv(pcb, NULL);
64
  mem_free(hs);
65
  tcp_close(pcb);
66
}
67
/*-----------------------------------------------------------------------------------*/
68
static void
69
send_data(struct tcp_pcb *pcb, struct http_state *hs)
70
{
71
  err_t err;
72
  u16_t len;
73
 
74
  /* We cannot send more data than space available in the send
75
     buffer. */
76
  if (tcp_sndbuf(pcb) < hs->left) {
77
    len = tcp_sndbuf(pcb);
78
  } else {
79
    len = hs->left;
80
  }
81
 
82
  do {
83
    err = tcp_write(pcb, hs->file, len, 0);
84
    if (err == ERR_MEM) {
85
      len /= 2;
86
    }
87
  } while (err == ERR_MEM && len > 1);
88
 
89
  if (err == ERR_OK) {
90
    hs->file += len;
91
    hs->left -= len;
92
    /*  } else {
93
        printf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb));*/
94
  }
95
}
96
/*-----------------------------------------------------------------------------------*/
97
err_t http_poll(void *arg, struct tcp_pcb *pcb)
98
{
99
  struct http_state *hs;
100
 
101
  hs = arg;
102
 
103
  /*  printf("Polll\n");*/
104
  if (hs == NULL) {
105
    /*    printf("Null, close\n");*/
106
    tcp_abort(pcb);
107
    return ERR_ABRT;
108
  } else {
109
    ++hs->retries;
110
    if (hs->retries == 4) {
111
      tcp_abort(pcb);
112
      return ERR_ABRT;
113
    }
114
    send_data(pcb, hs);
115
  }
116
 
117
  return ERR_OK;
118
}
119
/*-----------------------------------------------------------------------------------*/
120
err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
121
{
122
  struct http_state *hs;
123
 
124
  hs = arg;
125
 
126
  hs->retries = 0;
127
 
128
  if (hs->left > 0) {
129
    send_data(pcb, hs);
130
  } else {
131
    close_conn(pcb, hs);
132
  }
133
 
134
  return ERR_OK;
135
}
136
/*-----------------------------------------------------------------------------------*/
137
err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
138
{
139
  int i;
140
  char *data;
141
  struct fs_file file;
142
  struct http_state *hs;
143
 
144
  hs = arg;
145
 
146
  if (err == ERR_OK && p != NULL) {
147
 
148
    /* Inform TCP that we have taken the data. */
149
    tcp_recved(pcb, p->tot_len);
150
 
151
    if (hs->file == NULL) {
152
      data = p->payload;
153
 
154
      if (strncmp(data, "GET ", 4) == 0) {
155
        for(i = 0; i < 40; i++) {
156
          if (((char *)data + 4)[i] == ' ' ||
157
             ((char *)data + 4)[i] == '\r' ||
158
             ((char *)data + 4)[i] == '\n') {
159
            ((char *)data + 4)[i] = 0;
160
          }
161
        }
162
 
163
        if (*(char *)(data + 4) == '/' &&
164
           *(char *)(data + 5) == 0) {
165
          fs_open("/index.html", &file);
166
        }
167
        else {
168
          if (!fs_open((char *)data + 4, &file)) {
169
            fs_open("/404.html", &file);
170
          }
171
        }
172
        hs->file = file.data;
173
        hs->left = file.len;
174
        /*      printf("data %p len %ld\n", hs->file, hs->left);*/
175
 
176
        pbuf_free(p);
177
        send_data(pcb, hs);
178
 
179
        /* Tell TCP that we wish be to informed of data that has been
180
           successfully sent by a call to the http_sent() function. */
181
        tcp_sent(pcb, http_sent);
182
      } else {
183
        pbuf_free(p);
184
        close_conn(pcb, hs);
185
      }
186
    } else {
187
      pbuf_free(p);
188
   }
189
  }
190
 
191
  if (err == ERR_OK && p == NULL) {
192
    close_conn(pcb, hs);
193
  }
194
  return ERR_OK;
195
}
196
/*-----------------------------------------------------------------------------------*/
197
err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
198
{
199
  struct http_state *hs;
200
 
201
  tcp_setprio(pcb, TCP_PRIO_MIN);
202
 
203
  /* Allocate memory for the structure that holds the state of the
204
     connection. */
205
  hs = mem_malloc(sizeof(struct http_state));
206
 
207
  if (hs == NULL) {
208
//    printf("http_accept: Out of memory\n");
209
    return ERR_MEM;
210
  }
211
 
212
  /* Initialize the structure. */
213
  hs->file = NULL;
214
  hs->left = 0;
215
  hs->retries = 0;
216
 
217
  /* Tell TCP that this is the structure we wish to be passed for our
218
     callbacks. */
219
  tcp_arg(pcb, hs);
220
 
221
 
222
  /* Tell TCP that we wish to be informed of incoming data by a call
223
     to the http_recv() function. */
224
  tcp_recv(pcb, http_recv);
225
 
226
  tcp_err(pcb, conn_err);
227
 
228
  tcp_poll(pcb, http_poll, 4);
229
 
230
  return ERR_OK;
231
}
232
 
233
/*-----------------------------------------------------------------------------------*/
234
void httpd_init(void)
235
{
236
  struct tcp_pcb *pcb;
237
 
238
  pcb = tcp_new();
239
  tcp_bind(pcb, NULL, 80);
240
  pcb = tcp_listen(pcb);
241
  tcp_accept(pcb, http_accept);
242
}
243
/*-----------------------------------------------------------------------------------*/
244
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.