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

📄 http.lst

📁 cp2200网卡的tcp/ip源程序( 包含驱动),十分难得哦
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.50   HTTP                                                                  08/03/2006 09:34:43 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN Http.OBJ
COMPILER INVOKED BY: C:\Keil750\C51\BIN\c51.exe Http.c DB OE

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Copyright (c) 2002 Jim Brady
   3          // Do not use commercially without author's permission
   4          // Last revised August 2002
   5          // Net HTTP.C
   6          //
   7          // This module is the Web Server
   8          // It currently serves a html text page and a jpeg image, or handles
   9          // a POST message to turn an LED on or off.
  10          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
  11          //-----------------------------------------------------------------------------
  12          #include <string.h>
  13          //#include <stdlib.h>
  14          #include <ctype.h>              // toupper
  15          #include "C8051f.h"
  16          #include "net.h"
  17          #include "cksum.h"
  18          #include "analog.h"
  19          #include "ip.h"
  20          #include "tcp.h"
  21          #include "http.h"
  22          
  23          
  24          // These structures keep track of connection information
  25          extern CONNECTION xdata conxn[];
  26           
  27          extern ULONG code my_ipaddr;
  28          extern char xdata text[];
  29          extern UINT idata cpu_temperature;
  30          extern UINT idata air_temperature;
  31          extern UINT idata cpu_voltage;
  32          extern char code html_header[];
  33          extern char code web_page[];
  34          extern char code jpeg_header[];
  35          extern UCHAR code photo1_jpeg[];
  36          extern UCHAR idata rcve_buf_allocated;
  37          extern UCHAR idata debug;
  38          bit CONTROL_LED;
  39          void LightONOFF(bit b);
  40          extern char xdata outbuf1[];
  41          
  42          void init_http(void)
  43          {
  44   1        CONTROL_LED = 0;
  45   1        LightONOFF(CONTROL_LED);
  46   1      }
  47          
  48          char * itoa(UINT value, char * buf, UCHAR radix)
  49          {
  50   1              UINT i;
  51   1              char * ptr;
  52   1              char * temphold;
  53   1      
  54   1              temphold = buf;
  55   1              ptr = buf + 12;
C51 COMPILER V7.50   HTTP                                                                  08/03/2006 09:34:43 PAGE 2   

  56   1              *--ptr = 0;             // Insert NULL char
  57   1              do
  58   1              {
  59   2                 // First create string in reverse order
  60   2                 i = (value % radix) + 0x30;
  61   2                      if(i > 0x39) i += 7;
  62   2                      *--ptr = i;
  63   2            value = value / radix;
  64   2              } while(value != 0);
  65   1      
  66   1              // Next, move the string 6 places to the left
  67   1              // Include NULL character
  68   1              for( ; (*buf++ = *ptr++); );    
  69   1              return(temphold);
  70   1      }
  71          
  72          //------------------------------------------------------------------------
  73          // This function is the standard string search. The Keil library
  74          // does not provide it.  It looks for one string in another string
  75          // and returns a pointer to it if found, otherwise returns NULL. 
  76          //------------------------------------------------------------------------
  77          char * strstr(char * haystack, char * needle)
  78          {
  79   1              char *ptr1, *ptr2;
  80   1              
  81   1              // Protect against NULL pointer
  82   1              if (*needle == 0) return(haystack);
  83   1              for( ; *haystack; haystack++ )
  84   1              {
  85   2                      // Look for needle in haystack.  If there is a
  86   2            // match then this will continue all the way
  87   2            // until ptr1 reaches the NULL at the end of needle 
  88   2                      for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
  89   2                                                              
  90   2                      // If there is a match then return pointer to needle in haystack
  91   2                      if(*ptr1 == 0) return(haystack);        
  92   2              }
  93   1              return NULL;                    // no matching string found
  94   1      }
  95          
  96          
  97          
  98          //------------------------------------------------------------------------
  99          // This sends an TCP segment to the ip layer.  The segment is 
 100          // is normally either a web page or a graphic.
 101          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
 102          //------------------------------------------------------------------------
 103          void http_send(UCHAR xdata * outbuf, UINT len, UCHAR nr)
 104          {
 105   1         TCP_HEADER xdata * tcp;
 106   1         IP_HEADER xdata * ip;
 107   1         ULONG idata sum;
 108   1         UINT idata result;
 109   1                
 110   1         // Fill in TCP segment header
 111   1         tcp = (TCP_HEADER xdata *)(outbuf + 34);
 112   1         ip = (IP_HEADER xdata *)(outbuf + 14);
 113   1      
 114   1         tcp->source_port = HTTP_PORT;
 115   1         tcp->dest_port = conxn[nr].port;
 116   1         tcp->sequence = conxn[nr].my_sequence;
 117   1         tcp->ack_number = conxn[nr].his_sequence;
C51 COMPILER V7.50   HTTP                                                                  08/03/2006 09:34:43 PAGE 3   

 118   1            
 119   1              // Header is always 20 bytes long
 120   1         tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
 121   1         tcp->window = 1024;
 122   1         tcp->checksum = 0;
 123   1         tcp->urgent_ptr = 0;
 124   1         
 125   1         // Compute checksum including 12 bytes of pseudoheader
 126   1              // Must pre-fill 2 items in ip header to do this
 127   1              ip->dest_ipaddr = conxn[nr].ipaddr;
 128   1              ip->source_ipaddr = my_ipaddr;
 129   1                      
 130   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 131   1              sum = (ULONG)cksum(outbuf + 26, 8 + len);
 132   1                                      
 133   1              // Add in the rest of pseudoheader which is
 134   1              // protocol id and TCP segment length
 135   1              sum += (ULONG)0x0006;
 136   1              sum += (ULONG)len;
 137   1      
 138   1              // In case there was a carry, add it back around
 139   1              result = (UINT)(sum + (sum >> 16));
 140   1              tcp->checksum = ~result;
 141   1         
 142   1              ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
 143   1      
 144   1         // (Re)start TCP retransmit timer
 145   1         conxn[nr].timer = TCP_TIMEOUT;
 146   1      }
 147          
 148          
 149          
 150          //------------------------------------------------------------------------
 151          // This searches a web page looking for a specified tag.  If found,
 152          // it replaces the tag with the text in * sub.  Tags are fixed length -
 153          // The first 4 chars of the tag is always "TAG:" and the rest of it
 154          // is always 4 chars for a total of 8 chars. 
 155          //------------------------------------------------------------------------
 156          void replace_tag(UCHAR xdata * start, char * tag, char * sub) 
 157          { 
 158   1         UCHAR idata i, flg;
 159   1         UCHAR xdata * ptr;
 160   1         
 161   1         // Find tag.  If not found - just return
 162   1         ptr = strstr(start, tag);
 163   1              if (ptr == NULL) return;
 164   1         
 165   1              flg = TRUE;
 166   1      
 167   1         // Replace the 8 char tag with the substitute text
 168   1              // Pad on the right with spaces
 169   1         for (i=0; i < 8; i++)
 170   1              {
 171   2              if (sub[i] == 0) flg = FALSE;
 172   2              if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
 173   2              }
 174   1      }
 175          
 176          
 177          
 178          //------------------------------------------------------------------------
 179          //      This serves up either a HTML page, a JPEG image, or controls an 
C51 COMPILER V7.50   HTTP                                                                  08/03/2006 09:34:43 PAGE 4   

 180          // LED,  depending what it gets from the browser.  The received header
 181          // must contain the word "GET" or "POST" to be considered a valid request.
 182          // With HTTP 1.1 where the connection is left open, the header I send
 183          // should include content length. With HTTP 1.0 you can just close the
 184          // connection after sending the page and the browser knows its done. 
 185          //
 186          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
 187          //------------------------------------------------------------------------
 188          UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
 189          {
 190   1              UCHAR i;
 191   1              UINT idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
 192   1              UINT idata sent, remaining;
 193   1              UCHAR xdata * outbuf;
 194   1              UCHAR xdata * ptr;
 195   1              UCHAR xdata * tcp_data;
 196   1              UCHAR idata request;
 197   1         static UCHAR idata post_flg = FALSE;
 198   1                              
 199   1              // Make sure this is a valid connection
 200   1              if (nr == NO_CONNECTION) return 0;
 201   1              
 202   1              // Compute start of TCP data
 203   1         
 204   1         // Save first 20 chars and seq number just in case

⌨️ 快捷键说明

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