Ann: Proloxy -- Prolog reverse Proxy

122 views
Skip to first unread message

Markus Triska

unread,
Dec 23, 2015, 4:14:01 PM12/23/15
to SWI-Prolog
Hi all,

if you are running multiple web services on a single machine and want to make all of them accessible at the same time on the standard HTTP/HTTPS ports, then you need a so-called reverse proxy to dispatch requests based on flexible rules. Most of you are probably using Apache or nginx for this.... at least up to now!

Proloxy is a reverse proxy that is written entirely in SWI-Prolog. It is available under the MIT license from:


It uses Prolog rules for its configuration, advertising flexible and convenient rule-based programming.

The configuration language is flexible enough to emulate Apache Virtual Hosts.

A thousand thanks to Jan for his great support, and adding all features that are required for declarative proxying with SWI-Prolog's HTTP libraries!

I hope you enjoy this!

Merry Christmas!
Markus




Torbjörn Lager

unread,
Dec 23, 2015, 4:53:39 PM12/23/15
to Markus Triska, SWI-Prolog
Nice! I certainly would much prefer using Prolog for this than the rules provided by e.g Apache. 

Thanks,
Torbjörn

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Markus Triska

unread,
Dec 24, 2015, 6:06:34 AM12/24/15
to SWI-Prolog, tri...@logic.at
Hi Torbjörn,

great! It is certainly a huge advantage to have a full-fledged programming language available when analyzing and dispatching requests in more complex cases. Since the rules are just a Prolog file, you can also simply consult them to check for syntax errors.

Also, I have found that using SWI-Prolog throughout strengthens all parts of the system, and the proxy server implementation is a particularly good way to stress and improve many important aspects of the HTTP infrastructure, because it receives and sends both requests and responses, and I think the whole HTTP infrastructure of SWI-Prolog has already benefited a lot from the changes Jan has made to enable all of this.

My hope is that this simple reverse proxy (which is of course useful also for dispatching requests to web servers that are written in other languages) helps to advertise the power and convenience of Prolog-based configuration languages, and will attract more interested users to the great web capabilities of SWI-Prolog.

All the best!
Markus

Carlo Capelli

unread,
Dec 24, 2015, 6:16:56 AM12/24/15
to Markus Triska, SWI-Prolog
Hi Markus

thanks for sharing this... I've recently crafted a small parser (with UI based on PLGI) for collectd configuration files. Collectd uses the same syntax of Apache.
Could be worth to contribute it, to allow reusing / mantaining known configurations ? specially mod_rewrite, it always bite me when I need to deploy localhost development to the wild...

Carlo

Markus Triska

unread,
Dec 24, 2015, 6:35:21 AM12/24/15
to SWI-Prolog, tri...@logic.at
Hi Carlo,

awesome! I am very interested in making it easy for users to reuse or automatically convert their existing configuration to the flexible Prolog rules.

If you would like to contribute such a parser or conversion tool, I will include it! Alternatively, I will point to your project from the Proloxy documentation, if you prefer that. I am also very interested in seeing whether the current rule syntax is flexible enough to express existing user configurations, and I will make it more general if necessary. Please keep me posted.

Thank you and all the best!
Markus

Jan Burse

unread,
Dec 25, 2015, 2:01:58 PM12/25/15
to SWI-Prolog, tri...@logic.at
Dear All,

reverse Proxies, forward Proxies, etc are not to be confused
with Prolog Java proxy classes. The former is a network
component, dealing with loosely connected threads or processe.

The later is a reflection technique where for a language A an
object is dynamically created which might have method
implementations in a language B. Jekejeke has them since
release 1.1.0.

So a Prolog Java proxy class is a tight coupling technique and
not a loose coupling technique.

Bye

BTW: In the good old times when there wasn't so much memory,
reverse proxies were created by pipes between the streams. Can't
this also be done in Prolog?

What do you do if somebody requests a 4 GB video, you load
it first to your sever memory without caching and then deliver it?
Most likely resulting in a delay of the video start?

Jan Burse

unread,
Dec 25, 2015, 2:15:50 PM12/25/15
to SWI-Prolog
Dear All,

I guess with some extra effort, i.e. using throughput buffers, the
design could be improved.

But I am not sure whether the SWI http framework can do it,
I see strange things like throw/1 for the response.

Bye

Markus Triska

unread,
Dec 25, 2015, 4:14:46 PM12/25/15
to SWI-Prolog, tri...@logic.at
Hi Jan,


On Friday, 25 December 2015 20:01:58 UTC+1, Jan Burse wrote:

BTW: In the good old times when there wasn't so much memory,
reverse proxies were created by pipes between the streams. Can't
this also be done in Prolog?

This would make sense for processes on the same machine. Note though that Proloxy can also relay requests to different machines over the network. I'm definitely interested in all techniques that improve performance in cases that are important for users, so please let me know if you are running into bottlenecks and how they could be solved in a general way.

Carlo Capelli

unread,
Dec 25, 2015, 11:02:32 PM12/25/15
to Markus Triska, SWI-Prolog
Albeit not still related to Proloxy, I decided to show the Collectdcp prototype I was speaking of...

It's simple application to make an AST editable.
The AST, parsed with a DCG (parse_conf.pl), retains the comments as source of 'configured but disabled' reference data.
Nothing so fancy, anyway, some effort is quite 'low level'...
The GUI is designed with Glade - for sure I'm not an experienced UI designer... hints welcome !

There is some Prolog scripts to web scrape semi-formalized editable configurations from Collectd wiki.
Yet to be completed, will generate Gtk/Glade forms for detailed configuration editing...

Notably, SWI-Prolog IDE is quite usable to debug/develop the application. Thanks Keri !

The gtkmm (i.e. C++) counterpart is still under way, and I think will change a lot, but so far, could be useful to compare different Gnome languages 'at work'.
It's not a direct rewrite, most of a learning from scratch process about Gtk,Glib,PLGI,gtkmm,Glade, etc... I surely hit some rough points.
Gnome platform it's nice, and rather big... but I feel a little Gnome adept by now !

To run a test, PLGI is requested, as well as

$ sudo apt-get install libgtksourceviewmm-3.0-dev

then, since resources are currently bounded to C++ (available in CentOS 7 in stripped down version), the simplest steps:

$ git clone https://github.com/loadavg/collectdcp.git
$ cd collectdcp/swipl-plgi
$ swipl -g "[editapp],editapp"

Carlo

Jan Burse

unread,
Dec 26, 2015, 9:09:48 AM12/26/15
to SWI-Prolog
Hi,

Although I am against confusing reverse Proxies with my Prolog Java
Proxy classe, they are nevertheless related. Lets first start with
a different terminology:

- reverse Proxies --> Gateway
  yeah lets call them a gateway, they transport a request and
  response to and from a destination, via another destination.

- Prolog Java Proxy classes --> Server Stub
  yeah lets call them server stubs, they translate from a language
  A to a language B (in Jekejeke Java to Prolog).

- Auto Loaded classes --> Client Stub
  yeah lets call them client stubs, they translate from a language
  B to a language A (in Jekejeke Prolog to Java).

So how can we put them together. Well we can communicate as a client
in language B with a server in language B, although the gateway and
the destination only understands language A.

Here is the scheme:

        B    +--------+ A  +----------+ A  +--------+   B
      client | Stub   |----| Gateway  |----| Stub   | server
             +--------+    +----------+    +--------+

So basically Prolog could commincate with Prolog, even if there is
something else inbetween. For example a Prolog Java proxy class
could be easily plugged into a servlet container, eh voila the server
side would be already covered to some extend:

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#addServlet%28java.lang.String,%20java.lang.Class%29

Did not yet try this plugging, only the timer example with a Runnable.
Also no clue how one would for example create Prolog C++ proxy
classes. The usual C++ reflection would not work. Since it only
allows to enumerate classes and inspect or invoke members.

But for a Prolog C++ proxy class some magic would be needed, so
that for some given interfaces dynamically v-tables are created. In the
Jekejeke case the magic is done by the JDK 1.3 Proxy class, which
does under the hood some on the fly Java byte code generation.

I was googling yesterday but did not yet find an
analogue for bare bone C++.

On the other hand I expect it to exist in the old COM world
and also in the new C# world. But I am not yet sure about this. Its a
feature that is for example useful in the old IDE idea of GUI builders
by wiring, aka Visual Age from IBM. So it happens that Proxy classes
are used under the hood in java.beans.EventHandler:

https://docs.oracle.com/javase/7/docs/api/java/beans/EventHandler.html

Bye

Carlo Capelli

unread,
Dec 26, 2015, 5:58:14 PM12/26/15
to Jan Burse, SWI-Prolog, Markus Triska
Well, W3C cares about us ! The fastest standard approval in history ever, MPEG-DASH, is bringing us in the real of
multimedia streaming available on simple server nodes (chunked HTTP, quality adaption).

I had the surprise today, when I issued

carlo@carlo-ubuntu-64:~$ youtube-dl https://www.youtube.com/watch?v=W7GRekpZ-Ec
[youtube] W7GRekpZ-Ec: Downloading webpage
[youtube] W7GRekpZ-Ec: Downloading video info webpage
[youtube] W7GRekpZ-Ec: Extracting video information
[youtube] W7GRekpZ-Ec: Downloading DASH manifest
[youtube] W7GRekpZ-Ec: Downloading DASH manifest
WARNING: Your copy of avconv is outdated and unable to properly mux separate video and audio files, youtube-dl will download single file media. Update avconv to version 10-0 or newer to fix this.
[download] Destination: lucio battisti - il nostro caro angelo (inedito)-W7GRekpZ-Ec.mp4
[download] 100% of 15.94MiB in 00:23

Great ! Youtube is spreading DASH (Adaptive Streaming) technology. Briefly, we will be able to generate adapted media data,
for instance animated sequences (maybe interactive), automatic storytelling and feedback report, or simply storage for any level of media emitters.

The specification is Xml... sorry Jan W. Console yourself thinking of Svg, Scxml, X3d, ... :))

Ciao, Carlo

Anne Ogborn

unread,
Dec 26, 2015, 6:37:43 PM12/26/15
to Carlo Capelli, Jan Burse, SWI-Prolog, Markus Triska
Just thought I'd mention SWIG, a library that makes gluing C and C++ libraries to language X easier.

One writes a library telling SWIG how to emit bindings from a C/C++ function/method/etc. to X, then runs swig
against C/C++ code and gets binding code.

http://swig.org/


::Annie is here, writing something (she's not sure what) in a mixed Rebol, Verilog, LabView, LSL, and GCOS JCL environment.::

Jan Burse

unread,
Jan 6, 2016, 9:32:24 AM1/6/16
to SWI-Prolog
Hi,

Since I just posted a link to Mercury trailing. I must say
I am again and again amazed by Mercury.

For example what concerns proxy stuff, there was or is,
don't know exactly, MCORBA. The title of the paper
is a little misleading, but its still a nice read:

Expressive Type Systems for Logic Programming Languages

David Jeffer, 2002
http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.19.2178

Bye

Markus Triska

unread,
Jan 7, 2016, 11:27:44 AM1/7/16
to SWI-Prolog
Hi Jan, all,

good news regarding performance:

The latest git version of the SWI-Prolog HTTP package contains important changes that enable Keep-alive connections between Proloxy and the client. I have tested Proloxy by relaying requests to web services that rely on low latency to be useful, and the latest git version of the HTTP package noticably improves the user experience for several applications by now using Keep-alive connections by default. No changes are needed for Proloxy itself, just update SWI-Prolog and restart Proloxy. You can pass the --keep_alive_timeout=Seconds command line option when starting Proloxy to control the timeout for Keep-alive connections.

The connections between Proloxy and the eventual target are still created anew for each request, and that is OK for many use cases since Proloxy and the target typically reside on the same machine and the overhead for initiating a connection is low. My eventual goal is to transparently retain Keep-alive connections across the proxy too.

Do not let the throw/2-s distract you: This is a way to answer requests in the SWI HTTP framework. The Keep-alive connection persists regardless, as you can verfiy by inspecting the network traffic.

Enjoy!

All the best,
Markus
Reply all
Reply to author
Forward
0 new messages