-
Notifications
You must be signed in to change notification settings - Fork 0
/
http1response.cpp
55 lines (44 loc) · 1.54 KB
/
http1response.cpp
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
#include "http1response.h"
#include "http1header.h"
#include <iostream>
#include <iterator>
std::string HTTP1_Response::make_response(std::vector<std::string> &header, std::vector<std::string> &message_body) {
std::string server_response;
int header_size = header.size();
int body_size = message_body.size();
std::string tmp;
for (int i = 0; i < header_size; i++){
server_response.append(header[i].c_str());
}
for (int i = 0; i < body_size; i++){
server_response.append(message_body[i].c_str());
}
return server_response;
}
std::vector<std::string>& HTTP1_Response::make_header(int version, int body_length, int is_file_exist, std::string path) {
if (version == 1 && path != "") {
return HTTP1_Header::make_response302(path);
}
else if (version == 2) {
return HTTP1_Header::make_responseUpgrade();
}
else if (is_file_exist == 1) {
return HTTP1_Header::make_response404();
}
else {
return HTTP1_Header::make_response200(body_length);
}
static std::vector<std::string> response_header;
return response_header;
}
void HTTP1_Response::make_body (std::vector<std::string> &body_content, int &body_content_length, std::ifstream &output_file){
if (output_file.fail() != 0) {
//std::cout << "File was not found." << std::endl;
return;
}
char read_file_buf[BUF_SIZE];
output_file.read(read_file_buf, BUF_SIZE);
body_content.push_back(read_file_buf);
body_content_length = output_file.gcount();
return;
}