-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchunked.cpp
More file actions
58 lines (54 loc) · 2.01 KB
/
chunked.cpp
File metadata and controls
58 lines (54 loc) · 2.01 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* chunked.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarafi <akarafi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/06 20:27:39 by akarafi #+# #+# */
/* Updated: 2022/09/07 22:20:17 by akarafi ### ########.fr */
/* */
/* ************************************************************************** */
#include <string>
#include <sstream>
#include <iostream>
std::string decode_body(std::string const &encoded_body) {
std::string body = "";
std::stringstream sin(encoded_body);
std::string line;
while (std::getline(sin, line)) {
size_t size;
try {
size = std::stoi(line, 0, 16);
}
catch (std::exception &e) {
size = 0;
}
if (size == 0) {
break ;
}
std::string buff = "";
while (buff.size() < size) {
std::getline(sin, line);
buff += line + '\n';
}
buff.resize(size);
body += buff;
}
return body;
}
std::string encode_body(std::string const &body) {
std::string encoded_body = "";
std::stringstream sin(body);
std::string line;
while (std::getline(sin, line)) {
if (line[line.size() - 1] != '\r') line += '\r';
line += '\n';
std::stringstream size;
size << std::hex << line.size();
encoded_body += size.str() + "\r\n";
encoded_body += line + "\r\n";
}
encoded_body += "0\r\n\r\n";
return encoded_body;
}