how to read PAC file using C++ - c++

I am using libcurl for HTTP requests.
My application should be able to understand the proxy settings if the user has any
So it can be
Proxy by proxy server or
Proxy by Auto proxy Configuration
I see support for PAC isnt available in libcurl
Since my application is in C++, are there any extension / parser engine available?
Thanks for reading the post

PAC (Proxy Auto Config) file is simply Javascript that has function FindProxyForURL returning proxy config string.
Technically, this function can use anything that Javascript can do, so you must bundle some Javascript engine to interpret it.
tiny-js (simple single-file javascript interpreter written in C++) is the library that should fit the bill for this task.
UPDATE: pacparser library is pretty much ready-to-use engine designed specifically to parse pac files. Its downside that it bundles whole SpiderMonkey Javascript engine, which makes it rather heavy solution - it would add 1MB+ to your project binaries just to parse pac files.
If you can hack pacparser to use tiny-js instead of SpiderMonkey, that would be really nice solution.

Related

Getting a result from a URL in C++

I need a bare bones C++ version of some code. Using Visual Studio 2017.
Friend of mine wants to get results from a URL link using c++. He will write the .json parsers later, he then wants the code to visit the same URL domain and grab URL's that were in the same result.
I'm thinking it's got to be some sort of HTTP / HTTPS C++ GET requests maker function but I'm not sure.
looking for a possible template for this task, along with links to any necessary libraries for this project.
libcurl (http://curl.haxx.se/libcurl/) is definitely a great HTTP/HTTPS client library which has all possible bells and whistles.
But there are others like:
LibHTTP (https://www.libhttp.org/)
CivetWeb (https://github.com/civetweb/civetweb)
cpp-httplib (https://github.com/yhirose/cpp-httplib)
libwww (http://www.w3.org/Library/)
LibHTP (https://github.com/OISF/libhtp)
HTTP Parser (https://github.com/nodejs/http-parser)
There are already a lot of JSON parsers out there. To name a few:
cJSON (https://github.com/DaveGamble/cJSON)
ultrajson (https://github.com/ultrajson/ultrajson)
JsonCpp (https://github.com/open-source-parsers/jsoncpp)
CAJUN (https://sourceforge.net/projects/cajun-jsonapi/)
JSON for Modern C++ (https://github.com/nlohmann/json/)
RapidJSON (https://github.com/Tencent/rapidjson)
tiny-json (https://github.com/rafagafe/tiny-json)
json.h (https://github.com/sheredom/json.h)
json-parser (https://github.com/udp/json-parser)
libfastjson (https://github.com/rsyslog/libfastjson)
liblaxjson (https://github.com/andrewrk/liblaxjson)
But there are more of them out there...
Up to you to make your pick based on which criteria are important to you (like: low footprint, performance, C++ API).

How to run a JavaScript file - V8

I have embedded v8 into my c++ application. Referring https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc I am able to run a javascript. Tested and works fine.
I access the links from my c++ application, download html data, download javascript. Some embedded scripts in the html call functions in external script files. How do I ensure that the external scripts are available for the embedded ones?
The downloaded JavaScript files (one or more) may be of large size. In such a context, how do I execute the JavaScript api present in HTML using v8? Code to run a JavaScript in v8 is below,
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
v8::NewStringType::kNormal)
.ToLocalChecked();
// Compile the source code.
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
// Run the script to get the result.
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
Assuming downloaded javascript is 200KB, how can I feed such a large buffer to v8::Script::Compile api. And when I have more than one file present, how can feed I them to v8 ?
How do I ensure that the external scripts are available for the embedded ones?
You load the external scripts first.
How do I execute the JavaScript API present in HTML using v8?
Do you mean the DOM? window, document and the like? The DOM is not part of ECMAScript, so V8 knows nothing about it; it is provided by the embedder (i.e. usually Chrome). In your own embedding you need to provide all those objects yourself, using V8's API. Needless to say, this is a huge amount of work. If what you're after is a way to render websites, then I recommend that you use some existing component/library for that, for example the Chromium Embedded Framework, or your favorite GUI toolkit's WebView (or whatever it is called).
Assuming downloaded JavaScript is 200KB, how can I feed such a large buffer to v8::Script::Compile API?
Just like you feed a small script to V8: put it into a v8::Local<v8::String>, then call v8::Script::Compile and v8::Script::Run.
And when I have more than one file present, how can feed I them to v8 ?
Call v8::Script::Compile and v8::Script::Run repeatedly, possibly using a loop. For an example, see V8's shell sample, specifically the function RunMain.
As I receive partial JavaScript in HTTP packets (chunks), can I pass the partial JavaScript to V8?
Yes, V8 has a script streaming interface. See the API documentation for v8::ScriptCompiler::ExternalSourceStream. For examples on how to use it, you can study the tests. Streaming may or may not be worth it for scripts as small as 200KB; it is definitely not required.

How to load a shapefile in gwt-openlayers

We're building an application using GWT-Openlayers (not OpenLayers) and need to allow the user to load a polygon from a shapefile. Surprisingly, there doesn't seem to be an evident solution. The closest solutions are javascript libraries for interpreting shapefiles, but a javascript solution doesn't really help in a GWT application. Any recommendations?
Thanks in advance!
Lacking a simpler solution, the approach I used was as follows:
Use GWT FormPanel and FileUpload to allow user to select the file to upload
Create a custom servlet for handling the request
FormPanel sends a multipart POST of the file contents to the servlet
Servlet feeds the file content to a parser to convert to Well Known Text (WKT)
Servlet returns the WKT in the HttpResponse
Client side code converts the WKT to a gwt-openlayers vector feature and adds it to the map
Certainly not an elegant solution but seems to work. If anyone finds a better solution, it would be great to hear.

Building a Native Client app from nothing

What does it take to build a Native Client app from scratch? I have looked into the documentation, and fiddled with several apps, however, I am now moving onto making my own app and I don't see anything related to creating the foundation of a native client app.
Depending on the version of the SDK you want to use, you have a couple of options.
Pepper 16 and 17: use init_project.py or use an example as a starting point
If you are using pepper_16 or pepper_17, you will find a Python script init_project.py in the project_templates in the SDK. It will setup up a complete set of files (.cc, .html, .nmf) with comments indicating where you need to add code. Run python init_project.py -h to see what options it accepts. Additional documentation can be found at https://developers.google.com/native-client/pepper17/devguide/tutorial.
Pepper 18 and newer: use an example as the starting point
If you are using pepper_18 or newer, init_project.py is no longer included. Instead you can copy a very small example from the examples directory (e.g., hello_world_glibc or hello_world_newlib for C or hello_world_interactive for C++) and use that as a starting point.
Writing completely from scratch
If you want to write your app completely from scratch, first ensure that the SDK is working by compiling and running a few of the examples. Then a good next step is to look at the classes pp::Module and pp:Instance, which your app will need to implement.
On the HTML side, write a simple page with the EMBED element for the Native Client module. Then add the JavaScript event handlers for loadstart, progress, error, abort, load, loadend, and message and have the handlers write the event data to, e.g., the JavaScript console, so that it's possible to tell what went wrong if the Native Client module didn't load. The load_progress example shows how to do this.
Next, create the manifest file (.nmf). From pepper_18 and onwards you can use the generate_nmf.py script found in the tools/ directory for this. If you want to write it from scratch, the examples provide examples both for using newlib and glibc (the two Standard C librares currently supported). See hello_world_newlib/ and hello_world_glibc/, respectively.
If you haven't used a gcc-family compiler before, it is also a good idea to look at the Makefile for some of the examples to see what compiler and linker flags to use. Compiling both for 32-bit and 64-bit right from the beginning is recommended.
Easiest way is to follow the quick start doc at https://developers.google.com/native-client/pepper18/quick-start, in particular steps 5-7 of the tutorial ( https://developers.google.com/native-client/pepper18/devguide/tutorial ) which seems to be what you are asking about.

How to use V8's built in functions

I'm new in both javascript and V8. According to Google's Embedder's Guide, I saw something in the context section talking about built-in utility javascript functions. And I also found some .js files(e.g. math.js) in the downloaded source code, so I tried to write a simple program to call functions in these files, but I failed.
Does a context created by Persistent<Context> context = Context::New() have any built-in js functions? How can I access them?
Is there a way to first import existing js files as a library(something like src="xxx" type="text/javascript" in HTML page) and then run my own execute script?
Can I call google maps api through the embedded V8 library in app? How?
3. Google Maps needs a full browser DOM (or at least XMLHttpRequest I guess), you can't use it from just a Javascript library.
I think v8 gives you the Math.* functions for free.
You need to implement everything else yourself though, like loading other javascript files. shell.cc has some of the functions you might be looking for.
As for the maps API, I believe you would need a full blown rendering engine/javascript engine combo for that. You might be better off taking a look at Webkit or something that you can use to embed Webkit for what you're looking to do, I can't really say.
You can use for example the --allow_natives_syntax or --expose_natives_as option.
Here are examples with MathLog picked at random in src/math.js:
First compile a shell with
$ scons d8 -j8
Then use --expose_natives_as:
$ ./d8 --expose_natives_as nat
V8 version 3.12.7 (candidate) [console: dumb]
d8> nat.MathLog(100)
4.605170185988092
or use --allow_natives_syntax with the '%' prefix:
$ ./d8 --allow_natives_syntax
V8 version 3.12.7 (candidate) [console: dumb]
d8> %MathLog(100)
4.605170185988092