http_header.c 6.72 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 38 39 40 41 42 43 44
 * 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_header.c
 *
 * HTTP header handling.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdarg.h>

#include <assert.h>

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

47 48 49
/* 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
Pekka Pessi's avatar
Pekka Pessi committed
50 51
#define HTTP_STATIC_INLINE

52
#include "sofia-sip/http_parser.h"
Pekka Pessi's avatar
Pekka Pessi committed
53

54 55
#include <sofia-sip/http_header.h>
#include <sofia-sip/http_status.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57 58 59 60
#ifndef UINT32_MAX
#define UINT32_MAX (0xffffffffU)
#endif

Pekka Pessi's avatar
Pekka Pessi committed
61 62 63
/** Complete a HTTP request. */
int http_request_complete(msg_t *msg)
{
64
  size_t len = 0;
Pekka Pessi's avatar
Pekka Pessi committed
65
  http_t *http = http_object(msg);
66
  http_payload_t const *pl;
Pekka Pessi's avatar
Pekka Pessi committed
67 68 69 70 71 72 73 74 75
  su_home_t *home = msg_home(msg);

  if (!http)
    return -1;
  if (!http->http_request)
    return -1;
  if (!http->http_host)
    return -1;

76 77 78 79 80
    for (pl = http->http_payload; pl; pl = pl->pl_next)
      len += pl->pl_len;

  if (len > UINT32_MAX)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
81 82

  if (!http->http_content_length) {
83
    http->http_content_length = http_content_length_create(home, (uint32_t)len);
Pekka Pessi's avatar
Pekka Pessi committed
84 85 86
  }
  else {
    if (http->http_content_length->l_length != len) {
87
      http->http_content_length->l_length = (uint32_t)len;
Pekka Pessi's avatar
Pekka Pessi committed
88 89 90
      msg_fragment_clear(http->http_content_length->l_common);
    }
  }
91

Pekka Pessi's avatar
Pekka Pessi committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  if (!http->http_separator)
    http->http_separator = http_separator_create(home);

  return 0;
}

/** Remove schema, host, port, and fragment from HTTP/HTTPS URL */
int http_strip_hostport(url_t *url)
{
  if (url->url_type == url_http || url->url_type == url_https) {
    url->url_type = url_unknown;
    url->url_scheme = NULL;
    url->url_user = NULL;
    url->url_password = NULL;
    url->url_host = NULL;
    url->url_port = NULL;
    if (url->url_path == NULL) {
      url->url_root = '/';
      url->url_path = "";
    }
  }

  url->url_fragment = NULL;

  return 0;
}

/** Add a Content-Length and separator to a message */
int http_message_complete(msg_t *msg, http_t *http)
121
{
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125
#if 1
  if (!http->http_content_length) {
    http_content_length_t *l;
    http_payload_t *pl;
126
    size_t len = 0;
127

Pekka Pessi's avatar
Pekka Pessi committed
128 129 130
    for (pl = http->http_payload; pl; pl = pl->pl_next)
      len += pl->pl_len;

131 132 133 134
    if (len > UINT32_MAX)
      return -1;

    l = http_content_length_create(msg_home(msg), (uint32_t)len);
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    if (msg_header_insert(msg, http, (http_header_t *)l) < 0)
      return -1;
  }
#endif

  if (!http->http_separator) {
    http_separator_t *sep = http_separator_create(msg_home(msg));
    if (msg_header_insert(msg, http, (http_header_t *)sep) < 0)
      return -1;
  }

  return 0;
}

/** Add headers from the request to the response message. */
151 152
int http_complete_response(msg_t *msg,
			   int status, char const *phrase,
Pekka Pessi's avatar
Pekka Pessi committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 212
			   http_t const *request)
{
  su_home_t *home = msg_home(msg);
  http_t *http = msg_object(msg);

  if (!http || !request || !request->http_request)
    return -1;

  if (!http->http_status)
    http->http_status = http_status_create(home, status, phrase, NULL);

  if (!http->http_status)
    return -1;

  if (!http->http_separator) {
    http_separator_t *sep = http_separator_create(msg_home(msg));
    if (msg_header_insert(msg, http, (http_header_t *)sep) < 0)
      return -1;
  }

  return 0;
}

/** Copy a HTTP header. */
http_header_t *http_header_copy(su_home_t *home, http_header_t const *h)
{
  if (h == NULL || h == HTTP_NONE)
    return NULL;
  return msg_header_copy_as(home, h->sh_class, h);
}

/** Duplicate a HTTP header. */
http_header_t *http_header_dup(su_home_t *home, http_header_t const *h)
{
  if (h == NULL || h == HTTP_NONE)
    return NULL;
  return msg_header_dup_as(home, h->sh_class, h);

}

/** Decode a HTTP header. */
http_header_t *http_header_d(su_home_t *home, msg_t const *msg, char const *b)
{
  return msg_header_d(home, msg, b);
}

/** Encode a HTTP header. */
int http_header_e(char b[], int bsiz, http_header_t const *h, int flags)
{
  return msg_header_e(b, bsiz, h, flags);
}

/** Encode HTTP header contents. */
int http_header_field_e(char b[], int bsiz, http_header_t const *h, int flags)
{
  assert(h); assert(h->sh_class);

  return h->sh_class->hc_print(b, bsiz, h, flags);
}

213
http_header_t *http_header_format(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
				  msg_hclass_t *hc,
				  char const *fmt,
				  ...)
{
  http_header_t *h;
  va_list ap;

  va_start(ap, fmt);

  h = http_header_vformat(home, hc, fmt, ap);

  va_end(ap);

  return h;
}

/** Add a duplicate of header object to a HTTP message. */
int http_add_dup(msg_t *msg,
		 http_t *http,
		 http_header_t const *o)
{
  if (o == HTTP_NONE)
    return 0;

  if (msg == NULL || o == NULL)
    return -1;

  return msg_header_insert(msg, http, msg_header_dup(msg_home(msg), o));
}

int http_add_make(msg_t *msg,
		 http_t *http,
		 msg_hclass_t *hc,
		 char const *s)
{
  if (s == NULL)
    return 0;

  if (msg == NULL)
    return -1;

  return msg_header_insert(msg, http, msg_header_make(msg_home(msg), hc, s));
}

int http_add_format(msg_t *msg,
		    http_t *http,
		    msg_hclass_t *hc,
		    char const *fmt,
		    ...)
{
  http_header_t *h;
  va_list ap;

  if (fmt == NULL)
    return 0;

  if (msg == NULL)
    return -1;

  va_start(ap, fmt);
  h = http_header_vformat(msg_home(msg), hc, fmt, ap);
  va_end(ap);

  return msg_header_insert(msg, http, h);
}

/** Compare two HTTP URLs. */
int http_url_cmp(url_t const *a, url_t const *b)
{
  int rv;

  if ((rv = url_cmp(a, b)))
    return rv;

  if (a->url_path != b->url_path) {
    if (a->url_path == NULL) return -1;
    if (b->url_path == NULL) return +1;
    if ((rv = strcmp(a->url_path, b->url_path)))
      return rv;
  }

  /* Params? */

  /* Query */
  if (a->url_headers != b->url_headers) {
    if (a->url_headers == NULL) return -1;
    if (b->url_headers == NULL) return +1;
    if ((rv = strcmp(a->url_headers, b->url_headers)))
      return rv;
  }

  return 0;
}