1. 程式人生 > >使用poco庫搭建簡單http伺服器實現hello world

使用poco庫搭建簡單http伺服器實現hello world

原始碼例子如下:

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Util/ServerApplication.h"

#include <iostream>

using Poco::Net::ServerSocket;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPServerParams;
using Poco::Util::ServerApplication;
using Poco::Util::Application;

/*
poco提供了HTTPServer類建立一個http伺服器
官網介紹:TCPServer的子類,實現了全功能的多執行緒HTTP伺服器。
		 必須提供HTTPRequestHandlerFactory。 ServerSocket必須繫結並處於偵聽狀態。
		 要配置伺服器的各個方面,可以將HTTPServerParams物件傳遞給建構函式

HTTPServer有3個建構函式,用其中一個測試就行
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams)

*/


/*
HTTPRequestHandler官網介紹:
Derived classes must override the handleRequest() method. Furthermore, a HTTPRequestHandlerFactory must be provided

翻譯:派生類必須覆蓋handleRequest()方法。此外,必須提供HTTPRequestHandlerFactory

The handleRequest() method must perform the complete handling of the HTTP request connection.As soon as the handleRequest() method returns, the request handler object is destroyed.
A new HTTPRequestHandler object will be created for each new HTTP request that is received by the HTTPServer.

翻譯:handleRequest()方法必須執行HTTP請求連線的完整處理。 一旦handleRequest()方法返回,請求處理程式物件就被銷燬。
	  將為HTTPServer接收的每個新HTTP請求建立一個新的HTTPRequestHandler物件。

handleRequest()函式功能:
	  virtual void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) = 0
	  Must be overridden by subclasses:必須被子類覆蓋
	  Handles the given request:處理給定的請求
*/

class RequestHandLer :public HTTPRequestHandler
{
public:

	void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
	{
		response.setChunkedTransferEncoding(true);//設定分組傳輸編碼
		response.setContentType("text/html");//設定內容型別

		std::ostream& ostr = response.send();//這個函式返回一個響應請求的輸出流
		ostr << "<h1>Hello World!</h1>";
	}

};



class RequestHandlerFactory :public HTTPRequestHandlerFactory
{
public:
	/*
	為給定的HTTP請求建立一個新的請求處理程式,引數是HTTPServerRequest,上面要定義一個HTTPServerRequest物件傳過來
	該方法應該檢查給定的HTTPServerRequest物件(例如方法)
	和URI),並建立一個適當的HTTPRequestHandler物件來處理
	請求
	*/
	HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
	{
		return new RequestHandLer();
	}
};


/*
建立一個應用程式ServerApplication

HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams)

*/

class Myapp :public ServerApplication
{
protected:
	int main(const std::vector<std::string>& args)
	{

		HTTPServer HSer(new RequestHandlerFactory, ServerSocket(8080), new HTTPServerParams);// ServerSocket是匿名物件
		HSer.start();//啟動http伺服器
		waitForTerminationRequest();//
		std::cout << "關閉" << std::endl;
		HSer.stop();
		return Application::EXIT_OK;

	}


};
int main(int argc, char** argv)
{
	Myapp app;
	return app.run(argc, argv);
}

執行結果如下:開啟瀏覽器輸入:localhost:8080 請求,得到hello world