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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "utils/http.h"
  24.  
  25. #include "utils/http/generics.h"
  26. #include "utils/http/parameter_internal.h"
  27. #include "utils/http/primitives.h"
  28.  
  29. /* See content-type.h for documentation */
  30. nserror http_parse_content_type(const char *header_value,
  31.                 http_content_type **result)
  32. {
  33.         const char *pos = header_value;
  34.         lwc_string *type;
  35.         lwc_string *subtype = NULL;
  36.         http_parameter *params = NULL;
  37.         char *mime;
  38.         size_t mime_len;
  39.         lwc_string *imime;
  40.         http_content_type *ct;
  41.         nserror error;
  42.  
  43.         /* type "/" subtype *( ";" parameter ) */
  44.  
  45.         http__skip_LWS(&pos);
  46.  
  47.         error = http__parse_token(&pos, &type);
  48.         if (error != NSERROR_OK)
  49.                 return error;
  50.  
  51.         http__skip_LWS(&pos);
  52.  
  53.         if (*pos != '/') {
  54.                 lwc_string_unref(type);
  55.                 return NSERROR_NOT_FOUND;
  56.         }
  57.  
  58.         pos++;
  59.  
  60.         http__skip_LWS(&pos);
  61.  
  62.         error = http__parse_token(&pos, &subtype);
  63.         if (error != NSERROR_OK) {
  64.                 lwc_string_unref(type);
  65.                 return error;
  66.         }
  67.  
  68.         http__skip_LWS(&pos);
  69.  
  70.         if (*pos == ';') {
  71.                 error = http__item_list_parse(&pos,
  72.                                 http__parse_parameter, NULL, &params);
  73.                 if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
  74.                         lwc_string_unref(subtype);
  75.                         lwc_string_unref(type);
  76.                         return error;
  77.                 }
  78.         }
  79.  
  80.         /* <type> + <subtype> + '/' */
  81.         mime_len = lwc_string_length(type) + lwc_string_length(subtype) + 1;
  82.  
  83.         mime = malloc(mime_len + 1);
  84.         if (mime == NULL) {
  85.                 http_parameter_list_destroy(params);
  86.                 lwc_string_unref(subtype);
  87.                 lwc_string_unref(type);
  88.                 return NSERROR_NOMEM;
  89.         }
  90.  
  91.         sprintf(mime, "%.*s/%.*s",
  92.                 (int) lwc_string_length(type), lwc_string_data(type),
  93.                 (int) lwc_string_length(subtype), lwc_string_data(subtype));
  94.  
  95.         lwc_string_unref(subtype);
  96.         lwc_string_unref(type);
  97.  
  98.         if (lwc_intern_string(mime, mime_len, &imime) != lwc_error_ok) {
  99.                 http_parameter_list_destroy(params);
  100.                 free(mime);
  101.                 return NSERROR_NOMEM;
  102.         }
  103.  
  104.         free(mime);
  105.  
  106.         ct = malloc(sizeof(*ct));
  107.         if (ct == NULL) {
  108.                 lwc_string_unref(imime);
  109.                 http_parameter_list_destroy(params);
  110.                 return NSERROR_NOMEM;
  111.         }
  112.  
  113.         ct->media_type = imime;
  114.         ct->parameters = params;
  115.  
  116.         *result = ct;
  117.  
  118.         return NSERROR_OK;
  119. }
  120.  
  121. /* See content-type.h for documentation */
  122. void http_content_type_destroy(http_content_type *victim)
  123. {
  124.         lwc_string_unref(victim->media_type);
  125.         http_parameter_list_destroy(victim->parameters);
  126.         free(victim);
  127. }
  128.  
  129.