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.  
  21. #include "utils/http.h"
  22.  
  23. #include "utils/http/challenge_internal.h"
  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 challenge
  30.  */
  31. struct http_challenge {
  32.         http__item base;
  33.  
  34.         lwc_string *scheme;             /**< Challenge scheme */
  35.         http_parameter *params;         /**< Challenge parameters */
  36. };
  37.  
  38. /**
  39.  * Destroy an HTTP challenge
  40.  *
  41.  * \param self  Challenge to destroy
  42.  */
  43. static void http_destroy_challenge(http_challenge *self)
  44. {
  45.         lwc_string_unref(self->scheme);
  46.         http_parameter_list_destroy(self->params);
  47.         free(self);
  48. }
  49.  
  50. /**
  51.  * Parse an HTTP challenge
  52.  *
  53.  * \param input      Pointer to current input byte. Updated on exit.
  54.  * \param challenge  Pointer to location to receive challenge
  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 challenge is owned by the caller.
  60.  */
  61. nserror http__parse_challenge(const char **input, http_challenge **challenge)
  62. {
  63.         const char *pos = *input;
  64.         http_challenge *result;
  65.         lwc_string *scheme;
  66.         http_parameter *first = NULL;
  67.         http_parameter *params = NULL;
  68.         nserror error;
  69.  
  70.         /* challenge   = auth-scheme 1*SP 1#auth-param
  71.          * auth-scheme = token
  72.          * auth-param  = parameter
  73.          */
  74.  
  75.         error = http__parse_token(&pos, &scheme);
  76.         if (error != NSERROR_OK)
  77.                 return error;
  78.  
  79.         if (*pos != ' ' && *pos != '\t') {
  80.                 lwc_string_unref(scheme);
  81.                 return NSERROR_NOT_FOUND;
  82.         }
  83.  
  84.         http__skip_LWS(&pos);
  85.  
  86.         error = http__parse_parameter(&pos, &first);
  87.         if (error != NSERROR_OK) {
  88.                 lwc_string_unref(scheme);
  89.                 return error;
  90.         }
  91.  
  92.         http__skip_LWS(&pos);
  93.  
  94.         if (*pos == ',') {
  95.                 error = http__item_list_parse(&pos,
  96.                                 http__parse_parameter, first, &params);
  97.                 if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
  98.                         lwc_string_unref(scheme);
  99.                         return error;
  100.                 }
  101.         } else {
  102.                 params = first;
  103.         }
  104.  
  105.         result = malloc(sizeof(*result));
  106.         if (result == NULL) {
  107.                 http_parameter_list_destroy(params);
  108.                 lwc_string_unref(scheme);
  109.                 return NSERROR_NOMEM;
  110.         }
  111.  
  112.         HTTP__ITEM_INIT(result, NULL, http_destroy_challenge);
  113.         result->scheme = scheme;
  114.         result->params = params;
  115.  
  116.         *challenge = result;
  117.         *input = pos;
  118.  
  119.         return NSERROR_OK;
  120. }
  121.  
  122. /* See challenge.h for documentation */
  123. const http_challenge *http_challenge_list_iterate(const http_challenge *cur,
  124.                 lwc_string **scheme, http_parameter **parameters)
  125. {
  126.         if (cur == NULL)
  127.                 return NULL;
  128.  
  129.         *scheme = lwc_string_ref(cur->scheme);
  130.         *parameters = cur->params;
  131.  
  132.         return (http_challenge *) cur->base.next;
  133. }
  134.  
  135. /* See challenge.h for documentation */
  136. void http_challenge_list_destroy(http_challenge *list)
  137. {
  138.         http__item_list_destroy(list);
  139. }
  140.  
  141.