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

📄 _h_t_t_p_server_8cpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00122"></a>00122     boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00123"></a>00123     <span class="keywordflow">for</span> (ModuleMap::iterator i = m_modules.begin(); i != m_modules.end(); ++i)<a name="l00124"></a>00124         i-&gt;second.first-&gt;stop();<a name="l00125"></a>00125 }<a name="l00126"></a>00126 <a name="l00127"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#bb4bab0c980efa48976f3982c85db100">00127</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#bb4bab0c980efa48976f3982c85db100">HTTPServer::addModule</a>(<span class="keyword">const</span> std::string&amp; resource, <a class="code" href="classpion_1_1_h_t_t_p_module.html">HTTPModule</a> *module_ptr)<a name="l00128"></a>00128 {<a name="l00129"></a>00129     <a class="code" href="classpion_1_1_pion_plugin_ptr.html">PionPluginPtr&lt;HTTPModule&gt;</a> plugin_ptr;<a name="l00130"></a>00130     module_ptr-&gt;<a class="code" href="classpion_1_1_h_t_t_p_module.html#108d617af983e75e52d7563d53571e7d" title="sets the URI stem or resource that is bound to the module (strips any trailing slash)...">setResource</a>(resource);  <span class="comment">// strips any trailing '/' from the name</span><a name="l00131"></a>00131     boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00132"></a>00132     m_modules.insert(std::make_pair(module_ptr-&gt;<a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>(),<a name="l00133"></a>00133                                     std::make_pair(module_ptr, plugin_ptr)));<a name="l00134"></a>00134 }<a name="l00135"></a>00135 <a name="l00136"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#95bd8a7c5882d19cd203f902aa8924cf">00136</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#95bd8a7c5882d19cd203f902aa8924cf">HTTPServer::loadModule</a>(<span class="keyword">const</span> std::string&amp; resource, <span class="keyword">const</span> std::string&amp; module_name)<a name="l00137"></a>00137 {<a name="l00138"></a>00138     <span class="comment">// search for the plug-in file using the configured paths</span><a name="l00139"></a>00139     std::string module_file;<a name="l00140"></a>00140     <span class="keywordflow">if</span> (! <a class="code" href="classpion_1_1_pion_plugin.html#b2a5652e82e5b50143153d3b12f808e8">PionPlugin::findPluginFile</a>(module_file, module_name))<a name="l00141"></a>00141         <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_pion_plugin_1_1_plugin_not_found_exception.html" title="exception thrown if the plug-in file cannot be opened">PionPlugin::PluginNotFoundException</a>(module_name);<a name="l00142"></a>00142 <a name="l00143"></a>00143     <span class="comment">// open up the plug-in's shared object library</span><a name="l00144"></a>00144     <a class="code" href="classpion_1_1_pion_plugin_ptr.html">PionPluginPtr&lt;HTTPModule&gt;</a> plugin_ptr;<a name="l00145"></a>00145     plugin_ptr.<a class="code" href="classpion_1_1_pion_plugin.html#998d9f6ff965e708ad466ed3afac3b68">open</a>(module_file);   <span class="comment">// may throw</span><a name="l00146"></a>00146     <a name="l00147"></a>00147     <span class="comment">// create a new module using the plug-in library</span><a name="l00148"></a>00148     <a class="code" href="classpion_1_1_h_t_t_p_module.html">HTTPModule</a> *module_ptr(plugin_ptr.<a class="code" href="classpion_1_1_pion_plugin_ptr.html#5b0b5f11f5439ab9f0270d28f5424c42" title="creates a new instance of the plug-in object">create</a>());<a name="l00149"></a>00149     module_ptr-&gt;setResource(resource);  <span class="comment">// strips any trailing '/' from the name</span><a name="l00150"></a>00150 <a name="l00151"></a>00151     <span class="comment">// add the module to the server's collection</span><a name="l00152"></a>00152     boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00153"></a>00153     m_modules.insert(std::make_pair(module_ptr-&gt;getResource(),<a name="l00154"></a>00154                                     std::make_pair(module_ptr, plugin_ptr)));<a name="l00155"></a>00155     modules_lock.unlock();<a name="l00156"></a>00156 <a name="l00157"></a>00157     PION_LOG_INFO(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Loaded HTTP module for resource ("</span><a name="l00158"></a>00158                   &lt;&lt; resource &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; module_file);<a name="l00159"></a>00159 }<a name="l00160"></a>00160 <a name="l00161"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#ae48d9e0433e0b956b13513597c57655">00161</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#ae48d9e0433e0b956b13513597c57655">HTTPServer::setModuleOption</a>(<span class="keyword">const</span> std::string&amp; resource,<a name="l00162"></a>00162                                  <span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value)<a name="l00163"></a>00163 {<a name="l00164"></a>00164     boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00165"></a>00165 <a name="l00166"></a>00166     <span class="comment">// find the module associated with resource &amp; set the option</span><a name="l00167"></a>00167     <span class="comment">// if resource == "/" then look for module with an empty string</span><a name="l00168"></a>00168     ModuleMap::iterator i = (resource == <span class="stringliteral">"/"</span> ? m_modules.find(<span class="stringliteral">""</span>) : m_modules.find(resource));<a name="l00169"></a>00169     <span class="keywordflow">if</span> (i == m_modules.end())<a name="l00170"></a>00170         <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_h_t_t_p_server_1_1_module_not_found_exception.html" title="exception thrown if a module cannot be found">ModuleNotFoundException</a>(resource);<a name="l00171"></a>00171     i-&gt;second.first-&gt;setOption(name, value);<a name="l00172"></a>00172 <a name="l00173"></a>00173     modules_lock.unlock();<a name="l00174"></a>00174     PION_LOG_INFO(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Set module option for resource ("</span><a name="l00175"></a>00175                   &lt;&lt; resource &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; name &lt;&lt; <span class="charliteral">'='</span> &lt;&lt; value);<a name="l00176"></a>00176 }<a name="l00177"></a>00177 <a name="l00178"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#2cc8edf6440a62b1582ff7162a9dc067">00178</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#2cc8edf6440a62b1582ff7162a9dc067">HTTPServer::loadModuleConfig</a>(<span class="keyword">const</span> std::string&amp; config_name)<a name="l00179"></a>00179 {<a name="l00180"></a>00180     std::string config_file;<a name="l00181"></a>00181     <span class="keywordflow">if</span> (! <a class="code" href="classpion_1_1_pion_plugin.html#f5987395dce53c3921baa44be7d62a65">PionPlugin::findConfigFile</a>(config_file, config_name))<a name="l00182"></a>00182         <span class="keywordflow">throw</span> <a class="code" href="classpion_1_1_h_t_t_p_server_1_1_config_not_found_exception.html" title="exception thrown if the plug-in configuration file cannot be found">ConfigNotFoundException</a>(config_name);<a name="l00183"></a>00183     <a name="l00184"></a>00184     <span class="comment">// open the file for reading</span><a name="l00185"></a>00185     std::ifstream config_stream;<a name="l00186"></a>00186     config_stream.open(config_file.c_str(), std::ios::in);<a name="l00187"></a>00187     <span class="keywordflow">if</span> (! config_stream.is_open())<a name="l00188"></a>00188         <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="l00189"></a>00189     <a name="l00190"></a>00190     <span class="comment">// parse the contents of the file</span><a name="l00191"></a>00191     <span class="keyword">enum</span> ParseState {<a name="l00192"></a>00192         PARSE_NEWLINE, PARSE_COMMAND, PARSE_RESOURCE, PARSE_VALUE, PARSE_COMMENT<a name="l00193"></a>00193     } parse_state = PARSE_NEWLINE;<a name="l00194"></a>00194     std::string command_string;<a name="l00195"></a>00195     std::string resource_string;<a name="l00196"></a>00196     std::string value_string;<a name="l00197"></a>00197     std::string option_name_string;<a name="l00198"></a>00198     std::string option_value_string;<a name="l00199"></a>00199     <span class="keywordtype">int</span> c = config_stream.get();    <span class="comment">// read the first character</span><a name="l00200"></a>00200     <a name="l00201"></a>00201     <span class="keywordflow">while</span> (config_stream) {<a name="l00202"></a>00202         <span class="keywordflow">switch</span>(parse_state) {<a name="l00203"></a>00203         <span class="keywordflow">case</span> PARSE_NEWLINE:<a name="l00204"></a>00204             <span class="comment">// parsing command portion (or beginning of line)</span><a name="l00205"></a>00205             <span class="keywordflow">if</span> (c == <span class="charliteral">'#'</span>) {<a name="l00206"></a>00206                 <span class="comment">// line is a comment</span><a name="l00207"></a>00207                 parse_state = PARSE_COMMENT;<a name="l00208"></a>00208             } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (isalpha(c)) {<a name="l00209"></a>00209                 <span class="comment">// first char in command</span><a name="l00210"></a>00210                 parse_state = PARSE_COMMAND;<a name="l00211"></a>00211                 <span class="comment">// ignore case for commands</span><a name="l00212"></a>00212                 command_string += tolower(c);<a name="l00213"></a>00213             } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c != <span class="charliteral">'\r'</span> &amp;&amp; c != <span class="charliteral">'\n'</span>) {    <span class="comment">// check for blank lines</span><a name="l00214"></a>00214                 <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="l00215"></a>00215             }<a name="l00216"></a>00216             <span class="keywordflow">break</span>;<a name="l00217"></a>00217             <a name="l00218"></a>00218         <span class="keywordflow">case</span> PARSE_COMMAND:<a name="l00219"></a>00219             <span class="comment">// parsing command portion (or beginning of line)</span><a name="l00220"></a>00220             <span class="keywordflow">if</span> (c == <span class="charliteral">' '</span> || c == <span class="charliteral">'\t'</span>) {<a name="l00221"></a>00221                 <span class="comment">// command finished -&gt; check if valid</span><a name="l00222"></a>00222                 <span class="keywordflow">if</span> (command_string==<span class="stringliteral">"path"</span>) {<a name="l00223"></a>00223                     value_string.clear();<a name="l00224"></a>00224                     parse_state = PARSE_VALUE;<a name="l00225"></a>00225                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (command_string==<span class="stringliteral">"module"</span> || command_string==<span class="stringliteral">"option"</span>) {<a name="l00226"></a>00226                     resource_string.clear();<a name="l00227"></a>00227                     parse_state = PARSE_RESOURCE;<a name="l00228"></a>00228                 } <span class="keywordflow">else</span> {<a name="l00229"></a>00229                     <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="l00230"></a>00230                 }<a name="l00231"></a>00231             } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (! isalpha(c)) {<a name="l00232"></a>00232                 <span class="comment">// commands may only contain alpha chars</span><a name="l00233"></a>00233                 <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="l00234"></a>00234             } <span class="keywordflow">else</span> {<a name="l00235"></a>00235                 <span class="comment">// ignore case for commands</span><a name="l00236"></a>00236                 command_string += tolower(c);<a name="l00237"></a>00237             }<a name="l00238"></a>00238             <span class="keywordflow">break</span>;<a name="l00239"></a>00239 <a name="l00240"></a>00240         <span class="keywordflow">case</span> PARSE_RESOURCE:<a name="l00241"></a>00241             <span class="comment">// parsing resource portion (/hello)</span><a name="l00242"></a>00242             <span class="keywordflow">if</span> (c == <span class="charliteral">' '</span> || c == <span class="charliteral">'\t'</span>) {<a name="l00243"></a>00243                 <span class="comment">// check for leading whitespace</span><a name="l00244"></a>00244                 <span class="keywordflow">if</span> (! resource_string.empty()) {<a name="l00245"></a>00245                     <span class="comment">// resource finished</span><a name="l00246"></a>00246                     value_string.clear();<a name="l00247"></a>00247                     parse_state = PARSE_VALUE;<a name="l00248"></a>00248                 }<a name="l00249"></a>00249             } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c == <span class="charliteral">'\r'</span> || c == <span class="charliteral">'\n'</span>) {<a name="l00250"></a>00250                 <span class="comment">// line truncated before value</span><a name="l00251"></a>00251                 <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="l00252"></a>00252             } <span class="keywordflow">else</span> {<a name="l00253"></a>00253                 <span class="comment">// add char to resource</span><a name="l00254"></a>00254                 resource_string += c;<a name="l00255"></a>00255             }<a name="l00256"></a>00256             <span class="keywordflow">break</span>;<a name="l00257"></a>00257         

⌨️ 快捷键说明

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