blob: 9d16713d336593bcf37669f68f84fadc29449ae5 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- 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 Zhua9a7e1d2013-02-25 18:29:07 -080020//
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
36namespace http {
37namespace server {
38
39struct request;
40
41/// Parser for incoming requests.
42class request_parser
43{
44public:
45 /// Construct ready to parse the request method.
46 request_parser();
47
48 /// Reset to initial parser state.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049 void
50 reset();
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080051
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080057 boost::tuple<boost::tribool, InputIterator>
58 parse(request& req, InputIterator begin, InputIterator end)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080059 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 while (begin != end) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080061 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
69private:
70 /// Handle the next character of input.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 boost::tribool
72 consume(request& req, char input);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080073
74 /// Check if a byte is an HTTP character.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080075 static bool
76 is_char(int c);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080077
78 /// Check if a byte is an HTTP control character.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 static bool
80 is_ctl(int c);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080081
82 /// Check if a byte is defined as an HTTP tspecial character.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080083 static bool
84 is_tspecial(int c);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080085
86 /// Check if a byte is a digit.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 static bool
88 is_digit(int c);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080089
90 /// The current state of the parser.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 enum state {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080092 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