我有一个网站,用户可以在该网站上撰写博客文章。我正在使用 stackoverflow pagedown editor
允许用户通过插入链接来添加内容和图像。
但问题在于,如果用户插入以 http://
开头的链接,例如 http://example.com/image.jpg
,浏览器显示警告说,
Your Connection to this site is not Fully Secure.
Attackers might be able to see the images you are looking at
& trick you by modifying them
我想知道我们如何强制浏览器仅使用插入图像的站点的 https://
版本,尤其是当用户插入以 http 开头的链接时://
?
或者这个问题还有其他解决方案吗?
最佳答案
不幸的是,浏览器希望通过 ssl 提供所有 加载的资源。在您的情况下,您别无选择,只能自行存储所有图像或创建或代理从 http 到 https 的请求。但我不确定这样做是否真的安全。
例如你可以这样做:
我假设代码是 php,并通过 https
<?php
define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
// Read a file and display its content chunk by chunk
function readfile_chunked($filename, $retbytes = TRUE) {
$buffer = '';
$cnt = 0;
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$filename = 'http://domain.ltd/path/to/image.jpeg';
$mimetype = 'image/jpeg';
header('Content-Type: '.$mimetype );
readfile_chunked($filename);
_ 更新 1 _
Alternate solution to proxify steamed downloaded file in Python
_ 更新 2 _
在下面的代码中,您可以将数据从远程服务器流式传输到您的前端客户端,如果您的 Django 应用程序是通过 https 传输的,则内容将被正确传送。
目标是以 1024 位为一组读取您的原始图像,它们将每个组流式传输到您的浏览器。当您尝试加载大图像时,此方法可避免超时问题。
我建议您添加另一层以拥有本地缓存,而不是在每个请求上下载 -> 代理。
import requests
# have this function in file where you keep your util functions
def url2yield(url, chunksize=1024):
s = requests.Session()
# Note: here i enabled the streaming
response = s.get(url, stream=True)
chunk = True
while chunk :
chunk = response.raw.read(chunksize)
if not chunk:
break
yield chunk
# Then creation your view using StreamingHttpResponse
def get_image(request, img_id):
img_url = "domain.ltd/lorem.jpg"
return StreamingHttpResponse(url2yield(img_url), content_type="image/jpeg")
关于http - 与此站点的连接不完全安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851404/