c++利用mongoose實現http服務


近來在項目中需要實現一個http service的功能。雖然可以通過封裝socket自己實現http的發送和解析。但考慮到目前網絡上還是有大量的http的解析源碼,自己再從頭實現一番稍顯麻煩。因此在網絡上搜刮了一番,看到很多實現http的代碼, 無一不體現了其輕量級的特點。然而,雖然輕量級,但從代碼的量上來說,為了一個http service的功能,引入這許多的代碼,也着實不是十分情願呢。后在同事的推薦下選擇了mongoose的代碼。

 

mongoose的代碼着實輕量,先看看它的特點:

1. 在整個的實現是使用C語言編寫

2. 整個代碼也只有一個mongoose.c和mongoose.h兩個文件, 從引入第三方的考慮上也着實不多。

3. 實現的功能還是非常多的,從使用的層面上來說功能還是比較全面。只不過不知道是否是為了第三方使用的方便還是怎么地,它的代碼只用了兩個源文件罷了。諸多的功能也大以宏的開始與結束來區分。

4. 示例非常齊全,所有的功能都有單獨的示例

 

然而,不管它實現多少功能,對於我來說只需要三個:

1. 有http的解析等

2. 文件少,使用方便,不需要因為使用一個簡單的功能引入大量代碼,而且引入的代碼着實沒有用到。

3. 有完整的示例

 

當我們拿到一個第三方庫或者第三方源碼的時候,第一件事情就是看看代碼的示例,並且自己動手謝謝測試的代碼,完成自己想要的功能。於是,我花了一點時間自己寫了一個測試的代碼,最后發現測試的時候並不通。雖然它的代碼示例很全面,然而對於我們來說,或許在它的代碼中,有些函數我們不需要,而有些函數卻不再這個示例中使用,因此需要自己測試。耗費了一些時間以后,我個人做了一下的簡單封裝,算是簡單的實現一個http service的功能,其中使用到了一點C++11的特性。可丟磚頭,也可交流。

 

[cpp]  view plain  copy
 
  1. // File:        basic_http.h  
  2. // Description: ---  
  3. // Notes:       ---  
  4. // Author:      Haust <wyy123_2008@qq.com>  
  5. // Revision:    2015-11-19 by Haust  
  6.   
  7. #pragma once  
  8.   
  9. #include "mongoose.h"  
  10. #include <map>  
  11. #include <string>  
  12. #include <functional>  
  13.   
  14. class BasicHttp {  
  15. public:  
  16.     using handler = std::function<void(std::string, std::string)>;  
  17.   
  18. public:  
  19.     virtual ~BasicHttp(){};  
  20.     
  21.     void Init(uint32_t port);  
  22.     bool Start();  
  23.     bool Close();  
  24.     bool RegisterHandler(std::string uri, handler f);  
  25.     void UnRegisterHandler(std::string uri);  
  26.     void Loop(int milli);  
  27.   
  28.     void SendReply(std::string uri, std::string reply);  
  29.     void SendError(std::string uri, int errcode, std::string reply);  
  30.   
  31. protected:  
  32.     using handler_map = std::map<std::string, handler>;  
  33.     using connection_map = std::multimap<std::string, mg_connection*>;  
  34.   
  35. private:  
  36.     static void EvHandler(struct mg_connection* nc, int ev, void* ev_data);  
  37.     static void HandleRequst(struct mg_connection* nc, int ev, void *ev_data);   
  38.   
  39. public:  
  40.     static handler_map _handlers;  
  41.     static connection_map _connections;  
  42.   
  43.     char _port[11];  
  44.     struct mg_mgr _mgr;  
  45. };  
[cpp]  view plain  copy
 
  1. // File:        basic_http.cpp  
  2. // Description: ---  
  3. // Notes:       ---  
  4. // Author:      Haust <wyy123_2008@qq.com>  
  5. // Revision:    2015-11-19 by Haust  
  6.   
  7. #include "basic_http.h"  
  8.   
  9. BasicHttp::handler_map BasicHttp::_handlers;  
  10. BasicHttp::connection_map BasicHttp::_connections;  
  11.   
  12. void BasicHttp::Init(uint32_t port){  
  13.     memset(_port, 0, sizeof(_port));  
  14.     snprintf(_port, sizeof(_port), "%u", port);  
  15. }  
  16.   
  17. bool BasicHttp::Start(){  
  18.     mg_mgr_init(&_mgr, NULL);  
  19.     auto nc = mg_bind(&_mgr, _port, EvHandler);  
  20.   
  21.     if(nullptr == nc)  
  22.         return false;  
  23.   
  24.     mg_set_protocol_http_websocket(nc);  
  25.     return true;  
  26. }  
  27.   
  28. bool BasicHttp::Close(){  
  29.     mg_mgr_free(&_mgr);  
  30.     return true;  
  31. }  
  32.   
  33. bool BasicHttp::RegisterHandler(std::string uri, handler f){  
  34.     auto it = _handlers.find(uri);  
  35.     if(_handlers.end() != it)  
  36.         return false;  
  37.   
  38.     return _handlers.emplace(uri, f).second;  
  39. }  
  40.   
  41. void BasicHttp::UnRegisterHandler(std::string uri){  
  42.     auto it = _handlers.find(uri);  
  43.     if(_handlers.end() != it)  
  44.         _handlers.erase(it);  
  45. }  
  46.   
  47. void BasicHttp::Loop(int milli){  
  48.     mg_mgr_poll(&_mgr, milli);  
  49. }  
  50.   
  51. void BasicHttp::EvHandler(struct mg_connection* nc, int ev, void* ev_data){  
  52.    switch(ev){  
  53.         case MG_EV_HTTP_REQUEST:  
  54.             HandleRequst(nc, ev, ev_data);  
  55.             break;  
  56.        default:  
  57.            break;  
  58.    }  
  59. }  
  60.   
  61. void BasicHttp::HandleRequst(struct mg_connection *nc, int ev, void* ev_data){  
  62.     http_message* hm = (http_message*)ev_data;  
  63.     std::string uri(hm->uri.p, hm->uri.len);  
  64.   
  65.     auto it = _handlers.find(uri);  
  66.     if(_handlers.end() == it)  
  67.         return;  
  68.   
  69.     _connections.emplace(uri, nc);  
  70.     it->second(std::string(hm->query_string.p, hm->query_string.len),  
  71.             std::string(hm->body.p, hm->body.len));  
  72. }  
  73.   
  74. void BasicHttp::SendReply(std::string uri, std::string reply){  
  75.     auto range = _connections.equal_range(uri);  
  76.     if(range.first == range.second)  
  77.         return;  
  78.   
  79.     auto it = range.first;  
  80.     mg_printf(it->second, "HTTP/1.1 200 OK\r\niConnection: close\r\nContent-Type: text/html\r\nContent-Length: %u\r\n\r\n%s\r\n",   
  81.             (uint32_t)reply.length(), reply.c_str());  
  82.   
  83.     it->second->flags |= MG_F_SEND_AND_CLOSE;  
  84.     _connections.erase(it);  
  85. }  
  86.   
  87. void BasicHttp::SendError(std::string uri, int errcode, std::string reply){  
  88.     auto range = _connections.equal_range(uri);  
  89.     if(range.first == range.second)  
  90.         return;  
  91.   
  92.     auto it = range.first;  
  93.     mg_printf(it->second, "HTTP/1.1 %d %s\r\n", errcode, reply.c_str());  
  94.   
  95.     it->second->flags |= MG_F_SEND_AND_CLOSE;  
  96.     _connections.erase(it);  
  97. }  
  98.   
  99. #include "mongoose.c" 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM