簡介
- 理解http報文格式
- socket+epoll+多線程框架
- 請求和響應類封裝
- 正則表達式解析頭部
- 請求文件讀取
- php-cgi解析php文件
理解http報文格式
socket+epoll+多線程框架
請求和響應類封裝
為了方便操作以及功能模塊的拆分,這里我們將請求內容和響應內容進行封裝。這里我們只列出頭文件,封裝類中我們僅僅做了關系字段的提取。
Request.h
請求報文進行封裝,我們將請求的內容放到body中,然后調用parse進行解析,解析后的數據分別存放到定義的成員變量中。
#ifndef REQUEST_H
#define REQUEST_H
#include <iostream>
using namespace std;
#include <string.h>
#include <map>
class Request{
private:
string body;
string method;
string path;
string protocol;
string content;
string postfix;
map<string,string> headers;
map<string,string> params;
public:
Request();
~Request();
void setBody(string body);
bool parse();
string getMethod();
string getPath();
string getProtocol();
string getContent();
string getPostfix();
map<string,string> getParam();
map<string,string> getHeaders();
};
#endif
Reponse.h
對響應報文進行封裝,重要是響應狀態嗎、協議、Content-Length等。通過getData對頭部進行拼裝。
#ifndef RESPONSE_H
#define RESPONSE_H
#include <iostream>
using namespace std;
#include <map>
#include <string.h>
class Response{
private:
int code;
string msg;
string protocol;
map<string,string> headers;
public:
Response();
~Response();
void setCode(int code);
void setMsg(const char* msg);
void setProtocol(const char protocol);
void addHead(string,string);
void setContentLength(int length);
string getData();
};
#endif
正則表達式解析頭部
由于我的gcc版本為4.4.7,無法使用c++11 標準正則表達式。這里就是用了boost的Regex庫。使用方法和代碼一致。關于正則表達式的知識大家可在正則表達式基礎上學習。
前面我們學習到http請求數據格式為 起始行+首部+請求體。所以我們直接匹配第一行數據。然后將方法、路徑匹配出來。
boost::regex reg("^(\\w+) /(\\w*([.]\\w+)?) HTTP/1");
boost::smatch sm;
regex_search(body,sm,reg);
if(sm.size()==0) {
return false;
}else{
cout<<"Regex =>"<<sm[1]<<"|"<<sm[2]<<"|"<<sm[3]<<endl;
method=sm[1];
path.clear();
path.append("/").append(sm[2]);
postfix=sm[3];
}
正則表達式括號的內容代表我們要匹配的字符串,sm的第一個下標數據是匹配的完整的字符串,所以我們從sm[1]開始取值。
請求文件讀取
前面讀取到請求路徑后,我們就可以去讀取對應文件。
//讀寫文件
int fileSize=0;
FILE *file=fopen(path.c_str(),"r");
if(file!=NULL) {
fseek(file,0L,SEEK_END);
//獲取文件大小
fileSize=ftell(file);
fseek(file,0L,SEEK_SET);
}else{
cout<<"file read error"<<endl;
}
接著就可以將文件數據發送出去
if(file!=NULL) {
char dataBuf[1024*100]={0};
while(true) {
int len= fread(dataBuf,1,sizeof(dataBuf),file);
if(len<=0) {
break;
}
cout<<"===>send Data "<<len<<endl;
client->sendData(dataBuf,len);
}
fclose(file);
}
注意在發送文件數據前,我們先要想頭部發送出去
resp.setContentLength(fileSize);
// if(sm[3].str().empty()) {
resp.addHead("Content-Type","text/html");
// }else{
// resp.addHead("Content-Type","image/jpeg");
// }
resp.addHead("Server","eric http");
string headsStr=resp.getData();
cout<<"<<< "<<headsStr.c_str();
client->sendData(headsStr.c_str(),headsStr.size());
一個簡單的http容器我們搭建完成了。接下來就是如何支持php。
php-cgi解析php文件
首先先要安裝php。可以參考Linux下php安裝
我們可以先寫一個簡單的php代碼測試一下。
<?php
phpinfo();
?>
然后在命令行使用php-cgi
php-cgi index.php > index.php.html
成功生成了index.php.html。接下來我們回到代碼:
if(req.getPostfix()==".php") {
string cmd="php-cgi ";
string resFilePath;
resFilePath.append(path).append(".html");
cmd.append(path).append(" > ").append(resFilePath);
cout<<cmd<<endl;
system(cmd.c_str());
path=resFilePath;
}
首先我們判斷請求路徑的后綴是否為.php。如果是則執行php-cgi的命令并生成文件。然后得到文件路徑,接下來就和正常讀取文件并發送數據的流程一致了。