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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [http.c] - Blame information for rev 244

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

powered by: WebSVN 2.1.0

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