Showing posts with label httputil. Show all posts
Showing posts with label httputil. Show all posts

18 February 2011

Your First CGI script using C....

Hello buddies..

This is actually my first CGI script... at all..
So, let me transfer this piece of knowledge to you.

First we will install the server on our local machine. (I am assuming you are using Linux)

From you package manager, install mini-http (you can use apache server instead .. but step here is for mini-http)

In my case, I am using ubuntu, so from synabtic I searched for "mini-http" them mark it for installation.. and then I got installed in moments.

after installing mini-http.. cd your hame folder:
cd $HOME
#then:
mkdir cgi-bin

Now lets start the web server..
cd ~
sudo mini-httpd -c "cgi-bin/*" # now the mini-httpd should starts ..
#to stop it write:
#sudo fuser -k 80/tcp


start your browse at:
http://localhost/
it should show listing of files of the directory at which you started the server at (in our case, the home directory)

Now let's write our simple application... I'll used httputil which I created in a previous post

#include <stdio.h>
#include <stdlib.h>
#include "httputil.h"

static void writeHeaders();

int main(void)
{
    writeHeaders();
    char* empId = get_parameter("empId");
    printf("you enterd employee Id: %s\n", empId);

    free(empId);
    
    return 0;
}

static void writeHeaders()
{
    printf("content-type: text/plain;charset=UTF-8\n\n");
}


Now compile this source file ...
First, get the required .h and .c files from the link above:
so you'll have 5 files, 3 .c and 2.h
stringutil.h, stringutil.c, httputil.h, httputil.c and the one above, let's name "employee.c"
cc -c stringutil.c 
cc -c httputil.c
cc -o employee employee.c -L. *.o

then copy the generated "employee" file and past it in ~/cgi-bin/employee.cgi
Now restart the server:
sudo fuser -k 80/tcp
sudo mini-httpd -c "cgi-bin/*"

Now fire your browse at:
http://localhost/cgi-bin/employee.cgi?empId=20

You should see:

That's all...

Next post I'll try to making GWT make a cross-site call to another server running CGI
...

httputil: my c library for http utilities

Hello every body,
Salamo Alaykom Wa rahmat Allah,

In a previous post, I wrote about my string util.. now I'll take about my http util..
It contains just one function, `get_parameter`, get parameter value from `getenv("QUERY_STRING")` env variable (to be used within CGI)..

here's the lib:

#ifndef _HTTP_UTIL_
#define _HTTP_UTIL_

static char ***out;

/**
 * get parameter value from the QUERY_STRING
 * return NULL if QUERY_STRING is null or the key has no associated value or if key found but without a value
 * returns pointer to char that you should free after use.
 */
char* get_parameter(char*);

#endif


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stringutil.h"

static char ***out;

/**
 * get parameter value from the QUERY_STRING
 * return NULL if QUERY_STRING is null or the key has no associated value or if key found but without a value
 */
char* get_parameter(char* key)
{
    char *querystr = getenv("QUERY_STRING");
    if (querystr == NULL)
        return (void*)0;
    
    char s[strlen(querystr)] ;
    strcpy(s, querystr);
    const char delim = '&';
    const char delim2 = '=';
    static size_t size = 0;
    if (out == 0)
    {
        out = malloc(sizeof(char));
        size = split(s, &delim, out);
    }
    
    int i=0;
    for (; i<size; i++)
    {
        char* seg = (*out)[i];
        if (seg != NULL)
        {
            char ***iout = malloc(sizeof(char));
            int isize = split(seg, &delim2, iout);
            if (isize > 1 && ((*iout)[1]) != NULL && strcmp(key, (*iout)[0]) == 0)
            {
                size_t _size = strlen((*iout)[1]);
                char* value = (char*) malloc(_size*sizeof(char));
                strcpy(value, (*iout)[1]);
                free(iout);
                return value;
            }
        }
    }
    return (void*) 0;
}


This library uses the stringutil library
You can convert this library into a static lib on linux.. follow this post