LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2005, 06:45 AM   #1
jayashrik
LQ Newbie
 
Registered: Jan 2005
Location: pune
Posts: 3

Rep: Reputation: 0
Question How do I access the request data POSTed by client?


Hello

How do I access the request data POSTed by client?I want to write input filter in "c" which will access the request data POSTed by client?Could anyone please help me in this?

Thanking You

Jayashri
 
Old 03-03-2005, 10:23 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It depends what it will be a filter for. I understand you want to write an extenstion for a program, so you need to read its docs to see how the program is storing POST data. Because when received it's up to the program author.
 
Old 03-03-2005, 10:53 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I wrote a CGI library in C once. Hopefully this will help you out:
Code:
// cgi.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct cgi_opts
{
  char *name;
  char *val;
  struct cgi_opts *next;
} CGIOPT;

static CGIOPT *optlist = NULL;

typedef struct cookie
{
  char *name;
  char *val;
  struct cookie *next;
} COOKIE;

static COOKIE *cookielist = NULL;

static int hex_to_dec(char *str)
{
  char vals[] = "0123456789ABCDEF";
  unsigned int num;
  char *p, *q;

  p = strchr(vals, *str);
  q = strchr(vals, *(str+1));

  if(p && q)
  {
    num = (p - vals) << 4;
    num += q - vals;
  }
  else
    num = 0;

  return num;
}

static void cgi_decode(char *str, size_t maxlen)
{
  char buf[4096], *p, *b = buf;

  for(p = str;*p && b-buf < sizeof(buf)-2;p++, b++)
  {
    switch(*p)
    {
      case '+': *b = ' '; break;
      case '%': *b = hex_to_dec(p+1); p += 2; break;
      default:
        *b = *p;
    }
  }

  *b = '\0';
  strncpy(str, buf, maxlen);
  str[maxlen] = '\0';
}

void get_cgi_opts(void)
{
  CGIOPT *opt;
  char buf[4096], buf2[4096], *p = buf, *q = buf;

  fgets(buf, sizeof(buf), stdin);
  if(!*buf)
    strncpy(buf, getenv("QUERY_STRING"), sizeof(buf)-1);

  while(*p && *p != '\n')
    p++;
  if(*p == '\n')
    *p = '\0';
  p = buf;

  while((p = strchr(q, '=')))
  {
    if(!(opt = malloc(sizeof(CGIOPT))))
      return;
    opt->next = optlist;
    optlist = opt;

    strncpy(buf2, q, p-q);
    buf2[p-q] = '\0';
    cgi_decode(buf2, sizeof(buf2));
    if(!(opt->name = malloc(strlen(buf2)+1)))
    {
      optlist = opt->next;
      free(opt);
      return;
    }
    strcpy(opt->name, buf2);

    p++;
    q = strchr(p, '&');
    if(!q)
      q = p+strlen(p);
    strncpy(buf2, p, q-p);
    buf2[q-p] = '\0';
    cgi_decode(buf2, sizeof(buf2));
    if(!(opt->val = malloc(strlen(buf2)+1)))
    {
      optlist = opt->next;
      free(opt->name);
      free(opt);
      return;
    }
    strcpy(opt->val, buf2);

    if(q)
      q++;
  }
}

char *opt_val(char *name)
{
  CGIOPT *opt;

  for(opt = optlist;opt;opt = opt->next)
    if(!strcmp(opt->name, name))
      return opt->val;

  return "";
}

void get_cookies(void)
{
  char buf[4096], *p, *q;
  COOKIE *c;

  if(!(p = getenv("HTTP_COOKIE")))
    return;
  strcpy(buf, p);

  p = buf;
  do
  {
    if(!(c = malloc(sizeof(COOKIE))))
      return;

    while(isspace(*p))
      p++;
    if(!(q = strchr(p, '=')))
      return;
    *q++ = '\0';
    if(!(c->name = malloc(q-p)))
    {
      free(c);
      return;
    }
    strcpy(c->name, p);
    p = q;

    if(!(q = strchr(p, ';')))
      q = p+strlen(p);
    *q++ = '\0';
    if(!(c->val = malloc(q-p)))
    {
      free(c->name);
      free(c);
      return;
    }
    strcpy(c->val, p);
    p = q;

    c->next = cookielist;
    cookielist = c;
  } while(*p);
}

char *cookie_val(char *name)
{
  COOKIE *c;

  for(c = cookielist;c;c = c->next)
    if(!strcmp(c->name, name))
      return c->val;

  return "";
}

char *cgi_header(char *title)
{
  static char buf[4096];

  sprintf(buf, "Content-type: text/html\n\n<html>\n");
  if(title)
    sprintf(buf+strlen(buf), "<title>%s</title>\n", title);

  return buf;
}
Code:
// cgi.h
#ifndef CGI_H_
#define CGI_H_

void get_cgi_opts(void);
char *opt_val(char *);
char *cgi_header(char *);
void get_cookies(void);
char *cookie_val(char *);

#endif //CGI_H_
 
Old 03-04-2005, 04:13 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Here you've got examplanation and code: http://www.cs.tut.fi/~jkorpela/forms/cgic.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
opera: client-error-bad-request landroni Linux - Software 0 11-22-2005 10:22 AM
client-error-bad-request when trying to print with lp blas Linux - General 4 02-09-2005 10:30 AM
Redhat DHCP client always request 10.0.0.4? Chowroc Linux - Networking 12 12-30-2004 07:28 AM
SATA Data Request Error JulesVerne Linux - Hardware 1 01-15-2004 10:41 AM
using data posted by LQ-ers for an school asignment qanopus LQ Suggestions & Feedback 2 10-17-2003 08:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration