Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2010 John-Mark Bell <jmb@netsurf-browser.org>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #include "utils/http.h"
  23.  
  24. #include "utils/http/generics.h"
  25. #include "utils/http/parameter_internal.h"
  26. #include "utils/http/primitives.h"
  27.  
  28. /**
  29.  * Representation of an HTTP parameter
  30.  */
  31. struct http_parameter {
  32.         http__item base;
  33.  
  34.         lwc_string *name;               /**< Parameter name */
  35.         lwc_string *value;              /**< Parameter value */
  36. };
  37.  
  38. /**
  39.  * Destructor for an HTTP parameter
  40.  *
  41.  * \param self  Parameter to destroy
  42.  */
  43. static void http_destroy_parameter(http_parameter *self)
  44. {
  45.         lwc_string_unref(self->name);
  46.         lwc_string_unref(self->value);
  47.         free(self);
  48. }
  49.  
  50. /**
  51.  * Parse an HTTP parameter
  52.  *
  53.  * \param input      Pointer to current input byte. Updated on exit.
  54.  * \param parameter  Pointer to location to receive on-heap parameter.
  55.  * \return NSERROR_OK on success,
  56.  *         NSERROR_NOMEM on memory exhaustion,
  57.  *         NSERROR_NOT_FOUND if no parameter could be parsed
  58.  *
  59.  * The returned parameter is owned by the caller.
  60.  */
  61. nserror http__parse_parameter(const char **input, http_parameter **parameter)
  62. {
  63.         const char *pos = *input;
  64.         lwc_string *name;
  65.         lwc_string *value;
  66.         http_parameter *param;
  67.         nserror error;
  68.  
  69.         /* token "=" ( token | quoted-string ) */
  70.  
  71.         error = http__parse_token(&pos, &name);
  72.         if (error != NSERROR_OK)
  73.                 return error;
  74.  
  75.         http__skip_LWS(&pos);
  76.  
  77.         if (*pos != '=') {
  78.                 lwc_string_unref(name);
  79.                 return NSERROR_NOT_FOUND;
  80.         }
  81.  
  82.         pos++;
  83.  
  84.         http__skip_LWS(&pos);
  85.  
  86.         if (*pos == '"')
  87.                 error = http__parse_quoted_string(&pos, &value);
  88.         else
  89.                 error = http__parse_token(&pos, &value);
  90.  
  91.         if (error != NSERROR_OK) {
  92.                 lwc_string_unref(name);
  93.                 return error;
  94.         }
  95.  
  96.         param = malloc(sizeof(*param));
  97.         if (param == NULL) {
  98.                 lwc_string_unref(value);
  99.                 lwc_string_unref(name);
  100.                 return NSERROR_NOMEM;
  101.         }
  102.  
  103.         HTTP__ITEM_INIT(param, NULL, http_destroy_parameter);
  104.         param->name = name;
  105.         param->value = value;
  106.  
  107.         *parameter = param;
  108.         *input = pos;
  109.  
  110.         return NSERROR_OK;
  111. }
  112.  
  113. /* See parameter.h for documentation */
  114. nserror http_parameter_list_find_item(const http_parameter *list,
  115.                 lwc_string *name, lwc_string **value)
  116. {
  117.         bool match;
  118.  
  119.         while (list != NULL) {
  120.                 if (lwc_string_caseless_isequal(name, list->name,
  121.                                 &match) == lwc_error_ok && match)
  122.                         break;
  123.  
  124.                 list = (http_parameter *) list->base.next;
  125.         }
  126.  
  127.         if (list == NULL)
  128.                 return NSERROR_NOT_FOUND;
  129.  
  130.         *value = lwc_string_ref(list->value);
  131.  
  132.         return NSERROR_OK;
  133. }
  134.  
  135. /* See parameter.h for documentation */
  136. const http_parameter *http_parameter_list_iterate(const http_parameter *cur,
  137.                 lwc_string **name, lwc_string **value)
  138. {
  139.         if (cur == NULL)
  140.                 return NULL;
  141.  
  142.         *name = lwc_string_ref(cur->name);
  143.         *value = lwc_string_ref(cur->value);
  144.  
  145.         return (http_parameter *) cur->base.next;
  146. }
  147.  
  148. /* See parameter.h for documentation */
  149. void http_parameter_list_destroy(http_parameter *list)
  150. {
  151.         http__item_list_destroy(list);
  152. }
  153.  
  154.