Tuesday, 29 January 2013

CGI C++ Photo Uploader

This project was given to me as an entrance test for GD. So I started off preparing for it from the very time when it was given to me. I faced many problems and I got to learn lots of things.

The very first problem was I was not familiar with CGI and I had never used CGI with C++.
So the first thing was to learn about CGI So I searched for CGI and collected some information about it. After this was to the turn to configure apache to support CGI. So I searched How to do it and started doing it Everything was already configured and CGI was working already.

Now I could start working with the project. CGI files are placed in /usr/lib/cgi-bin/ But you can change this directory and use your own directory by changing apache configuration in /etc/apache2/site-available//default file. Now I can start working on the project.

The very first problem that how to parse the POST, GET data and how to handle File Uploads with CGI. So After researching a little I found a library RUDECGI which allowed me to do that. It is a very simple library with lots of usefull function.

Installation procedure is pretty simple
sudo make install

How to use RUDECGI

Include file: rude/cgi.h

and use -lrudecgi while compiling to link the libraries. like this

g++ -o main main.cpp -lrudecgi

Initializing CGI object syntax: CGI cgi
There are many methods of the object but I have used only the following method in the program
cgi.value("")  This function to get value of get or post data.
cgi.isFile() Check if file was uploaded then
cgi.filename to get the name of the uploaded file

Now was the turn to learn about database. I was only familiar with MySQL So I thought of using some other DBMS this time. I had heard about SQLite and that it is very small and takes very small space and is even used by many mobile operating systems. So I thought about using it for my project

Sqlite header files can be installed by by executing the following commands in terminal

sudo apt-get install libsqlite3-dev sqlite3

to create a database you can use the following command

sqlite3 <database-name>

and then start entering your sql comands

or you can use sqlitebrowser

sudo apt-get install sqlitebrowser

After creating database, Now was the time to create connection to database through c++

sqlite3_open to open connection to the database
sqlite3_exec to execute sql commands which do not return anything such as INSERT, DELETE

To execute sql commands which return something we will use series of commands
sqlite3_prepare_v2() to prepare the sql statement if this return SQLITE_OK then you can proceed
sqlite3_step() to process all the rows

This was all about the database, the next problem that I faced took a lot of my time. Everything that you will put in the cgi-bin folder apache will consider it a cgi script and execute it. So you cannot store any of your javascript files,css files, and in my case photos. So I searched alot and people suggested to keep these files in document root folder for eg in /var/www as it is outside of cgi-bin folder so cgi will not execute it.

The other error I got was of C++ such as illegal conversion from const char* to char* etc which can be solved by using sstream.

All went well in the end and My task was complete and I got to learn lots of things in this week. :)) Happy Coding..