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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [http.c] - Blame information for rev 425

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

Line No. Rev Author Line
1 155 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma TCP/IP HTTP Server
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 4/22/06
5
 * FILENAME: http.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Plasma TCP/IP HTTP Server
11
 *--------------------------------------------------------------------*/
12
#include "rtos.h"
13
#include "tcpip.h"
14
#ifdef WIN32
15
#define UartPrintf printf
16 192 rhoads
#define OS_MQueueCreate(A,B,C) 0
17
#define OS_MQueueGet(A,B,C) 0
18
#define OS_ThreadCreate(A,B,C,D,E) 0
19 155 rhoads
#endif
20
 
21 237 rhoads
static const char pageGif[]=
22 155 rhoads
{
23
   "HTTP/1.0 200 OK\r\n"
24 192 rhoads
   "Content-Length: %d\r\n"
25 155 rhoads
   "Content-Type: binary/gif\r\n\r\n"
26
};
27 237 rhoads
static const char pageGif2[]=
28 155 rhoads
{
29
   "HTTP/1.0 200 OK\r\n"
30 237 rhoads
   "Content-Type: binary/gif\r\n\r\n"
31
};
32
static const char pageBinary[]=
33
{
34
   "HTTP/1.0 200 OK\r\n"
35 192 rhoads
   "Content-Length: %d\r\n"
36 155 rhoads
   "Content-Type: binary/binary\r\n\r\n"
37
};
38 237 rhoads
static const char pageBinary2[]=
39
{
40 155 rhoads
   "HTTP/1.0 200 OK\r\n"
41 237 rhoads
   "Content-Type: binary/binary\r\n\r\n"
42
};
43
static const char pageHtml[]={
44
   "HTTP/1.0 200 OK\r\n"
45 192 rhoads
   "Content-Length: %d\r\n"
46 155 rhoads
   "Content-Type: text/html\r\n\r\n"
47
};
48 237 rhoads
static const char pageHtml2[]={
49 155 rhoads
   "HTTP/1.0 200 OK\r\n"
50 237 rhoads
   "Content-Type: text/html\r\n\r\n"
51
};
52
static const char pageText[]={
53
   "HTTP/1.0 200 OK\r\n"
54 192 rhoads
   "Content-Length: %d\r\n"
55 155 rhoads
   "Content-Type: text/text\r\n\r\n"
56
};
57 237 rhoads
static const char pageEmpty[]=
58 155 rhoads
{
59
   "HTTP/1.0 404 OK\r\n"
60 192 rhoads
   "Content-Length: 0\r\n"
61 155 rhoads
   "Content-Type: text/html\r\n\r\n"
62
};
63
 
64
static const PageEntry_t *HtmlPages;
65
static int HtmlFiles;
66
 
67
 
