-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxhrinterception.cpp
155 lines (118 loc) · 6.15 KB
/
xhrinterception.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdio>
#include "tools.h"
#include "httplib.h"
#include "xhrinterception.h"
#include "json.hpp"
// CefResourceRequestHandler
CefResourceRequestHandler::ReturnValue XhrInterception::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback) {
TRACE("XhrRequestResponse::OnBeforeResourceLoad: {}", request->GetURL().ToString());
client = new XhrRequestClient(callback);
url_request = CefURLRequest::Create(request, client.get(), nullptr);
return RV_CONTINUE_ASYNC;
}
XhrInterception::XhrInterception() {
}
bool XhrInterception::Open(CefRefPtr<CefRequest> request, bool& handle_request, CefRefPtr<CefCallback> callback) {
TRACE("XhrInterception::Open: {}", request->GetURL().ToString());
handle_request = true;
return true;
}
bool XhrInterception::Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback) {
DEBUG("XhrInterception::Read: {}, Size {}, Offset {}", bytes_to_read, client->download_data.length(), client->offset);
size_t size = client->download_data.length();
if (client->offset < size) {
int transfer_size = std::min(bytes_to_read, static_cast<int>(size - client->offset));
memcpy(data_out, client->download_data.c_str() + client->offset, transfer_size);
client->offset += transfer_size;
bytes_read = transfer_size;
return true;
}
return false;
}
void XhrInterception::GetResponseHeaders(CefRefPtr<CefResponse> response, int64_t &response_length, CefString &redirectUrl) {
DEBUG("XhrInterception::XhrInterception: Error1={}, Error2={}, Status1={}, Status2={}, StatusText={}",
(int)url_request->GetResponse()->GetError(), (int)url_request->GetRequestError(),
(int)url_request->GetResponse()->GetStatus(), (int)url_request->GetRequestStatus(),
url_request->GetResponse()->GetStatusText().ToString() );
if (url_request->GetResponse()->GetStatus() != 200) {
response->SetStatus(url_request->GetResponse()->GetStatus());
response->SetStatusText(url_request->GetResponse()->GetStatusText());
response_length = 0;
return;
}
CefResponse::HeaderMap responseHeader;
url_request->GetResponse()->GetHeaderMap(responseHeader);
for (auto itr = responseHeader.begin(); itr != responseHeader.end(); ++itr) {
TRACE("ResponseHeader: {} -> {}", itr->first.ToString(), itr->second.ToString());
}
CefRequest::HeaderMap requestHeader;
url_request->GetRequest()->GetHeaderMap(requestHeader);
for (auto itr = requestHeader.begin(); itr != requestHeader.end(); ++itr) {
TRACE("RequestHeader: {} -> {}", itr->first.ToString(), itr->second.ToString());
}
response->SetStatus(200);
response->SetStatusText("OK");
response->SetHeaderMap(responseHeader);
response_length = client->download_total;
}
void XhrInterception::Cancel() {
}
void XhrInterception::OnResourceLoadComplete(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response,
CefResourceRequestHandler::URLRequestStatus status,
int64_t received_content_length) {
DEBUG("XhrInterception::OnResourceLoadComplete: {} -> {}", (int)status, received_content_length);
CefResourceRequestHandler::OnResourceLoadComplete(browser, frame, request, response, status, received_content_length);
}
XhrRequestClient::XhrRequestClient(CefRefPtr<CefCallback>& resourceCallback) : upload_total(0), download_total(0), offset(0), callback(resourceCallback) {
}
void XhrRequestClient::OnRequestComplete(CefRefPtr<CefURLRequest> request) {
TRACE("XhrRequestClient::OnRequestComplete: {}, {}, {}", (int) request->GetRequestStatus(),
(int) request->GetRequestError(), request->GetResponse()->GetMimeType().ToString());
request->GetResponse()->GetHeaderMap(headerMap);
// hbbtv.zdf.de
if (request->GetRequest()->GetURL().ToString().find("-hbbtv.zdf.de/ds/configuration") != std::string::npos) {
auto dataJson = nlohmann::ordered_json::parse(download_data);
if (dataJson.find("dash") != dataJson.end()) {
dataJson["dash"] = false;
}
if (dataJson.find("dashJsInHbbtv") != dataJson.end()) {
// dataJson["dashJsInHbbtv"] = true;
}
download_data = dataJson.dump();
} else if (request->GetRequest()->GetURL().ToString().find("hbbtv.zdf.de") != std::string::npos) {
auto dataJson = nlohmann::ordered_json::parse(download_data);
// Version 1
if (dataJson.find("data") != dataJson.end()) {
dataJson["data"].erase("ageControl");
}
// Version 2
if (dataJson["fsk"]["age"] != nullptr) {
dataJson["fsk"].erase("age");
} if (dataJson["fsk"] != nullptr) {
dataJson.erase("fsk");
}
// Version 3
if (dataJson.find("videoMetaData") != dataJson.end()) {
dataJson["videoMetaData"].erase("fsk");
}
download_data = dataJson.dump();
}
callback->Continue();
}
void XhrRequestClient::OnUploadProgress(CefRefPtr<CefURLRequest> request, int64_t current, int64_t total) {
upload_total = total;
}
void XhrRequestClient::OnDownloadProgress(CefRefPtr<CefURLRequest> request, int64_t current, int64_t total) {
download_total = total;
}
void XhrRequestClient::OnDownloadData(CefRefPtr<CefURLRequest> request, const void *data, size_t data_length) {
DEBUG("XhrRequestClient::OnDownloadData: {}: {} -> {}", request->GetRequest()->GetURL().ToString(), data_length, download_data.length());
std::string downloadChunk = std::string(static_cast<const char*>(data), data_length);
download_data += downloadChunk;
}
bool XhrRequestClient::GetAuthCredentials(bool isProxy, const CefString &host, int port, const CefString &realm,
const CefString &scheme, CefRefPtr<CefAuthCallback> callback) {
TRACE("XhrRequestClient::GetAuthCredentials: {}, {}", host.ToString(), port);
return false;
}