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

Subversion Repositories mlite

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

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

powered by: WebSVN 2.1.0

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