如何在c ++中解析http请求 [英] how to parse http request in c++
问题描述
我想写一个小的c ++ webserver处理GET,POST,HEAD请求。我的问题是我不知道如何解析标头,消息体等。它正在侦听套接字,我甚至可以写出的东西出来到浏览器很好,但我很好奇我应该如何在c ++中这样做。
Afaik标准的GET / POST请求应如下所示:
GET / index HTTP / 1.1
Host:192.168.0.199:80
Connection:keep-alive
接受:* / *
User-Agent:Mozilla / 5.0 NT 6.1; WOW64)AppleWebKit / 537.22(KHTML,like Gecko)Chrome / 25.0.1364.97 Safari / 537.22
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en; q = 0.8
Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
这是消息正文
所有行以'\r\\\
'结尾。
我只是拆分请求在'\\\
'和修剪它们(如果是如此)?
也是如何处理post数据中的文件?
我想实现的主要是获取一个包含headers key => value对的向量,请求方法,post数据(如在PHP中,如果它存在)和查询字符串(例如/ index)作为字符串或向量由'/'分割。
感谢!
在自己做一切之前,我向您介绍 Poco ,但您应阅读 HTTP规范。
如下所示:
class MyHTTPRequestHandler:public HTTPRequestHandler
{
public:
virtual void handleRequest(HTTPServerRequest& request,
HTTPServerResponse& response){
// in res object
}
};
类MyRequestHandlerFactory:public HTTPRequestHandlerFactory
{
MyHTTPRequestHandler handler;
public:
MyRequestHandlerFactory(){}
HTTPRequestHandler * createRequestHandler(const HTTPServerRequest& request)
{
const string method = request.getMethod ;
if(method ==get|| method ==post)
return& handler;
return 0;
}
};
int main()
{
HTTPServerParams params;
params.setMaxQueued(100);
params.setMaxThreads(16);
ServerSocket svs(80);
MyRequestHandlerFactory factory;
HTTPServer srv(& factory,svs,& params);
srv.start();
while(true)
Thread :: sleep(1000);
}
I'm trying to write a small c++ webserver which handles GET, POST, HEAD requests. My problem is I don't know how to parse the headers, message body, etc. It's listening on the socket, I can even write stuff out to the browser just fine, but I'm curious how should I do this in c++.
Afaik a standard GET/POST request should look something like this:
GET /index HTTP/1.1
Host: 192.168.0.199:80
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
this is the message body
All lines ended with '\r\n'.
Should I just split the request at '\n' and trim them (and if so how)? Also how to handle files in post data?
Main thing I want to achieve is to get a vector containing headers key=>value pairs, a string with request method, the post data (like in PHP, if it's present), and the query string (/index for example) as string or vector splitted by '/'.
Thanks!
Before doing everything yourself, I introduce you Poco, However you should read HTTP specification too.
Something like below:
class MyHTTPRequestHandler : public HTTPRequestHandler
{
public:
virtual void handleRequest(HTTPServerRequest & request,
HTTPServerResponse & response) {
// Write your HTML response in res object
}
};
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
MyHTTPRequestHandler handler;
public:
MyRequestHandlerFactory(){}
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
const string method = request.getMethod();
if (method == "get" || method == "post")
return &handler;
return 0;
}
};
int main()
{
HTTPServerParams params;
params.setMaxQueued(100);
params.setMaxThreads(16);
ServerSocket svs(80);
MyRequestHandlerFactory factory;
HTTPServer srv(&factory, svs, ¶ms);
srv.start();
while (true)
Thread::sleep(1000);
}
这篇关于如何在c ++中解析http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!