📄 http.c
字号:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Jim Brady
// Do not use commercially without author's permission
// Last revised August 2002
// 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 <stdlib.h>
#include <ctype.h> // toupper
#include "net.h"
#include "cksum.h"
#include "ip.h"
#include "tcp.h"
#include "http.h"
#include "utils.h"
// These structures keep track of connection information
extern CONNECTION conxn[];
extern ulong my_ipaddr;
extern char text[];
extern char html_header[];
extern char web_page[];
extern char jpeg_header[];
extern UCHAR photo1_jpeg[];
extern UCHAR rcve_buf_allocated;
extern UCHAR debug;
//bit CONTROL_LED;
//void LightONOFF(bit b);
char cnt = 0;
void init_http(void)
{
// CONTROL_LED = 0;
// LightONOFF(CONTROL_LED);
}
//------------------------------------------------------------------------
// 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 * 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(UCHAR * outbuf, uint len, UCHAR nr)
{
TCP_HEADER * tcp;
IP_HEADER * ip;
ulong sum;
uint result;
// Fill in TCP segment header
tcp = (TCP_HEADER *)(outbuf + 34);
ip = (IP_HEADER *)(outbuf + 14);
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;
#ifdef __LITTLEENDIAN__
tcp->flags = ntohs(tcp->flags);
tcp->window = ntohs(tcp->window);
tcp->source_port = ntohs(tcp->source_port);
tcp->sequence = ntohl(tcp->sequence);
tcp->dest_port = ntohs(tcp->dest_port);
tcp->ack_number = ntohl(tcp->ack_number);
ip->source_ipaddr = ntohl(my_ipaddr);
#endif
// 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));
tcp->checksum = ~result;
#ifdef __LITTLEENDIAN__
tcp->checksum = ntohs(tcp->checksum);
// ip_send(outbuf, ntohl(conxn[nr].ipaddr), TCP_TYPE, len);
conxn[nr].ipaddr = ntohl(conxn[nr].ipaddr);
#endif
// if (debug) serial_send("TCP: Sending msg to IP layer\r");
ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
#ifdef __LITTLEENDIAN__
free(outbuf);
#endif
// (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.
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// 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/
//------------------------------------------------------------------------
uint http_server(UCHAR * inbuf, uint header_len, UCHAR nr, UCHAR resend, int data_len)
{
UCHAR i;
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
// 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((char*)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
if(data_len>0)
{
//printf(tcp_data);
jpeg_len = data_len;//6194;
body_len = jpeg_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;}
// First send the header and enough of the jpeg to make 1000 bytes.
// The value of 1000 is arbitrary, but must be stay under 1500.
/* if (body_len < 1000)
remaining = body_len;
else
remaining = 1000; */
// outbuf = (UCHAR *)malloc(54 + data_len + 1);
outbuf = TX_BUFF;
if (outbuf == NULL)
{
// if (debug) serial_send("TCP: Oops, out of memory\r");
return 0;
}
memcpy(outbuf + 54, tcp_data, data_len);
http_send(outbuf, 20 + data_len, nr);
//#ifdef __LITTLEENDIAN__
conxn[nr].my_sequence += data_len;
//#endif
}
return 0;
// }
// Return number of bytes sent, not including TCP header
return(body_len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -