How web servers gives data to script? - web-services

I have some experience with php / python programming and i am curious to know how actually servers like apache / light httpd gives data to the script?
Can it be bypassed to a c/c++ program ? and handled via that?
Sorry for this kinds of question. I am too much curious. Googled it but can not find a really good answer.

Today, probably the most used and with less overhead is SAPI:
http://en.wikipedia.org/wiki/Server_Application_Programming_Interface
Some examples of SAPI is ISAPI that is mostly for IIS, Apache modules for Apache,
and the list extends with Servers.
ISAPI:
Uses a system dynamic linked library (DLL) so it "attach" to the webserver and functions
can be called direct on it.
http://en.wikipedia.org/wiki/Internet_Server_Application_Programming_Interface
CGI: http://www.w3.org/CGI/
normally a new process is created for each request, the data from the server is normally passed in stdin and the program writes to stdout, some information is passed as environment variables.
FastCGI: http://www.fastcgi.com/drupal/
As the name suggest, its like CGI but does not need to run the program every time a request is made, being faster and using less resources.
Xitami have their own too, called LRWP http://legacy.imatix.com/html/xitami/index12.htm
But each server can implement their own.
Note: The module or external program is what parses the script. The SAPI, CGI, FastCGI and whatever will integrate with the interpreter of the script, a binary. The interpreter then receive the request path, find the script file, and parse it.
Ex: PHP has its apache and IIS modules.

These things follow a request-response pattern where a request is made to a web server. The web server will process the request and execute any server side code (script) associated with the url that the request resolves to (web service or web page typically). The script will execute and result in a response that is sent back to the caller.

Related

Cloning PyQt app in django framework

I've designed a desktop app using PyQt GUI toolkit and now I need to embed this app on my Django website. Do I need to clone it using django's own logic or is there a way to get it up on website using some interface. Coz I need this to work on my website same way it works as desktop. Do I need to find out packages in django to remake it over the web or is there way to simplify the task?
Please help.
I'm not aware of any libraries to port a PyQT desktop app to a django webapp. Django certainly does nothing to enable this one way or another. I think, you'll find that you have to rewrite it for the web. Django is a great framework and depending on the complexity of your app, it might not be too difficult. If you haven't done much with web development, there is a lot to learn!
If it seemed like common sense to you that you should be able to run a desktop app as a webapp, consider this:
Almost all web communication that you likely encounter is done via HTTP. HTTP is a protocol for passing data between servers and clients (often, browsers). What this means is that any communication that takes place must be resolved into discrete chunks. Consider an example flow:
You go to google in your browser.
Your browser then hits a DNS server (or cache) that resolves the name google.com to some IP address.
Cool, now your browser makes a request to that IP address and says "get me some stuff".
Google decides to send you back a minimal amount of HTML and lots of minified JavaScript in the page.
Your browser realizes that there are some image links in the HTML and so it makes additional requests to google to get each of the images so that it can display them.
Now all the content is loaded on your browser so it starts to execute the JavaScript code, and that code needs some more data from google so it starts sending requests to google too.
This is just a small example of how fundamentally different a web application operates than how a desktop application does. On a desktop app you have the added convenience that any operation doesn't need to be "packaged up" and sent, then have an action taken, etc (unless you're using a messaging architecture, but that's relatively uncommon outside of enterprise apps).

Two way communication using AJAX from an HTML page to a C++ application running in same server

Is it possible to communicate from a web browser(Loaded an HTM page from server) to an application running in the same server using AJAX. Need to send the request from browser using a button click and update the page with responses received from one another application running in the same server machine?
I am using HTML pages to create website and not using any PHP or ASP like server side scripting. In server machine data are manipulated using a C++ application.
I think you can use any sort of Javascript functions to do that. But you might need to use jQuery or similar frameworks to make your live easier. You might need to search for "Comet Programming" to know exactly how to do 2-way communication between client and server
Updated:
Well, this kind of stuff requires you to read a lot (if you have not already known). Basically, what you need is a server that can do long-polling (or eventsource, websockets). There are many open-source ones that might help you to get started. I can list a several good ones here. There are a lot more
http://www.ape-project.org/
http://cometd.org/
http://socket.io/
http://code.google.com/p/erlycomet/
http://faye.jcoglan.com/
So after you have the comet server up and running you will need to setup the client side (probably Javascript). For those listed projects, most of them come with the client side code to interact with the server (Except for erlycomet). Therefore, you can just use the examples provided and run a quick prototype. If you want to use your raspberry pi, you can use nodejs which provide a lot of ease for dealing with real-time communication (socket.io, faye). And lately, http://www.meteor.com/
I would think of the problem this way: you want to provide a web front end to an existing c++ application. To achieve this you need to think about how your web server communicates with your c++ application. Communication between the browser and web server can be thought of as a separate problem - as you say AJAX calls can be used, or maybe have a look at websockets.
Once you have your request in the web server you need to communicate it to the C++ application (and/or visa versa). This can be done a number of ways, e.g. sockets or RPC. I found this question here which has some good advice.

Installing a Test Application on Amazon EC2

I want to use AWS products to build some application on it. For now, i want to test this -
1) Create a webpage hosted at AWS with a simple text box and a submit button, for checking if a number is prime.
2) Compile a C++ program on EC2 to accept a number and reply if it is prime.
Can someone list the steps involved in doing this?
(The above example mirrors simplistically the actual application that i have in mind, with a http frontend and a c++ backend)
If you use the default Linux AMI, you will gave a standard Apache installation ready to go. It sounds like the invocation style of your app is request-response, so at least to begin with, you could just use CGI to get Apache to run your app.
To achieve this, you would do something like this:
Create a static html page with a form and a submit button which passes the form data to your app via CGI
Install your app into an appropriate directory (see the Apache config for details) to run it via CGI, taking care to ensure the correct permissions are set
Have your app parse the CGI environment variables to gather the input
Perform the processing required
Generate the resulting output as an HTTP response (to get started, just use text/plain).
Please note that there are many security issues to keep in mind here, so it is very important to perform strict validation on all data supplied by the web user for escaping issues, buffer overflows and so on.
If you aren't familiar with the above, you will need to read up on HTML forms, Apache configuration and basic HTTP headers at at minimum. There are plenty of examples out there, and some great books covering the topic.
To this end, various libraries have been developed to facilitate this:
Which C++ Library for CGI Programming?
There are also many other options for interfacing your app with Apache, such as FastCGI.

Writing a server-side language program to render web sites (HTML, etc..)

Similar to technologies like ASP.NET, PHP, JSP and some other web languages which run on the server side are often called within a web pages code. For instance a web server may parse through a file hit
I don't have any difficulty writing server / client programs, only I am unsure where to tie in to web servers such that it will realize my own tag, run it through my software and output the resulting text to the requested file (most likely a web page.)
What does one need to do in order to accomplish this tie-in with a simple custom language?
The software is coded in C++.
To implement your own simple custom language you would need to do the following under Apache and something very similar under IIS.
For example for an Apache server to process PHP the following configuration is required.
LoadModule php5_module modules/libphp5.so
ScriptAlias /php/ "/usr/local/php/bin/"
AddType application/x-httpd-php .php
Action application/x-httpd-php /php/php
This directs the server to pass all calls to the server with the file extension .php to the php processor and replaces and execute the tags in the file.
IIS has a similar mechanism which I think is configured through 'Handler Mappings'
I don't personally think that creating a custom tag language is the correct route to go. You would be better served by writing a wrapper in either ASP or PHP to your C++ application. This would give you full access to the range of function in PHP or ASP that you will probably need e.g. Request parameter handling, etc.

Lightweight HTTP/HTTPS server in C++ (not C)

I need to build a lightweight http server for my application basically it's a server which listen to a port and outputs a status information on requests, https, other functionality. But I would like to know first if something like this existe in C++, for linux and open source.
Does anyone know a program like that?
Thanks.
EDIT: It should be able to support high load.
If you can use boost, the asio library provides an http example. It does not use SSL, but asio can use OpenSSL very easily.
If you want to handle high loads I would suggest following:
Use proper web server with all goodies it comes with like Lighttpd, Nginx or Apache (in that order).
It would do great job in serving static files and handle your application. And they are very lightweight.
Write an Application in C++ using proper web framework - CppCMS - that is designed for high loads
Connect Web Application to the server via FastCGI or SCGI protocol (in this order).
Disclaimer: I'm the author of CppCMS
A quick google search for "C++ web application framework" shows things called CppCMS and something else called WT. That might get you started.
Or, as Sam already answered: boost.asio comes with a HTTP example that may be sufficient if your needs are simple. (Real HTTP request handling is actually surprisingly complex: http://webmachine.basho.com/diagram.html )
See thttpd. Supposibly the fastest open source file server on all machines with a single CPU.
If not using HTTPS, it's about a two hour exercise to write a static file server.