summaryrefslogtreecommitdiffstats
path: root/BrowserSource/Proxy/HTTPParser.h
blob: e97f8964eba8e5ee368ed7023920a732ae131376 (plain)
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
#pragma once

#include <string>
#include <string_view>
#include <map>

namespace WebServer {

	// A simple HTTP/1.1 message parser based on RFC 2616.
	class HTTPParser
	{
	public:
		HTTPParser();

		bool Parse(const std::string& raw_http, std::string& err);

		const std::string& GetMethod() const;
		const std::string& GetPath() const;
		bool GetHeader(const std::string& header, std::string& value) const;
		const std::map<std::string, std::string>& GetHeaders() const;
		const std::string& GetPayload() const;

	private:
		enum ParserState {
			PARSER_STATE_START_LINE,
			PARSER_STATE_HEADERS,
			PARSER_STATE_PAYLOAD,
		};

		bool ParseSegment(
			const std::string_view segment,
			ParserState& state,
			std::string& err);
		bool ParseStartLine(
			const std::string_view segment,
			ParserState& state,
			std::string& err);
		bool ParseHeaders(
			const std::string_view segment,
			ParserState& state,
			std::string& err);
		bool ParsePayload(
			const std::string_view segment,
			ParserState& state,
			std::string& err);

		std::string method_;
		std::string path_;
		std::map<std::string, std::string> headers_;
		std::string payload_;
	};
}