added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / http_parser.hpp
blob7d308ca36f1ca26de1262001ee16d8aab27df737
1 /*
3 Copyright (c) 2008, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_HTTP_PARSER_HPP_INCLUDED
34 #define TORRENT_HTTP_PARSER_HPP_INCLUDED
36 #include <map>
37 #include <string>
38 #include <utility>
40 #ifdef _MSC_VER
41 #pragma warning(push, 1)
42 #endif
44 #include <boost/cstdint.hpp>
45 #include <boost/tuple/tuple.hpp>
46 #include <boost/lexical_cast.hpp>
48 #ifdef _MSC_VER
49 #pragma warning(pop)
50 #endif
52 #include "libtorrent/config.hpp"
53 #include "libtorrent/buffer.hpp"
54 #include "libtorrent/size_type.hpp"
56 namespace libtorrent
59 class http_parser
61 public:
62 http_parser();
63 std::string const& header(char const* key) const
65 static std::string empty;
66 std::map<std::string, std::string>::const_iterator i
67 = m_header.find(key);
68 if (i == m_header.end()) return empty;
69 return i->second;
72 std::string const& protocol() const { return m_protocol; }
73 int status_code() const { return m_status_code; }
74 std::string const& method() const { return m_method; }
75 std::string const& path() const { return m_path; }
76 std::string const& message() const { return m_server_message; }
77 buffer::const_interval get_body() const;
78 bool header_finished() const { return m_state == read_body; }
79 bool finished() const { return m_finished; }
80 boost::tuple<int, int> incoming(buffer::const_interval recv_buffer
81 , bool& error);
82 int body_start() const { return m_body_start_pos; }
83 size_type content_length() const { return m_content_length; }
85 void reset();
87 std::map<std::string, std::string> const& headers() const { return m_header; }
89 private:
90 int m_recv_pos;
91 int m_status_code;
92 std::string m_method;
93 std::string m_path;
94 std::string m_protocol;
95 std::string m_server_message;
97 size_type m_content_length;
99 enum { read_status, read_header, read_body, error_state } m_state;
101 std::map<std::string, std::string> m_header;
102 buffer::const_interval m_recv_buffer;
103 int m_body_start_pos;
105 bool m_finished;
110 #endif // TORRENT_HTTP_PARSER_HPP_INCLUDED