68 224 rhoads
void HttpServer(IPSocket *socket)
69 155 rhoads
{
70
   uint8 buf[600];
71 302 rhoads
   char filename[80];
72 155 rhoads
   int bytes, i, length, len, needFooter;
73
   char *name=NULL, *page=NULL;
74 192 rhoads
   const char *header, *header2;
75 155 rhoads
 
76
   if(socket == NULL)
77
      return;
78 316 rhoads
   if(socket->funcPtr != HttpServer && socket->funcPtr)
79
   {
80
      socket->funcPtr(socket);
81
      return;
82
   }
83 155 rhoads
   bytes = IPRead(socket, buf, sizeof(buf)-1);
84
   if(bytes)
85
   {
86
      buf[bytes] = 0;
87
      if(strncmp((char*)buf, "GET /", 5) == 0)
88
      {
89
         for(i = 0; ; ++i)
90
         {
91
            length = HtmlPages[i].length;
92
            if(length == -1)
93
               break;
94
            name = (char*)HtmlPages[i].name;
95
            page = (char*)HtmlPages[i].page;
96
            len = (int)strlen(name);
97
            if(strncmp((char*)buf+4, name, len) == 0)
98
               break;
99
         }
100 416 rhoads
#ifndef EXCLUDE_FILESYS
101 237 rhoads
         if(length == HTML_LENGTH_LIST_END && HtmlFiles)
102 155 rhoads
         {
103
            FILE *file;
104
            char *ptr;
105
 
106
            name = (char*)buf + 5;
107
            ptr = strstr(name, " ");
108
            if(ptr)
109
               *ptr = 0;
110 302 rhoads
            strcpy(filename, "/web/");
111
            strncat(filename, name, 60);
112
            file = fopen(filename, "rb");
113
            if(file == NULL)
114
            {
115
               strcpy(filename, "/flash/web/");
116
               strncat(filename, name, 60);
117
               file = fopen(filename, "rb");
118
            }
119 155 rhoads
            if(file)
120
            {
121 237 rhoads
               if(strstr(name, ".htm"))
122 310 rhoads
                  IPWrite(socket, (uint8*)pageHtml2, sizeof(pageHtml2)-1);
123 237 rhoads
               else if(strstr(name, ".gif"))
124 310 rhoads
                  IPWrite(socket, (uint8*)pageGif2, sizeof(pageGif2)-1);
125 237 rhoads
               else
126 310 rhoads
                  IPWrite(socket, (uint8*)pageBinary2, sizeof(pageBinary2)-1);
127 237 rhoads
               for(;;)
128
               {
129
                  len = fread(buf, 1, sizeof(buf), file);
130
                  if(len == 0)
131
                     break;
132
                  IPWrite(socket, (uint8*)buf, len);
133
               }
134 155 rhoads
               fclose(file);
135 237 rhoads
               IPWriteFlush(socket);
136
               IPClose(socket);
137
               return;
138 155 rhoads
            }
139
         }
140 416 rhoads
#endif //!EXCLUDE_FILESYS
141 161 rhoads
         if(length != HTML_LENGTH_LIST_END)
142 155 rhoads
         {
143 161 rhoads
            if(length == HTML_LENGTH_CALLBACK)
144 155 rhoads
            {
145 400 rhoads
               IPCallbackPtr funcPtr = (IPCallbackPtr)(uint32)page;
146 161 rhoads
               funcPtr(socket, buf, bytes);
147 155 rhoads
               return;
148
            }
149
            if(length == 0)
150
               length = (int)strlen(page);
151
            needFooter = 0;
152 192 rhoads
            header2 = NULL;
153
            if(strstr(name, ".html"))
154
               header = pageHtml;
155
            else if(strstr(name, ".htm") || strcmp(name, "/ ") == 0)
156
            {
157
               header = pageHtml;
158
               header2 = HtmlPages[0].page;
159
               needFooter = 1;
160
            }
161
            else if(strstr(HtmlPages[i].name, ".gif"))
162
               header = pageGif;
163
            else
164
               header = pageBinary;
165
            len = 0;
166
            if(header2)
167
               len += (int)strlen(header2) + (int)strlen(HtmlPages[1].page);
168
            sprintf((char*)buf, header, length + len);
169
            IPWrite(socket, buf, (int)strlen((char*)buf));
170
            if(header2)
171
               IPWrite(socket, (uint8*)header2, (int)strlen(header2));
172 155 rhoads
            IPWrite(socket, (uint8*)page, length);
173
            if(needFooter)
174
               IPWrite(socket, (uint8*)HtmlPages[1].page, (int)strlen(HtmlPages[1].page));
175
         }
176
         else
177
         {
178
            IPWrite(socket, (uint8*)pageEmpty, (int)strlen(pageEmpty));
179
         }
180
         IPClose(socket);
181
      }
182
   }
183
}
184
 
185
 
186
void HttpInit(const PageEntry_t *Pages, int UseFiles)
187
{
188
   HtmlPages = Pages;
189
   HtmlFiles = UseFiles;
190
   IPOpen(IP_MODE_TCP, 0, 80, HttpServer);
191
   IPOpen(IP_MODE_TCP, 0, 8080, HttpServer);
192
}
193
 
194
 
195 244 rhoads
#ifdef EXAMPLE_HTML
196 155 rhoads
//Example test code
197 161 rhoads
static void MyProg(IPSocket *socket, char *request, int bytes)
198 155 rhoads
{
199
   char *text="HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
200
              "<html><body>Hello World!</body></html>";
201 161 rhoads
   (void)request; (void)bytes;
202 244 rhoads
   IPWrite(socket, (uint8*)text, (int)strlen(text));
203 155 rhoads
   IPClose(socket);
204
}
205
static const PageEntry_t pageEntry[]=
206
{  //name, length, htmlText
207 192 rhoads
   {"/Header", 0, "<HTML><HEAD><TITLE>Plasma CPU</TITLE></HEAD>\n<BODY>"},
208 155 rhoads
   {"/Footer", 0, "</BODY></HTML>"},
209
   {"/ ", 0, "<h2>Home Page</h2>Welcome!  <a href='/other.htm'>Other</a>"
210
             " <a href='/cgi/myprog'>myprog</a>"},
211
   {"/other.htm ", 0, "<h2>Other</h2>Other."},
212
   //{"/binary/plasma.gif ", 1945, PlasmaGif},
213 400 rhoads
   {"/cgi/myprog", HTML_LENGTH_CALLBACK, (char*)(int)MyProg},
214 155 rhoads
   {"", HTML_LENGTH_LIST_END, NULL}
215
};
216 244 rhoads
void HtmlInit(int UseFiles)
217 155 rhoads
{
218 244 rhoads
   (void)UseFiles;
219 237 rhoads
   HttpInit(pageEntry, 1);
220 155 rhoads
}
221
#endif
222
 
223
 

powered by: WebSVN 2.1.0

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