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. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 49 | void |
| 50 | reset(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 51 | |
| 52 | /// Parse some data. The tribool return value is true when a complete request |
| 53 | /// has been parsed, false if the data is invalid, indeterminate when more |
| 54 | /// data is required. The InputIterator return value indicates how much of the |
| 55 | /// input has been consumed. |
| 56 | template <typename InputIterator> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 57 | boost::tuple<boost::tribool, InputIterator> |
| 58 | parse(request& req, InputIterator begin, InputIterator end) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 59 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 60 | while (begin != end) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 71 | boost::tribool |
| 72 | consume(request& req, char input); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 73 | |
| 74 | /// Check if a byte is an HTTP character. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 75 | static bool |
| 76 | is_char(int c); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 77 | |
| 78 | /// Check if a byte is an HTTP control character. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 79 | static bool |
| 80 | is_ctl(int c); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 81 | |
| 82 | /// Check if a byte is defined as an HTTP tspecial character. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 83 | static bool |
| 84 | is_tspecial(int c); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 85 | |
| 86 | /// Check if a byte is a digit. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 87 | static bool |
| 88 | is_digit(int c); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 89 | |
| 90 | /// The current state of the parser. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 91 | enum state { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 92 | method_start, |
| 93 | method, |
| 94 | uri, |
| 95 | http_version_h, |
| 96 | http_version_t_1, |
| 97 | http_version_t_2, |
| 98 | http_version_p, |
| 99 | http_version_slash, |
| 100 | http_version_major_start, |
| 101 | http_version_major, |
| 102 | http_version_minor_start, |
| 103 | http_version_minor, |
| 104 | expecting_newline_1, |
| 105 | header_line_start, |
| 106 | header_lws, |
| 107 | header_name, |
| 108 | space_before_header_value, |
| 109 | header_value, |
| 110 | expecting_newline_2, |
| 111 | expecting_newline_3 |
| 112 | } state_; |
| 113 | }; |
| 114 | |
| 115 | } // namespace server |
| 116 | } // namespace http |
| 117 | |
| 118 | #endif // HTTP_REQUEST_PARSER_HPP |