http_header.h 7.97 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
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25 26
#ifndef HTTP_HEADER_H
/** Defined when <sofia-sip/http_header.h> has been included.*/
Pekka Pessi's avatar
Pekka Pessi committed
27
#define HTTP_HEADER_H
Pekka Pessi's avatar
Pekka Pessi committed
28

Pekka Pessi's avatar
Pekka Pessi committed
29
/**@file sofia-sip/http_header.h 
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35 36 37 38
 *
 * HTTP library prototypes.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date  Created: Tue Jun 13 02:58:26 2000 ppessi
 */

#ifndef SU_ALLOC_H
39
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
40 41 42
#endif

#ifndef SU_TAG_H
43
#include <sofia-sip/su_tag.h>
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46
#endif

#ifndef HTTP_H
47
#include <sofia-sip/http.h>
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50
#endif

#ifndef MSG_HEADER_H
51
#include <sofia-sip/msg_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
52 53
#endif

54 55
SOFIA_BEGIN_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
56 57 58 59 60
/* ----------------------------------------------------------------------
 * 1) Macros
 */

/** Initialize a HTTP header structure. */
61 62 63 64
#define HTTP_HEADER_INIT(h, http_class, size)			\
  ((void)memset((h), 0, (size)),				\
   (void)(((msg_common_t *)(h))->h_class = (http_class)),	\
   (h))
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72

#define HTTP_METHOD_NAME(method, name) \
 ((method) == http_method_unknown ? (name) : http_method_name(method, name))

/* ----------------------------------------------------------------------
 * 2) Variables
 */

Pekka Pessi's avatar
Pekka Pessi committed
73 74 75 76 77 78 79 80
SOFIAPUBVAR char const http_method_name_get[];
SOFIAPUBVAR char const http_method_name_post[];
SOFIAPUBVAR char const http_method_name_head[];
SOFIAPUBVAR char const http_method_name_options[];
SOFIAPUBVAR char const http_method_name_put[];
SOFIAPUBVAR char const http_method_name_delete[];
SOFIAPUBVAR char const http_method_name_trace[];
SOFIAPUBVAR char const http_method_name_connect[];
Pekka Pessi's avatar
Pekka Pessi committed
81 82

/** HTTP 0.9 */
Pekka Pessi's avatar
Pekka Pessi committed
83
SOFIAPUBVAR char const http_version_0_9[];
Pekka Pessi's avatar
Pekka Pessi committed
84 85

/** HTTP 1.0 */
Pekka Pessi's avatar
Pekka Pessi committed
86
SOFIAPUBVAR char const http_version_1_0[];
Pekka Pessi's avatar
Pekka Pessi committed
87 88

/** HTTP 1.1 version. */ 
Pekka Pessi's avatar
Pekka Pessi committed
89
SOFIAPUBVAR char const http_version_1_1[];
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93

#define HTTP_VERSION_CURRENT http_version_1_1

/* ----------------------------------------------------------------------
Pekka Pessi's avatar
Pekka Pessi committed
94 95 96 97
 * 3) Prototypes 
 */

/** HTTP parser description. */
98
SOFIAPUBFUN msg_mclass_t const *http_default_mclass(void);
Pekka Pessi's avatar
Pekka Pessi committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

/** Complete a HTTP request. */
SOFIAPUBFUN int http_request_complete(msg_t *msg);

/** Complete a HTTP message. */
SOFIAPUBFUN int http_message_complete(msg_t *msg, http_t *http);

/** Add a duplicate of header object to a HTTP message. */
SOFIAPUBFUN int http_add_dup(msg_t *, http_t *, http_header_t const *);

/** Add a header to the HTTP message. */
SOFIAPUBFUN int http_add_make(msg_t *msg, http_t *http,
			      msg_hclass_t *hc, char const *s);

/** Add a header to the HTTP message. */
SOFIAPUBFUN int http_add_format(msg_t *msg, http_t *http, msg_hclass_t *hc,
				char const *fmt, ...);

/** Add tagged headers to the HTTP message */
SOFIAPUBFUN int http_add_tl(msg_t *msg, http_t *http,
			    tag_type_t tag, tag_value_t value, ...);

/** Remove schema, host, and port from URL */
SOFIAPUBFUN int http_strip_hostport(url_t *url);

/** Add required headers to the response message */
SOFIAPUBFUN int http_complete_response(msg_t *msg,
				       int status, char const *phrase, 
				       http_t const *request);

/** Return string corresponding to the method. */
SOFIAPUBFUN char const *http_method_name(http_method_t method,
					 char const *name);

/** Return enum corresponding to the method name */
SOFIAPUBFUN http_method_t http_method_code(char const *name);

