Poco 是基于C++ 的一个网络库,主要有以下特点:
- 使用高效的,现代的标准ANSI/ISO C++,并基于STL
- 高可移植性,支持多种平台,包括Windows,Linux,OS X 等
- 开源,并使用Boost Software License发布,完全免费
本文介绍一下使用Poco 在本地建立Http Server,用于响应Client的Http 请求。
HTTPServer:
根据官网的介绍,HTTPServer 是TCPServer 的子类,用于实现一个多种特性的多线程HTTP Server,使用的时候必须提供一个HTTPRequestHandlerFactory ,并且 ServerSocket 必须提升到监听状态,为了配置server端,可以传递一个 HTTPServerParams 给构造函数。
Server 支持:
- HTTP/1.0 and HTTP/1.1
- 自动处理持久的连接
- 使用分块传输编码自动对请求的消息体解码,并对响应的消息体编码
HTTPRequestHandlerFactory :
是 HTTPRequestHandler 对象的一个工厂,子类必须要重写 createRequestHandler() 方法。
下面我们看一下createRequestHandler() 方法 的定义:
virtual HTTPRequestHandler * createRequestHandler(
const HTTPServerRequest & request
) = 0;
方法中需要提供一个HTTPServerRequest 对象的引用作为参数。
HTTPRequestHandler :
HTTPRequestHandler 是由 HTTPServer 创建出来的抽象基类,派生类必须要重写 handleRequest() 方法,此外,还需要提供一个 HTTPRequestHandlerFactory 。
并且,handleRequest() 方法必须完整的处理HTTP 请求,一旦handleRequest() 方法执行完,请求处理的对象立即销毁。
下面看一下handleRequest() 方法怎么定义的:
virtual void handleRequest(
HTTPServerRequest & request,
HTTPServerResponse & response
) = 0;
需要两个参数,分别是HTTPServerRequest 和 HTTPServerRespose 的对象。
ServerApplicaiton:
最后,为了实现我们的HTTPServer 程序,我们需要用到一个Application类的子类ServerApplication 来实现。
ServerApplication 允许程序运行一个Windows service 或 一个Unix daemon(守护进程),不需要额外的代码。
使用ServerApplication 需要遵循一些规则:
- 子系统需要在构造函数中注册
- 所有特殊的初始化必须在方法 initialize() 中完成
- main() 函数的末尾,需要调用waitForTerminationRequest() 方法
- 新线程只能在initialize() 和 main() 以及这二者调用的函数中创建,而不能在application类的构造函数和实例变量的构造函数中创建。原因是fork() 函数将被调用用来创建守护进程,而在调用 fork() 之前创建线程将不能接管守护进程。
- main(argc, argv) 函数按照以下格式使用:
int main(int argc, char** argv)
{
MyServerApplication app;
return app.run(argc, argv);
}
好了,使用的方法就介绍这么多,更详细的情查阅官网说明: 点击打开链接
下面看代码,首先我们要建立一个HTTPRequestHandler,简单打印一个“Hello World”。
class MyRequestHandler : public HTTPRequestHandler
{
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
ostream & out = resp.send();
out << "<h1>Hello World!</h1>";
}
};
根据文中介绍,我们还需要提供一个HTTPRequestHandlerFactory,继续看代码:
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &)
{
return new MyRequestHandler;
}
};
确实,根据官网介绍的文档,在类中重写createRequestHandler() 方法。
现在,HTTPRequestHandlerFactory 和 HTTPRequestHandler都有了,我们需要在ServerApplication类中实现我们的HTTPServer。
class MyServerApp :public ServerApplication
{
protected:
int main(const vector<string> &)
{
HTTPServer s(new MyRequestHandlerFactory, ServerSocket(8080), new HTTPServerParams);
s.start();
cout << endl << "Server started" << endl;
waitForTerminationRequest(); // wait for CTRL-C or kill
cout << endl << "Shutting down..." << endl;
s.stop();
return Application::EXIT_OK;
}
};
对于HTTPServer 的构造函数需要三个参数,下面是构造函数定义说明,HTTPServer 有3个构造函数,我们这边用其中一个。
使用提供的ServerSocket 创建 HTTPRequestHandlerFactory,server 接管HTTPRequestHandlerFactory,并且不需要时就删除它。
server同时接管 HTTPServerParams 对象,同时,新的线程从默认线程池中创建(注意,这里新的线程在main()函数中创建)。
最后就是定义我们程序的main() 函数了,参照官网给出的格式:
int main(int argc, char **argv)
{
MyServerApp app;
return app.run(argc, argv);
}
下面附上程序完整代码:
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HttpServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Util/ServerApplication.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace Poco::Net;
using namespace Poco::Util;
class MyRequestHandler : public HTTPRequestHandler
{
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
ostream & out = resp.send();
out << "<h1>Hello World!</h1>";
}
};
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &)
{
return new MyRequestHandler;
}
};
class MyServerApp :public ServerApplication
{
protected:
int main(const vector<string> &)
{
HTTPServer s(new MyRequestHandlerFactory, ServerSocket(8080), new HTTPServerParams);
s.start();
cout << endl << "Server started" << endl;
waitForTerminationRequest(); // wait for CTRL-C or kill
cout << endl << "Shutting down..." << endl;
s.stop();
return Application::EXIT_OK;
}
};
int main(int argc, char **argv)
{
MyServerApp app;
return app.run(argc, argv);
}