📄 httpd.c
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : httpd.c
* Programmer(s): Kim Wooyoul
* Created : 2002/05/23
* Modified : Date - 2002/10/28
* Description - Modify Default HTML in DoHTML() function
* - Process escape characters(%XX-->0xXX)
* Description : web server related functions
********************************************************************************
*/
#include <reg51.h> // 8051 SFR definition file
//#include <stdio.h> // To use 'printf'
#include "serial.h" // serial related functions
#include "socket.h" // W3100A driver file
#include "lcd.h" // LCD related functions
#include "util.h" // utility function
#include "httpd.h" // web server related functions
/*
********************************************************************************
* Function to copy data to Tx buffer
*
* Description:
* Arguments : Tx_Buf : sending buffer
* Stream : data to be copied
* Returns : size of copied data
* Note :
********************************************************************************
*/
unsigned int WriteBuf(unsigned char xdata *TX_Buf, unsigned char *Stream)
{
unsigned int Length;
for (Length = 0; *Stream != '\0'; Length++)
*TX_Buf++ = *Stream++;
return Length;
}
/*
********************************************************************************
* Function to create HTML header to send to the Tx buffer
*
* Description:
* Arguments : Tx_Buf : sending buffer
* content_type : type of HTML
* Returns : header size.
* Note :
********************************************************************************
*/
unsigned int PrintHeader(unsigned char *TX_Buf, unsigned char content_type)
{
unsigned int Length;
Length = WriteBuf(TX_Buf, "HTTP/1.1 200 OK\r\n");
Length +=WriteBuf(TX_Buf + Length, "Server: Apache/1.3.9 (Unix)\r\n");
switch (content_type) {
case 't': // text
Length += WriteBuf(TX_Buf + Length, "Content-Type: text/plain\r\n");
break;
case 'g': // gif image
Length += WriteBuf(TX_Buf + Length, "Content-Type: image/gif\r\n");
break;
case 'j': // jpeg image
Length += WriteBuf(TX_Buf + Length, "Content-Type: image/jpeg\r\n");
break;
case 'h': // html doc.
Length += WriteBuf(TX_Buf + Length, "Content-Type: text/html\r\n");
break;
}
return Length;
}
/*
********************************************************************************
* Function to output length of HTML document in ASCII
*
* Description:
* Arguments : Tx_Buf : sending buffer
* DataLength : size of HTML document
* Returns : total size.
* Note :
********************************************************************************
*/
unsigned int MSG_Length(unsigned char *TX_Buf, unsigned int DataLength)
{
unsigned int Length;
Length = WriteBuf(TX_Buf, "Content-Length: ");
*(TX_Buf + Length++) = D2C(DataLength / 10000);
*(TX_Buf + Length++) = D2C((DataLength / 1000) % 10);
*(TX_Buf + Length++) = D2C((DataLength / 100) % 10);
*(TX_Buf + Length++) = D2C((DataLength / 10) % 10);
*(TX_Buf + Length++) = D2C(DataLength % 10);
Length += WriteBuf(TX_Buf + Length, "\r\n\r\n");
return Length;
}
/*
********************************************************************************
* Function to create HTML document
*
* Description: Due to limited memory of 8051, only one document is allowed.
* Arguments : Tx_Buf : sending buffer
* Returns : size.
* Note :
********************************************************************************
*/
unsigned int DoHTML(unsigned char *TX_Buf)
{
unsigned int Length;
unsigned char *Msg;
Length = MSG_Length(TX_Buf, 1136);
Msg = "<html><head><title>== WEB CONTROL (8051 EVB V3.0) ==</title></head>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<body bgcolor=\"#FFFFFF\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<p><br><div align=\"center\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"500\">";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<tr><td align=\"center\"><p><font face=\"Arial,Helvetica\" size=\"5\">";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<b>8051 Evaluation Board for W3100A</b></font></td></tr></table></div><p><br>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<form name=\"form1\" method=\"get\" action = \"LCD.CGI\" >";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<table width=\"100\%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#000000\" bordercolordark=\"#FFFFFF\">";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<tr><td width=\"20\%\" align=\"center\" bgcolor=\"#9999FF\" height=\"21\"><b>LCD TEXTBOX(<16)</b></td>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<td width=\"30\%\" align=\"center\" height=\"21\" bgcolor=\"#CCCCFF\"><input type=\"text\" name=\"tfield\"></td>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<td width=\"20\%\" align=\"center\" bgcolor=\"#FF6090\"><p><b>8051 LED</b></td>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<td width=\"30\%\" align=\"center\" bgcolor=\"#FF9080\"><form name=\"form2\" method=\"get\" action =\"LED.CGI\">";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "<p><input type=\"radio\" name=\"led\" value=\"on\"> LED #1<br><input type=\"radio\" name=\"led\" value=\"off\"> LED #2 </td>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
Msg = "</tr></table><div align=\"center\"><p><input type=\"submit\" name=\"Submit\" value=\"O K\"></div></form></body></html>";
Length += WriteBuf(TX_Buf + Length, Msg); // prints the page
return Length;
}
/*
********************************************************************************
* Function to analyze request from the browser
*
* Description: For simple implementatioon, compare first character of file name
* Arguments : Data_Buf : receiving buffer
* Returns : request type.
* Note :
********************************************************************************
*/
unsigned char ParseReq(unsigned char *Data_Buf)
{
unsigned char *Pointer;
unsigned char i = 0;
Pointer = Data_Buf;
while(*Pointer++ != 0x2E); // Search '.'
if (*Pointer == 'g') {
//printf("GIF\r\n");
PutStringLn("GIF");
return 'g';
} else if (*Pointer == 'j') {
//printf("JPG\r\n");
PutStringLn("JPEG");
return 'j';
} else if (*Pointer == 'h') {
//printf("HTML\r\n");
PutStringLn("HTML");
return 'h';
} else if (*Pointer == 'C') { // Text LCD Display & LED ON/OFF CGI Function
PutStringLn("CGI");
if ((*(Pointer + 4) == 't') && (*(Pointer + 5) == 'f')) {
Pointer += 11;
GotoXY(0,1);
Puts(" ");
GotoXY(0,1);
while ((*Pointer != '&') && (*Pointer != 0x20)) { // Output TEXT on LCD
if (*Pointer == 0x2B)
Putch(0x20);
else if(*Pointer == '%')
Putch(C2D(*++Pointer)*0x10+C2D(*++Pointer));
else
Putch(*Pointer);
Pointer++;
}
// LED ON/OFF
if (*(Pointer + 6) == 'n') {
T0 = 0;
T1 = 1;
} else if (*(Pointer + 6) == 'f') {
T0 = 1;
T1 = 0;
} else {
T0 = 1;
T1 = 1;
}
return 'c';
} else return 'x';
} else {
PutStringLn("Any Type");
return 'a';
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -