📄 http.c
字号:
//-----------------------------------http.c-----------------------------
//#define DEBUG 1
#include<string.h>
#include"includes.h"
#include"logo.c"
#define NONE 0
#define GET_PAGE 1
#define GET_JPEG 2
#define POST_PAGE 3
extern TCB xdata conn[5];
extern char code html_header[];
extern char code web_page[];
extern char code jpeg_header[];
extern uchar code photo1_jpeg[];
void http_send(uchar xdata * outbuf, uint len, uchar nr)
{
Tcp_Header xdata * tcp;
Ip_Header xdata * ip;
ulong idata sum;
uint idata result;
TRACE("ENTER http_send",NULL,0,0);
tcp = (Tcp_Header xdata *)(outbuf + 34);
ip = (Ip_Header xdata *)(outbuf + 14);
#ifdef OSCAR_DEBUG
// TRACE("HttpData=",outbuf+54,100,0);
TRACE("HttpData=",0,0,0);
uart_send(outbuf, 100);
#endif
//填充TCP报文首部
tcp->source_port = HTTP_PORT;
tcp->dest_port = conn[nr].port;
tcp->sn = conn[nr].my_sn;
tcp->ack_sn = conn[nr].his_sn;
//20 bytes首部长度
tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
tcp->window = 1024;
tcp->checksum = 0;
tcp->urgent_ptr = 0;
//计算校验码包括12byte的伪首部
ip->dest_ipaddr = conn[nr].ipaddr;
ip->source_ipaddr = myipaddr;
sum = (ulong)cksum(outbuf + 26, 8 + len);
//加上协议类型和TCP总长度
sum += (ulong)0x0006;
sum += (ulong)len;
//进位处理
result = (uint)(sum + (sum >> 16));
tcp->checksum = ~result;
ip_send(outbuf, conn[nr].ipaddr, TCP_TYPE, len);
// (Re)start TCP retransmit timer
conn[nr].timer = TCP_TIMEOUT;
}
uint http_server(uchar xdata * inbuf, uint header_len, uchar nr, uchar resend)
{
uint idata body_len, hhdr_len, page_len;
char xdata text[10];
uchar xdata * tcp_data;
uchar idata request;
static uchar idata post_flg = FALSE;
uchar xdata * ptr;
TRACE("ENTER http_server",NULL,0,0);
// Make sure this is a valid connection
if (nr == NO_CONN) return 0;
if (!resend)
{
tcp_data = inbuf + 34 + header_len;
memcpy(conn[nr].query, tcp_data, 20);
conn[nr].old_sn = conn[nr].my_sn;
TRACE("HTTP_HEARD = ",tcp_data,300,1);
}
// If this is a resend, set sequence number to what it was
else
{
tcp_data = inbuf;
conn[nr].my_sn = conn[nr].old_sn;
}
request = NONE;
if (strfind(tcp_data, "POST") != NULL)
{
post_flg = TRUE;
TRACE("RECV > POST",NULL,0,0);
}
// See if this is a GET message
else if (strfind(tcp_data, "GET") != NULL)
{
TRACE("RECV > GET",NULL,0,0);
post_flg = FALSE;
if (strfind(tcp_data, "photo1") != NULL) request = GET_JPEG;
else if (strfind(tcp_data, "index") != NULL) request = GET_PAGE;
else if (strfind(tcp_data, "/ ") != NULL) request = GET_PAGE;
}
if (post_flg)
{
ptr = strfind(tcp_data, "switch=");
if (ptr != NULL)
{
post_flg = FALSE;
request = POST_PAGE;
// ptr += 7;
// if (*ptr == '1') {CONTROL_LED=0x0;}
// else if (*ptr == '0') {CONTROL_LED=0x1;}
// LightONOFF(CONTROL_LED);
}
}
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) {;}
if (outbuf == NULL)
{
return 0;
}
// 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
itoa(page_len, text, 10);
replace_tag(outbuf + 54, "TAG:LEN1", text,8);
replace_tag(outbuf + 54, "123456789012", __DATE__,12);
replace_tag(outbuf + 54, "987654321", __TIME__,9);
http_send(outbuf, body_len + 20, nr);
conn[nr].my_sn += body_len;
}
else if (request == GET_JPEG)
{
// Figure out sizes
// hhdr_len = strlen(bmp_GTCLogo);
// page_len = strlen(web_page);
body_len = strlen(bmp_GTCLogo);
// 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) {;}
if (outbuf == NULL)
{
return 0;
}
// memcpy(outbuf + 54, html_header, hhdr_len);
memcpy(outbuf + 54 , bmp_GTCLogo, body_len);
outbuf[54 + body_len] = 0; // Append NULL
itoa(page_len, text, 10);
http_send(outbuf, body_len + 20, nr);
}
else
{
return 0;
}
// Return number of bytes sent, not including TCP header
return(body_len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -