資源簡(jiǎn)介
在linux下面,自動(dòng)動(dòng)手用socket實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的php web server。經(jīng)過(guò)測(cè)試,可以使用。

代碼片段和文件信息
/*
?*?=====================================================================================
?*
?*???????Filename:??phpServer.cpp
?*????????Version:??0.1
?*????????Created:??2011年05月16日?07時(shí)40分56秒
?*???????Compiler:??g++
?*
?*?????????Author:??zhxm501205289@qq.com?
?*????????Company:??xd-1301
?*
?*?=====================================================================================
?*/
#include?
#include?
#include?
#include?
#include
#include
#include
#include
#include
#include
#include
#include
#include?
#include?
#include?
using?namespace?std;
//緩沖區(qū)大小
#define?MAX_BUF_SIZE?1024*1024
//分段發(fā)送函數(shù)
int??mySend(void*?bufint?sizeint?sockfd)
{
????int?sended=0;
????while(1)
????{
????????int?flag?=?send(sockfd?(char*)buf+sendedsize-sended0);
????????if(flag?==-1)//發(fā)送失敗
????????{
????????????//重新試一試
?????????????flag?=?send(sockfd?(char*)buf+sendedsize-sended0);
?????????????if(flag?==?-1)//如果還是失敗,則放棄發(fā)送
?????????????????break;
????????}
????????sended?+=flag;
????????if(sended>=size)
????????{
????????????break;
????????}
????}
????return?sended;
}
//返回?PHP-cgi消息的頭和內(nèi)容的分界點(diǎn)
//php-cgi處理php的返回消息格式如下
//head?\r\n
//\r\n
//content?\r\n
char*?dealPHPMes(char*?dataint?len)
{
????char*?p?=?data;
????char*?end?=?p+len-3;
????for(;p ????{
????????if(*p==‘\r‘?&&?*(p+1)?==‘\n‘?&&?*(p+2)==‘\r‘?&&?*(p+3)==‘\n‘)
????????????return?p+4;
????}
????return?data;
}
//從http請(qǐng)求頭中獲取請(qǐng)求的文件
//http典型請(qǐng)求頭如下:
//GET?/a.php?a=10&b=10?HTTP/1.1?\r\n
//
//函數(shù)返回./a.php
string?getReq(char*?bufint?len)
{
????string?temp?=?“.“;
????char?*p=buf;
????char*?end?=?buf+len;
????for(;p ????????if(*p==‘/‘)?break;
????for(;p ????{
????????//空格?或者\(yùn)r\n是典型的結(jié)束符號(hào)
????????if(*p==‘?‘?||?*p?==?‘?‘)?break;
????????temp?+=*p;
????}
????return?temp;
}
//從http請(qǐng)求頭中獲取請(qǐng)求的文件的類(lèi)型
//http典型請(qǐng)求頭如下:
//GET?/a.php?a=10&b=10?HTTP/1.1?\r\n
//
//函數(shù)返回“php“
string?getType(char*?bufint?len)
{
????string?temp?=?““;
????char?*p=buf;
????char*?end?=?buf+len;
????for(;p ????????if(*p==‘.‘)?break;
????for(p++;p ????{
????????if(*p==‘?‘?||?*p==‘?‘)?break;
????????temp?+=*p;
????}
????return?temp;
}
//從http請(qǐng)求頭中獲取請(qǐng)求的參數(shù)
//http典型請(qǐng)求頭如下:
//GET?/a.php?a=10&b=10?HTTP/1.1?\r\n
//
//函數(shù)返回“a=10?b=10“
string?getArg(char*?bufint?len)
{
????string?temp?=?““;
????string?method;
????char?tempBuf[10];
????char?*p=buf;
????char*?end?=?buf+len;
????sscanf(buf“%s“tempBuf);
????method=tempBuf;
????if(method==“GET“)//只考慮GET?POST?兩種方法?GET方法參數(shù)緊跟請(qǐng)求文件后面?如a.php?a=10&b=10
????{
????????for(;p ?????????????if(*p==‘?‘)?break;//‘?‘是參數(shù)列表的開(kāi)始符號(hào)
????????for(p++;p ????????{???
????????????if(*p==‘\r‘?&&?*(p+1)==‘\n‘)?break;
????????????if(*p==‘?‘)break;
????????????if(*p==‘&‘)//a=10&b=10?轉(zhuǎn)成a=10?b=10
????????????????temp?+=‘?‘;
????????????else?
????????????????temp?+=?*p;????????
????????}
????????return?temp;
????}
?
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件??????37002??2011-05-05?19:24??0.png
?????文件????????386??2011-05-17?13:02??a.php
?????文件???????9268??2011-05-17?20:04??phpServer.cpp
-----------?---------??----------?-----??----
????????????????46656????????????????????3
評(píng)論
共有 條評(píng)論