如何在c ++中解析http请求 [英] how to parse http request in c++

查看:183
本文介绍了如何在c ++中解析http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个小的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, &params);
    srv.start();
    while (true)
        Thread::sleep(1000);
}

这篇关于如何在c ++中解析http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