📄 http_server.c
字号:
https_activatesession((UINT8)session);
return(1);
case TCP_EVENT_CLOSE:
if(session < 0)
return(-1);
https_deletesession((UINT8)session);
return(1);
case TCP_EVENT_ACK:
if(session < 0)
return(-1);
https[session].fpoint += https[session].funacked;
https[session].funacked = 0;
return(1);
case TCP_EVENT_DATA:
/* Check for GET request */
if(session < 0)
return(-1);
if(https[session].fstart == 0)
{
if(par1 <= 3)
return(1);
/* Check for GET */
if(RECEIVE_NETWORK_B() != 'G')
return(1);
if(RECEIVE_NETWORK_B() != 'E')
return(1);
if(RECEIVE_NETWORK_B() != 'T')
return(1);
par1 -= 3;
/* Search for '/' */
for(i=0; i<par1; i++)
{
if(RECEIVE_NETWORK_B() == '/')
{
i++;
break;
}
}
par1 -= i;
/* Calculate Hash */
i = https_calculatehash(par1);
if(i < 0)
{
/* Invalid GET */
return(1);
}
/* Get FileRef */
i = https_findfile((UINT8)i, (UINT8)session);
return(1);
}
return(1);
case TCP_EVENT_REGENERATE:
if(session < 0)
return(-1);
if(https[session].state != HTTPS_STATE_ACTIVE)
return(-1);
i = https_loadbuffer(session, &net_buf[TCP_APP_OFFSET], (UINT16)par1);
if(i<0)
return(-1);
tcp_send(https[session].ownersocket, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i);
return(i);
default:
return(-1);
}
}
void https_deletesession (UINT8 ses)
{
https[ses].state = HTTPS_STATE_FREE;
https[ses].fstart = 0;
https[ses].fpoint = 0;
https[ses].flen = 0;
https[ses].funacked = 0;
https[ses].headersent = 0;
https[ses].bodysent = 0;
https[ses].unicount = 0;
https[ses].portPin = 0;
https[ses].filesize = 0;
}
INT16 https_searchsession (UINT8 soch)
{
UINT8 i;
for(i=0; i<NO_OF_HTTP_SESSIONS; i++)
{
if(https[i].ownersocket == soch)
return(i);
}
return(-1);
}
INT16 https_bindsession (UINT8 soch)
{
UINT8 i;
for(i=0; i<NO_OF_HTTP_SESSIONS; i++)
{
if(https[i].ownersocket == soch)
{
if(https[i].state == HTTPS_STATE_FREE)
{
https[i].state = HTTPS_STATE_RESERVED;
return(i);
}
}
}
return(-1);
}
void https_activatesession (UINT8 ses)
{
https[ses].state = HTTPS_STATE_ACTIVE;
}
/* read two encoded bytes from HTTP URI and return
* decoded byte
*/
UINT8 https_read_encoded(void)
{
UINT8 temp,ch;
temp = RECEIVE_NETWORK_B();
if((temp>='0')&&(temp<='9')){
ch = (temp-'0')<<4;
}else{
if((temp>='a')&&(temp<='f')){
ch = (temp-'a'+10)<<4;
}else{
ch = (temp-'A'+10)<<4;
}
}
temp = RECEIVE_NETWORK_B();
if((temp>='0')&&(temp<='9')){
ch |= (temp-'0');
}else{
if((temp>='a')&&(temp<='f')){
ch |= (temp-'a'+10);
}else{
ch |= (temp-'A'+10);
}
}
return ch;
}
INT16 https_calculatehash (UINT32 len)
{
UINT8 hash=0;
UINT8 ch;
UINT8 i;
UINT8 iCount;
/* Read Max 60 characters */
if(len > 60)
len = 60;
for( i=0; i<len; i++)
{
ch = RECEIVE_NETWORK_B();
if( ch ==' ') /* End reached? */
break;
if( ch =='?') /* Fetch the GET message from stream */
{
UINT8 stringcount = 0;
/* Read the GET after '?' but before space (0x20) */
do
{
GETmessage.GETmsg[stringcount] = RECEIVE_NETWORK_B();
} while(GETmessage.GETmsg[stringcount++] != ' ');
/* separate KEY=VALUE */
do
{
/* Set index to the next (&, _, \0) */
if((GETmessage.GETpos = strcspn(GETmessage.GETmsg, "& \0")))
{
/* Search for the = */
if(iCount = strcspn(GETmessage.GETmsg, "="))
{
/* replace = with 0 */
GETmessage.GETmsg[iCount] = 0;
/* Controls MCU Port pins from mcustats.html */
if(strncmp("LED", GETmessage.GETmsg, strlen("LED")) == 0)
{
unsigned char pin, port;
port = GETmessage.GETmsg[iCount+1] - 0x30;
pin = 1<<GETmessage.GETmsg[iCount+2] -0x30;
*((&PDR00)+port) ^= pin;
//printf("LED String found %i;)\n", (int) GETmessage.GETmsg[iCount+1]);
}
/* Prints the UART Text from FORM */
if(strncmp("UART", GETmessage.GETmsg, strlen("UART")) == 0)
{
unsigned char i2 = 1;
int position = 0;
while(GETmessage.GETmsg[iCount+i2] != 0x20)
{
if(GETmessage.GETmsg[iCount+i2] == '+')
{
GETmessage.GETmsg[iCount+i2] = ' ';
}
/* Check for unicode = */
if(GETmessage.GETmsg[iCount+i2] == '%' && GETmessage.GETmsg[iCount+i2+1] == '3' && GETmessage.GETmsg[iCount+i2+2] == 'D')
{
// Ok, next is the = as %3D
printf("=");
i2 += 3;
}
printf("%c", GETmessage.GETmsg[iCount+i2]);
i2++;
}
printf("\n");
}
}
}
} while(0);
/* Save length of GET in File */
GETmessage.GETlen = stringcount - 1;
break;
}
/* encoded HTTP URI ? */
if( ch == '%')
{
ch = https_read_encoded();
/* is this UNICODE char encoded? (for now allow only
* one byte encoded UNICODE chars)
*/
if( ( ch & 0xe0 ) == 0xc0)
{
/* yes */
ch = ( ch & 0x1F ) << 6;
RECEIVE_NETWORK_B(); /* skip first % */
ch |= (https_read_encoded() & 0x3F);
}
}
hash *= 37;
hash += ch;
}
if(i==len)
return(-1);
/* construct address for Hash table */
if(hash == 0) /* User asked defaul file */
{
/* Set hash to index.html value */
hash = 0x0B;
}
/* Now we have hash value calculated */
return( hash );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -