• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            SPWebServer:一個基于 SPServer 的 web 服務器框架

            from:http://iunknown.javaeye.com/blog/78561

            關鍵字: C++   libevent spserver http embedded web 服務器   

            看到這個題目,估計很多人會問:為什么要再實現一個 web 服務器?

            這里有幾個原因:

            1.這是一個 web 服務器框架,不是一個完整的 web 服務器。也就是說 SPWebServer 提供的是一套 API 和類庫,可以方便地集成到現有的應用程序中。可以稱 SPWebServer 為 embedded web server

            2.有些時候,我們需要的不是一個功能強大完整的 web 服務器(例如 apache ),我們只是需要一個能提供最基本的 http 功能的服務器框架。比如要用 C/C++ 實現 XML-RPC,JSON-RPC 服務器端,或者為 jabberd 實現一個 http bind 的時候。這些場景下,也可以使用 apache,但是使用 embedded web server 可以簡化配置。

            3.在如下的場合中,apache 顯得不合適,而 embedded web server 卻正好合適:在一個現有的應用程序中,用 http 接口來提供一些簡單的控制和統(tǒng)計功能。

            基于以上原因,也為了
            1.豐富 SPServer 項目(請參考: SPServer : 一個基于 Half-Sync/Half-Async 模式的高并發(fā) server 框架)的功能;
            2.正好作為 SPServer 框架的一個比較好的示范例子;
            因此基于 SPServer 框架實現了一個 web 服務器框架。

            源代碼下載:
            http://spserver.googlecode.com/files/spserver-0.5.src.tar.gz
            http://code.google.com/p/spserver/downloads/list

            下面來看一個使用 SPWebServer 的簡單例子。


            class SP_HttpEchoHandler : public SP_HttpHandler {   
            public:   
                SP_HttpEchoHandler()
            {}   
                
            virtual ~SP_HttpEchoHandler(){}   
              
                
            virtual void handle( SP_HttpRequest * request, SP_HttpResponse * response ) {   
                    response
            ->setStatusCode( 200 );   
                    response
            ->appendContent( "<html><head>"   
                        
            "<title>Welcome to simple http</title>"   
                        
            "</head><body>" );   
              
                    
            char buffer[ 512 ] = 0 };   
                    snprintf( buffer, 
            sizeof( buffer ),   
                        
            "<p>The requested URI is : %s.</p>", request->getURI() );   
                    response
            ->appendContent( buffer );   
              
                    snprintf( buffer, 
            sizeof( buffer ),   
                        
            "<p>Client IP is : %s.</p>", request->getClientIP() );   
                    response
            ->appendContent( buffer );   
              
                    
            forint i = 0; i < request->getParamCount(); i++ ) {   
                        snprintf( buffer, 
            sizeof( buffer ),   
                            
            "<p>Param - %s = %s<p>", request->getParamName( i ),   
                            request
            ->getParamValue( i ) );   
                        response
            ->appendContent( buffer );   
                    }
                      
              
                    
            forint i = 0; i < request->getHeaderCount(); i++ ) {   
                        snprintf( buffer, 
            sizeof( buffer ),   
                            
            "<p>Header - %s: %s<p>", request->getHeaderName( i ),   
                            request
            ->getHeaderValue( i ) );   
                        response
            ->appendContent( buffer );   
                    }
                      
              
                    
            if( NULL != request->getContent() ) {   
                        response
            ->appendContent( "<p>" );   
                        response
            ->appendContent( request->getContent(),   
                            request
            ->getContentLength() );   
                        response
            ->appendContent( "</p>" );   
                    }
                      
              
                    response
            ->appendContent( "</body></html>\n" );   
                }
                  
            }
            ;   
              
            class SP_HttpEchoHandlerFactory : public SP_HttpHandlerFactory {   
            public:   
                SP_HttpEchoHandlerFactory()
            {}   
                
            virtual ~SP_HttpEchoHandlerFactory(){}   
              
                
            virtual SP_HttpHandler * create() const {   
                    
            return new SP_HttpEchoHandler();   
                }
               
            }
            ;   
              
            //---------------------------------------------------------   
              
            int main( int argc, char * argv[] )   
            {   
                
            int port = 8080;   
              
                SP_Server server( 
            "", port,   
                    
            new SP_HttpHandlerAdapterFactory( new SP_HttpEchoHandlerFactory() ) );   
              
                server.runForever();   
              
                
            return 0;   
            }
               

            上面的代碼演示的是一個 http echo server ,即把 client 發(fā)過來的請求信息顯示在頁面上。

            在最簡單的情況下,使用 SPWebServer 需要實現兩個類:SP_HttpHandler 的子類 和 SP_HttpHandlerFactory 的子類。
            SP_HttpHandler 的子類負責處理具體的 http 請求。
            SP_HttpHandlerFactory 的子類協(xié)助 spserver 為每一個連接創(chuàng)建一個 SP_HttpHandler 子類實例。

            在實現過程中,使用了 Adapter 模式,把 SP_HttpHandler 和 SP_HttpHandlerFactory 適配為 SP_Handler 和 SP_HandlerAdapterFactory。這兩個 Adapter 都已經實現,不需要使用者來實現。

            posted on 2007-08-21 01:26 楊粼波 閱讀(410) 評論(0)  編輯 收藏 引用

            国产一区二区久久久| 无码8090精品久久一区 | 国产激情久久久久影院老熟女免费 | 三上悠亚久久精品| 中文字幕日本人妻久久久免费 | 久久精品国产WWW456C0M| 久久99国产精品久久99| 久久天天躁狠狠躁夜夜躁2O2O| 久久精品免费一区二区| 亚洲国产精品18久久久久久| 久久久国产亚洲精品| 国产美女亚洲精品久久久综合| 综合久久一区二区三区 | 亚洲午夜无码AV毛片久久| 久久婷婷色香五月综合激情| 亚洲伊人久久大香线蕉综合图片 | 久久WWW免费人成—看片| 久久精品国产欧美日韩| 久久笫一福利免费导航| 久久精品亚洲日本波多野结衣| 2021久久国自产拍精品| 久久99精品国产99久久6| 久久久久人妻一区二区三区 | 9久久9久久精品| 久久久久国产一区二区| 狠狠色丁香久久婷婷综合| 国内精品久久久久| 久久亚洲欧洲国产综合| 日韩乱码人妻无码中文字幕久久 | 久久婷婷激情综合色综合俺也去| 99久久免费国产精精品| 亚洲国产成人久久一区WWW| 久久99国产乱子伦精品免费| 亚洲乱亚洲乱淫久久| 久久精品国产亚洲av麻豆蜜芽| 精品久久777| 久久精品国产男包| 久久久久亚洲AV无码专区网站| 97久久国产综合精品女不卡| 国产精品成人久久久久久久| 中文字幕久久精品无码|