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
No comments:
Post a Comment