-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.cpp
More file actions
73 lines (60 loc) · 1.84 KB
/
Copy pathHttp.cpp
File metadata and controls
73 lines (60 loc) · 1.84 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
71
72
73
#include "Http.h"
#include <sstream>
#include <iostream>
bool HttpRequest::parseRequest(const std::string &request)
{
std::istringstream stream(request);
std::string line;
// Parse the first line (method, path, version)
if (!std::getline(stream, line))
return false;
std::istringstream firstLine(line);
if (!(firstLine >> method >> path >> version))
return false;
// Parse headers
while (std::getline(stream, line) && !line.empty() && line != "\r")
{
size_t colonPos = line.find(':');
if (colonPos != std::string::npos)
{
std::string key = line.substr(0, colonPos);
std::string value = line.substr(colonPos + 1);
// Remove spaces
key.erase(0, key.find_first_not_of(" \t"));
key.erase(key.find_last_not_of(" \t\r") + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t\r") + 1);
headers[key] = value;
}
}
// Read request body
std::string bodyLine;
while (std::getline(stream, bodyLine))
{
body += bodyLine + "\n";
}
if (!body.empty())
{
body.pop_back(); // Remove the last \n
}
return true;
}
HttpResponse::HttpResponse(int code, const std::string &text)
: statusCode(code), statusText(text)
{
headers["Content-Type"] = "application/json";
headers["Connection"] = "close";
}
std::string HttpResponse::toString() const
{
std::ostringstream response;
response << "HTTP/1.1 " << statusCode << " " << statusText << "\r\n";
for (const auto &header : headers)
{
response << header.first << ": " << header.second << "\r\n";
}
response << "Content-Length: " << body.length() << "\r\n";
response << "\r\n";
response << body;
return response.str();
}