📄 _h_t_t_p_request_parser_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>libpion: /Users/mdickey/code/libpion-export/src/HTTPRequestParser.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul></div><h1>/Users/mdickey/code/libpion-export/src/HTTPRequestParser.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// -----------------------------------------------------------------</span><a name="l00002"></a>00002 <span class="comment">// libpion: a C++ framework for building lightweight HTTP interfaces</span><a name="l00003"></a>00003 <span class="comment">// -----------------------------------------------------------------</span><a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007 Atomic Labs, Inc. (http://www.atomiclabs.com)</span><a name="l00005"></a>00005 <span class="comment">//</span><a name="l00006"></a>00006 <span class="comment">// Distributed under the Boost Software License, Version 1.0.</span><a name="l00007"></a>00007 <span class="comment">// See accompanying file COPYING or copy at http://www.boost.org/LICENSE_1_0.txt</span><a name="l00008"></a>00008 <span class="comment">//</span><a name="l00009"></a>00009 <a name="l00010"></a>00010 <span class="preprocessor">#include <libpion/HTTPRequestParser.hpp></span><a name="l00011"></a>00011 <span class="preprocessor">#include <boost/bind.hpp></span><a name="l00012"></a>00012 <span class="preprocessor">#include <boost/asio.hpp></span><a name="l00013"></a>00013 <a name="l00014"></a>00014 <a name="l00015"></a>00015 <span class="keyword">namespace </span>pion { <span class="comment">// begin namespace pion</span><a name="l00016"></a>00016 <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="comment">// static members of HTTPRequestParser</span><a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::METHOD_MAX = 1024; <span class="comment">// 1 KB</span><a name="l00021"></a>00021 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::RESOURCE_MAX = 256 * 1024; <span class="comment">// 256 KB</span><a name="l00022"></a>00022 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::QUERY_STRING_MAX = 1024 * 1024; <span class="comment">// 1 MB</span><a name="l00023"></a>00023 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::HEADER_NAME_MAX = 1024; <span class="comment">// 1 KB</span><a name="l00024"></a>00024 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::HEADER_VALUE_MAX = 1024 * 1024; <span class="comment">// 1 MB</span><a name="l00025"></a>00025 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::QUERY_NAME_MAX = 1024; <span class="comment">// 1 KB</span><a name="l00026"></a>00026 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::QUERY_VALUE_MAX = 1024 * 1024; <span class="comment">// 1 MB</span><a name="l00027"></a>00027 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::COOKIE_NAME_MAX = 1024; <span class="comment">// 1 KB</span><a name="l00028"></a>00028 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::COOKIE_VALUE_MAX = 1024 * 1024; <span class="comment">// 1 MB</span><a name="l00029"></a>00029 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> HTTPRequestParser::POST_CONTENT_MAX = 1024 * 1024; <span class="comment">// 1 MB</span><a name="l00030"></a>00030 <a name="l00031"></a>00031 <a name="l00032"></a>00032 <span class="comment">// HTTPRequestParser member functions</span><a name="l00033"></a>00033 <a name="l00034"></a><a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#9208a39a2d32349d65ffaee6bca5c849">00034</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#9208a39a2d32349d65ffaee6bca5c849" title="Incrementally reads & parses a new HTTP request.">HTTPRequestParser::readRequest</a>(<span class="keywordtype">void</span>)<a name="l00035"></a>00035 {<a name="l00036"></a>00036 <span class="keywordflow">if</span> (m_tcp_conn->getSSLFlag()) {<a name="l00037"></a>00037 #ifdef PION_HAVE_SSL<a name="l00038"></a>00038 m_tcp_conn->getSSLSocket().async_read_some(boost::asio::buffer(m_read_buffer),<a name="l00039"></a>00039 boost::bind(&<a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#921419675678bf79df13d9f3ee0a893a">HTTPRequestParser::readHeaderBytes</a>,<a name="l00040"></a>00040 shared_from_this(),<a name="l00041"></a>00041 boost::asio::placeholders::error,<a name="l00042"></a>00042 boost::asio::placeholders::bytes_transferred));<a name="l00043"></a>00043 <span class="preprocessor">#else</span><a name="l00044"></a>00044 <span class="preprocessor"></span> PION_LOG_ERROR(m_logger, <span class="stringliteral">"SSL flag set for server, but support is not enabled"</span>);<a name="l00045"></a>00045 m_tcp_conn->finish();<a name="l00046"></a>00046 <span class="preprocessor">#endif </span><a name="l00047"></a>00047 <span class="preprocessor"></span> } <span class="keywordflow">else</span> {<a name="l00048"></a>00048 m_tcp_conn->getSocket().async_read_some(boost::asio::buffer(m_read_buffer),<a name="l00049"></a>00049 boost::bind(&<a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#921419675678bf79df13d9f3ee0a893a">HTTPRequestParser::readHeaderBytes</a>,<a name="l00050"></a>00050 shared_from_this(),<a name="l00051"></a>00051 boost::asio::placeholders::error,<a name="l00052"></a>00052 boost::asio::placeholders::bytes_transferred));<a name="l00053"></a>00053 }<a name="l00054"></a>00054 }<a name="l00055"></a>00055 <a name="l00056"></a><a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#921419675678bf79df13d9f3ee0a893a">00056</a> <span class="keywordtype">void</span> HTTPRequestParser::readHeaderBytes(<span class="keyword">const</span> boost::system::error_code& read_error,<a name="l00057"></a>00057 std::size_t bytes_read)<a name="l00058"></a>00058 {<a name="l00059"></a>00059 <span class="keywordflow">if</span> (read_error) {<a name="l00060"></a>00060 <span class="comment">// a read error occured</span><a name="l00061"></a>00061 <a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#af553431fca527b6f1eb41489f7a7b1e">handleReadError</a>(read_error);<a name="l00062"></a>00062 <span class="keywordflow">return</span>;<a name="l00063"></a>00063 }<a name="l00064"></a>00064 <a name="l00065"></a>00065 PION_LOG_DEBUG(m_logger, <span class="stringliteral">"Read "</span> << bytes_read << <span class="stringliteral">" bytes from HTTP request"</span>);<a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="comment">// parse the bytes read from the last operation</span><a name="l00068"></a>00068 <span class="keyword">const</span> <span class="keywordtype">char</span> *read_ptr = m_read_buffer.data();<a name="l00069"></a>00069 <span class="keyword">const</span> <span class="keywordtype">char</span> * <span class="keyword">const</span> read_end = read_ptr + bytes_read;<a name="l00070"></a>00070 boost::tribool result = <a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#67d29971d989f532be4160153ff7d071">parseRequestHeaders</a>(read_ptr, bytes_read);<a name="l00071"></a>00071 <a name="l00072"></a>00072 PION_LOG_DEBUG(m_logger, <span class="stringliteral">"Parsed "</span> << (read_ptr - m_read_buffer.data()) << <span class="stringliteral">" HTTP header bytes"</span>);<a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="keywordflow">if</span> (result) {<a name="l00075"></a>00075 <span class="comment">// finished reading request headers and they are valid</span><a name="l00076"></a>00076 <a name="l00077"></a>00077 <span class="comment">// check if we have post content to read</span><a name="l00078"></a>00078 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> content_length =<a name="l00079"></a>00079 m_http_request->hasHeader(<a class="code" href="structpion_1_1_h_t_t_p_types.html#41f4bc64d3ffedbcde771de646f9f920">HTTPTypes::HEADER_CONTENT_LENGTH</a>)<a name="l00080"></a>00080 ? strtoul(m_http_request->getHeader(<a class="code" href="structpion_1_1_h_t_t_p_types.html#41f4bc64d3ffedbcde771de646f9f920">HTTPTypes::HEADER_CONTENT_LENGTH</a>).c_str(), 0, 10)<a name="l00081"></a>00081 : 0;<a name="l00082"></a>00082 <a name="l00083"></a>00083 <span class="keywordflow">if</span> (content_length == 0) {<a name="l00084"></a>00084 <a name="l00085"></a>00085 <span class="comment">// there is no post content to read</span><a name="l00086"></a>00086 <a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#20e7739e89be3603f6463801e8bdc489">readContentBytes</a>(read_error, 0);<a name="l00087"></a>00087 <a name="l00088"></a>00088 } <span class="keywordflow">else</span> {<a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="comment">// read the post content</span><a name="l00091"></a>00091 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> content_bytes_to_read = content_length;<a name="l00092"></a>00092 m_http_request->setContentLength(content_length);<a name="l00093"></a>00093 <span class="keywordtype">char</span> *post_buffer = m_http_request->createPostContentBuffer();<a name="l00094"></a>00094 <a name="l00095"></a>00095 <span class="keywordflow">if</span> (read_ptr < read_end) {<a name="l00096"></a>00096 <span class="comment">// there are extra bytes left from the last read operation</span><a name="l00097"></a>00097 <span class="comment">// copy them into the beginning of the content buffer</span><a name="l00098"></a>00098 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bytes_left_in_read_buffer = read_end - read_ptr;<a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="keywordflow">if</span> (bytes_left_in_read_buffer >= content_length) {<a name="l00101"></a>00101 <span class="comment">// the last read operation included all of the post content</span><a name="l00102"></a>00102 memcpy(post_buffer, read_ptr, content_length);<a name="l00103"></a>00103 content_bytes_to_read = 0;<a name="l00104"></a>00104 PION_LOG_DEBUG(m_logger, <span class="stringliteral">"Parsed "</span> << content_length << <span class="stringliteral">" request content bytes from last read operation (finished)"</span>);<a name="l00105"></a>00105 } <span class="keywordflow">else</span> {<a name="l00106"></a>00106 <span class="comment">// only some of the post content has been read so far</span><a name="l00107"></a>00107 memcpy(post_buffer, read_ptr, bytes_left_in_read_buffer);<a name="l00108"></a>00108 content_bytes_to_read -= bytes_left_in_read_buffer;<a name="l00109"></a>00109 post_buffer += bytes_left_in_read_buffer;<a name="l00110"></a>00110 PION_LOG_DEBUG(m_logger, <span class="stringliteral">"Parsed "</span> << bytes_left_in_read_buffer << <span class="stringliteral">" request content bytes from last read operation (partial)"</span>);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -