Major Update: Link Library
[link.git] / Source / Request.cpp
blobcf8b3ad5ab01439531f7a50565d4b1d7bce17c9a
1 /*
2 * ______________________________________
3 * | _ _ _ _ |
4 * | | \ | |/ |/ | Date: 06/24/2022 |
5 * | | \| |- |- | Author: Levi Hicks |
6 * | | || || | |
7 * | | |\ || || | |
8 * | |_| \_||_||_| File: Request.cpp |
9 * | |
10 * | |
11 * | Please do not remove this header. |
12 * |______________________________________|
15 #include <iostream>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <filesystem>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <arpa/inet.h>
24 #include <unistd.h>
25 #include <string>
26 #include <sstream>
27 #include <fstream>
28 #include <map>
29 #include <stdexcept>
30 #include <iomanip>
31 #include <cstring>
32 #include <chrono>
33 #include <vector>
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) {
39 this->sock = sock;
40 this->addr = addr;
41 this->path = path;
42 this->method = method;
43 this->request = request;
44 this->queries = queries;
46 std::string header;
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) {
61 close(this->sock);
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() {
77 return this->path;
80 int Request::GetSocket() {
81 return this->sock;
84 std::string Request::GetMethod() {
85 return this->method;
88 std::string Request::GetRequest() {
89 return this->request;
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() {
101 return this->body;
104 std::string Request::GetFormParam(std::string param) {
105 return this->params[param];
108 struct sockaddr_in* Request::GetAddress() {
109 return this->addr;