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

Subversion Repositories mlite

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

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

powered by: WebSVN 2.1.0

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