HTTP web server cgi processing - c++

I'm writing simple HTTP webserver in c++ for my college project and I want it to have CGI, so I can write some script in PHP or Perl. The question is: how to understand if I should call cgi program and grab its output, or simply give html page as response to a client. Only by a requested file's extension? I see no other options

Related

How to host CGI application?

Up to now i have tested hunchentoot-cgi(CL), mighttpd2(Haskell), ocsigen(OCaml), yaws(erlang) to host an old python CGI application. Except hunchentoot-cgi, all work great. I like Lisp-style programming, so i really want to host the application in a Lisp style web server.
In erlang, i just need to config it as below:
>cat ~/yaws.conf
...
<server 192.168.1.2>
port = 8000
listen = 0.0.0.0
docroot = /media/G/www/qachina/
access_log = false
appmods = <cgi-bin, yaws_appmod_cgi>
</server>
...
then the python CGI application can work.
How about Noir/Ring ?
Ring (and, consequently, Noir) is not a web server in the sense you seem to think. It is a framework for creating web applications. It does not provide access to external applications; rather it allows you to write your own dynamic web applications in Clojure. Then these applications can be deployed to standard Java servlet container or run manually using embedded Jetty web server.
It is of course possible to create such web application which would take an output of an external CGI program, process it and feed the results to the client, but you have to write all CGI processing by yourself (e.g. parse HTTP headers and construct standard Ring response map). As far as I know, there are no CGI wrappers for Ring stack.

How do I copy a file on a http server, from the client?

In the past I used an ftp server, connected via "ftp" from the client and "GET" to copy one file from the remote machine to the local machine.
Is it possible to do the same but with the server only running a http server?
Server: GoAhead Web Server.
Both client and http server on Windows.
The copy can be either initiated from the browser or if need a separate program can be written on the client. (i.e. - any windows api calls to copy a file from http server?)
(Also, the files may not be in the http root web directory but somewhere else on the server....can that happen?)
HTTP servers will only serve up files that are located within the site's document root. If you want to get at files that are outside the document root, you'll have to have a script serve up that file from the server (php, perl, cgi, etc...), or find some way of getting that file "inside" the document root.
To download files within the site's document root, you just hit a url pointing at that file - that's the core point of HTTP - you're just downloading content from the site.
HTTP servers will also not accept uploads without an intermediate script to handle it. If they did, you could upload any file you wanted to any server, anywhere.
What others mentioned about HTTP servers is true, but GoAhead Web Server is not a only a HTTP server. It provides many other features on top of that. And file upload seems possible, with help of a patch. More info:
https://embedthis.com/goahead/
Use WebDav for this purpose.

apache httpd extension help for special protocol

I want to create a apache extension for my special protocol which listens on the same port as apache but on a specific directory or file like PHP. I need this to power my game with a custom server but not with PHP. I can't listen on a different port or different server.
I don't want to create a PHP file or something similar. I need a complete executable program where I can dynamically allocate memory etc. the program is completely outside of the apache server, but the apache 'passes-through' special requests to this program (for example the .foo files or the /foo/ directory.
I need a tutorial or a help to create a custom extension for apache.
UPDATE:
I want to create a daemon running in the background of my server, and then when apache sends a request, it forwarded this to my daemon, and then the daemon generates the request, and then send an answer. this is important that is not like a php script file or a perl because this is not executed once, the program is running all time, and waiting for the apache to send something. I don't know how to communicate with apache. But I think this is not a CGI, because if I read well, the CGI running is like: apache gets the request, and then START a new process for my php or perl file, sends the data through arguments and stdIN, and then when the process ends, reads the answer form the stdOut and send it back, the process then finished.
But my program still running. I need to run my program in the background all time because I need to store data in the memory which is loaded at startup.
like this.:
http://i53.tinypic.com/v45jzo.jpg
You don't need a special extension, just register a CGI handler that calls your processing code.
Edit
You can setup apache to proxy requests to your daemon.
You will need to return a properly formatted HTTP response or it wont work. You should read up on Apache and web based communications in general to get a better idea what is needed in your daemon.
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar
P.S. Writing an Apache extension is much more difficult an not portable.
To answer your second question about what CGI is
CGI stands for common gateway interface.
when you register a handler as Byron pointed out you tell apache to give the 'request' to your application. apache listens to the output stdout of your application and returns the result to the user.
The parameters for the application are all supplied via the environment which you can access from your application.
But if you don't understand this stuff you are going to run into problems. such as you must set certain headers.
DC

Upload a file to a web server using C++

I want to upload files from a client location to a server. At present, I have a client-server socket program by which I could send/receive files across, but I would like to improvise it.
My idea would be to transfer the file using HTTP PUT/POST from client (most of the coding on client side) to the server. Since I have no idea about HTTP programming, so I need some guidance on how to achieve that. I want to use C++ with BSD sockets in doing that, and no other libraries. My aim is to send the server a form, like as given below with a HTTP POST/PUT request, and get the file "main.cpp" uploaded to the server.
PUT http://localhost/ HTTP/1.0
Host: localhost
Content-type: form-data
Content-length: 90
FileUpload: /Users/SG/files/main.cpp
I was able to write a dummy program that does some PUT from a client, and the web server running Apache returns a HTTP 200. What I am failing to understand currently would be the following two things, which I guess are somewhat connected:
How one could specify a file to be uploaded from the client in the form above?
If I understand correctly, the file would be read at client site and then the file content would be sent to the server, where a server side script would read the bytes from client and create a copy of the file at the server. Is it correct?
Any help is appreciated.
Thanks,
Sayan
As stated it's not possible. C++ alone has no sockets API. (Edit: with the addition of a BSD sockets API, it's now possible).
Your implementation might provide an OS-specific sockets API, in which case your question boils down to, "how do I write an HTTP client?". To which the answer is (a) don't, or (b) read the HTTP specification very carefully and do as it tells you. The basic steps are:
(Possibly) parse a URL and use gethostbyname to get an IP address.
Open a TCP socket, using connect.
Write the request (you already have an idea what that looks like) using send.
Read the response using read.
Close the connection.
The difficult part is parsing the response robustly, because there are a lot of features to worry about. If your client is tied to a particular server, then its responses will be quite predictable, and you can ignore quite a lot of the HTTP spec (at least until you change the configuration of the server, update its software, etc).
If you're ready to give up before you finish, there are perfectly good HTTP libraries available, such as libcURL.
I want to use C++ for doing this,
without the help of any libraries or
system() calls.
Socket programming always requires system calls. C++ is an extremely general language. The ISO language definition does not specify anything about sockets or network programming, so in order to do this you need to rely on another system-specific library/standard, such as the BSD socket API on UNIX/Linux. Usually, in C++ you would use a higher-level third-party library that wraps these lower level system calls, such as Boost.ASIO. However, even that takes a bit of learning, to say nothing of correctly implementing the HTTP standard itself on top of your sockets.
I was able to attain what I wanted. My http post request string looks like below:
"POST %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Content-type: multipart/form-data\r\n"
"Content-length: %d\r\n\r\n"
"Content-Disposition: %s; filename: %s\n"
I am sending the above string from a cpp program in client to the server, running Apache, which handles the data through a cgi script, also written in cpp. The data is placed with the Content Disposition attribute. Can anybody please point it out to me as to how would I transform the above to a HTTP PUT?

How to upload a file in C/C++ using HTTP with libcurl?

I would like to upload a file (a picture, in my case) in C/C++ using HTTP with libcurl.
It will be great to have a working sample in C/C++ with (optional) the php code for the server side.
Check out the tutorial at the libcurl site.