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

📄 http.c

📁 44B0+8019系统
💻 C
字号:
//-----------------------------------------------------------------------------
// Net HTTP.C
//
// This module is the Web Server
// It currently serves a html text page and a jpeg image, or handles
// a POST message to turn an LED on or off.
// The HTTP protocol specification is at http://www.w3.org/Protocols/ 
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>		// toupper

#include "net.h"
#include "cksum.h"
#include "ip.h"
#include "tcp.h"
#include "http.h"

#include "..\44blibs\io_port.h"
#include "..\44blibs\Adc.h"
//#include "..\uCosDRV\os_adc.h"
//extern unsigned int __ADC_ON;
unsigned int __ADC_ON;

// These structures keep track of connection information
extern CONNECTION conxn[];
extern unsigned int my_ipaddr;


extern unsigned int ETH_SendPackets;
extern unsigned int ETH_ReceivePackets;
extern unsigned int ETH_EVENT;
/*
unsigned short int cpu_temperature = 1;
unsigned short int air_temperature = 2;
unsigned short int cpu_voltage = 3;
*/

extern char html_header[];
extern char web_page[];
//extern unsigned char rcve_buf_allocated;

unsigned char text[256];

unsigned char CONTROL_LED = 1;

void itoa(unsigned int i,unsigned char *str, unsigned int x);

char *http_strstr(char *haystack, char *needle);
void http_send(unsigned char *outbuf, unsigned short int len, unsigned char nr);
void replace_tag(unsigned char *start, char *tag, char *sub);

void itoa(unsigned int i,unsigned char *str, unsigned int x)
{
   sprintf((char*)str,"%d",i);

   /*
   unsigned int x,y,i = 0;;
   do
   {
      str[i] = 
   }
   */

}
void init_http(void)
{
}



