web services in c with cgi

I was trying to setup a simply webservice to reply to POST requests. Earlier this was being done in tomcat which seem a little overkill to me since I already had a webserver up and running. So, a quick c program to respond to request is all that I needed. And here is the result.

/*
 * =====================================================================================
 *
 *       Filename:  Login.cpp
 *
 *    Description:
 *
 *        Version:  1.0

 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Amit Agarwal (),
 *   Organization:
*
* == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
    */
#include 
#include 
#include 


#define MAXLEN 1024

#ifdef DBG
#define DEBUG(a,b) fprintf(stderr, a, b);
#else
#define DEBUG(a,b) 
#endif

#define HEAD "n"
#define TAIL ""

int main(void)
{

    char input[MAXLEN];
    char * data;
    char*lenstr;
    long len;


    printf("%s%c%cnn",
           "Content-Type:text/xml;charset=iso-8859-1", 13, 10);

    // len = getenv("QUERY_STRING"); // This is for GET
    lenstr = getenv("CONTENT_LENGTH");
    DEBUG( "Length string is %sn", lenstr);

    if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN)
        printf("

Error in invocation – wrong FORM probably.”);
DEBUG( “Length is %ldn”, len);
{


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    int count=0;
    while ( count < len )
      input[count++] = getchar();
    input[count]='\0';
    DEBUG ( "Read characters = %dn", count)
  }

//fprintf(stderr, "VAlue is %sn", input);
///unencode(input + 5  , input + len, data);

data = input;
DEBUG("Data value is %sn", data);

printf(HEAD);
printf("Everything else goes heren");
printf(TAIL);

fprintf(stderr, "Sent resp: tid[%s], eaid[%s], tcode[%s]n", tid, eaid, tcode);

}

Additionally, the makefile:

all:
gcc -o Login Login.c -Wall

debug:
gcc -o Login Login.c -Wall -DDBG=1

And finally, compile this with make command and put in the cgi-bin folder and that is all that you need 🙂

git – plot the history

git has a very simple solution to see all the commits in a graphical form. If you need to understand all the commits history all you need is

git graphviz|dot -Tpng  -o /tmp/a.png

This will generate a file called /tmp/a.png which will diplay the complete commit history.

firewalld – enable logging

firewalld by default does not allow packets that are dropped. In some cases, you need to find out if some packet is being dropped or not. For doing so you may want to enable logging of dropped packets with the following command:

sudo firewall-cmd  --set-log-denied=all

This will enable logging all the packets and help you figure out if firewalld is dropping the packet.