-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentry.cpp
More file actions
129 lines (101 loc) · 3.32 KB
/
entry.cpp
File metadata and controls
129 lines (101 loc) · 3.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "entry.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include "httpheader.hpp"
SimpleCacheEntry::SimpleCacheEntry(const std::string &__path) : __path(__path) {
this->get_key();
}
std::string SimpleCacheEntry::get_key() const {
std::ifstream ifs{this->__path};
SimpleFileHeader h;
ifs.read((char *)&h, sizeof(SimpleFileHeader));
// std::cout << this->__pah << std::endl;
std::string key;
key.resize(h.key_length);
ifs.read(&key[0], h.key_length);
// std::cout << key << std::endl;
ifs.close();
return key;
}
HttpHeader SimpleCacheEntry::get_header() const {
// read from back
std::ifstream ifs{this->__path};
// stream 0 eof
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::end);
SimpleFileEOF eof0;
ifs.read((char *)&eof0, sizeof(SimpleFileEOF));
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::end);
if (eof0.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
// skip sha256
ifs.seekg(-32, std::ios::cur);
}
// stream 0 data
ifs.seekg(-(long)eof0.stream_size, std::ios::cur);
SimpleMeta meta;
// std::streampos stream_0_start = ifs.tellg();
ifs.read((char *)&meta, sizeof(SimpleMeta));
std::string buf(meta.header_size, '\0');
ifs.read(&buf[0], meta.header_size);
// ifs.seekg(stream_0_start);
ifs.close();
// parse header
HttpHeader h;
size_t start = 0;
while (buf[start] != '\0') {
size_t end = buf.find_first_of('\0', start);
if (start == 0) {
h.status_source = buf.substr(start, end - start + 1);
} else {
size_t coron = buf.find_first_of(':', start);
auto key = buf.substr(start, coron - start);
std::transform(key.begin(), key.end(), key.begin(), [](char c) {
return std::tolower(c);
});
auto value = buf.substr(coron + 1, end - 1 - coron);
h.headers[key] = value;
}
start = end + 1;
}
return h;
}
std::unique_ptr<std::vector<char>> SimpleCacheEntry::get_data() const {
// read from back
std::ifstream ifs{this->__path};
// stream 0 eof
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::end);
SimpleFileEOF eof0;
ifs.read((char *)&eof0, sizeof(SimpleFileEOF));
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::end);
if (eof0.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) {
// skip sha256
ifs.seekg(-32, std::ios::cur);
}
// stream 0 data
ifs.seekg(-(long)eof0.stream_size, std::ios::cur);
// stream 1 eof
SimpleFileEOF eof1;
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::cur);
ifs.read((char *)&eof1, sizeof(SimpleFileEOF));
ifs.seekg(-sizeof(SimpleFileEOF), std::ios::cur);
// stream 1 data
ifs.seekg(-(long)eof1.stream_size, std::ios::cur);
auto ptr = std::unique_ptr<std::vector<char>>(new std::vector<char>(eof1.stream_size));
if (eof1.stream_size > 0) {
ifs.read(&ptr->at(0), eof1.stream_size);
}
ifs.close();
return ptr;
}
bool SimpleCacheEntry::save(const std::string &__path) const {
auto ptr = this->get_data();
if (ptr->size() > 0) {
std::ofstream ofs{__path, std::ios::binary};
ofs.write(&ptr->at(0), ptr->size());
ofs.close();
return true;
} else {
return false;
}
}