2 * ______________________________________
4 * | | \ | |/ |/ | Date: 06/24/2022 |
5 * | | \| |- |- | Author: Levi Hicks |
8 * | |_| \_||_||_| File: Request.cpp |
11 * | Please do not remove this header. |
12 * |______________________________________|
17 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <arpa/inet.h>
34 #include <bits/stdc++.h>
35 #include "../Includes/Link/Request.hpp"
36 #include "../Includes/Link/HTTPTools.hpp"
38 Request::Request(int sock
, sockaddr_in
* addr
, std::string path
, std::string method
, std::string request
, std::map
<std::string
, std::string
> queries
) {
42 this->method
= method
;
43 this->request
= request
;
44 this->queries
= queries
;
47 std::istringstream
stream(decodeHTTP(request
));
48 while(getline(stream
, header
)) {
49 if (header
.find(":") != std::string::npos
&& header
.find_first_of(":")+2 < header
.length()) {
50 std::string key
= header
.substr(0, header
.find_first_of(":"));
51 std::transform(key
.begin(), key
.end(), key
.begin(), [](unsigned char c
) { return std::tolower(c
); });
52 std::string value
= header
.substr(header
.find_first_of(":")+2);
53 this->headers
[key
] = sanitize(value
);
54 } else this->body
+= header
+ "\n";
57 if (this->headers
["connection"] == "keep-alive") {
58 int time
= 7200, interval
= 60, retry
= 3, opt
= 1;
59 if (setsockopt(this->sock
, SOL_SOCKET
, SO_KEEPALIVE
, &opt
, sizeof(opt
)) < 0 || setsockopt(this->sock
, IPPROTO_TCP
, TCP_KEEPIDLE
, &time
, sizeof(time
)) < 0 ||
60 setsockopt(this->sock
, IPPROTO_TCP
, TCP_KEEPINTVL
, &interval
, sizeof(interval
)) < 0 || setsockopt(this->sock
, IPPROTO_TCP
, TCP_KEEPCNT
, &retry
, sizeof(retry
)) < 0) {
64 if (this->body
.size()>0) this->body
= this->body
.substr(2, this->body
.length()-3);
65 if (this->method
== "POST" && headers
["content-type"].find("application/x-www-form-urlencoded") != std::string::npos
) {
66 std::istringstream
stream(this->body
);
67 std::string parameter
;
68 while(getline(stream
, parameter
, '&')) {
69 std::string key
= parameter
.substr(0, parameter
.find_first_of("="));
70 std::string value
= parameter
.substr(parameter
.find_first_of("=")+1);
71 this->params
[key
] = sanitize(value
);
76 std::string
Request::GetPath() {
80 int Request::GetSocket() {
84 std::string
Request::GetMethod() {
88 std::string
Request::GetRequest() {
92 std::string
Request::GetHeader(std::string header
) {
93 return this->headers
[header
];
96 std::string
Request::GetQuery(std::string query
) {
97 return this->queries
[query
];
100 std::string
Request::GetBody() {
104 std::string
Request::GetFormParam(std::string param
) {
105 return this->params
[param
];
108 struct sockaddr_in
* Request::GetAddress() {