1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
//
// request_parser.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "request_parser.hpp"
#include <algorithm>
#include <cctype>
#include <boost/lexical_cast.hpp>
#include "request.hpp"
namespace http {
namespace server4 {
// Enable the pseudo-keywords reenter, yield and fork.
#include <boost/asio/yield.hpp>
std::string request_parser::content_length_name_ = "Content-Length";
boost::tribool request_parser::consume(request& req, char c)
{
reenter (this)
{
req.method.clear();
req.uri.clear();
req.http_version_major = 0;
req.http_version_minor = 0;
req.headers.clear();
req.content.clear();
content_length_ = 0;
// Request method.
while (is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != ' ')
{
req.method.push_back(c);
yield return boost::indeterminate;
}
if (req.method.empty())
return false;
// Space.
if (c != ' ') return false;
yield return boost::indeterminate;
// URI.
while (!is_ctl(c) && c != ' ')
{
req.uri.push_back(c);
yield return boost::indeterminate;
}
if (req.uri.empty()) return false;
// Space.
if (c != ' ') return false;
yield return boost::indeterminate;
// HTTP protocol identifier.
if (c != 'H') return false;
yield return boost::indeterminate;
if (c != 'T') return false;
yield return boost::indeterminate;
if (c != 'T') return false;
yield return boost::indeterminate;
if (c != 'P') return false;
yield return boost::indeterminate;
// Slash.
if (c != '/') return false;
yield return boost::indeterminate;
// Major version number.
if (!is_digit(c)) return false;
while (is_digit(c))
{
req.http_version_major = req.http_version_major * 10 + c - '0';
yield return boost::indeterminate;
}
// Dot.
if (c != '.') return false;
yield return boost::indeterminate;
// Minor version number.
if (!is_digit(c)) return false;
while (is_digit(c))
{
req.http_version_minor = req.http_version_minor * 10 + c - '0';
yield return boost::indeterminate;
}
// CRLF.
if (c != '\r') return false;
yield return boost::indeterminate;
if (c != '\n') return false;
yield return boost::indeterminate;
// Headers.
while ((is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != '\r')
|| (c == ' ' || c == '\t'))
{
if (c == ' ' || c == '\t')
{
// Leading whitespace. Must be continuation of previous header's value.
if (req.headers.empty()) return false;
while (c == ' ' || c == '\t')
yield return boost::indeterminate;
}
else
{
// Start the next header.
req.headers.push_back(header());
// Header name.
while (is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != ':')
{
req.headers.back().name.push_back(c);
yield return boost::indeterminate;
}
// Colon and space separates the header name from the header value.
if (c != ':') return false;
yield return boost::indeterminate;
if (c != ' ') return false;
yield return boost::indeterminate;
}
// Header value.
while (is_char(c) && !is_ctl(c) && c != '\r')
{
req.headers.back().value.push_back(c);
yield return boost::indeterminate;
}
// CRLF.
if (c != '\r') return false;
yield return boost::indeterminate;
if (c != '\n') return false;
yield return boost::indeterminate;
}
// CRLF.
if (c != '\r') return false;
yield return boost::indeterminate;
if (c != '\n') return false;
// Check for optional Content-Length header.
for (std::size_t i = 0; i < req.headers.size(); ++i)
{
if (headers_equal(req.headers[i].name, content_length_name_))
{
try
{
content_length_ =
boost::lexical_cast<std::size_t>(req.headers[i].value);
}
catch (boost::bad_lexical_cast&)
{
return false;
}
}
}
// Content.
while (req.content.size() < content_length_)
{
yield return boost::indeterminate;
req.content.push_back(c);
}
}
return true;
}
// Disable the pseudo-keywords reenter, yield and fork.
#include <boost/asio/unyield.hpp>
bool request_parser::is_char(int c)
{
return c >= 0 && c <= 127;
}
bool request_parser::is_ctl(int c)
{
return (c >= 0 && c <= 31) || (c == 127);
}
bool request_parser::is_tspecial(int c)
{
switch (c)
{
case '(': case ')': case '<': case '>': case '@':
case ',': case ';': case ':': case '\\': case '"':
case '/': case '[': case ']': case '?': case '=':
case '{': case '}': case ' ': case '\t':
return true;
default:
return false;
}
}
bool request_parser::is_digit(int c)
{
return c >= '0' && c <= '9';
}
bool request_parser::tolower_compare(char a, char b)
{
return std::tolower(a) == std::tolower(b);
}
bool request_parser::headers_equal(const std::string& a, const std::string& b)
{
if (a.length() != b.length())
return false;
return std::equal(a.begin(), a.end(), b.begin(),
&request_parser::tolower_compare);
}
} // namespace server4
} // namespace http
|