//------------------------------------------------------------------------
// This function is the standard string search. The Keil library
// does not provide it.  It looks for one string in another string
// and returns a pointer to it if found, otherwise returns NULL. 
//------------------------------------------------------------------------
char *http_strstr(char *haystack, char *needle)
{
	char *ptr1, *ptr2;
	
	// Protect against NULL pointer
	if (*needle == 0) return(haystack);
	for( ; *haystack; haystack++ )
	{
		// Look for needle in haystack.  If there is a
      // match then this will continue all the way
      // until ptr1 reaches the NULL at the end of needle 
		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
}



//------------------------------------------------------------------------
// This sends an TCP segment to the ip layer.  The segment is 
// is normally either a web page or a graphic.
// See "TCP/IP Illustrated, Volume 1" Sect 17.3
//------------------------------------------------------------------------
void http_send(unsigned char *outbuf, unsigned short int len, unsigned char nr)
{
   TCP_HEADER *tcp,tcph;
   IP_HEADER *ip,iph;

   unsigned int sum;
   unsigned short int result;
          
   // Fill in TCP segment header
   tcp = &tcph;
   ip = &iph;

   tcp_getHader(outbuf,tcp);
   ip_getHader(outbuf,ip);

   tcp->source_port = HTTP_PORT;
   tcp->dest_port = conxn[nr].port;
   tcp->sequence = conxn[nr].my_sequence;
   tcp->ack_number = conxn[nr].his_sequence;
      
	// Header is always 20 bytes long
   tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
   tcp->window = 1024;
   tcp->checksum = 0;
   tcp->urgent_ptr = 0;
   
   // Compute checksum including 12 bytes of pseudoheader
	// Must pre-fill 2 items in ip header to do this
	ip->dest_ipaddr = conxn[nr].ipaddr;
	ip->source_ipaddr = my_ipaddr;
		
	// Sum source_ipaddr, dest_ipaddr, and entire TCP message 
   tcp_setHader(outbuf,tcp);
   ip_setHader(outbuf,ip);

	sum = (unsigned int)cksum(outbuf + 26, 8 + len);
				
	// Add in the rest of pseudoheader which is
	// protocol id and TCP segment length
	sum += (unsigned int)0x0006;
	sum += (unsigned int)len;

	// In case there was a carry, add it back around
	tcp_getHader(outbuf,tcp);
   ip_getHader(outbuf,ip);
   
   result = (unsigned short int)(sum + (sum >> 16));
	tcp->checksum = ~result;

   tcp_setHader(outbuf,tcp);
   ip_setHader(outbuf,ip);
   
	ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);

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



//------------------------------------------------------------------------
// This searches a web page looking for a specified tag.  If found,
// it replaces the tag with the text in * sub.  Tags are fixed length -
// The first 4 chars of the tag is always "TAG:" and the rest of it
// is always 4 chars for a total of 8 chars. 
//------------------------------------------------------------------------
void replace_tag(unsigned char *start, char *tag, char *sub) 
{ 
   unsigned char i, flg;
   unsigned char *ptr;
   
   // Find tag.  If not found - just return
   ptr = (unsigned char*)http_strstr((char*)start, (char*)tag);
	if (ptr == NULL) return;
   
	flg = TRUE;

   // Replace the 8 char tag with the substitute text
	// Pad on the right with spaces
   for (i=0; i < 8; i++)
	{
   	    if (sub[i] == 0) flg = FALSE;
   	    if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
	}
}



//------------------------------------------------------------------------
//	This serves up either a HTML page, a JPEG image, or controls an 
// LED,  depending what it gets from the browser.  The received header
// must contain the word "GET" or "POST" to be considered a valid request.
// With HTTP 1.1 where the connection is left open, the header I send
// should include content length. With HTTP 1.0 you can just close the
// connection after sending the page and the browser knows its done. 
//
// The HTTP protocol specification is at http://www.w3.org/Protocols/ 
//------------------------------------------------------------------------
unsigned int http_server(unsigned char *inbuf, unsigned short int header_len, unsigned char nr, unsigned char resend)
{
	unsigned char i;
	unsigned short int body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
	unsigned short int sent, remaining;
	unsigned char outbuf[1520];
	unsigned char *ptr;
	unsigned char *tcp_data;
	unsigned char request;
   static unsigned char post_flg = FALSE;
		    	
	// Make sure this is a valid connection
	if (nr == NO_CONNECTION) return 0;
	
	// Compute start of TCP data
   
   // Save first 20 chars and seq number just in case
   // we need to re-generate page
   // TODO: if post, then save switch state infomation
   if (!resend)
   {
      tcp_data = inbuf + 34 + header_len;
      memcpy(conxn[nr].query, tcp_data, 20);
      conxn[nr].old_sequence = conxn[nr].my_sequence;
   }
   // If this is a resend, set sequence number to what it was
   // the last time we sent this
   else
   {
      tcp_data = inbuf;
      conxn[nr].my_sequence = conxn[nr].old_sequence;   
   }
   
   // Start off with no request
   request = NONE;
      
	// TODO: Calling http_strstr() on a large buffer takes a lot of time
   // so perhaps we could speed things up by limiting the search
   // range to the portion of the buffer where the item is expected
   // to be found
	
	// If it is a POST, then set a flag to start looking for the post
	// data of interest, which is the string "switch=".  It may arrive
	// in a later segment (Netscape seems to split up the POST message)
   if (http_strstr((char*)tcp_data, "POST") != NULL) post_flg = TRUE; 
	   
   // See if this is a GET message
   else if (http_strstr((char*)tcp_data, "GET") != NULL)
   {
      post_flg = FALSE;
      if (http_strstr((char*)tcp_data, "index") != NULL) request = GET_PAGE;
      else if (http_strstr((char*)tcp_data, "/ ") != NULL) request = GET_PAGE;
   }
   
   // If POST flag is "armed" then look for the "switch=" string
   // and if found, turn the LED on or off according to whether 
   // the browser is sending a 1 or a 0.
   if (post_flg)
   {
      ptr = (unsigned char*)http_strstr((char*)tcp_data, "switch=");
      if (ptr != NULL)
      {
         // Move to space after equals sign
         // Set control indicator accordingly
         post_flg = FALSE;
         request = POST_PAGE;
         ptr += 7;


         if (*ptr == '0')
         {
            IO_PortSetLed(LEDON);
            CONTROL_LED = 1;
            __ADC_ON = 1;
            Adc_Start(ADCON);

         }   
         else if (*ptr == '1') 
         {
            IO_PortSetLed(LEDOFF);
            CONTROL_LED = 0;
            __ADC_ON = 0;
         }
      }
   }

   if ((request == GET_PAGE) || (request == POST_PAGE))
   {
      // Figure out sizes
      hhdr_len = strlen(html_header);
      page_len = strlen(web_page);
      body_len = hhdr_len + page_len;
      
      // Free memory holding received message.  The message from the
      // browser can be 500+ bytes long so this is a significant 
      // chunk out of the available malloc space of 1500 bytes
      if (!resend) 
      {
         //rcve_buf_allocated = FALSE;
      }

      // Copy page data.  This moves data from flash into RAM.  It is
      // an undesirable process, but must get data into RAM to replace
      // tags 
		memcpy(outbuf + 54, html_header, hhdr_len);
		memcpy(outbuf + 54 + hhdr_len, web_page, page_len);
   	   	   	
   	outbuf[54 + body_len] = 0;		// Append NULL 
   
      // Replace length tag with actual value
      itoa(page_len, text, 10);
		replace_tag(outbuf + 54, "TAG:LEN1", (char*)text);
		
		// Replace CPU temperature tag with actual value
		itoa( ETH_ReceivePackets , text, 10);
		replace_tag(outbuf + 54, "TAG:TMP1", (char*)text);

		itoa(ETH_SendPackets, text, 10);
		replace_tag(outbuf + 54, "TAG:TMP2", (char*)text);

		// Replace CPU voltage tag with actual value X 100
		// Insert decimal point between first and second digits
		itoa( ETH_SendPackets + ETH_ReceivePackets , text, 10);
		replace_tag(outbuf + 54, "TAG:VOL1", (char*)text);
      
      // Insert the word CHECKED into the html so the browser will
		// check the correct LED state indicator box 
      if (CONTROL_LED == 0) replace_tag(outbuf + 54, "TAG:CHK1", "CHECKED");
      else replace_tag(outbuf + 54, "TAG:CHK2", "CHECKED");
      
      // Segment length = body_len + 20
      http_send(outbuf, body_len + 20, nr);
      conxn[nr].my_sequence += body_len;
   }
   else
   {
      // The incoming HTTP message did not warrant a response
      return 0;
   }
   
   // Return number of bytes sent, not including TCP header
	return(body_len);
}





⌨️ 快捷键说明

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