How to redirect QWebEngineUrlRequestInfo to local files? - c++

I have a simple Qt application that loads a page in a QWebEngineView. I would like to redirect all http requests with the word "static" in the url to local files. Using WebUrlRequestInterceptor I reimplemented the interceptRequest method. Here is the code:
class MyWebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
void interceptRequest(QWebEngineUrlRequestInfo &info) {
if (info.requestUrl().toString().contains("static")) {
QString newUrl = QDir::currentPath() + info.requestUrl().toString().mid(21, info.requestUrl().toString().length());
qDebug() << "new url = " << newUrl;
info.redirect(QUrl::fromLocalFile(newUrl));
}
}
};
In main function I did
MyWebUrlRequestInterceptor *wuri = new MyWebUrlRequestInterceptor();
QWebEngineProfile::defaultProfile()->setRequestInterceptor(wuri);
And the interceptRequest seems to work fine, but I get a message.
Not allowed to load local resource
I searched online and I many people are saying that I should add the --disable-web-security flag. So i did in my .pro file:
QMAKE_CXXFLAGS += --disable-web-security
But it doesn't seem to be working. Am I doing something wrong? Is there a different solutions? Could I make custom protocols to serve local files and redirect to them in Qt as a workaround?
I'm using Qt 5.9.1 and QtCreator 4.3.1.

I would block the request using QWebEngineUrlRequestInfo::block and load the local file in the QWebEngineView (even read the local file on the fly and pass its content to the view through QWebEngineView::setHtml).
Or listen locally for incoming http requests, using local dir as web root, and eventually redirect to localhost when needed, which implies using an external web server or building a minimal one, maybe integrated in your app. If you go for integrating it, I would consider this one: https://github.com/cesanta/mongoose.

Related

Open a c++ application installed on computer with a custom url in browser [duplicate]

How do i set up a custom protocol handler in chrome? Something like:
myprotocol://testfile
I would need this to send a request to http://example.com?query=testfile, then send the httpresponse to my extension.
The following method registers an application to a URI Scheme. So, you can use mycustproto: in your HTML code to trigger a local application. It works on a Google Chrome Version 51.0.2704.79 m (64-bit).
I mainly used this method for printing document silently without the print dialog popping up. The result is pretty good and is a seamless solution to integrate the external application with the browser.
HTML code (simple):
Click Me
HTML code (alternative):
<input id="DealerName" />
<button id="PrintBtn"></button>
$('#PrintBtn').on('click', function(event){
event.preventDefault();
window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
URI Scheme will look like this:
You can create the URI Scheme manually in registry, or run the "mycustproto.reg" file (see below).
HKEY_CURRENT_USER\Software\Classes
mycustproto
(Default) = "URL:MyCustProto Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "myprogram.exe,1"
shell
open
command
(Default) = "C:\Program Files\MyProgram\myprogram.exe" "%1"
mycustproto.reg example:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\mycustproto]
"URL Protocol"="\"\""
#="\"URL:MyCustProto Protocol\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\DefaultIcon]
#="\"mycustproto.exe,1\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open\command]
#="\"C:\\Program Files\\MyProgram\\myprogram.exe\" \"%1\""
C# console application - myprogram.exe:
using System;
using System.Collections.Generic;
using System.Text;
namespace myprogram
{
class Program
{
static string ProcessInput(string s)
{
// TODO Verify and validate the input
// string as appropriate for your application.
return s;
}
static void Main(string[] args)
{
Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
Console.WriteLine("\n\nArguments:\n");
foreach (string s in args)
{
Console.WriteLine("\t" + ProcessInput(s));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Try to run the program first to make sure the program has been placed in the correct path:
cmd> "C:\Program Files\MyProgram\myprogram.exe" "mycustproto:Hello World"
Click the link on your HTML page:
You will see a warning window popup for the first time.
To reset the external protocol handler setting in Chrome:
If you have ever accepted the custom protocol in Chrome and would like to reset the setting, do this (currently, there is no UI in Chrome to change the setting):
Edit "Local State" this file under this path:
C:\Users\Username\AppData\Local\Google\Chrome\User Data\
or Simply go to:
%USERPROFILE%\AppData\Local\Google\Chrome\User Data\
Then, search for this string: protocol_handler
You will see the custom protocol from there.
Note: Please close your Google Chrome before editing the file. Otherwise, the change you have made will be overwritten by Chrome.
Reference:
https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
Chrome 13 now supports the navigator.registerProtocolHandler API. For example,
navigator.registerProtocolHandler(
'web+custom', 'http://example.com/rph?q=%s', 'My App');
Note that your protocol name has to start with web+, with a few exceptions for common ones (like mailto, etc). For more details, see: http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler
This question is old now, but there's been a recent update to Chrome (at least where packaged apps are concerned)...
http://developer.chrome.com/apps/manifest/url_handlers
and
https://github.com/GoogleChrome/chrome-extensions-samples/blob/e716678b67fd30a5876a552b9665e9f847d6d84b/apps/samples/url-handler/README.md
It allows you to register a handler for a URL (as long as you own it). Sadly no myprotocol:// but at least you can do http://myprotocol.mysite.com and can create a webpage there that points people to the app in the app store.
This is how I did it. Your app would need to install a few reg keys on installation, then in any browser you can just link to foo:\anythingHere.txt and it will open your app and pass it that value.
This is not my code, just something I found on the web when searching the same question. Just change all "foo" in the text below to the protocol name you want and change the path to your exe as well.
(put this in to a text file as save as foo.reg on your desktop, then double click it to install the keys)
-----Below this line goes into the .reg file (NOT including this line)------
REGEDIT4
[HKEY_CLASSES_ROOT\foo]
#="URL:foo Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\foo\shell]
[HKEY_CLASSES_ROOT\foo\shell\open]
[HKEY_CLASSES_ROOT\foo\shell\open\command]
#="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""
Not sure whether this is the right place for my answer, but as I found very few helpful threads and this was one of them, I am posting my solution here.
Problem: I wanted Linux Mint 19.2 Cinnamon to open Evolution when clicking on mailto links in Chromium. Gmail was registered as default handler in chrome://settings/handlers and I could not choose any other handler.
Solution:
Use the xdg-settings in the console
xdg-settings set default-url-scheme-handler mailto org.gnome.Evolution.desktop
Solution was found here https://alt.os.linux.ubuntu.narkive.com/U3Gy7inF/kubuntu-mailto-links-in-chrome-doesn-t-open-evolution and adapted for my case.
I've found the solution by Jun Hsieh and MuffinMan generally works when it comes to clicking links on pages in Chrome or pasting into the URL bar, but it doesn't seem to work in a specific case of passing the string on the command line.
For example, both of the following commands open a blank Chrome window which then does nothing.
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "foo://C:/test.txt"
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --new-window "foo://C:/test.txt"
For comparison, feeding Chrome an http or https URL with either of these commands causes the web page to be opened.
This became apparent because one of our customers reported that clicking links for our product from a PDF being displayed within Adobe Reader fails to invoke our product when Chrome is the default browser. (It works fine with MSIE and Firefox as default, but not when either Chrome or Edge are default.)
I'm guessing that instead of just telling Windows to invoke the URL and letting Windows figure things out, the Adobe product is finding the default browser, which is Chrome in this case, and then passing the URL on the command line.
I'd be interested if anyone knows of Chrome security or other settings which might be relevant here so that Chrome will fully handle a protocol handler, even if it's provided via the command line. I've been looking but so far haven't found anything.
I've been testing this against Chrome 88.0.4324.182.
open
C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default
open Preferences then search for excluded_schemes you will find it in 'protocol_handler' delete this excluded scheme(s) to reset chrome to open url with default application

Using a HTTP Module on a Virtual Directory in IIS

I have a default website in my IIS where I have created one virtual directory "wsdls".
I would want to gather statistics on how many requests are triggered to my virtual directory. This would need a request interception at web server level and gather statistics. "HTTPModule" was one of the many solutions I have considered which is suitable for such scenario. Hence I have started building one.
For testing purpose, I wanted to create a HTTP Module and apply it on a particular extension files (say *.wsdl) and on every GET request of any .wsdl files in this virtual directory, I will want to redirect the application to "www.google.com". This would demonstrate a good example of how HTTP Module can be used and deployed on IIS.
HTTPModule which is written using Visual Studio is shown below,
namespace Handler.App_Code
{
public class HelloWorldModule : IHttpModule
{
public HelloWorldModule(){
}
public String ModuleName{
get { return "HelloWorldModule"; }
}
// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application){
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
application.EndRequest +=
(new EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source,
EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Redirect("www.google.com");
}
private void Application_EndRequest(Object source, EventArgs e)
{
//Nothing to be done here
}
public void Dispose() { }
}
}
Now I have done a build of this project for x64 version and I am able to browser successfully the "dll" file. Now I have to register this dll in IIS and whenever I try to access the *.wsdl files, the requests automatically divert to "www.google.com". Here is the next step I have done,
Then I have enabled the Handler mappings as shown below,
I am assuming that is it!! Nothing more to be done. I should be able to intercept the requests for all HTTP requests which are of the form "*.wsdl". This means whenever I access any wsdl from the server, control should be going back to google(Because of the logic written in begin request ). But unfortunately, I failed in achieving it. What can be done here?
One thing I noticed is that when you are trying to redirect to an external URL use
http://
So change
context.Response.Redirect("www.google.com");
to
context.Response.Redirect("http://www.google.com", true);
I could solve the problem what I am facing and below are the observations which were missing in my understanding and which helped me in solving my problem:
Locating proper web.config file :
Every website in IIS will be having a web.config file to have control over the application.
Since I am working with "Default Website", this refers to the directory "C:\\inetpub\\wwwroot"
There will be a "web.config" file which would be present in this director. Please create it if not already present.
Modifying web.config :
Once you have identified the file which needs to be modified, just add necessary module configuration to web.config
In this case, we would want to add a Module to the default website, the probably setting would be shown below,
Adding contents to bin directory :
Now if you try to run the application, the IIS would not find any dll or executable to run and hence we would need to keep the executables at a particular location.
Create a director if not already present with the name "bin" at the root of the directory and place all the dlls which you would want this website to execute. Sample shown below,
General Points to be considered:
Proper access must be given for the folder which consists of dll.
It is ideally not suggested to modify the entire website. It would be ideal if one works only on their web application.
If web.config is not found, we can create one.
If bin is not present in the web root directory, we can create one.

QWebView - Error 400 when giving url

I'm trying to build a little application that needs to run the url that is generated by the script at this link: http://blogs.aws.amazon.com/security/post/Tx70F69I9G8TYG/How-to-enable-cross-account-access-to-the-AWS-Management-Console
The application is build with Qt4 and Pyqt4. I create a QWebView and want to load the url that is generated at the end of the script in the link inside the webview.
url = QUrl(ConnectionScript.generateURL())
self.webView.load(url)
self.webView.show()
but this code gives me a "HTTP Status 400 - BadRequest" error. I've tried to change the "load" with "setUrl" but there is no change.
The useful code is only this, other lines are just setting up the GUI (and it's doing fine). Any suggestion about how to fix this and what might the problem be? I think it's something very easy to fix but i can't do it right...
Edit1: i forgot to mention that when i open the generated link in a web browser (like chrome or firefox) all goes well and it gives me no such error
Found out that the problem was this line of code:
request_parameters += urllib.quote_plus("https://console.aws.amazon.com/")
The quote_plus encoded : / so the webView load couldn't process the url in the right way.
Just don't use the urllib.quote_plus method and everything will go as expected.

How to handle downloads with GTK WebKit?

I'm stuck with an issue that I'm gonna try and explain clearly. I'm using a gtkmm webkit browser in a sort of embedded application to access php/html files. The point is that I fail to code a way for this webkit to handle downloads.
Oh, and I'm coding in C++.
The file to download currently looks like below in my HTML code :
<a href='excel/template_users.php'><img src='imgs/excelbis.png'/>Download Excel File</a>
The "template_users.php" file is just a way of forcing the user to download the file (you can also access to the website via a "classic" web browser). It contains only :
<?php
header("Content-disposition: attachment; filename=template_users.xls");
header("Content-type: application/vnd.ms-excel:");
readfile("template_users.xls");
?>
I've been trying to handle downloads by adding to my IntegratedBrowser class constructor (which instantiate the webkit) a connector to the signal "download-requested" :
gboolean ret = FALSE;
g_signal_connect(webView, "download-requested", G_CALLBACK(&IntegratedBrowser::downloadRequested), &ret);
And the associated method :
gboolean IntegratedBrowser::downloadRequested(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
const gchar* dest = webkit_download_get_uri(download);
webkit_download_set_destination_uri(download, dest);
return FALSE;
}
I should precise that everything runs of course fine when I'm trying to download the file via a "classic web browser". The c++ code compile smoothly, however, nothing happens when I click on the link using the webkit. I would like the file to be downloaded in a location chosen by the user if possible (downloading the file into a specific location is viable as well) when clicking on the link.
Have you got any idea of what I am doing incorrectly ?
Thanks for your answers !
Edit : now my downloadRequested method looks like that :
gboolean IntegratedBrowser::downloadRequested(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
const gchar* dest = g_strdup_printf("%s", "file:///home/user/test.xls");
webkit_download_set_destination_uri(download, dest);
return TRUE;
}
The return FALSE from your IntegratedBrowser::downloadRequested() method cancels the download. return TRUE should start it.
Also, you are getting the URI that you are downloading with webkit_download_get_uri() (e.g. http://my.website.com/myfile.jpeg) and setting that to be the destination URI. Set the destination URI to point to a local file instead (although leaving it unset probably downloads the file to a default location.)
If you want a file selector for choosing where to save the download, use GtkFileChooser and when you are done with it, retrieve a URI using gtk_file_chooser_get_uri().

Autologin during development with Jetty

I have the usual setup: A webapp with a login screen and a small Java class which sets up Jetty to launch the app.
During development, we all waste a couple of seconds to log in after every change to the code which forces a restart. (No, JRebel doesn't help since it doesn't run the constructors again so it can miss some changes).
So I was wondering if I could patch the Jetty setup in such a way:
If I request /index.jsp, instead of going to the real JSP, it should load a servlet which fills in the username and password of the typical development user, logs him in, and then redirects to the main JSP of the app.
To make everything safe, I'll put this auto-login code into the test path, so it can't be deployed accidentally.
Now the question: How do I configure URL redirection/rewriting in Jetty from Java code? For obvious reasons, I don't want to touch web.xml.
Following the examples in , I came up with this code:
private WebAppContext webapp;
private void configureAutoLogin() {
ServletHolder holder = webapp.getServletHandler().newServletHolder();
holder.setName("autologin");
holder.setClassName( AutoLoginServlet.class.getName() );
webapp.getServletHandler().addServlet(holder);
ServletMapping mapping = new ServletMapping();
mapping.setServletName(holder.getName());
String[] paths = { "/autologin" };
mapping.setPathSpecs( paths );
webapp.getServletHandler().addServletMapping(mapping);
}
To make it more simple for users, I also created a directory jetty/ which contains a test.html where developers can add links to such URLs. To make sure this test HTML page can't be deployed accidentally, I add the jetty/ directory to the base resource of the WebAppContext:
File webAppDir = new File( "src/main/webapp" );
Resource webAppResource = new FileResource( webAppDir.toURI().toURL() );
Resource jettyDir = new FileResource( new File( "jetty" ).toURI().toURL() );
ResourceCollection resources = new ResourceCollection( webAppResource, jettyDir );
webapp.setBaseResource( resources );