Poco教程(一):使用Poco库建立Http Server

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);
}

程序到这里就结束了,编译通过后在后台执行该程序(Linux 平台记得编译时链接poco 库),然后在浏览器中输入“localhost:8080”,就能看到打印出来的“Hello World”。(这里指定端口8080,也可改成别的,只要端口没有被别的程序占用)

下面附上程序完整代码:
#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);
}

  • 13
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值