HTTP Parser and message builder / objects in plain C Snapshot
Classes | Defines | Typedefs | Functions
query.h File Reference
#include <cutils/bhash.h>
#include <hutils/uri.h>

Go to the source code of this file.

Classes

struct  tagHASHNAMEVALUE
struct  tagQUERYSTRING

Defines

#define _QUERY_STRING_H

Typedefs

typedef struct tagHASHNAMEVALUE HASHNAMEVALUE
typedef struct tagQUERYSTRING QUERYSTRING

Functions

int QUERYSTRING_init (QUERYSTRING *query, URI *uri)
int QUERYSTRING_free (QUERYSTRING *query)
M_INLINE const char * QUERYSTRING_find (QUERYSTRING *query, const char *name)

Define Documentation

#define _QUERY_STRING_H

Definition at line 2 of file query.h.


Typedef Documentation

typedef struct tagQUERYSTRING QUERYSTRING

Function Documentation

M_INLINE const char* QUERYSTRING_find ( QUERYSTRING query,
const char *  name 
)

Definition at line 20 of file query.h.

{
   HASHNAMEVALUE *value;

   value = (HASHNAMEVALUE *) HASH_find( &query->name_to_value, (void *) name, -1 );
   if (value) {
     return value->value;
   }
   return 0;
}
int QUERYSTRING_free ( QUERYSTRING query)

Definition at line 95 of file query.c.

{
  HASH_Entry *cur;
  HASHNAMEVALUE *nv;

  HASH_DELETEALL( cur, &query->name_to_value )
    nv = (HASHNAMEVALUE *) cur;
    free( nv->name );
    free( nv->value );
  HASH_DELETEALL_END
  HASH_free( &query->name_to_value );
  return 0;
}
int QUERYSTRING_init ( QUERYSTRING query,
URI uri 
)

Definition at line 36 of file query.c.

{
   char *start, *pos, *name;    
   int state = 0;
   size_t off;  
   char is_escaped_char;

   pos = uri->query;

   if (HASH_init( &query->name_to_value, 32, 1, compare_entry, 0 ) ) {
     return -1;
   }

   for( ; *pos != 0 ; ++pos ) {
      off = pos - uri->cdata;
      is_escaped_char = uri->cdata_is_escaped[ off ];

      switch(state) {
         case 0: // start parsing of name component.
           start = pos;
           state = 1;
         //break;

         case 1:
           if (is_escaped_char != 0 || *pos != '=') {
              break;
           }
           state = 2;
         //break;

         case 2: // end of name
           name = strdup_range(start, pos);
           state = 3;
           start = pos + 1;
           break;

        case 3:  // parsing the value component.
           if (is_escaped_char == 0 && *pos == '&') {
              state = 0;
              if (QUERYSTRING_add( query, name,  strdup_range(start, pos) ) ) {
                return -1;
              }
           }
           break;
      }
   }
   if (state <= 2) {
         if (QUERYSTRING_add( query,  strdup_range(start, pos), strdup("") ) ) {
                return -1;
         }

   } else {
         if (QUERYSTRING_add( query, name, strdup_range(start, pos) ) ) {
             return -1;
         }
   }
   return 0;
}