⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpd.c

📁 W3100是WIZnet公司专门为以太网互联和嵌入式设备推出的硬件TCP/IP协议栈芯片
💻 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 :
* Description : web server related functions
********************************************************************************
*/

#include <reg51.h>
#include <serial.h>
#include <httpd.h>
#include "util.h"

/*
********************************************************************************
*              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 = MemCopy(TX_Buf, "HTTP/1.1 200 OK\r\n");
	Length +=MemCopy(TX_Buf + Length, "Server: Apache/1.3.9 (Unix)\r\n");

	switch (content_type) { 
		case 't':
			Length += MemCopy(TX_Buf + Length, "Content-Type: text/plain\r\n");
			break;
		case 'g':
			Length += MemCopy(TX_Buf + Length, "Content-Type: image/gif\r\n");
			break;
		case 'j':
			Length += MemCopy(TX_Buf + Length, "Content-Type: image/jpeg\r\n");
			break;
		case 'h':
			Length += MemCopy(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 = MemCopy(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 += MemCopy(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;
	Length = MSG_Length(TX_Buf, 0x3C1);
	Length += MemCopy(TX_Buf+Length,
	"<HTML>"
	"<HEAD><TITLE> SMTP Sample by using i2Chip W3100 </TITLE></HEAD>"
	"<BODY><CENTER><form method=\"get\" name=\"form1\" action=\"SEND.CGI\">"
	"<table width=50%><tr><hr> <h2 align=center>< SMTP SAMPLE ></h2><hr></tr>"
	"<tr bgcolor=#FFFFCC><td> SMTP Server IP </td><td><input type=\"text\" name=\"SVRIP\" value=\"211.32.116.69\" size=17></td></tr>");
	Length += MemCopy(TX_Buf+Length,	
	"<tr><td> Account </td><td><input type=\"text\" name=\"ACC\" value=\"test\" size=17></td></tr>"
	"<tr bgcolor=#FFFFCC><td> Sender Mail Address </td><td><input type=\"text\" name=\"SENDER\" value=\"test@wiznet.co.kr\" size=40></td></tr>"
        "<tr><td> Recipient Mail Address </td><td><input type=\"text\" name=\"RECV\" value=\"\" size=40></td></tr>"
	"<tr bgcolor=#FFFFCC><td> Title </td><td><input type=\"text\" name=\"Title\" value=\"\" size=40></td></tr>"
	"<tr rowspan=1><td> Contents </td><td><textarea name=\"CON\"  rows=12 cols=40></textarea></td></tr>"
	"</table><hr width=50%><br>"
	"<input type=\"submit\" value=\"  Send  \" ></tr><br><br><hr width=50%></form>"
	"</CENTER></BODY></HTML>\r\n");
	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')	return 'g';
	else if (*Pointer == 'j')	return 'j';
	else if (*Pointer == 'h')	return 'h';                       
	else if (*Pointer == 'C')	return 'c';
	else 	return 'a';
}

/*
********************************************************************************
*              Function to create Error HTML document
*
* Description: Web page to be displayed on web browser when mail transfer is failed
* Arguments  : TX_Buf : Stream Pointer to write Error HTML document
* Returns    : size.
* Note       : 
********************************************************************************
*/
UINT DoErrHTML(UCHAR* TX_Buf)
{
	unsigned int Length;
	Length = MSG_Length(TX_Buf, 85);
	Length += MemCopy(TX_Buf+Length,"<HTML><BODY><CENTER><h2 align=center>< Send Mail Failed ></h2><hr></tr><BODY></HTML>\r\n");

	return Length;
}

/*
********************************************************************************
*              Function to convert special character(Escape character %XX) to 1Byte ASCII
*
* Description: To convert special character (Escape character %XX) in a request of client to 1Byte ASCII
* Arguments  : src: stream pointer to convert
* Returns    : none.
* Note       : 
********************************************************************************
*/
void XEscape(UCHAR* src)
{
	UCHAR c1,c2;
	UCHAR * tsrc = src;
	while(*src !='\0')
	{
		*tsrc = *src;
		if(*src == '%')
		{
			c1 = C2D(*(src+1));  // convert a character to number
			c2 = C2D(*(src+2));  // convert a character to number
			if( *(src+1) != c1  &&  *(src+2) != c2 )
			{
				*tsrc = (c1*0x10 + c2); // convert 2 character number to single number
				src+=2; // move as converted position
			}
		}
		src++;
		tsrc++;
	}
	*tsrc =  '\0';
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -