⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _h_t_t_p_server_8cpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00258"></a>00258         <span class="keywordflow">case</span> PARSE_VALUE:<a name="l00259"></a>00259             <span class="comment">// parsing value portion</span><a name="l00260"></a>00260             <span class="keywordflow">if</span> (c == <span class="charliteral">'\r'</span> || c == <span class="charliteral">'\n'</span>) {<a name="l00261"></a>00261                 <span class="comment">// value is finished</span><a name="l00262"></a>00262                 <span class="keywordflow">if</span> (value_string.empty()) {<a name="l00263"></a>00263                     <span class="comment">// value must not be empty</span><a name="l00264"></a>00264                     <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_h_t_t_p_server_1_1_config_parsing_exception.html" title="exception thrown if the plug-in file cannot be opened">ConfigParsingException</a>(config_name);<a name="l00265"></a>00265                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (command_string == <span class="stringliteral">"path"</span>) {<a name="l00266"></a>00266                     <span class="comment">// finished path command</span><a name="l00267"></a>00267                     <a class="code" href="classpion_1_1_pion_plugin.html#8b8215145cd47f8f87d87cad24bbc488" title="appends a directory to the plug-in search path">PionPlugin::addPluginDirectory</a>(value_string);<a name="l00268"></a>00268                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (command_string == <span class="stringliteral">"module"</span>) {<a name="l00269"></a>00269                     <span class="comment">// finished module command</span><a name="l00270"></a>00270                     <a class="code" href="classpion_1_1_h_t_t_p_server.html#95bd8a7c5882d19cd203f902aa8924cf">loadModule</a>(resource_string, value_string);<a name="l00271"></a>00271                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (command_string == <span class="stringliteral">"option"</span>) {<a name="l00272"></a>00272                     <span class="comment">// finished option command</span><a name="l00273"></a>00273                     std::string::size_type pos = value_string.find(<span class="charliteral">'='</span>);<a name="l00274"></a>00274                     <span class="keywordflow">if</span> (pos == std::string::npos)<a name="l00275"></a>00275                         <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_h_t_t_p_server_1_1_config_parsing_exception.html" title="exception thrown if the plug-in file cannot be opened">ConfigParsingException</a>(config_name);<a name="l00276"></a>00276                     option_name_string = value_string.substr(0, pos);<a name="l00277"></a>00277                     option_value_string = value_string.substr(pos + 1);<a name="l00278"></a>00278                     <a class="code" href="classpion_1_1_h_t_t_p_server.html#ae48d9e0433e0b956b13513597c57655">setModuleOption</a>(resource_string, option_name_string,<a name="l00279"></a>00279                                     option_value_string);<a name="l00280"></a>00280                 }<a name="l00281"></a>00281                 command_string.clear();<a name="l00282"></a>00282                 parse_state = PARSE_NEWLINE;<a name="l00283"></a>00283             } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c == <span class="charliteral">' '</span> || c == <span class="charliteral">'\t'</span>) {<a name="l00284"></a>00284                 <span class="comment">// only skip leading whitespace (value may contain spaces, etc)</span><a name="l00285"></a>00285                 <span class="keywordflow">if</span> (! value_string.empty())<a name="l00286"></a>00286                     value_string += c;<a name="l00287"></a>00287             } <span class="keywordflow">else</span> {<a name="l00288"></a>00288                 <span class="comment">// add char to value</span><a name="l00289"></a>00289                 value_string += c;<a name="l00290"></a>00290             }<a name="l00291"></a>00291             <span class="keywordflow">break</span>;<a name="l00292"></a>00292         <a name="l00293"></a>00293         <span class="keywordflow">case</span> PARSE_COMMENT:<a name="l00294"></a>00294             <span class="comment">// skipping comment line</span><a name="l00295"></a>00295             <span class="keywordflow">if</span> (c == <span class="charliteral">'\r'</span> || c == <span class="charliteral">'\n'</span>)<a name="l00296"></a>00296                 parse_state = PARSE_NEWLINE;<a name="l00297"></a>00297             <span class="keywordflow">break</span>;<a name="l00298"></a>00298         }<a name="l00299"></a>00299 <a name="l00300"></a>00300         <span class="comment">// read the next character</span><a name="l00301"></a>00301         c = config_stream.get();<a name="l00302"></a>00302     }<a name="l00303"></a>00303 }<a name="l00304"></a>00304 <a name="l00305"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#6f73057b1407cb4dcbd3ffc4612539e9">00305</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#6f73057b1407cb4dcbd3ffc4612539e9" title="clears all the modules that are currently configured">HTTPServer::clearModules</a>(<span class="keywordtype">void</span>)<a name="l00306"></a>00306 {<a name="l00307"></a>00307     boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00308"></a>00308     m_modules.clear();<a name="l00309"></a>00309 }<a name="l00310"></a>00310 <a name="l00311"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#aec0c406558aa20ccc076db57fdae0ee">00311</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#aec0c406558aa20ccc076db57fdae0ee">HTTPServer::handleBadRequest</a>(<a class="code" href="namespacepion.html#3c0f3fad0f449ebd309eaf4872f2b222" title="data type for a HTTP request pointer">HTTPRequestPtr</a>&amp; http_request,<a name="l00312"></a>00312                                    <a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>&amp; tcp_conn)<a name="l00313"></a>00313 {<a name="l00314"></a>00314     <span class="keyword">static</span> <span class="keyword">const</span> std::string BAD_REQUEST_HTML =<a name="l00315"></a>00315         <span class="stringliteral">"&lt;html&gt;&lt;head&gt;\n"</span><a name="l00316"></a>00316         <span class="stringliteral">"&lt;title&gt;400 Bad Request&lt;/title&gt;\n"</span><a name="l00317"></a>00317         <span class="stringliteral">"&lt;/head&gt;&lt;body&gt;\n"</span><a name="l00318"></a>00318         <span class="stringliteral">"&lt;h1&gt;Bad Request&lt;/h1&gt;\n"</span><a name="l00319"></a>00319         <span class="stringliteral">"&lt;p&gt;Your browser sent a request that this server could not understand.&lt;/p&gt;\n"</span><a name="l00320"></a>00320         <span class="stringliteral">"&lt;/body&gt;&lt;/html&gt;\n"</span>;<a name="l00321"></a>00321     <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a> response(<a class="code" href="classpion_1_1_h_t_t_p_response.html#bebd43da5cc60c5ee4245066c58fe244" title="creates new HTTPResponse objects">HTTPResponse::create</a>());<a name="l00322"></a>00322     response-&gt;setResponseCode(<a class="code" href="structpion_1_1_h_t_t_p_types.html#5b932c332af476686f33fc7a27b31904">HTTPTypes::RESPONSE_CODE_BAD_REQUEST</a>);<a name="l00323"></a>00323     response-&gt;setResponseMessage(<a class="code" href="structpion_1_1_h_t_t_p_types.html#acf4c800c03bd54bff311737eb077365">HTTPTypes::RESPONSE_MESSAGE_BAD_REQUEST</a>);<a name="l00324"></a>00324     response-&gt;writeNoCopy(BAD_REQUEST_HTML);<a name="l00325"></a>00325     response-&gt;send(tcp_conn);<a name="l00326"></a>00326 }<a name="l00327"></a>00327 <a name="l00328"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#bafcdd0c6b9533433e57821c5a312972">00328</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#bafcdd0c6b9533433e57821c5a312972">HTTPServer::handleNotFoundRequest</a>(<a class="code" href="namespacepion.html#3c0f3fad0f449ebd309eaf4872f2b222" title="data type for a HTTP request pointer">HTTPRequestPtr</a>&amp; http_request,<a name="l00329"></a>00329                                        <a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>&amp; tcp_conn)<a name="l00330"></a>00330 {<a name="l00331"></a>00331     <span class="keyword">static</span> <span class="keyword">const</span> std::string NOT_FOUND_HTML_START =<a name="l00332"></a>00332         <span class="stringliteral">"&lt;html&gt;&lt;head&gt;\n"</span><a name="l00333"></a>00333         <span class="stringliteral">"&lt;title&gt;404 Not Found&lt;/title&gt;\n"</span><a name="l00334"></a>00334         <span class="stringliteral">"&lt;/head&gt;&lt;body&gt;\n"</span><a name="l00335"></a>00335         <span class="stringliteral">"&lt;h1&gt;Not Found&lt;/h1&gt;\n"</span><a name="l00336"></a>00336         <span class="stringliteral">"&lt;p&gt;The requested URL "</span>;<a name="l00337"></a>00337     <span class="keyword">static</span> <span class="keyword">const</span> std::string NOT_FOUND_HTML_FINISH =<a name="l00338"></a>00338         <span class="stringliteral">" was not found on this server.&lt;/p&gt;\n"</span><a name="l00339"></a>00339         <span class="stringliteral">"&lt;/body&gt;&lt;/html&gt;\n"</span>;<a name="l00340"></a>00340     <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a> response(<a class="code" href="classpion_1_1_h_t_t_p_response.html#bebd43da5cc60c5ee4245066c58fe244" title="creates new HTTPResponse objects">HTTPResponse::create</a>());<a name="l00341"></a>00341     response-&gt;setResponseCode(<a class="code" href="structpion_1_1_h_t_t_p_types.html#aae74062dadc6e333d0cfe1dee070589">HTTPTypes::RESPONSE_CODE_NOT_FOUND</a>);<a name="l00342"></a>00342     response-&gt;setResponseMessage(<a class="code" href="structpion_1_1_h_t_t_p_types.html#a10df841f758d75f19c6e5e2ff67a1f6">HTTPTypes::RESPONSE_MESSAGE_NOT_FOUND</a>);<a name="l00343"></a>00343     response-&gt;writeNoCopy(NOT_FOUND_HTML_START);<a name="l00344"></a>00344     response &lt;&lt; http_request-&gt;getResource();<a name="l00345"></a>00345     response-&gt;writeNoCopy(NOT_FOUND_HTML_FINISH);<a name="l00346"></a>00346     response-&gt;send(tcp_conn);<a name="l00347"></a>00347 }<a name="l00348"></a>00348 <a name="l00349"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#dfa99550f6ce2e3be147904b32e077e5">00349</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#dfa99550f6ce2e3be147904b32e077e5">HTTPServer::handleServerError</a>(<a class="code" href="namespacepion.html#3c0f3fad0f449ebd309eaf4872f2b222" title="data type for a HTTP request pointer">HTTPRequestPtr</a>&amp; http_request,<a name="l00350"></a>00350                                    <a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>&amp; tcp_conn,<a name="l00351"></a>00351                                    <span class="keyword">const</span> std::string&amp; error_msg)<a name="l00352"></a>00352 {<a name="l00353"></a>00353     <span class="keyword">static</span> <span class="keyword">const</span> std::string SERVER_ERROR_HTML_START =<a name="l00354"></a>00354         <span class="stringliteral">"&lt;html&gt;&lt;head&gt;\n"</span><a name="l00355"></a>00355         <span class="stringliteral">"&lt;title&gt;500 Server Error&lt;/title&gt;\n"</span><a name="l00356"></a>00356         <span class="stringliteral">"&lt;/head&gt;&lt;body&gt;\n"</span><a name="l00357"></a>00357         <span class="stringliteral">"&lt;h1&gt;Internal Server Error&lt;/h1&gt;\n"</span><a name="l00358"></a>00358         <span class="stringliteral">"&lt;p&gt;The server encountered an internal error: &lt;strong&gt;"</span>;<a name="l00359"></a>00359     <span class="keyword">static</span> <span class="keyword">const</span> std::string SERVER_ERROR_HTML_FINISH =<a name="l00360"></a>00360         <span class="stringliteral">"&lt;/strong&gt;&lt;/p&gt;\n"</span><a name="l00361"></a>00361         <span class="stringliteral">"&lt;/body&gt;&lt;/html&gt;\n"</span>;<a name="l00362"></a>00362     <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a> response(<a class="code" href="classpion_1_1_h_t_t_p_response.html#bebd43da5cc60c5ee4245066c58fe244" title="creates new HTTPResponse objects">HTTPResponse::create</a>());<a name="l00363"></a>00363     response-&gt;setResponseCode(<a class="code" href="structpion_1_1_h_t_t_p_types.html#523b04e26f21894e6b0de7fc17a564f4">HTTPTypes::RESPONSE_CODE_SERVER_ERROR</a>);<a name="l00364"></a>00364     response-&gt;setResponseMessage(<a class="code" href="structpion_1_1_h_t_t_p_types.html#903ce5abee73a4c99612234b1422a089">HTTPTypes::RESPONSE_MESSAGE_SERVER_ERROR</a>);<a name="l00365"></a>00365     response-&gt;writeNoCopy(SERVER_ERROR_HTML_START);<a name="l00366"></a>00366     response &lt;&lt; error_msg;<a name="l00367"></a>00367     response-&gt;writeNoCopy(SERVER_ERROR_HTML_FINISH);<a name="l00368"></a>00368     response-&gt;send(tcp_conn);<a name="l00369"></a>00369 }<a name="l00370"></a>00370 <a name="l00371"></a>00371 <a name="l00372"></a>00372 <span class="comment">// HTTPServer::ModuleMap member functions</span><a name="l00373"></a>00373 <a name="l00374"></a>00374 <span class="keywordtype">void</span> HTTPServer::ModuleMap::clear(<span class="keywordtype">void</span>) {<a name="l00375"></a>00375     <span class="keywordflow">for</span> (iterator i = begin(); i != end(); ++i) {<a name="l00376"></a>00376         <span class="keywordflow">if</span> (i-&gt;second.second.is_open()) {<a name="l00377"></a>00377             i-&gt;second.second.destroy(i-&gt;second.first);<a name="l00378"></a>00378             i-&gt;second.second.close();<a name="l00379"></a>00379         } <span class="keywordflow">else</span> {<a name="l00380"></a>00380             <span class="keyword">delete</span> i-&gt;second.first;<a name="l00381"></a>00381         }<a name="l00382"></a>00382     }<a name="l00383"></a>00383     std::map&lt;std::string, PluginPair&gt;::clear();<a name="l00384"></a>00384 }<a name="l00385"></a>00385 <a name="l00386"></a>00386 }   <span class="comment">// end namespace pion</span><a name="l00387"></a>00387 </pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 19 13:29:22 2007 for libpion by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -