文章紧跟web服务器处理HTTP框架之后
1. HTTP报文报头与空行的读取
首先先按行读取HTTP报头。报头分为两部分,请求行与首部字段。
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include"Util.h"
#include<string>
#include<vector>
#include"Log.h"
class HttpResponse{
public:
std::string StatusLine_HTTP;
std::vector<std::string>ResponHeads;
std::string ResponBlank;
std::string ResponBody;
};
class HttpRequest{
public:
std::string RequestLine_HTTP;
std::vector<std::string>RequestHeads;
std::string RequestBlank;
std::string RequestBody;
};
class EndPoint{
private:
int sock;
HttpRequest http_request;
HttpResponse http_response;
private:
void GetHttpRequestLine(){
Util::ReadLine(sock,http_request.RequestLine_HTTP);
http_request.RequestLine_HTTP.pop_back();
ERRORLOG(INFO,http_request.RequestLine_HTTP);
}
void GetHttpRequstHeads(){
std::string line;
while(true){
Util::ReadLine(sock,line);
if(line=="\n"){
ERRORLOG(INFO,line);
http_request.RequestBlank=line;
break;
}
line.pop_back();
http_request.RequestHeads.push_back(line);
ERRORLOG(INFO,line);
line.clear();
}
}
public:
EndPoint(int _sock):sock(_sock){}
void RecvQuest_HTTP(){
GetHttpRequestLine();
GetHttpRequstHeads();
}
void AnalyQuest_HTTP(){
}
void MakeRespon_HTTP(){
}
void SendRespon_HTTP(){
}
~EndPoint(){}
};
运行结果如下图:
读取报文正文时需要对报头字段进行分析。
2. 解析HTTP请求行
首先解析HTTP请求行,需要将一个字符串分成三个字符串保存起来
这里使用stringstream函数格式化字符串,这个函数默认按照空格打散字符串 测试代码
#include<iostream>
#include<sstream>
int main(){
std::string msg="Get /reference/sstream HTTP/1.1";
std::string method;
std::string url;
std::string version;
std::stringstream ss(msg);
ss>>method>>url>>version;
std::cout<<method<<"+"<<url<<"+"<<version<<std::endl;
return 0;
}
class HttpRequest{
public:
std::string RequestLine_HTTP;
std::vector<std::string>RequestHeads;
std::string RequestBlank;
std::string RequestBody;
std::string Method;
std::string URI;
std::string Version;
};
class EndPoint{
private:
int sock;
HttpRequest http_request;
HttpResponse http_response;
private:
void AnalyQuestLine(){
std::stringstream Str(http_request.RequestLine_HTTP);
Str>>http_request.Method>>http_request.URI>>http_request.Version;
}
public:
EndPoint(int _sock):sock(_sock){}
void AnalyQuest_HTTP(){
AnalyQuestLine();
}
~EndPoint(){}
};
3. 解析HTTP首部字段
首部字段解析成KeyValue模型,通过查找:(:+空格两个字符分开的)来把字符串分成两部分
class HttpRequest{
public:
std::string RequestLine_HTTP;
std::vector<std::string>RequestHeads;
std::string RequestBlank;
std::string RequestBody;
std::string Method;
std::string URI;
std::string Version;
std::unordered_map<std::string,std::string>Head_KVS;
};
在工具类中新添加切分函数
class Util{
public:
static bool CutString(std::string&src,std::string& key,std::string&value,std::string gist){
size_t pos=src.find(gist);
if(pos!=std::string::npos){
key=src.substr(0,pos);
value=src.substr(pos+gist.size());
return true;
}
return false;
}
};
class EndPoint{
private:
int sock;
HttpRequest http_request;
HttpResponse http_response;
private:
void AnalyQuestLine(){
std::stringstream Str(http_request.RequestLine_HTTP);
Str>>http_request.Method>>http_request.URI>>http_request.Version;
}
void AnalyuestHeadS(){
std::string key;
std::string value;
for(auto&line:http_request.RequestHeads){
if(Util::CutString(line,key,value,": ")){
http_request.Head_KVS.insert(std::make_pair(key,value));
}
else{
ERRORLOG(FATA,"AnalyuestHeadS error");
}
}
}
public:
EndPoint(int _sock):sock(_sock){}
void AnalyQuest_HTTP(){
AnalyQuestLine();
AnalyuestHeadS();
}
~EndPoint(){}
};
4.根据解析后的HTTP首部(Content-Length)读取HTTP空行
注意:此web服务器先考虑GET 和POST 方法 如果HTTP请求方法是GET方法,没有正文。 POST方法才有正文。
如果需要读取正文,正文长度在Content-Length中。
#pragma once
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include"Util.h"
#include<string>
#include<vector>
#include"Log.h"
#include<sstream>
#include<unordered_map>
class HttpRequest{
public:
std::string RequestLine_HTTP;
std::vector<std::string>RequestHeads;
std::string RequestBlank;
std::string RequestBody;
std::string Method;
std::string URI;
std::string Version;
std::unordered_map<std::string,std::string>Head_KVS;
int Content_Lenth=0;
};
class EndPoint{
private:
int sock;
HttpRequest http_request;
HttpResponse http_response;
private:
bool HaveHttpBody(){
std::string& Method=http_request.Method;
if(Method=="POST"){
std::unordered_map<std::string,std::string>::iterator iter=http_request.Head_KVS.find("Content-Lenth");
if(iter!=http_request.Head_KVS.end()){
http_request.Content_Lenth=atoi(iter->second.c_str());
return true;
}
}
return false;
}
void GetHttpBody(){
if(HaveHttpBody()){
int Content_Lenth=http_request.Content_Lenth;
char ch=0;
while(Content_Lenth>0){
ssize_t size=recv(sock,&ch,1,0);
if(size>0){
http_request.RequestBody.push_back(ch);
Content_Lenth--;
}
else{
break;
}
}
}
}
public:
EndPoint(int _sock):sock(_sock){}
void RecvQuest_HTTP(){
GetHttpRequestLine();
GetHttpRequstHeads();
}
void AnalyQuest_HTTP(){
AnalyQuestLine();
AnalyuestHeadS();
GetHttpBody();
}
~EndPoint(){}
};
HTTP请求与解析完整代码
#pragma once
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include"Util.h"
#include<string>
#include<vector>
#include"Log.h"
#include<sstream>
#include<unordered_map>
class HttpResponse{
public:
std::string StatusLine_HTTP;
std::vector<std::string>ResponHeads;
std::string ResponBlank;
std::string ResponBody;
};
class HttpRequest{
public:
std::string RequestLine_HTTP;
std::vector<std::string>RequestHeads;
std::string RequestBlank;
std::string RequestBody;
std::string Method;
std::string URI;
std::string Version;
std::unordered_map<std::string,std::string>Head_KVS;
int Content_Lenth=0;
};
class EndPoint{
private:
int sock;
HttpRequest http_request;
HttpResponse http_response;
private:
void GetHttpRequestLine(){
Util::ReadLine(sock,http_request.RequestLine_HTTP);
http_request.RequestLine_HTTP.pop_back();
ERRORLOG(INFO,http_request.RequestLine_HTTP);
}
void GetHttpRequstHeads(){
std::string line;
while(true){
Util::ReadLine(sock,line);
if(line=="\n"){
ERRORLOG(INFO,line);
http_request.RequestBlank=line;
break;
}
line.pop_back();
http_request.RequestHeads.push_back(line);
ERRORLOG(INFO,line);
line.clear();
}
}
void AnalyQuestLine(){
std::stringstream Str(http_request.RequestLine_HTTP);
Str>>http_request.Method>>http_request.URI>>http_request.Version;
}
void AnalyuestHeadS(){
std::string key;
std::string value;
for(auto&line:http_request.RequestHeads){
if(Util::CutString(line,key,value,": ")){
http_request.Head_KVS.insert(std::make_pair(key,value));
}
else{
ERRORLOG(FATA,"AnalyuestHeadS error");
}
}
}
bool HaveHttpBody(){
std::string& Method=http_request.Method;
if(Method=="POST"){
std::unordered_map<std::string,std::string>::iterator iter=http_request.Head_KVS.find("Content-Lenth");
if(iter!=http_request.Head_KVS.end()){
http_request.Content_Lenth=atoi(iter->second.c_str());
return true;
}
}
return false;
}
void GetHttpBody(){
if(HaveHttpBody()){
int Content_Lenth=http_request.Content_Lenth;
char ch=0;
while(Content_Lenth>0){
ssize_t size=recv(sock,&ch,1,0);
if(size>0){
http_request.RequestBody.push_back(ch);
Content_Lenth--;
}
else{
break;
}
}
}
}
public:
EndPoint(int _sock):sock(_sock){}
void RecvQuest_HTTP(){
GetHttpRequestLine();
GetHttpRequstHeads();
}
void AnalyQuest_HTTP(){
AnalyQuestLine();
AnalyuestHeadS();
GetHttpBody();
}
void MakeRespon_HTTP(){
}
void SendRespon_HTTP(){
}
~EndPoint(){}
};
class Entry{
public:
static void*SolveQuest(void*_sock){
ERRORLOG(INFO,"Processing Requests...");
int sock=*(int*)_sock;
delete(int*)_sock;
EndPoint* endpoint=new EndPoint(sock);
endpoint->RecvQuest_HTTP();
endpoint->AnalyQuest_HTTP();
endpoint->MakeRespon_HTTP();
endpoint->SendRespon_HTTP();
delete endpoint;
ERRORLOG(INFO,"Processing Request End!");
return nullptr;
}
};
Github项目代码位置 Gitee项目代码位置
|