📄 http.c
字号:
/*
DM&P Mity-Mite Demo Program.
Copyright (C) 2002 by DM&P.
This file is from example code of DSock.
*/
#include "http.h"
#include "smtp.h"
#include "grlcd.h"
#include <stdio.h>
#include <string.h>
#include <dos.h>
/* Convert decoded query string to normal string. */
void Convert(char *p2);
BOOL HttpServer(SOCKET s)
{
char szBuf[1024];
char szBuf2[1024];
char *p,*p2;
DWORD dwIp;
BOOL bPost;
if(SocketAccept(s,&dwIp)==FALSE)
return TRUE;
inet_ntoa(szBuf,dwIp);
printf("HTTP: Accepting request from %s: ",szBuf);
SocketGetString(s,szBuf,1024);
printf("%s\n",szBuf);
/* Request string: <GET|POST> </PATH> <HTTP/VERSION> */
/* Get path */
p = szBuf;
while(*p!='/')
p++;
p2 = p;
while(*p2!=' ')
p2++;
*p2 = 0; /* Now, p point to ASCIIZ of path. */
bPost = strnicmp(szBuf,"POST",4)==0;
if(bPost)
{
/* Query string of POST method will start with a null line. */
while(SocketDataReady(s))
{
SocketGetString(s,szBuf2,1024);
if(stricmp(szBuf2,"")==0)
break;
}
SocketGetString(s,szBuf2,1024);
p2 = szBuf2;
}
else
{
/* Query string of GET method is start with '?' */
p2 = p;
while((*p2!='?') && (*p2!=0))
p2++;
*p2 = 0;
p2++;
}
/* Now, p2 point to query string. */
if(stricmp(p,"/")==0) /* Send default page */
p = "/default.htm";
if(stricmp(p,"/mail")==0) /* Send mail */
{
char *pcKey, *pcValue;
char szMailServer[128];
char szFrom[128];
char szTo[128];
char szSubject[128];
char szContent[1024];
BOOL bResult;
DWORD dwIp;
Convert(p2);
/* Split "key1=value1&key2=value2&... */
while(*p2)
{
pcKey = p2;
while(*p2!='=')
p2++;
*p2 = 0;
pcValue = ++p2;
while(*p2 && *p2!='&')
p2++;
if(*p2)
{
*p2 = 0;
p2++;
}
if(stricmp(pcKey,"ms")==0)
strcpy(szMailServer,pcValue);
if(stricmp(pcKey,"from")==0)
strcpy(szFrom,pcValue);
if(stricmp(pcKey,"to")==0)
strcpy(szTo,pcValue);
if(stricmp(pcKey,"subject")==0)
strcpy(szSubject,pcValue);
if(stricmp(pcKey,"content")==0)
strcpy(szContent,pcValue);
}
/* Is mail for me ? */
if(DSock_Resolve(szMailServer)==DSock_GetHostIp())
{
int i;
bResult = TRUE;
SetLed(1);
GrLcd_printf(0,3*8,"You got mail.");
Beep3();
sprintf(_ppcMailBuf[0],"Subject: %s",szSubject);
_nMailLine = 1;
p = szContent;
p2 = p;
/* Copy content to mail buffer */
while(*p)
{
if(*p==0x0a)
{
*(p-1) = 0;
strncpy(_ppcMailBuf[_nMailLine],p2,MAIL_LINE_SIZE-1);
_ppcMailBuf[_nMailLine][MAIL_LINE_SIZE] = 0;
_nMailLine++;
p2 = p+1;
}
p++;
}
}
else
{
bResult = SendMail(szMailServer,szFrom,szTo,szSubject,szContent);
}
SocketPutString(s,"<html><head><title>Mity-Mite Module</title></head><body>\n");
if(bResult==FALSE)
SocketPutString(s,"<h2>Send mail error!</h2>\n");
else
SocketPutString(s,"<h2>Mail is sent.</h2>\n");
SocketPutString(s,"<p><hr><a href=\"/\">Back</a>\n");
SocketPutString(s,"</body></html>\n");
}
else if(stricmp(p,"/lcd")==0) /* Send text to LCD */
{
/* Prase your query string here */
SocketPutString(s,"<html><head><title>Mity-Mite Module Web Server Demo</title></head><body>\n");
while(*p2!='=') p2++;
p2++;
Convert(p2);
GrLcd_printf(0,8*2,"Msg:%-25s",p2);
SocketPutString(s,"The string \"%s\" was sent to LCD.\n",p2);
SocketPutString(s,"<p><hr><a href=\"/\">Back</a>\n");
SocketPutString(s,"</body></html>\n");
Beep2();
}
else /* Send file only */
{
FILE *fp = fopen(p+1,"rb");
if(fp==NULL)
{
SocketPutString(s,"<html><head><title>Error</title></head><body>\n");
SocketPutString(s,"<h2>Unable to open '%s'</h2>\n",p+1);
SocketPutString(s,"<p><hr><a href=\"/\">Back</a>\n");
SocketPutString(s,"</body></html>\n");
}
else
{
int nLen;
while(!feof(fp))
{
nLen = fread(szBuf,1,1024,fp);
SocketSend(s,(uchar *)szBuf,nLen);
}
fclose(fp);
}
}
SocketClose(s);
SocketBind(s,0L,HTTP_PORT);
SocketListen(s);
return TRUE;
}
void Convert(char *p2)
{
char *p = p2;
while(*p)
{
if(*p=='+')
*p2 = ' ';
else if(*p=='%')
{
char c;
p++;
c = *p>='A' ? *p - 'A' + 10 : *p - '0';
p++;
c *= 16;
c += (*p>='A' ? *p - 'A' + 10 : *p - '0');
*p2 = c;
}
else
*p2 = *p;
p++;
p2++;
}
*p2 = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -