-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_requests.cpp
More file actions
55 lines (46 loc) · 1.72 KB
/
client_requests.cpp
File metadata and controls
55 lines (46 loc) · 1.72 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
//
// Created by heshamovic on 11/16/18.
//
#include "client_requests.h"
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
void request::GET(const std::string file_name, const std::string http_version, const std::string user_agent,
const std::string host, bool keep_alive, const std::string type, client::net::socketstream &ss) {
std::stringstream iss;
iss << "GET " << file_name << " " << http_version << "\r\n";
iss << "User-Agent: " << user_agent << "\r\n";
iss << "Host: " << host << "\r\n";
iss << "Accept-Language: en-us\r\n";
if (keep_alive)
iss << "Connection: keep-alive" << "\r\n";
else
iss << "Connection: close" << "\r\n";
iss << "Accept: " << type << "\r\n";
iss << "\r\n";
ss << iss.str() << std::flush;
}
void request::POST(const std::string file_name, const std::string http_version, const std::string user_agent,
const std::string host, bool keep_alive, client::net::socketstream &ss) {
std::stringstream iss;
std::ifstream inFile(file_name);
if (!inFile) {
std::cerr << "Unable to open file: " << file_name;
return;
}
std::string data((std::istreambuf_iterator<char>(inFile)),
std::istreambuf_iterator<char>());
iss << "POST " << file_name << " " << http_version << "\r\n";
iss << "Host: " << host << "\r\n";
if (keep_alive)
iss << "Connection: keep-alive" << "\r\n";
else
iss << "Connection: close" << "\r\n";
iss << "Content-Type: application/x-www-form-urlencoded" << "\r\n";
iss << "Content-Length: " << data.length() << "\r\n";
iss << "\r\n";
iss << data;
ss << iss.str() << std::flush;
inFile.close();
}