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

📄 http.c

📁 DSP2407控制8019芯片实现的网页服务器
💻 C
字号:
/*********************************************************************************
*  http.c	v1.00       webserver程序	                                         *
*  版权(c) 	2004-		北京百科融创科技有限公司		                         *
*  设计者:	赵治心																 *
*  邮箱:   ourui.wl@263.net								                     *
**********************************************************************************/
#include "stdio.h"
#include "tcp_ip.h"
#include "8019head.h"
#include "global.h"

char html_header[] = {
"HTTP/1.1 200 OK\n"
"Cache-control: no-cache\n"
"Connection: Keep-Alive\n"
"Content-Length: 186\n"
"Content-Type: text/html\r\n\r\n" };



// This page displays a table with weather data and a switch
// to turn a port pin on and off. 
char web_page[] = {
"<html>\n"
"<head>\n"
"<title>Title of page</title>\n"
"</head>\n"
"<body>\n"
"<marquee height=40 width=100% bgcolor=aaeeaa>\n"
"<h1>北京百科融创科技有限公司欢迎您选用本公司产品!</h1>\n"
"</marquee>\n"
"</body>\n"
"</html>\n" 
};

UINT strlen(char *string)
{
     char  *rstr = string;
     UINT n    = 0;

     while (*rstr++) ++n;
     return (n);
}

char * strstr(char *haystack, char *needle)
{
	char *ptr1, *ptr2;
	
	// Protect against NULL pointer
	if (*needle == 0) return(haystack);
	for( ; *haystack; haystack++ )
	{
		for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
							
		// If there is a match then return pointer to needle in haystack
		if(*ptr1 == 0) return(haystack);	
	}
	return NULL;			// no matching string found
}

void http_send(UCHAR * outbuf, UINT len, UCHAR nr)
{
   TCP_HEADER * tcp;
   IP_HEADER * ip;
   ULONG  sum;
   UINT result;
   int i;
          
   // Fill in TCP segment header
   tcp = (TCP_HEADER *)(outbuf + 34);
   ip = (IP_HEADER *)(outbuf + 14);

   tcp->source_port[0] = (HTTP_PORT&0xff00)>>8;
   tcp->source_port[1] = HTTP_PORT&0x00ff;
   tcp->dest_port[0] = conxn[nr].port[0];
   tcp->dest_port[1] = conxn[nr].port[1];
   for(i=0;i<4;i++)
   {
   		tcp->sequence[i] = conxn[nr].my_sequence[i];
   		tcp->ack_number[i] = conxn[nr].his_sequence[i];
   }  
	// Header is always 20 bytes long
   i = 0x5000 | FLG_ACK | FLG_PSH;
   tcp->flags[0] = (i&0xff00)>>8;
   tcp->flags[1] = i&0x00ff;
   tcp->window[0] = 4;
   tcp->window[1] = 0;
   tcp->checksum[0] = 0;
   tcp->checksum[1] = 0;
   tcp->urgent_ptr[0] = 0;
   tcp->urgent_ptr[1] = 0;
   
   // Compute checksum including 12 bytes of pseudoheader
	// Must pre-fill 2 items in ip header to do this
   for(i=0;i<4;i++)
   {
		ip->dest_ipaddr[i] = conxn[nr].ipaddr[i];
		ip->source_ipaddr[i] = sed_lclIPAddr[i];
    }
		
	// Sum source_ipaddr, dest_ipaddr, and entire TCP message 
	sum = (ULONG)cksum(outbuf + 26, 8 + len);
				
	// Add in the rest of pseudoheader which is
	// protocol id and TCP segment length
	sum += (ULONG)0x0006;
	sum += (ULONG)len;

	// In case there was a carry, add it back around
	result = (UINT)(sum + (sum >> 16));
	result = ~result;
	tcp->checksum[0] = (result&0xff00)>>8;
    tcp->checksum[1] = result&0x00ff;
   //if (debug) printf("TCP: Sending msg to IP layer\n");
	ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);

   // (Re)start TCP retransmit timer
   conxn[nr].timer = TCP_TIMEOUT;
}


UINT http_server(UCHAR * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
{
	ULONG sum;
	int i,temp;
	UINT body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
	UINT sent, remaining;
	UCHAR * outbuf;
	UCHAR * ptr;
	UCHAR * tcp_data;
	UCHAR request;
   static UCHAR post_flg = FALSE;
		    	
	// Make sure this is a valid connection
	if (nr == NO_CONNECTION) return 0;
	
	// Compute start of TCP data
   if (!resend)
   {
      tcp_data = inbuf + 34 + header_len;
      for(i=0;i<20;i++)
      	conxn[nr].query[i] = *(tcp_data+1);
      for(i=0;i<4;i++)
      	conxn[nr].old_sequence[i] = conxn[nr].my_sequence[i];
   }
   // If this is a resend, set sequence number to what it was
   // the last time we sent this
   else
   {
      tcp_data = inbuf;
      for(i=0;i<4;i++)
     	 conxn[nr].my_sequence[i] = conxn[nr].old_sequence[i];   
   }
   
   // Start off with no request
   request = NONE;
	   
   // See if this is a GET message
   if (strstr((char *)tcp_data, (char *)"GET") != NULL)
   {
      post_flg = FALSE;
      if (strstr((char *)tcp_data, (char *)"/ ") != NULL) request = GET_PAGE;
      else if (strstr((char *)tcp_data, (char *)"index") != NULL) request = GET_PAGE;     
   }
   
   if (request == GET_PAGE) 
   {
      // Figure out sizes
      hhdr_len = strlen(html_header);
      page_len = strlen(web_page);
      body_len = hhdr_len + page_len;

      outbuf = (UCHAR *)SendBuffer;
 
      for(i=0;i<hhdr_len;i++)
      	*(outbuf+i+54) = html_header[i];
      for(i=0;i<page_len;i++)
      	*(outbuf+i+hhdr_len+54) = web_page[i];  	   	   	
   		*(outbuf + body_len+54) = 0;		// Append NULL 
      
      // Segment length = body_len + 20
      http_send(outbuf, body_len + 20, nr);
      temp = (conxn[nr].my_sequence[2]<<8)+conxn[nr].my_sequence[3];
	  sum = (conxn[nr].my_sequence[0]<<8)+conxn[nr].my_sequence[1];
	  sum = (sum<<16)+(LONG)temp+body_len;
	  temp = (UINT)(sum>>16);
	  conxn[nr].my_sequence[0] = (temp&0xff00)>>8;
 	  conxn[nr].my_sequence[1] = temp&0x00ff;
	  temp = (UINT)sum;
	  conxn[nr].my_sequence[2] = (temp&0xff00)>>8;
	  conxn[nr].my_sequence[3] = temp&0x00ff;
   }

   else
      return 0;
	return(body_len);
}


⌨️ 快捷键说明

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