-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferHTTP.h
More file actions
54 lines (44 loc) · 1.78 KB
/
BufferHTTP.h
File metadata and controls
54 lines (44 loc) · 1.78 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
#ifndef BUFFERHTTP_H
#define BUFFERHTTP_H
#include <iosfwd>
#include <cstdio>
#include <sstream>
#include "server_errors.h"
#include "definitions.h"
#include "SocketTCP.h"
class BufferHTTP {
public:
BufferHTTP() : http_buffer_ss(std::string()) {}
void clear_buffer() {
http_buffer_ss.str(std::string());
http_buffer_ss.clear();
}
// funkcja pozwalająca pobierać komunikaty wygodnie na poziomie HTTP, a nie TCP
// @throws msg_too_long, std::system_error z soket_read()
std::string get_line(SocketTCP & msg_socket, char* tcp_buffer) {
std::stringstream prefix;
prefix.str("");
std::string read = std::string(); // jest nadpisywany kolejnymi wynikami getline()
http_buffer_ss.clear();
getline(http_buffer_ss, read, '\n');
prefix << read; // dodajemy do prefiksu wynik ostatniego getline, który nie skończył się na "\n"
while (!http_buffer_ss.good()) {
http_buffer_ss.clear();
http_buffer_ss.str(""); // czyścimy bo już wszystko z niego przeczytaliśmy
ssize_t len = msg_socket.socket_read(tcp_buffer, BUFFER_SIZE); // BUFFER_SIZE jest o jeden mniejsze niż faktycznyu rozmiar bufora TCP
tcp_buffer[len] = '\0';
http_buffer_ss << tcp_buffer;
getline(http_buffer_ss, read, '\n');
prefix << read; // dodajemy do prefiksu wynik ostatniego getline, który nie skończył się na "\n"
prefix.seekg(0, std::ios::end);
long size = prefix.tellg();
if (size > MAX_STR_LEN)
throw error400();
}
// jeśli ostatni getline() skończył się '\n' to wyjdzie z pętli
return prefix.str();
}
private:
std::stringstream http_buffer_ss;
};
#endif //BUFFERHTTP_H