📄 httpd.c
字号:
if(!strncmp(httpMethod, "GET ",3)) //check if it is a GET request
{
TCPbufferFlush(socket); //clear the buffer
httpUriVersion=strtok(NULL,"\r\n"); //get the rest of the request line
httpUriVerLength=strlen(httpUriVersion);
httpUri=strtok(httpUriVersion," ");
if(httpUriVerLength==strlen(httpUri)) //no httpVersion given, assume 0.9
{
http09ver=1;
}
else
{
httpVer=strtok(NULL," ");
if(!strncmp(httpVer, "HTTP/0.9",8))
{
http09ver=1;
}
else if(strncmp(httpVer, "HTTP/1.0",8) && strncmp(httpVer, "HTTP/1.1",8)) //check if valid version
{
SendHtmlFlash(HTTP_STATUSLINE_BADREQUEST,HTTP_HTML_BADREQUEST_TITLE,HTTP_HTML_BADREQUEST_BODY,socket,1);
return;
}
}
if(!strncmp(httpUri,"/\0",2)) //if no file is given, return default, index.htm
{
httpUri="/index.htm\0";
}
if(!strncmp(httpUri,"/server.ini\0",11)) //refuse to display server.ini, this would reveal FTP login
{
SendHtmlFlash(HTTP_STATUSLINE_FORBIDDEN,HTTP_HTML_FORBIDDEN_TITLE,HTTP_HTML_FORBIDDEN_BODY,socket,!http09ver);
return;
}
httpFile=ffopen(++httpUri,'r'); //++ to discard inital '/'
if(httpFile==NULL)
{
str = strtok(httpUri, ".");
strncpy(filename, str, 8);
str = strtok(NULL, " \r\n");
strncpy(fileext, str, 3);
if(ffindFile(filename, fileext)==0xffff) //file not found
{
SendHtmlFlash(HTTP_STATUSLINE_NOTFOUND,HTTP_HTML_NOTFOUND_TITLE,HTTP_HTML_NOTFOUND_BODY,socket,!http09ver);
}
else //file exsits, but is not available
{
SendHtmlFlash(HTTP_STATUSLINE_UNAVAILABLE,HTTP_HTML_UNAVAILABLE_TITLE,HTTP_HTML_UNAVAILABLE_BODY,socket,!http09ver);
}
}
else
{
sendFile(!http09ver,httpFile,socket); //sends the file
}
}
else if(!strncmp(httpMethod, "POST ",4))
{
do
{
bytesRead=TCPreadln(socket, HTTP_BUFFER-1, httpBuffer, 0); //read one line from buffer
if ( (httpBuffer[bytesRead-1]=='\r' || httpBuffer[bytesRead-1]=='\n') //check for end of line
&& (httpBuffer[bytesRead-2]=='\r' || httpBuffer[bytesRead-2]=='\n') )
{
httpBuffer[bytesRead]='\0'; //terminates line
if (!strncmp(httpBuffer, "Content-length:",15))//content-length field has been found
{
sscanf(httpBuffer, "Content-length: %d", &contentLength);
}
else if (!strncmp(httpBuffer, "Content-Length:",15))
{
sscanf(httpBuffer, "Content-Length: %d", &contentLength);
}
if (bytesRead==2 && eol && contentLength) //content has been found
{
if (contentLength>(HTTP_BUFFER-2)) //content exceeds internal buffersize
{
SendHtmlFlash(HTTP_STATUSLINE_SERVERERROR,HTTP_HTML_SERVERERROR_TITLE,HTTP_HTML_SERVERERROR_BODY,socket,1);
return;
}
if ( (bytesRead=TCPread(socket, contentLength, httpBuffer, 0)) == contentLength)
{//finished reading
httpBuffer[bytesRead]='\0';//terminate datastring
httpBuffer[bytesRead+1]='\0';//terminate datastring
ewsScript(socket,httpBuffer);
return;
}
else //data is incomplete
{
return;
}
}//content
eol=1;
}//read to end of line
else if (bytesRead==HTTP_BUFFER) //buffer is full
{
eol=0; //did not read to end of line
}
else //not a complete POST request
{
return; //TCP is not ready for reading
}
}while (bytesRead>0);
}
else //request not recognized
{
TCPget(socket,bytesRead,httpBuffer); //remove the request-line from the tcp-buffer
SendHtmlFlash(HTTP_STATUSLINE_NOTIMPLEMENTED,HTTP_HTML_NOTIMPLEMENTED_TITLE,HTTP_HTML_NOTIMPLEMENTED_BODY,socket,1);
}
}
}
/*SendHtmlFlash, sends a HTML page whos body and title are stored in flash.
This function is used to send various status codes to the client browser,
like 404 Not Found*/
void SendHtmlFlash(char flash * statusLine,char flash * htmlTitle,char flash * htmlBody, SOCKET * socket, unsigned char addHeaders)
{
char tempBuf[12];
unsigned int datalength;
if(addHeaders)
{
TCPsend_P(socket,strlen_P(statusLine),statusLine);
TCPsend_P(socket,sizeof(HTTP_HEADER_SERVER),HTTP_HEADER_SERVER);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_TYPE),HTTP_HEADER_CONTENT_TYPE);
TCPsend_P(socket,sizeof(HTTP_MIME_HTML),HTTP_MIME_HTML);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_LENGTH),HTTP_HEADER_CONTENT_LENGTH);
datalength=HTTP_HTML_TAG_LENGTH+strlen_P(htmlTitle)+strlen_P(htmlBody);
sprintf(tempBuf,"%d\0",datalength); //convert size to ascii
TCPsend(socket,strlen(tempBuf),tempBuf);
TCPsend_P(socket,sizeof(HTTP_CRLF_CRLF),HTTP_CRLF_CRLF);
}
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_1),HTTP_HTML_TAG_1);
TCPsend_P(socket,strlen_P(htmlTitle),htmlTitle);
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_2),HTTP_HTML_TAG_2);
TCPsend_P(socket,strlen_P(htmlBody),htmlBody);
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_3),HTTP_HTML_TAG_3);
TCPclose(socket);
}
/*sendHtml, sends a HTML-page. This function can be used in scripts to
generate and send HTML-pages*/
void sendHtml(char * title, char * body, SOCKET * socket, unsigned char addHeaders)
{
char tempBuf[12];
unsigned int datalength;
if(addHeaders)
{
/*add headers*/
TCPsend_P(socket,sizeof(HTTP_STATUSLINE_OK),HTTP_STATUSLINE_OK);
TCPsend_P(socket,sizeof(HTTP_HEADER_SERVER),HTTP_HEADER_SERVER);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_TYPE),HTTP_HEADER_CONTENT_TYPE);
TCPsend_P(socket,sizeof(HTTP_MIME_HTML),HTTP_MIME_HTML);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_LENGTH),HTTP_HEADER_CONTENT_LENGTH);
datalength=HTTP_HTML_TAG_LENGTH+strlen(title)+strlen(body);
sprintf(tempBuf,"%d\0",datalength); //convert size to ascii
TCPsend(socket,strlen(tempBuf),tempBuf);
TCPsend_P(socket,sizeof(HTTP_CRLF_CRLF),HTTP_CRLF_CRLF);
}
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_1),HTTP_HTML_TAG_1);
TCPsend(socket,strlen(title),title);
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_2),HTTP_HTML_TAG_2);
TCPsend(socket,strlen(body),body);
TCPsend_P(socket,sizeof(HTTP_HTML_TAG_3),HTTP_HTML_TAG_3);
TCPclose(socket);
}
/*sendFile sends the http headers, and adds the file to the
filelist, httpSend will then transmit the file*/
void sendFile(unsigned char addHeaders, FFILE * httpFile,SOCKET * socket)
{
char tempBuf[12];
char i;
if(addHeaders) //if version is above 0.9, add headers
{
for(i = 0; i < 3; i++)
{
tempBuf[i] = tolower(httpFile->file_ext[i]);
}
TCPsend_P(socket,sizeof(HTTP_STATUSLINE_OK),HTTP_STATUSLINE_OK);
TCPsend_P(socket,sizeof(HTTP_HEADER_SERVER),HTTP_HEADER_SERVER);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_TYPE),HTTP_HEADER_CONTENT_TYPE);
/*deduce MIME-type*/
if(!strncmp(tempBuf, "htm", 3)) TCPsend_P(socket,sizeof(HTTP_MIME_HTML),HTTP_MIME_HTML);
else if(!strncmp(tempBuf, "txt", 3)) TCPsend_P(socket,sizeof(HTTP_MIME_PLAIN),HTTP_MIME_PLAIN);
else if(!strncmp(tempBuf, "gif", 3)) TCPsend_P(socket,sizeof(HTTP_MIME_GIF),HTTP_MIME_GIF);
else if(!strncmp(tempBuf, "jpg", 3)) TCPsend_P(socket,sizeof(HTTP_MIME_JPEG),HTTP_MIME_JPEG);
else if(!strncmp(tempBuf, "png", 3)) TCPsend_P(socket,sizeof(HTTP_MIME_PNG),HTTP_MIME_PNG);
else TCPsend_P(socket,sizeof(HTTP_MIME_PNG),HTTP_MIME_PNG);
TCPsend_P(socket,sizeof(HTTP_HEADER_CONTENT_LENGTH),HTTP_HEADER_CONTENT_LENGTH);
longToAscii(httpFile->file_size,tempBuf);
TCPsend(socket,strlen(tempBuf),tempBuf);
TCPsend_P(socket,sizeof(HTTP_CRLF_CRLF),HTTP_CRLF_CRLF);
}
httpFiles[socket->socketNr].hisIP0=socket->hisIP0;
httpFiles[socket->socketNr].hisIP1=socket->hisIP1;
httpFiles[socket->socketNr].hisPort=socket->hisPort;
httpFiles[socket->socketNr].file=httpFile; //add file to filelist, file will be sent by sendFile()
}
/* method to deduce the MIME-type */
char flash * mimeType(char *fileext)
{
fileext[0] = tolower(fileext[0]);
fileext[1] = tolower(fileext[1]);
fileext[2] = tolower(fileext[2]);
if(!strncmp(fileext, "htm", 3)) return HTTP_MIME_HTML;
if(!strncmp(fileext, "txt", 3)) return HTTP_MIME_PLAIN;
if(!strncmp(fileext, "gif", 3)) return HTTP_MIME_GIF;
if(!strncmp(fileext, "jpg", 3)) return HTTP_MIME_JPEG;
if(!strncmp(fileext, "png", 3)) return HTTP_MIME_PNG;
return HTTP_MIME_PLAIN;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -