為了給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++