-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpParser.h
More file actions
54 lines (45 loc) · 1.36 KB
/
HttpParser.h
File metadata and controls
54 lines (45 loc) · 1.36 KB
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
#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H
#include <string>
#include <sstream>
#include <unordered_map>
#include <iostream>
struct HttpRequest {
std::string method;
std::string path;
std::string version;
std::unordered_map<std::string, std::string> headers;
std::string body;
};
class HttpParser {
public:
static HttpRequest parse(const std::string& raw_request) {
HttpRequest req;
std::istringstream stream(raw_request);
std::string line;
// 1. Request Line
if (std::getline(stream, line)) {
std::istringstream line_stream(line);
line_stream >> req.method >> req.path >> req.version;
}
// 2. Headers
while (std::getline(stream, line) && line != "\r") {
if (line.empty()) break;
if (line.back() == '\r') line.pop_back();
auto delimiterPos = line.find(": ");
if (delimiterPos != std::string::npos) {
std::string key = line.substr(0, delimiterPos);
std::string value = line.substr(delimiterPos + 2);
req.headers[key] = value;
}
}
// 3. Body
std::string body_content;
while (std::getline(stream, line)) {
body_content += line + "\n";
}
req.body = body_content;
return req;
}
};
#endif