forked from obsproject/obs-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-scheme.cpp
126 lines (101 loc) · 3.37 KB
/
browser-scheme.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
/******************************************************************************
Copyright (C) 2014 by John R. Bradley <[email protected]>
Copyright (C) 2018 by Hugh Bailey ("Jim") <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "browser-scheme.hpp"
#include "wide-string.hpp"
/* ------------------------------------------------------------------------- */
CefRefPtr<CefResourceHandler>
BrowserSchemeHandlerFactory::Create(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame>, const CefString &,
CefRefPtr<CefRequest> request)
{
if (!browser || !request)
return nullptr;
return CefRefPtr<BrowserSchemeHandler>(new BrowserSchemeHandler());
}
/* ------------------------------------------------------------------------- */
bool BrowserSchemeHandler::ProcessRequest(CefRefPtr<CefRequest> request,
CefRefPtr<CefCallback> callback)
{
CefURLParts parts;
CefParseURL(request->GetURL(), parts);
std::string path = CefString(&parts.path);
path = CefURIDecode(path, true, cef_uri_unescape_rule_t::UU_SPACES);
path = CefURIDecode(
path, true,
cef_uri_unescape_rule_t::
UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS);
#ifdef WIN32
inputStream.open(to_wide(path.erase(0, 1)), std::ifstream::binary);
#else
inputStream.open(path, std::ifstream::binary);
#endif
/* Set fileName for use in GetResponseHeaders */
fileName = path;
if (!inputStream.is_open()) {
return false;
}
inputStream.seekg(0, std::ifstream::end);
length = remaining = inputStream.tellg();
inputStream.seekg(0, std::ifstream::beg);
callback->Continue();
return true;
}
void BrowserSchemeHandler::GetResponseHeaders(CefRefPtr<CefResponse> response,
int64 &response_length,
CefString &redirectUrl)
{
if (!response) {
response_length = -1;
redirectUrl = "";
return;
}
std::string fileExtension =
fileName.substr(fileName.find_last_of(".") + 1);
for (char &ch : fileExtension)
ch = (char)tolower(ch);
if (fileExtension.compare("woff2") == 0)
fileExtension = "woff";
response->SetStatus(200);
response->SetMimeType(CefGetMimeType(fileExtension));
response->SetStatusText("OK");
response_length = length;
redirectUrl = "";
}
bool BrowserSchemeHandler::ReadResponse(void *data_out, int bytes_to_read,
int &bytes_read, CefRefPtr<CefCallback>)
{
if (!data_out || !inputStream.is_open()) {
bytes_read = 0;
inputStream.close();
return false;
}
if (isComplete) {
bytes_read = 0;
return false;
}
inputStream.read((char *)data_out, bytes_to_read);
bytes_read = (int)inputStream.gcount();
remaining -= bytes_read;
if (remaining == 0) {
isComplete = true;
inputStream.close();
}
return true;
}
void BrowserSchemeHandler::Cancel()
{
if (inputStream.is_open())
inputStream.close();
}