1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "nweb_handler_impl_test_for_intercept.h"
17
18 #include <cstdio>
19 #include <cstring>
20 #include <fstream>
21 #include <string>
22 #include <sstream>
23 #include <unistd.h>
24 #include "nweb_test_log.h"
25 #include "nweb_url_resource_request.h"
26 #include "nweb_url_resource_response.h"
27 #include "securec.h"
28
29 namespace OHOS::NWeb {
30 std::string g_data;
SetNWeb(std::shared_ptr<NWeb> nweb)31 void NWebHandlerImplTestForIntercept::SetNWeb(std::shared_ptr<NWeb> nweb)
32 {
33 nwebweak_ = nweb;
34 }
35
ReadFileToString(const char * path,std::string & data)36 bool ReadFileToString(const char* path, std::string& data)
37 {
38 TESTLOG_I(" ReadFileToString-1111%{public}s", path);
39 // Implementation adapted from base/file_util.cc
40 FILE* file = fopen(path, "rb");
41 if (!file) {
42 return false;
43 }
44 char buf[1 << 16];
45 size_t len;
46 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
47 data.append(buf, len);
48 }
49 int ret = fclose(file);
50 if (ret < 0) {
51 TESTLOG_I("fclose error");
52 }
53 return true;
54 }
55
GetResourceDir(std::string & dir)56 bool GetResourceDir(std::string& dir)
57 {
58 char buff[1024]; // 1024 size
59 ssize_t len = readlink("/proc/self/exe", buff, sizeof(buff) - 1);
60 if (len == -1) {
61 return false;
62 }
63
64 buff[len] = 0;
65 char* pos = strrchr(buff, '/');
66 if (!pos) {
67 return false;
68 }
69
70 if (strcpy_s(pos + 1, len, "files") != 0) {
71 TESTLOG_I(" strcpy_s error");
72 return false;
73 }
74 dir = std::string(buff);
75 return true;
76 }
77
LoadBinaryResource(const char * resource_name,std::string & resource_data)78 bool LoadBinaryResource(const char* resource_name, std::string& resource_data)
79 {
80 std::string path;
81 if (!GetResourceDir(path)) {
82 return false;
83 }
84 path.append("/");
85 path.append(resource_name);
86 return ReadFileToString(path.c_str(), resource_data);
87 }
88
OnHandleInterceptRequest(std::shared_ptr<NWebUrlResourceRequest> request)89 std::shared_ptr<NWebUrlResourceResponse> NWebHandlerImplTestForIntercept::OnHandleInterceptRequest(
90 std::shared_ptr<NWebUrlResourceRequest> request)
91 {
92 std::string url = request->Url();
93 if (strstr(url.c_str(), ".png") != nullptr) {
94 // system/bin/files
95 LoadBinaryResource("2.png", g_data); // define 2.png name image
96 std::shared_ptr<NWebUrlResourceResponse> response =
97 std::make_shared<NWebUrlResourceResponse>("text/html", "UTF-8", g_data);
98 return response;
99 }
100 return nullptr;
101 }
102 } // namespace OHOS::NWeb