• <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>
            隨筆-341  評論-2670  文章-0  trackbacks-0
                為了給C++的反射做Demo,不得不研究一下HTTP的協(xié)議。后來發(fā)現(xiàn)Windows自帶了API可以用,于是就寫了個小東西。程序打開之后,如果檢測到【http://localhost:8080/vczh/FILENAME】這樣子的請求,就將一個目錄下面的東西讀出來,然后返回。于是就可以用IE來運(yùn)行某個地方的網(wǎng)頁了。

                首先看效果圖:


                這是一個只返回網(wǎng)頁內(nèi)容的HTTP服務(wù)器程序。
              1 #include <sdkddkver.h>
              2 #include <http.h>
              3 #include "..\..\..\..\VL++\Library\Platform\VL_Console.h"
              4 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
              5 #include "..\..\..\..\VL++\Library\Data\VL_Stream.h"
              6 #include "..\..\..\..\VL++\Library\Data\VL_Communication.h"
              7 
              8 #pragma comment(lib,"Httpapi.lib")
              9 
             10 using namespace vl;
             11 using namespace vl::platform;
             12 using namespace vl::stream;
             13 using namespace vl::system;
             14 using namespace vl::communication;
             15 
             16 class HttpThread : public VL_Thread
             17 {
             18 protected:
             19     HANDLE                FHttpHandle;
             20     VByte                FBuffer[65536];
             21     PHTTP_REQUEST        FRequest;
             22     VUnicodeString        FWorkPath;
             23 
             24     VUnicodeString ToString(PCWChar Buffer)
             25     {
             26         return Buffer?Buffer:L"";
             27     }
             28 
             29     void Run()
             30     {
             31         while(!FNeedToTerminate)
             32         {
             33             ULONG Bytes=0;
             34             ULONG ErrorCode=0;
             35             if((ErrorCode=HttpReceiveHttpRequest(FHttpHandle,HTTP_NULL_ID,0,FRequest,sizeof(FBuffer),&Bytes,NULL))==NO_ERROR)
             36             {
             37                 VUnicodeString URL=ToString(FRequest->CookedUrl.pFullUrl);
             38                 VUnicodeString Query=ToString(FRequest->CookedUrl.pQueryString);
             39                 VInt PortPos=URL.Pos(L":8080/vczh/")+11;
             40                 VUnicodeString FilePath=FWorkPath+URL.SubString(PortPos,URL.Length()-PortPos-Query.Length());
             41                 GetConsole()->WriteLine(L"請求地址:"+URL);
             42                 GetConsole()->WriteLine(L"文件地址:"+FilePath);
             43 
             44                 HTTP_VERSION Version=HTTP_VERSION_1_1;
             45                 HTTP_RESPONSE Response;
             46                 memset(&Response,0,sizeof(Response));
             47                 Response.Version=Version;
             48 
             49                 if(VFSO_FileExists(FilePath))
             50                 {
             51                     VL_FileStream HtmlFile(FilePath,VL_FileStream::vomRead);
             52                     VBuffer Buffer=new VByte[(VInt)HtmlFile.Size()];
             53                     HtmlFile.Read(Buffer,(VInt)HtmlFile.Size());
             54 
             55                     HTTP_DATA_CHUNK Chunk;
             56                     Chunk.DataChunkType=HttpDataChunkFromMemory;
             57                     Chunk.FromMemory.BufferLength=(ULONG)HtmlFile.Size();
             58                     Chunk.FromMemory.pBuffer=Buffer;
             59 
             60                     Response.StatusCode=200;
             61                     Response.EntityChunkCount=1;
             62                     Response.pEntityChunks=&Chunk;
             63                     if((ErrorCode=HttpSendHttpResponse(FHttpHandle,FRequest->RequestId,HTTP_SEND_RESPONSE_FLAG_DISCONNECT,&Response,NULL,&Bytes,NULL,NULL,NULL,NULL))!=NO_ERROR)
             64                     {
             65                         GetConsole()->WriteLine(L"HttpSendHttpResponse發(fā)送頁面不存在回復(fù)失敗("+VUnicodeString((VInt)ErrorCode)+L")。");
             66                     }
             67 
             68                     delete[] Buffer;
             69                 }
             70                 else
             71                 {
             72                     Response.StatusCode=404;
             73                     if((ErrorCode=HttpSendHttpResponse(FHttpHandle,FRequest->RequestId,HTTP_SEND_RESPONSE_FLAG_DISCONNECT,&Response,NULL,&Bytes,NULL,NULL,NULL,NULL))!=NO_ERROR)
             74                     {
             75                         GetConsole()->WriteLine(L"HttpSendHttpResponse發(fā)送頁面不存在回復(fù)失敗("+VUnicodeString((VInt)ErrorCode)+L")。");
             76                     }
             77                 }
             78             }
             79             else
             80             {
             81                 GetConsole()->WriteLine(L"HttpReceiveHttpRequest失敗("+VUnicodeString((VInt)ErrorCode)+L")。");
             82             }
             83         }
             84     }
             85 public:
             86     HttpThread(HANDLE HttpHandle , VUnicodeString WorkPath):VL_Thread(true,false)
             87     {
             88         FHttpHandle=HttpHandle;
             89         FRequest=(PHTTP_REQUEST)FBuffer;
             90         FWorkPath=WorkPath;
             91     }
             92 };
             93 
             94 void vlmain()
             95 {
             96     GetConsole()->SetTitle(L"Vczh HTTP Server");
             97     GetConsole()->SetTestMemoryLeaks(true);
             98     GetConsole()->SetPauseOnExit(true);
             99     VUnicodeString WorkPath=VFileName(GetConsole()->GetAppPath()).MakeAbsolute(L"..\\Web\\").GetStrW();
            100     GetConsole()->WriteLine(L"網(wǎng)站:"+WorkPath);
            101     GetConsole()->WriteLine(L"主機(jī)名稱:"+GetHostName());
            102     GetConsole()->WriteLine(L"主機(jī)地址:"+GetIpv4Address());
            103 
            104     ULONG ErrorCode=0;
            105 
            106     HTTPAPI_VERSION Version=HTTPAPI_VERSION_1;
            107     if(HttpInitialize(Version,HTTP_INITIALIZE_SERVER,NULL)!=NO_ERROR)
            108     {
            109         GetConsole()->WriteLine(L"HttpInitialize失敗。");
            110         goto VCZH_HTTP_SERVER_INITIALIZE_FAIL;
            111     }
            112 
            113     HANDLE HttpHandle=0;
            114     if(HttpCreateHttpHandle(&HttpHandle,NULL)!=NO_ERROR)
            115     {
            116         GetConsole()->WriteLine(L"HttpCreateHttpHandle失敗。");
            117         goto VCZH_HTTP_SERVER_CREATE_HANDLE_FAIL;
            118     }
            119 
            120     if(HttpAddUrl(HttpHandle,L"http://+:8080/vczh/",NULL)!=NO_ERROR)
            121     {
            122         GetConsole()->WriteLine(L"HttpAddUrl失敗,需要使用管理員身份啟動。");
            123         goto VCZH_HTTP_SERVER_ADD_URL_FAIL;
            124     }
            125 
            126     {
            127         HttpThread Thread(HttpHandle,WorkPath);
            128         Thread.Execute();
            129         GetConsole()->WriteLine(L"按回車結(jié)束服務(wù)程序:");
            130         GetConsole()->WaitForEnter();
            131         Thread.SetNeedToTerminate(true);
            132         Thread.WaitFor();
            133     }
            134 
            135     if(HttpRemoveUrl(HttpHandle,L"http://+:8080/vczh/")!=NO_ERROR)
            136     {
            137         GetConsole()->WriteLine(L"HttpRemoveUrl失敗。");
            138     }
            139 VCZH_HTTP_SERVER_ADD_URL_FAIL:
            140 
            141     if(!CloseHandle(HttpHandle))
            142     {
            143         GetConsole()->WriteLine(L"CloseHandle失敗。");
            144     }
            145 VCZH_HTTP_SERVER_CREATE_HANDLE_FAIL:
            146 
            147     if(HttpTerminate(HTTP_INITIALIZE_SERVER,NULL)!=NO_ERROR)
            148     {
            149         GetConsole()->WriteLine(L"HttpTerminate失敗。");
            150     }
            151 VCZH_HTTP_SERVER_INITIALIZE_FAIL:
            152 
            153     GetConsole()->WriteLine(L"Vczh HTTP Server執(zhí)行結(jié)束。");
            154 }
            posted on 2009-06-29 05:19 陳梓瀚(vczh) 閱讀(3895) 評論(4)  編輯 收藏 引用 所屬分類: C++

            評論:
            # re: 殘廢版HTTP Server之小試牛刀 2009-06-29 17:09 | 巫云
            崇拜……  回復(fù)  更多評論
              
            # re: 殘廢版HTTP Server之小試牛刀 2009-07-06 09:19 | 余秋雨他老爹
            博主是Delphi出身的哈  回復(fù)  更多評論
              
            # re: 殘廢版HTTP Server之小試牛刀 2009-07-06 18:47 | 陳梓瀚(vczh)
            @余秋雨他老爹
            被你看出來了- -b  回復(fù)  更多評論
              
            # re: 殘廢版HTTP Server之小試牛刀 2011-02-10 18:58 | loop
            那幾個goto看著不是很爽,呵呵。  回復(fù)  更多評論
              
            国产精品狼人久久久久影院| 久久亚洲sm情趣捆绑调教| 久久精品国产亚洲网站| 久久夜色tv网站| 无码精品久久一区二区三区| 欧美熟妇另类久久久久久不卡| 97久久超碰国产精品旧版| 久久久久国产一区二区| 狠狠综合久久AV一区二区三区| 色噜噜狠狠先锋影音久久| 中文字幕无码久久人妻| 国产精品福利一区二区久久| 色播久久人人爽人人爽人人片aV| 久久国产热精品波多野结衣AV| 久久久久久国产精品免费免费| 欧美黑人又粗又大久久久| 久久人搡人人玩人妻精品首页| 久久精品国产99久久无毒不卡| 尹人香蕉久久99天天拍| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品国产亚洲AV蜜臀色欲 | 亚洲国产精品嫩草影院久久| AV无码久久久久不卡蜜桃| 无码乱码观看精品久久| 亚洲综合精品香蕉久久网97| 久久精品国产亚洲AV香蕉| 四虎亚洲国产成人久久精品| 99久久www免费人成精品| 久久人妻少妇嫩草AV无码专区| 久久久久久曰本AV免费免费| 久久人人超碰精品CAOPOREN | 2021久久国自产拍精品| 一本色道久久88精品综合| 亚洲七七久久精品中文国产| 国产毛片久久久久久国产毛片| 久久精品国产影库免费看| 精品久久久久久久无码| 国内精品伊人久久久久av一坑| 无码人妻久久一区二区三区| 无码专区久久综合久中文字幕| 久久人人爽爽爽人久久久|