http_tag_class.c 5.32 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE http_tag_class.c  HTTP tag classes
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Fri Feb 23 12:46:42 2001 ppessi
 */

#include "config.h"

#include <assert.h>
#include <stddef.h>
#include <string.h>

38
#include <sofia-sip/su.h>
Pekka Pessi's avatar
Pekka Pessi committed
39

40 41 42 43
/* Avoid casting http_t to msg_pub_t and http_header_t to msg_header_t  */
#define MSG_PUB_T struct http_s
#define MSG_HDR_T union http_header_u

44
#include <sofia-sip/http_parser.h>
Pekka Pessi's avatar
Pekka Pessi committed
45

46 47 48 49 50
#include <sofia-sip/http_tag.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_inline.h>
#include <sofia-sip/http_tag_class.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
51

52
tag_class_t httphdrtag_class[1] =
Pekka Pessi's avatar
Pekka Pessi committed
53 54 55 56 57 58 59 60 61 62 63 64
  {{
    sizeof(httphdrtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     msghdrtag_xtra,
    /* tc_dup */      msghdrtag_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ msghdrtag_snprintf,
    /* tc_filter */   httptag_filter,
    /* tc_ref_set */  t_ptr_ref_set,
65
    /* tc_scan */     msghdrtag_scan,
Pekka Pessi's avatar
Pekka Pessi committed
66 67
  }};

68
tag_class_t httpstrtag_class[1] =
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73 74 75 76 77 78 79 80
  {{
    sizeof(httpstrtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     t_str_xtra,
    /* tc_dup */      t_str_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_str_snprintf,
    /* tc_filter */   NULL /* msgtag_str_filter */,
    /* tc_ref_set */  t_ptr_ref_set,
81
    /* tc_scan */     msghdrtag_scan,
Pekka Pessi's avatar
Pekka Pessi committed
82 83
  }};

84
tag_class_t httpmsgtag_class[1] =
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87 88 89 90 91 92 93 94 95 96
  {{
    sizeof(httpmsgtag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     msgobjtag_xtra,
    /* tc_dup */      msgobjtag_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ msgobjtag_snprintf,
    /* tc_filter */   NULL /* httptag_http_filter */,
    /* tc_ref_set */  t_ptr_ref_set,
97
    /* tc_scan */     t_str_scan
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102
  }};

/** Filter a HTTP header structure. */
tagi_t *httptag_filter(tagi_t *dst,
		      tagi_t const f[],
103
		      tagi_t const *src,
Pekka Pessi's avatar
Pekka Pessi committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		      void **bb)
{
  tagi_t stub[2] = {{ NULL }};
  tag_type_t sctt, tt = f->t_tag;
  msg_hclass_t *hc = (msg_hclass_t *)tt->tt_magic;

  assert(src);

  sctt = src->t_tag;

  if (sctt && sctt->tt_class == httpmsgtag_class) {
    http_t const *http;
    msg_mclass_t const *mc;
    http_header_t const *h, **hh;

    http = (http_t const *)src->t_value;
120 121 122
    if (http == NULL)
      return dst;

Pekka Pessi's avatar
Pekka Pessi committed
123
    mc = (void *)http->http_common->h_class;
124
    hh = (void *)msg_hclass_offset(mc, http, hc);
Pekka Pessi's avatar
Pekka Pessi committed
125

126
    if (hh == NULL ||
127 128
	(char *)hh >= ((char *)http + http->http_size) ||
	(char *)hh < (char *)&http->http_request)
Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132 133 134 135 136
      return dst;

    h = *hh;

    if (h == NULL)
      return dst;

    stub[0].t_tag = tt;
137
    stub[0].t_value = (tag_value_t)h;
Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    src = stub; sctt = tt;
  }

  if (tt != sctt)
    return dst;

  if (!src->t_value)
    return dst;
  else if (dst) {
    return t_dup(dst, src, bb);
  }
  else {
    *bb = (char *)*bb + t_xtra(src, (size_t)*bb);
    return dst + 1;
  }
}

155
/** Duplicate headers from taglist and add them to the HTTP message.
Pekka Pessi's avatar
Pekka Pessi committed
156 157 158 159 160 161 162 163 164 165
 *
 * Return the number of headers added to the HTTP message.
 */
int http_add_tl(msg_t *msg, http_t *http,
	       tag_type_t tag, tag_value_t value, ...)
{
  tagi_t const *t;
  ta_list ta;
  int retval = 0;

166
  if (msg == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
167
    return -1;
168 169
  if (http == NULL)
    http = msg_object(msg);
Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

  ta_start(ta, tag, value);

  for (t = ta_args(ta); t; t = tl_next(t)) {
    if (!(tag = t->t_tag) || !(value = t->t_value))
      continue;

    if (HTTPTAG_P(tag)) {
      msg_hclass_t *hc = (msg_hclass_t *)tag->tt_magic;
      http_header_t *h = (http_header_t *)value, **hh;

      if (h == HTTP_NONE) { /* Remove header(s) */
	if (hc == NULL)
	  break;

	hh = msg_hclass_offset(msg_mclass(msg), http, hc);
	if (!hh)
	  break;

	while (*hh) {
	  msg_header_remove(msg, http, *hh);
	}
      }
      else if (h == NULL) {
	continue;
      } else {
	if (tag == httptag_header)
	  hc = h->sh_class;

	if (msg_header_add_dup_as(msg, http, hc, h) < 0)
	  break;
      }
    }
    else if (HTTPTAG_STR_P(tag)) {
      msg_hclass_t *hc = (msg_hclass_t *)tag->tt_magic;
      char const *s = (char const *)value;
      if (s && msg_header_add_make(msg, http, hc, s) < 0)
	break;
    }
    else if (tag == httptag_header_str) {
      if (msg_header_add_str(msg, http, (char const *)value) < 0)
	break;
212 213
    }
    else
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220
      continue;

    retval++;
  }

  ta_end(ta);

221
  if (t)
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224 225
    return -1;

  return retval;
}