136
#if !SU_HAVE_INLINE
Pekka Pessi's avatar
Pekka Pessi committed
137 138 139 140 141 142 143 144
SOFIAPUBFUN http_t *http_object(msg_t *msg);
SOFIAPUBFUN int http_header_insert(msg_t *msg, http_t *http, http_header_t *h);
SOFIAPUBFUN int http_header_remove(msg_t *msg, http_t *http, http_header_t *h);
SOFIAPUBFUN char const *http_header_name(http_header_t const *h, int compact);
SOFIAPUBFUN void *http_header_data(http_header_t *h);
SOFIAPUBFUN http_content_length_t *http_content_length_create(su_home_t *home, 
							      uint32_t n);
SOFIAPUBFUN http_payload_t *http_payload_create(su_home_t *home, 
145
						void const *data, usize_t len);
Pekka Pessi's avatar
Pekka Pessi committed
146
SOFIAPUBFUN http_separator_t *http_separator_create(su_home_t *home);
147
#endif
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 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

SOFIAPUBFUN http_header_t *http_header_format(su_home_t *home, msg_hclass_t *,
					      char const *fmt, ...);


/** Create a request line object. */
SOFIAPUBFUN http_request_t *http_request_create(su_home_t *home,
						http_method_t method,
						const char *name,
						url_string_t const *url,
						char const *version);

/** Create a status line object. */
SOFIAPUBFUN http_status_t *http_status_create(su_home_t *home,
					      unsigned status,
					      char const *phrase,
					      char const *version);

/** Create an @b Host header object. */
SOFIAPUBFUN http_host_t *http_host_create(su_home_t *home, 
					  char const *host,
					  char const *port);

/** Create an @b Date header object. */
SOFIAPUBFUN http_date_t *http_date_create(su_home_t *home, http_time_t t);

/** Create an @b Expires header object. */
SOFIAPUBFUN http_expires_t *http_expires_create(su_home_t *home,
						http_time_t delta);

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

/** Parse query part in HTTP URL. */
182 183 184
SOFIAPUBFUN issize_t http_query_parse(char *query,
				      /* char const *key, char **return_value, */
				      ...);
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187

/* ----------------------------------------------------------------------
 * 4) Inlined functions 
Pekka Pessi's avatar
Pekka Pessi committed
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 213 214 215
 */

#if SU_HAVE_INLINE
/** Get HTTP structure from msg. */
su_inline
http_t *http_object(msg_t *msg)
{
  return (http_t *)msg_public(msg, HTTP_PROTOCOL_TAG);
}

/** Insert a (list of) header(s) to the header structure and fragment chain.
 *
 * The function @c http_header_insert() inserts header or list of headers
 * into a HTTP message.  It also inserts them into the the message fragment
 * chain, if it exists.
 *
 * When inserting headers into the fragment chain, a request (or status) is
 * inserted first and replaces the existing request (or status).  The Via
 * headers are inserted after the request or status, and rest of the headers
 * after request, status, or Via headers.
 *
 * If the header is a singleton, existing headers with the same class are
 * removed.
 *
 * @param msg message owning the fragment chain
 * @param http HTTP message structure to which header is added
 * @param h   list of header(s) to be added
 */
Pekka Pessi's avatar
Pekka Pessi committed
216
su_inline 
Pekka Pessi's avatar
Pekka Pessi committed
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
int http_header_insert(msg_t *msg, http_t *http, http_header_t *h)
{
  return msg_header_insert(msg, (msg_pub_t *)http, (msg_header_t *)h);
}

/** Remove a header from a HTTP message. */ 
su_inline
int http_header_remove(msg_t *msg, http_t *http, http_header_t *h)
{
  return msg_header_remove(msg, (msg_pub_t *)http, (msg_header_t *)h);
}

/** Return name of the header. */
su_inline
char const *http_header_name(http_header_t const *h, int compact)
{
  if (compact && h->sh_class->hc_short[0])
    return h->sh_class->hc_short;
  else
    return h->sh_class->hc_name;
}

/** Return data after header structure. */
su_inline
void *http_header_data(http_header_t *h)
{
  return h && h != HTTP_NONE ? h->sh_class->hc_size + (char *)h : NULL;
}

su_inline 
http_content_length_t *http_content_length_create(su_home_t *home, uint32_t n)
{
  return msg_content_length_create(home, n);
}

su_inline 
253
http_payload_t *http_payload_create(su_home_t *home, void const *data, isize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
254 255 256 257 258 259 260 261 262 263 264
{
  return msg_payload_create(home, data, len);
}

su_inline 
http_separator_t *http_separator_create(su_home_t *home)
{
  return msg_separator_create(home);
}
#endif

265 266
SOFIA_END_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
267
#ifndef HTTP_PROTOS_H
268
#include <sofia-sip/http_protos.h>
Pekka Pessi's avatar
Pekka Pessi committed
269 270 271
#endif

#endif /* !defined(HTTP_HEADER_H) */