http.c
changeset 27 1e79b4cc8f1b
parent 18 86f2e5b7191b
equal deleted inserted replaced
26:6d615203d963 27:1e79b4cc8f1b
     1 #include <sys/queue.h>
     1 #include <sys/queue.h>
     2 #include <string.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     3 #include <stdlib.h>
       
     4 #include <limits.h>
       
     5 #include <assert.h>
     4 
     6 
     5 #include <event2/event_struct.h>
     7 #include <event2/event_struct.h>
     6 
     8 
     7 #include "http.h"
     9 #include "http.h"
     8 #include "common.h"
    10 #include "common.h"
    19     struct evkeyvalq qargs;
    21     struct evkeyvalq qargs;
    20     
    22     
    21     // parse the uri into a list
    23     // parse the uri into a list
    22     evhttp_parse_query(uri, &qargs);
    24     evhttp_parse_query(uri, &qargs);
    23     
    25     
       
    26     // the http_qarg that we iterate with
       
    27     struct http_qarg *qarg_info;
       
    28 
    24     // iterate over the list of given query arugments
    29     // iterate over the list of given query arugments
    25     TAILQ_FOREACH(qarg, &qargs, next) {
    30     TAILQ_FOREACH(qarg, &qargs, next) {
    26         struct http_qarg *qarg_info;
       
    27         
    31         
    28         // match them against our http_qarg structs
    32         // match them against our http_qarg structs
    29         for (qarg_info = qarg_spec; qarg_info->hqa_type != QARG_END; qarg_info++) {
    33         for (qarg_info = qarg_spec; qarg_info->hqa_type != QARG_END; qarg_info++) {
    30             if (strcmp(qarg->key, qarg_info->hqa_key) == 0) {
    34             if (strcmp(qarg->key, qarg_info->hqa_key) == 0) {
    31                 // found a match
    35                 // found a match
    32                 
    36                 
    33                 // extract a value from the qarg
    37                 // extract a value from the qarg
    34                 char *cptr = NULL;
    38                 char *cptr = NULL;
    35                 signed long int int_val;
    39                 unsigned long long int uint_val;
    36 
    40 
    37                 switch (qarg_info->hqa_type) {
    41                 switch (qarg_info->hqa_type) {
    38                     case QARG_UINT :
    42                     case QARG_UINT16 :
    39                         int_val = strtol(qarg->value, &cptr, 10);
    43                     case QARG_UINT32 :
       
    44                     case QARG_UINT64 :
       
    45                         uint_val = strtoull(qarg->value, &cptr, 10);
    40 
    46 
    41                         if (*qarg->value == '\0' || *cptr != '\0' || int_val < 0)
    47                         if (
    42                             ERROR("invalid QARG_UINT: %s: %s -> %ld", qarg->key, qarg->value, int_val);
    48                                 (*qarg->value == '\0' || *cptr != '\0')
    43 
    49                             ||  (qarg_info->hqa_type == QARG_UINT16 && uint_val > SHRT_MAX)
    44                         *((unsigned long int *) qarg_info->hqa_addr) = (unsigned long int) int_val;
    50                             ||  (qarg_info->hqa_type == QARG_UINT32 && uint_val > UINT_MAX)
       
    51                             ||  (qarg_info->hqa_type == QARG_UINT64 && uint_val > ULLONG_MAX)
       
    52                         )
       
    53                             ERROR("invalid QARG_UINT: %s: %s -> %lld", qarg->key, qarg->value, uint_val);
       
    54                         
       
    55                         if (qarg_info->hqa_type == QARG_UINT16)
       
    56                             *((unsigned short int *) qarg_info->hqa_addr) = (unsigned short int) uint_val;
       
    57                         else if (qarg_info->hqa_type == QARG_UINT32)
       
    58                             *((unsigned int *) qarg_info->hqa_addr) = (unsigned int) uint_val;
       
    59                         else if (qarg_info->hqa_type == QARG_UINT64)
       
    60                             *((unsigned long long int *) qarg_info->hqa_addr) = (unsigned long long int) uint_val;
       
    61                         else
       
    62                             assert(0);
    45 
    63 
    46                         break;
    64                         break;
    47                     
    65                     
    48                     case QARG_INVALID :
    66                     case QARG_INVALID :
    49                         ERROR("QARG_INVALID: %s: %s", qarg->key, qarg->value);
    67                         ERROR("QARG_INVALID: %s: %s", qarg->key, qarg->value);
    50 
    68 
    51                     default :
    69                     default :
    52                         FATAL("invalid qarg_info->hqa_type: %d", qarg_info->hqa_type);
    70                         FATAL("invalid qarg_info->hqa_type: %d", qarg_info->hqa_type);
    53                 }
    71                 }
       
    72 
       
    73                 // clear the QARG_REQUIRED flag
       
    74                 qarg_info->hqa_flags &= ~QARG_REQUIRED;
    54             }
    75             }
    55         }
    76         }
    56     }
    77     }
    57     
    78 
       
    79     // missing qargs?
       
    80     for (qarg_info = qarg_spec; qarg_info->hqa_type != QARG_END; qarg_info++) {
       
    81         if (qarg_info->hqa_flags & QARG_REQUIRED)
       
    82             ERROR("missing value for required qarg: %s", qarg_info->hqa_key);
       
    83     }
       
    84 
    58     // success
    85     // success
    59     error = 0;
    86     error = 0;
    60 error:    
    87 error:    
    61 
    88 
    62     // clean up the qargs (badly named functions :< )
    89     // clean up the qargs (badly named functions :< )