Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // request_parser.hpp |
| 22 | // ~~~~~~~~~~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #ifndef HTTP_REQUEST_PARSER_HPP |
| 31 | #define HTTP_REQUEST_PARSER_HPP |
| 32 | |
| 33 | #include <boost/logic/tribool.hpp> |
| 34 | #include <boost/tuple/tuple.hpp> |
| 35 | |
| 36 | namespace http { |
| 37 | namespace server { |
| 38 | |
| 39 | struct request; |
| 40 | |
| 41 | /// Parser for incoming requests. |
| 42 | class request_parser |
| 43 | { |
| 44 | public: |
| 45 | /// Construct ready to parse the request method. |
| 46 | request_parser(); |
| 47 | |
| 48 | /// Reset to initial parser state. |
| 49 | void reset(); |
| 50 | |
| 51 | /// Parse some data. The tribool return value is true when a complete request |
| 52 | /// has been parsed, false if the data is invalid, indeterminate when more |
| 53 | /// data is required. The InputIterator return value indicates how much of the |
| 54 | /// input has been consumed. |
| 55 | template <typename InputIterator> |
| 56 | boost::tuple<boost::tribool, InputIterator> parse(request& req, |
| 57 | InputIterator begin, InputIterator end) |
| 58 | { |
| 59 | while (begin != end) |
| 60 | { |
| 61 | boost::tribool result = consume(req, *begin++); |
| 62 | if (result || !result) |
| 63 | return boost::make_tuple(result, begin); |
| 64 | } |
| 65 | boost::tribool result = boost::indeterminate; |
| 66 | return boost::make_tuple(result, begin); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | /// Handle the next character of input. |
| 71 | boost::tribool consume(request& req, char input); |
| 72 | |
| 73 | /// Check if a byte is an HTTP character. |
| 74 | static bool is_char(int c); |
| 75 | |
| 76 | /// Check if a byte is an HTTP control character. |
| 77 | static bool is_ctl(int c); |
| 78 | |
| 79 | /// Check if a byte is defined as an HTTP tspecial character. |
| 80 | static bool is_tspecial(int c); |
| 81 | |
| 82 | /// Check if a byte is a digit. |
| 83 | static bool is_digit(int c); |
| 84 | |
| 85 | /// The current state of the parser. |
| 86 | enum state |
| 87 | { |
| 88 | method_start, |
| 89 | method, |
| 90 | uri, |
| 91 | http_version_h, |
| 92 | http_version_t_1, |
| 93 | http_version_t_2, |
| 94 | http_version_p, |
| 95 | http_version_slash, |
| 96 | http_version_major_start, |
| 97 | http_version_major, |
| 98 | http_version_minor_start, |
| 99 | http_version_minor, |
| 100 | expecting_newline_1, |
| 101 | header_line_start, |
| 102 | header_lws, |
| 103 | header_name, |
| 104 | space_before_header_value, |
| 105 | header_value, |
| 106 | expecting_newline_2, |
| 107 | expecting_newline_3 |
| 108 | } state_; |
| 109 | }; |
| 110 | |
| 111 | } // namespace server |
| 112 | } // namespace http |
| 113 | |
| 114 | #endif // HTTP_REQUEST_PARSER_HPP |