-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebResponse.cpp
More file actions
70 lines (52 loc) · 1.66 KB
/
WebResponse.cpp
File metadata and controls
70 lines (52 loc) · 1.66 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by Lynx on 2025-05-13.
//
#include "WebResponse.h"
#include <iostream>
#include <set>
#include <sstream>
#include <windows.h>
std::string trim(const std::string& s) {
auto start = s.find_first_not_of(" \t");
auto end = s.find_last_not_of(" \t");
return (start == std::string::npos) ? "" : s.substr(start, end - start + 1);
}
std::pair<std::string, std::string> parseHeader(const std::string& input) { // parse headers formatted like 'Name: Value'
auto pos = input.find(':');
if (pos == std::string::npos) return {trim(input), ""};
return {trim(input.substr(0, pos)), trim(input.substr(pos + 1))};
}
int WebResponse::_processHeaders() {
std::string result;
std::istringstream iss(raw);
std::cout << raw;
int i = 0;
for (std::string line; std::getline(iss, line); i++) {
if (i == 0) continue;
if (line == "\r") { // stop of headers
return i
}
auto [headerName, headerValue] = parseHeader(line);
headers[headerName] = headerValue;
}
}
void WebResponse::_processStatus() {
std::istringstream iss(raw.substr(0, raw.find('\n')));
std::string version, code;
if (!(iss >> version >> code)) {
_error = "Failed to read the status line.";
return;
}
if (version.find('/') == std::string::npos) {
_error = "Could not parse HTTP version.";
return;
}
httpVersion = version.substr(version.find('/') + 1);
statusCode = std::stoi(code);
std::getline(iss >> std::ws, statusMessage);
}
void WebResponse::_process() {
_processStatus();
int textStartLineNo = _processHeaders();
isGood = statusCode < 400;
}