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

📄 http.lst

📁 Keil_C51程序,C8051实现的TCP/IP功能源码
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.20   HTTP                                                                  03/07/2006 14:49:15 PAGE 1   


C51 COMPILER V7.20, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN Http.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Http.c OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Net HTTP.C
   3          //
   4          // This module is the Web Server
   5          // It currently serves a html text page and a jpeg image, or handles
   6          // a POST message to turn an LED on or off.
   7          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
   8          //-----------------------------------------------------------------------------
   9          #include <string.h>
  10          #include <stdlib.h>
  11          #include <ctype.h>              // toupper
  12          #include "C8051f.h"
  13          #include "net.h"
  14          #include "serial.h"  
  15          #include "cksum.h"
  16          //#include "analog.h"
  17          #include "ip.h"
  18          #include "tcp.h"
  19          #include "http.h"
  20          
  21          
  22          // These structures keep track of connection information
  23          extern CONNECTION xdata conxn[];
  24           
  25          extern ULONG code my_ipaddr;
  26          extern char xdata text[];
  27          extern UINT idata cpu_temperature;
  28          extern UINT idata air_temperature;
  29          //extern UINT idata cpu_voltage;
  30          extern char code html_header[];
  31          extern char code web_page[];
  32          extern char code jpeg_header[];
  33          extern UCHAR code photo1_jpeg[];
  34          extern UCHAR idata rcve_buf_allocated;
  35          extern UCHAR idata debug;
  36          bit CONTROL_LED;
  37          //void LightONOFF(bit b);
  38          
  39          void init_http(void)
  40          {
  41   1      //  CONTROL_LED = 0;//主要利用灯来显示
  42   1      //  LightONOFF(CONTROL_LED);
  43   1      }
  44          
  45          //------------------------------------------------------------------------
  46          // This function is the standard string search. The Keil library
  47          // does not provide it.  It looks for one string in another string
  48          // and returns a pointer to it if found, otherwise returns NULL. 
  49          // 
  50          //------------------------------------------------------------------------
  51          char * strstr(char * haystack, char * needle)
  52          {
  53   1              char *ptr1, *ptr2;
  54   1              
  55   1              // Protect against NULL pointer
C51 COMPILER V7.20   HTTP                                                                  03/07/2006 14:49:15 PAGE 2   

  56   1              if (*needle == 0) return(haystack);
  57   1              for( ; *haystack; haystack++ )
  58   1              {
  59   2                      // Look for needle in haystack.  If there is a
  60   2            // match then this will continue all the way
  61   2            // until ptr1 reaches the NULL at the end of needle 
  62   2                      for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
  63   2                                                              
  64   2                      // If there is a match then return pointer to needle in haystack
  65   2                      if(*ptr1 == 0) return(haystack);        
  66   2              }
  67   1              return NULL;                    // no matching string found
  68   1      }
  69          
  70          
  71          //------------------------------------------------------------------------
  72          // This sends an TCP segment to the ip layer.  The segment is 
  73          // is normally either a web page or a graphic.
  74          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
  75          // HTTP发送,传送到IP层,这个段通常是网页或者图像。
  76          //------------------------------------------------------------------------
  77          void http_send(UCHAR xdata * outbuf, UINT len, UCHAR nr)
  78          {
  79   1         TCP_HEADER xdata * tcp;
  80   1         IP_HEADER xdata * ip;
  81   1         ULONG idata sum;
  82   1         UINT idata result;
  83   1                
  84   1         // Fill in TCP segment header
  85   1         tcp = (TCP_HEADER xdata *)(outbuf + 34);
  86   1         ip = (IP_HEADER xdata *)(outbuf + 14);
  87   1      
  88   1         tcp->source_port = HTTP_PORT;
  89   1         tcp->dest_port = conxn[nr].port;
  90   1         tcp->sequence = conxn[nr].my_sequence;
  91   1         tcp->ack_number = conxn[nr].his_sequence;
  92   1            
  93   1              // Header is always 20 bytes long
  94   1         tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
  95   1         tcp->window = 1024;
  96   1         tcp->checksum = 0;
  97   1         tcp->urgent_ptr = 0;
  98   1         
  99   1         // Compute checksum including 12 bytes of pseudoheader
 100   1              // Must pre-fill 2 items in ip header to do this
 101   1              ip->dest_ipaddr = conxn[nr].ipaddr;
 102   1              ip->source_ipaddr = my_ipaddr;
 103   1                      
 104   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 105   1              sum = (ULONG)cksum(outbuf + 26, 8 + len);
 106   1                                      
 107   1              // Add in the rest of pseudoheader which is
 108   1              // protocol id and TCP segment length
 109   1              sum += (ULONG)0x0006;
 110   1              sum += (ULONG)len;
 111   1      
 112   1              // In case there was a carry, add it back around
 113   1              result = (UINT)(sum + (sum >> 16));
 114   1              tcp->checksum = ~result;
 115   1         
 116   1         if (debug) SendCommString("TCP: Sending msg to IP layer\r");
 117   1              ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
C51 COMPILER V7.20   HTTP                                                                  03/07/2006 14:49:15 PAGE 3   

 118   1      
 119   1         // (Re)start TCP retransmit timer
 120   1         conxn[nr].timer = TCP_TIMEOUT;
 121   1      }
 122          
 123          
 124          
 125          //------------------------------------------------------------------------
 126          // This searches a web page looking for a specified tag.  If found,
 127          // it replaces the tag with the text in * sub.  Tags are fixed length -
 128          // The first 4 chars of the tag is always "TAG:" and the rest of it
 129          // is always 4 chars for a total of 8 chars. 
 130          //------------------------------------------------------------------------
 131          void replace_tag(UCHAR xdata * start, char * tag, char * sub) 
 132          { 
 133   1         UCHAR idata i, flg;
 134   1         UCHAR xdata * ptr;
 135   1         
 136   1         // Find tag.  If not found - just return
 137   1         ptr = strstr(start, tag);
 138   1              if (ptr == NULL) return;
 139   1         
 140   1              flg = TRUE;
 141   1      
 142   1         // Replace the 8 char tag with the substitute text
 143   1              // Pad on the right with spaces
 144   1         for (i=0; i < 8; i++)
 145   1              {
 146   2              if (sub[i] == 0) flg = FALSE;
 147   2              if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
 148   2              }
 149   1      }
 150          
 151          
 152          
 153          //------------------------------------------------------------------------
 154          //      This serves up either a HTML page, a JPEG image, or controls an 
 155          // LED,  depending what it gets from the browser.  The received header
 156          // must contain the word "GET" or "POST" to be considered a valid request.
 157          // With HTTP 1.1 where the connection is left open, the header I send
 158          // should include content length. With HTTP 1.0 you can just close the
 159          // connection after sending the page and the browser knows its done. 
 160          //
 161          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
 162          //------------------------------------------------------------------------
 163          UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
 164          {
 165   1              UCHAR i;
 166   1              UINT idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
 167   1              UINT idata sent, remaining;
 168   1              UCHAR xdata * outbuf;
 169   1              UCHAR xdata * ptr;
 170   1              UCHAR xdata * tcp_data;
 171   1              UCHAR idata request;
 172   1         static UCHAR idata post_flg = FALSE;
 173   1                              
 174   1              // Make sure this is a valid connection
 175   1              if (nr == NO_CONNECTION) return 0;
 176   1              
 177   1              // Compute start of TCP data
 178   1         
 179   1         // Save first 20 chars and seq number just in case
C51 COMPILER V7.20   HTTP                                                                  03/07/2006 14:49:15 PAGE 4   

 180   1         // we need to re-generate page
 181   1         // TODO: if post, then save switch state infomation
 182   1         if (!resend)
 183   1         {
 184   2            tcp_data = inbuf + 34 + header_len;
 185   2            memcpy(conxn[nr].query, tcp_data, 20);
 186   2            conxn[nr].old_sequence = conxn[nr].my_sequence;
 187   2         }
 188   1         // If this is a resend, set sequence number to what it was
 189   1         // the last time we sent this
 190   1         else
 191   1         {
 192   2            tcp_data = inbuf;

⌨️ 快捷键说明

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